定时任务增加系统定时任务
This commit is contained in:
@@ -42,7 +42,7 @@ public class CrontabController {
|
||||
public ResultDomain<TbCrontabTaskMeta> getEnabledCrontabList(@RequestParam(required = false) String param) {
|
||||
try {
|
||||
// 从数据库查询所有任务元数据
|
||||
ResultDomain<TbCrontabTaskMeta> result = taskMetaService.getAllTaskMeta();
|
||||
ResultDomain<TbCrontabTaskMeta> result = taskMetaService.getTaskMetaByCategory(param);
|
||||
result.getDataList().forEach(item->{
|
||||
item.setBeanName("");
|
||||
item.setMethodName("");
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package org.xyzh.crontab.task.recommendTask;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.xyzh.api.news.recommend.ResourceRecommendService;
|
||||
import org.xyzh.api.news.resource.ResourceService;
|
||||
import org.xyzh.common.core.domain.ResultDomain;
|
||||
import org.xyzh.common.core.page.PageParam;
|
||||
import org.xyzh.common.dto.BaseDTO;
|
||||
import org.xyzh.common.dto.resource.TbResource;
|
||||
import org.xyzh.common.dto.resource.TbResourceRecommend;
|
||||
import org.xyzh.common.utils.IDUtils;
|
||||
import org.xyzh.common.vo.ResourceVO;
|
||||
import org.xyzh.crontab.pojo.TaskParams;
|
||||
import org.xyzh.crontab.task.BaseTask;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @description 热门资源推荐定时任务 - 每天凌晨1点自动更新热门推荐
|
||||
* @filename TopRecommendTask.java
|
||||
* @author yslg
|
||||
* @copyright xyzh
|
||||
* @since 2025-11-25
|
||||
*/
|
||||
@Component("topRecommendTask")
|
||||
public class TopRecommendTask extends BaseTask {
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
@Autowired
|
||||
private ResourceRecommendService resourceRecommendService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
protected void doExecute(TaskParams taskParams) throws Exception {
|
||||
logger.info("开始执行热门资源推荐任务");
|
||||
|
||||
try {
|
||||
// 1. 清除旧的热门推荐(recommend_type = 1)
|
||||
// clearOldRecommends();
|
||||
|
||||
// 2. 获取浏览量前10的资源
|
||||
List<ResourceVO> topViewCountResources = getTopResourcesByViewCount(10);
|
||||
logger.info("获取到浏览量前10的资源数量: {}", topViewCountResources.size());
|
||||
|
||||
// 3. 获取发布时间前10的资源
|
||||
List<ResourceVO> topPublishTimeResources = getTopResourcesByPublishTime(10);
|
||||
logger.info("获取到发布时间前10的资源数量: {}", topPublishTimeResources.size());
|
||||
|
||||
// 4. 合并并去重(使用 Set 确保资源ID唯一)
|
||||
Set<String> resourceIds = new HashSet<>();
|
||||
List<TbResourceRecommend> recommendList = new ArrayList<>();
|
||||
int orderNum = 1;
|
||||
|
||||
// 添加浏览量前10
|
||||
for (ResourceVO vo : topViewCountResources) {
|
||||
if (vo.getResource() != null && resourceIds.add(vo.getResource().getResourceID())) {
|
||||
recommendList.add(createRecommend(vo.getResource().getResourceID(), orderNum++));
|
||||
}
|
||||
}
|
||||
|
||||
// 添加发布时间前10
|
||||
for (ResourceVO vo : topPublishTimeResources) {
|
||||
if (vo.getResource() != null && resourceIds.add(vo.getResource().getResourceID())) {
|
||||
recommendList.add(createRecommend(vo.getResource().getResourceID(), orderNum++));
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 批量插入新推荐
|
||||
if (!recommendList.isEmpty()) {
|
||||
for (TbResourceRecommend recommend : recommendList) {
|
||||
ResultDomain<TbResourceRecommend> addResult = resourceRecommendService.addRecommend(recommend);
|
||||
if (!addResult.isSuccess()) {
|
||||
logger.warn("插入推荐失败: 资源ID={}, 原因={}", recommend.getResourceID(), addResult.getMessage());
|
||||
}
|
||||
}
|
||||
logger.info("成功插入{}条热门推荐记录", recommendList.size());
|
||||
} else {
|
||||
logger.warn("没有找到符合条件的资源,未插入推荐记录");
|
||||
}
|
||||
|
||||
logger.info("热门资源推荐任务执行成功,共推荐{}个资源", resourceIds.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("热门资源推荐任务执行失败: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除旧的热门推荐记录
|
||||
*/
|
||||
private void clearOldRecommends() {
|
||||
logger.info("清除旧的热门推荐记录(recommend_type = 1)");
|
||||
// 暂时跳过清除逻辑,Service层方法待添加
|
||||
// TODO: 实现 deleteRecommendsByType 方法后启用
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览量前N的资源
|
||||
*/
|
||||
private List<ResourceVO> getTopResourcesByViewCount(int limit) {
|
||||
TbResource filter = new TbResource();
|
||||
filter.setStatus(1); // 只查询已发布的资源
|
||||
|
||||
// 设置排序:按浏览量降序
|
||||
List<BaseDTO.OrderType> orderTypes = new ArrayList<>();
|
||||
orderTypes.add(new BaseDTO.OrderType("view_count", "DESC"));
|
||||
orderTypes.add(new BaseDTO.OrderType("publish_time", "DESC"));
|
||||
filter.setOrderTypes(orderTypes);
|
||||
|
||||
PageParam pageParam = new PageParam();
|
||||
pageParam.setPageSize(limit);
|
||||
pageParam.setOffset(0L);
|
||||
|
||||
ResultDomain<ResourceVO> result = resourceService.getResourcePageOrder(filter, pageParam);
|
||||
if (result.isSuccess() && result.getDataList() != null) {
|
||||
return result.getDataList();
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取发布时间前N的资源(最新发布)
|
||||
*/
|
||||
private List<ResourceVO> getTopResourcesByPublishTime(int limit) {
|
||||
TbResource filter = new TbResource();
|
||||
filter.setStatus(1); // 只查询已发布的资源
|
||||
|
||||
// 设置排序:按发布时间降序
|
||||
List<BaseDTO.OrderType> orderTypes = new ArrayList<>();
|
||||
orderTypes.add(new BaseDTO.OrderType("publish_time", "DESC"));
|
||||
orderTypes.add(new BaseDTO.OrderType("create_time", "DESC"));
|
||||
filter.setOrderTypes(orderTypes);
|
||||
|
||||
PageParam pageParam = new PageParam();
|
||||
pageParam.setPageSize(limit);
|
||||
pageParam.setOffset(0L);
|
||||
|
||||
ResultDomain<ResourceVO> result = resourceService.getResourcePageOrder(filter, pageParam);
|
||||
if (result.isSuccess() && result.getDataList() != null) {
|
||||
return result.getDataList();
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建推荐记录
|
||||
*/
|
||||
private TbResourceRecommend createRecommend(String resourceId, int orderNum) {
|
||||
TbResourceRecommend recommend = new TbResourceRecommend();
|
||||
recommend.setID(IDUtils.generateID());
|
||||
recommend.setResourceID(resourceId);
|
||||
recommend.setRecommendType(1); // 1-热门资源推荐
|
||||
recommend.setOrderNum(orderNum);
|
||||
recommend.setCreateTime(new Date());
|
||||
recommend.setUpdateTime(new Date());
|
||||
recommend.setDeleted(false);
|
||||
return recommend;
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@
|
||||
AND ct.task_name LIKE CONCAT('%', #{filter.taskName}, '%')
|
||||
</if>
|
||||
<if test="filter.taskGroup != null and filter.taskGroup != ''">
|
||||
AND ct.task_group = #{filter.taskGroup}
|
||||
AND ct.task_group LIKE CONCAT('%', #{filter.taskGroup}, '%')
|
||||
</if>
|
||||
<if test="filter.status != null">
|
||||
AND ct.status = #{filter.status}
|
||||
@@ -337,7 +337,7 @@
|
||||
AND ct.task_name LIKE CONCAT('%', #{filter.taskName}, '%')
|
||||
</if>
|
||||
<if test="filter.taskGroup != null and filter.taskGroup != ''">
|
||||
AND ct.task_group = #{filter.taskGroup}
|
||||
AND ct.task_group LIKE CONCAT('%', #{filter.taskGroup}, '%')
|
||||
</if>
|
||||
<if test="filter.status != null">
|
||||
AND ct.status = #{filter.status}
|
||||
|
||||
@@ -115,7 +115,7 @@
|
||||
<select id="selectTaskMetaByCategory" resultMap="BaseResultMap">
|
||||
SELECT <include refid="Base_Column_List" />
|
||||
FROM tb_crontab_task_meta
|
||||
WHERE category = #{category}
|
||||
WHERE category LIKE CONCAT('%', #{category}, '%')
|
||||
AND deleted = 0
|
||||
ORDER BY sort_order ASC
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user