|
|
|
|
@ -29,11 +29,15 @@ import java.util.stream.Collectors;
|
|
|
|
|
import static com.aurora.constant.CommonConstant.UNKNOWN;
|
|
|
|
|
import static com.aurora.constant.RedisConstant.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定时任务调度类,用于执行各种定时任务逻辑。
|
|
|
|
|
* 包括统计唯一访问量、清理缓存、用户地区统计、百度SEO推送、清除任务日志、导入Swagger资源以及将文章数据导入Elasticsearch等操作。
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component("auroraQuartz")
|
|
|
|
|
public class AuroraQuartz {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
@Autowired//注解,用于装配依赖注入,无需手动创建对象
|
|
|
|
|
private RedisService redisService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
@ -64,8 +68,15 @@ public class AuroraQuartz {
|
|
|
|
|
@Value("${website.url}")
|
|
|
|
|
private String websiteUrl;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将Redis中记录的昨日唯一访客数保存到数据库,并清空对应键值。
|
|
|
|
|
* 每天凌晨调用此方法进行持久化处理。
|
|
|
|
|
*/
|
|
|
|
|
public void saveUniqueView() {
|
|
|
|
|
// 获取当前Redis中的唯一访客数量
|
|
|
|
|
Long count = redisService.sSize(UNIQUE_VISITOR);
|
|
|
|
|
|
|
|
|
|
// 构造并插入新的唯一访问记录(时间为昨天)
|
|
|
|
|
UniqueView uniqueView = UniqueView.builder()
|
|
|
|
|
.createTime(LocalDateTimeUtil.offset(LocalDateTime.now(), -1, ChronoUnit.DAYS))
|
|
|
|
|
.viewsCount(Optional.of(count.intValue()).orElse(0))
|
|
|
|
|
@ -73,12 +84,21 @@ public class AuroraQuartz {
|
|
|
|
|
uniqueViewMapper.insert(uniqueView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清除Redis中与访客相关的临时统计数据。
|
|
|
|
|
* 主要包括:唯一访客集合、访客区域分布信息。
|
|
|
|
|
*/
|
|
|
|
|
public void clear() {
|
|
|
|
|
redisService.del(UNIQUE_VISITOR);
|
|
|
|
|
redisService.del(VISITOR_AREA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统计所有用户的注册来源地区分布情况,并将结果存储至Redis中供前端展示使用。
|
|
|
|
|
* 使用IP地址解析工具获取省份信息,未知归属地统一标记为"UNKNOWN"。
|
|
|
|
|
*/
|
|
|
|
|
public void statisticalUserArea() {
|
|
|
|
|
// 查询所有用户认证信息中的IP来源字段
|
|
|
|
|
Map<String, Long> userAreaMap = userAuthMapper.selectList(new LambdaQueryWrapper<UserAuth>().select(UserAuth::getIpSource))
|
|
|
|
|
.stream()
|
|
|
|
|
.map(item -> {
|
|
|
|
|
@ -88,22 +108,34 @@ public class AuroraQuartz {
|
|
|
|
|
return UNKNOWN;
|
|
|
|
|
})
|
|
|
|
|
.collect(Collectors.groupingBy(item -> item, Collectors.counting()));
|
|
|
|
|
|
|
|
|
|
// 转换为DTO列表格式并存入Redis
|
|
|
|
|
List<UserAreaDTO> userAreaList = userAreaMap.entrySet().stream()
|
|
|
|
|
.map(item -> UserAreaDTO.builder()
|
|
|
|
|
.name(item.getKey())
|
|
|
|
|
.value(item.getValue())
|
|
|
|
|
.build())
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
redisService.set(USER_AREA, JSON.toJSONString(userAreaList));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 向百度搜索引擎提交网站的文章链接以加快收录速度。
|
|
|
|
|
* 遍历所有已发布的文章ID拼接URL后通过HTTP POST方式发送给百度服务器。
|
|
|
|
|
*/
|
|
|
|
|
public void baiduSeo() {
|
|
|
|
|
// 提取所有文章ID构造完整URL路径
|
|
|
|
|
List<Integer> ids = articleService.list().stream().map(Article::getId).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 设置请求头模拟curl行为
|
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
headers.add("Host", "data.zz.baidu.com");
|
|
|
|
|
headers.add("User-Agent", "curl/7.12.1");
|
|
|
|
|
headers.add("Content-Length", "83");
|
|
|
|
|
headers.add("Content-Type", "text/plain");
|
|
|
|
|
|
|
|
|
|
// 对每篇文章发起POST请求通知百度爬虫更新索引
|
|
|
|
|
ids.forEach(item -> {
|
|
|
|
|
String url = websiteUrl + "/articles/" + item;
|
|
|
|
|
HttpEntity<String> entity = new HttpEntity<>(url, headers);
|
|
|
|
|
@ -111,13 +143,26 @@ public class AuroraQuartz {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清理历史任务执行日志记录。
|
|
|
|
|
* 委托JobLogService完成实际的日志删除工作。
|
|
|
|
|
*/
|
|
|
|
|
public void clearJobLogs() {
|
|
|
|
|
jobLogService.cleanJobLogs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导入Swagger接口文档定义的API资源到系统资源表中,
|
|
|
|
|
* 并自动赋予管理员角色全部新增资源权限。
|
|
|
|
|
*/
|
|
|
|
|
public void importSwagger() {
|
|
|
|
|
// 执行Swagger资源导入动作
|
|
|
|
|
resourceService.importSwagger();
|
|
|
|
|
|
|
|
|
|
// 获取刚导入的所有资源ID
|
|
|
|
|
List<Integer> resourceIds = resourceService.list().stream().map(Resource::getId).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
// 构建管理员角色与这些资源之间的关联关系对象列表
|
|
|
|
|
List<RoleResource> roleResources = new ArrayList<>();
|
|
|
|
|
for (Integer resourceId : resourceIds) {
|
|
|
|
|
roleResources.add(RoleResource.builder()
|
|
|
|
|
@ -125,12 +170,23 @@ public class AuroraQuartz {
|
|
|
|
|
.resourceId(resourceId)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量保存角色-资源映射关系
|
|
|
|
|
roleResourceService.saveBatch(roleResources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全量同步MySQL中的文章数据到Elasticsearch搜索引擎中。
|
|
|
|
|
* 在每次全量导入前会先删除原有的索引内容确保一致性。
|
|
|
|
|
*/
|
|
|
|
|
public void importDataIntoES() {
|
|
|
|
|
// 删除旧有全文检索索引数据
|
|
|
|
|
elasticsearchMapper.deleteAll();
|
|
|
|
|
|
|
|
|
|
// 加载最新文章列表
|
|
|
|
|
List<Article> articles = articleService.list();
|
|
|
|
|
|
|
|
|
|
// 复制转换为搜索专用DTO结构体并逐条写入ES
|
|
|
|
|
for (Article article : articles) {
|
|
|
|
|
elasticsearchMapper.save(BeanCopyUtil.copyObject(article, ArticleSearchDTO.class));
|
|
|
|
|
}
|
|
|
|
|
|