Commit f1b83e01 by 林永健

refactor(jobs): 重构数据报告快照生成任务

- 将周期键构建逻辑修改为直接设置到请求对象中
- 统一处理周报和月报的时间范围设置
- 修改buildLastMonthPeriodKey和buildLastWeekPeriodKey方法签名
- 更新单元测试中的执行上下文传递方式
- 在枚举中添加当前和上次描述信息
- 更新服务层方法以支持报告类型参数传递
1 parent cd195a8e
......@@ -22,6 +22,7 @@ public class DataReportGenerateReq implements Serializable {
/**
* 报告类型:1-周报,2-月报。
* @see cn.com.glsx.dj.peanalysis.enums.DataReportTypeEnum
*/
@NotNull(message = "报告类型不能为空")
@ApiModelProperty(notes = "报告类型:1-周报,2-月报")
......
......@@ -16,16 +16,19 @@ public enum DataReportTypeEnum {
/**
* 展业周报。
*/
WEEKLY((byte) 1, "weekly"),
WEEKLY((byte) 1, "weekly", "本周", "上周"),
/**
* 展业月报。
*/
MONTHLY((byte) 2, "monthly");
MONTHLY((byte) 2, "monthly", "本月", "上月");
private final byte code;
private final String name;
private final String currentDesc;
private final String lastDesc;
public static DataReportTypeEnum fromCode(Byte code) {
if (code == null) {
return null;
......
......@@ -21,7 +21,7 @@ import java.util.List;
@Slf4j
@Component
@ElasticJobConf(
name = "AiTextSummaryPollJob",
name = "DataReportAiTextSummaryPollJob",
cron = "0 */5 * * * ?", // 每 5 分钟执行一次(可根据需要调整)
description = "AI 文本摘要结果轮询",
overwrite = false
......
......@@ -21,7 +21,7 @@ import java.util.List;
@Slf4j
@Component
@ElasticJobConf(
name = "AiTextSummarySubmitJob",
name = "DataReportAiTextSummarySubmitJob",
cron = "0 */10 * * * ?", // 每 10 分钟执行一次(可根据需要调整)
description = "AI 文本摘要任务提交",
overwrite = false
......
......@@ -31,17 +31,14 @@ public class DataReportMonthlySnapshotJob implements SimpleJob {
log.info("开始执行数据报告月报快照生成任务");
try {
DataReportGenerateReq req = new DataReportGenerateReq();
// 构建上月的周期键(格式:YYYYMM,如2026M03)
String periodKey = buildLastMonthPeriodKey(shardingContext);
buildLastMonthPeriodKey(req, shardingContext);
String periodKey = req.getPeriodKey();
log.info("计算得到上月周期键:{}", periodKey);
// 构建生成请求参数
DataReportGenerateReq req = new DataReportGenerateReq();
req.setReportType((byte) 2); // 2-月报
req.setPeriodKey(periodKey);
req.setPeriodStart(DateUtil.beginOfMonth(DateUtil.offsetMonth(new Date(), -1)));
req.setPeriodEnd(DateUtil.endOfMonth(DateUtil.offsetMonth(new Date(), -1)));
req.setPeriodKey(periodKey);
req.setOperator("SYSTEM"); // 系统任务
// 调用服务生成快照
......@@ -61,10 +58,8 @@ public class DataReportMonthlySnapshotJob implements SimpleJob {
/**
* 构建上月的周期键。
* 格式为YYYYMmm(如2026M03),其中mm为两位数的月份
*
* @return 上月周期键字符串
*/
private String buildLastMonthPeriodKey(ShardingContext shardingContext) {
private void buildLastMonthPeriodKey(DataReportGenerateReq req, ShardingContext shardingContext) {
Date date = shardingContext != null && shardingContext.getJobParameter() != null
? DateUtil.parseDate(shardingContext.getJobParameter())
: DateUtil.lastMonth();
......@@ -73,6 +68,7 @@ public class DataReportMonthlySnapshotJob implements SimpleJob {
String periodKey = DateUtil.format(date, "yyyy'M'MM");
log.debug("构建上月周期键:日期={}, 结果={}", date, periodKey);
return periodKey;
req.setPeriodStart(DateUtil.beginOfMonth(date));
req.setPeriodEnd(DateUtil.endOfMonth(date));
}
}
......@@ -31,16 +31,14 @@ public class DataReportWeeklySnapshotJob implements SimpleJob {
log.info("开始执行数据报告周报快照生成任务");
try {
DataReportGenerateReq req = new DataReportGenerateReq();
// 构建上周的周期键(格式:YYYYWww,如2026W12)
String periodKey = buildLastWeekPeriodKey(shardingContext);
buildLastWeekPeriodKey(req, shardingContext);
String periodKey = req.getPeriodKey();
log.info("计算得到上周周期键:{}", periodKey);
// 构建生成请求参数
DataReportGenerateReq req = new DataReportGenerateReq();
req.setReportType((byte) 1); // 1-周报
req.setPeriodKey(periodKey);
req.setPeriodStart(DateUtil.beginOfMonth(DateUtil.offsetMonth(new Date(), -1)));
req.setPeriodEnd(DateUtil.endOfMonth(DateUtil.offsetMonth(new Date(), -1)));
req.setOperator("SYSTEM"); // 系统任务
// 调用服务生成快照
......@@ -60,10 +58,8 @@ public class DataReportWeeklySnapshotJob implements SimpleJob {
/**
* 构建上周的周期键。
* 使用中国周计算标准(周一为一周的开始),格式为YYYYWww(如2026W12)
*
* @return 上周周期键字符串
*/
private String buildLastWeekPeriodKey(ShardingContext shardingContext) {
private void buildLastWeekPeriodKey(DataReportGenerateReq req, ShardingContext shardingContext) {
Date date = shardingContext != null && shardingContext.getJobParameter() != null
? DateUtil.parseDate(shardingContext.getJobParameter())
: DateUtil.lastWeek();
......@@ -72,6 +68,7 @@ public class DataReportWeeklySnapshotJob implements SimpleJob {
String periodKey = DateUtil.format(date, "yyyy'W'ww");
log.debug("构建上周周期键:日期={}, 结果={}", date, periodKey);
return periodKey;
req.setPeriodStart(DateUtil.beginOfWeek(date));
req.setPeriodEnd(DateUtil.endOfWeek(date));
}
}
......@@ -14,7 +14,6 @@ import cn.com.glsx.dj.peanalysis.mapper.DataReportBrandSnapshotMapper;
import cn.com.glsx.dj.peanalysis.mapper.DataReportBusinessSnapshotMapper;
import cn.com.glsx.dj.peanalysis.model.DataReportBrandSnapshot;
import cn.com.glsx.dj.peanalysis.model.DataReportBusinessSnapshot;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.equity.util.CopyUtils;
import cn.hutool.core.date.DateUtil;
......@@ -267,7 +266,7 @@ public class DataReportSnapshotService {
DataBizQueryParamsDTO queryParams = buildQueryParams(request, bizType);
queryParams.setExts(null);
Map<String, List<DataBusinessDTO>> dataMap = getDataStatsService.parallelCallListMap(itemCodes, queryParams);
resp.setSummaryPayload(buildPayloadFromDataBusiness(dataMap, itemCodes));
resp.setSummaryPayload(buildPayloadFromDataBusiness(request.getReportType(), dataMap, itemCodes));
log.info("查询业务统计数据完成:bizType={}, 耗时:{}", bizType, (System.currentTimeMillis() - ts));
return resp;
......@@ -293,7 +292,7 @@ public class DataReportSnapshotService {
queryParams.setBizs((int) bizType);
Map<String, List<DataBusinessDTO>> dataMap = getDataStatsService.parallelCallListMap(itemCodes, queryParams);
// 构建品牌指标
resp.setMetricPayload(buildPayloadFromDataBusiness(dataMap, itemCodes));
resp.setMetricPayload(buildPayloadFromDataBusiness(request.getReportType(), dataMap, itemCodes));
log.info("查询品牌统计数据完成:bizType={}, brandId={}, 返回行数={}, 耗时:{}",
bizType, brandId, dataMap != null ? dataMap.size() : 0, (System.currentTimeMillis() - ts));
......@@ -373,13 +372,14 @@ public class DataReportSnapshotService {
/**
* 从索引数据构建品牌指标数据 JSON
*/
private String buildPayloadFromDataBusiness(Map<String, List<DataBusinessDTO>> dataMap, List<String> itemCodes) {
private String buildPayloadFromDataBusiness(Byte reportType, Map<String, List<DataBusinessDTO>> dataMap, List<String> itemCodes) {
if (MapUtils.isEmpty(dataMap)) {
log.error("查询指标数据为空");
return "{}";
}
DataReportTypeEnum reportTypeEnum = DataReportTypeEnum.fromCode(reportType);
try {
Map<String, Object> payload = new HashMap<>();
Map<String, Object> payload = new LinkedHashMap<>();
// 本周数据
Map<String, Object> currentData = new HashMap<>();
// 上周数据
......@@ -405,8 +405,8 @@ public class DataReportSnapshotService {
changeData.put(itemName, BigDecimal.ZERO);
}
}
payload.put("本周数据", currentData);
payload.put("上周数据", lastData);
payload.put(reportTypeEnum.getCurrentDesc() + "数据", currentData);
payload.put(reportTypeEnum.getLastDesc() + "数据", lastData);
payload.put("环比变化", changeData);
return JSONUtil.toJsonStr(payload);
......
......@@ -5,9 +5,13 @@ import cn.com.glsx.dj.peanalysis.job.DataReportAiTextSummarySubmitJob;
import cn.com.glsx.dj.peanalysis.job.DataReportMonthlySnapshotJob;
import cn.com.glsx.dj.peanalysis.job.DataReportWeeklySnapshotJob;
import cn.com.glsx.dj.peanalysis.remote.BaseRemoteTest;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collections;
/**
* 数据报告快照服务单元测试。
*
......@@ -33,7 +37,8 @@ public class DataReportSnapshotServiceTest extends BaseRemoteTest {
@Test
public void dataReportMonthlySnapshotJobTest() {
dataReportMonthlySnapshotJob.execute(null);
ShardingContexts mockContexts = new ShardingContexts(null, null, 0, "2026-02-01", Collections.emptyMap(), 0);
dataReportMonthlySnapshotJob.execute(new ShardingContext(mockContexts, 0));
}
@Test
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!