Commit 9836f6db by 黄炜

feat: 事项逾期状态定时任务

1 parent f5b908a3
...@@ -29,9 +29,14 @@ public class TaskInfoDTO implements Serializable { ...@@ -29,9 +29,14 @@ public class TaskInfoDTO implements Serializable {
@ApiModelProperty(notes = "任务类型名称") @ApiModelProperty(notes = "任务类型名称")
private String taskTypeName; private String taskTypeName;
@ApiModelProperty(notes = "状态:0-待完成,1-已完成") @ApiModelProperty(notes = "状态:0-待完成,1-已完成,2-已逾期")
private Integer status; private Integer status;
@ApiModelProperty(notes = "任务截止时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date dueTime;
@ApiModelProperty(notes = "归属门店名称") @ApiModelProperty(notes = "归属门店名称")
private String storeName; private String storeName;
......
...@@ -14,7 +14,7 @@ public class TaskInfoQueryDTO implements Serializable { ...@@ -14,7 +14,7 @@ public class TaskInfoQueryDTO implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ApiModelProperty(notes = "状态:0-待完成,1-已完成,null-全部") @ApiModelProperty(notes = "状态:0-待完成,1-已完成,2-已逾期,null-全部")
private Integer status; private Integer status;
@ApiModelProperty(notes = "任务类型列表:staff-replace-access(人员更换评估)、policy-update-access" + @ApiModelProperty(notes = "任务类型列表:staff-replace-access(人员更换评估)、policy-update-access" +
......
...@@ -38,10 +38,15 @@ public class TaskInfo implements Serializable { ...@@ -38,10 +38,15 @@ public class TaskInfo implements Serializable {
@Column(name = "task_type_name") @Column(name = "task_type_name")
private String taskTypeName; private String taskTypeName;
/** 状态:0-待完成,1-已完成 */ /** 状态:0-待完成,1-已完成,2-已逾期 */
@Column(name = "status") @Column(name = "status")
private Integer status; private Integer status;
/** 任务截止时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@Column(name = "due_time")
private Date dueTime;
/** 归属门店名称 */ /** 归属门店名称 */
@Column(name = "store_name") @Column(name = "store_name")
private String storeName; private String storeName;
......
package cn.com.glsx.dj.staff.job;
import cn.com.glsx.dj.staff.service.TaskDueTimeService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
/**
* 创建满7天且已设置截止时间的待完成任务状态更新任务。
*/
@Slf4j
@ElasticJobConf(name = "TaskDueTimeJob", cron = "0 0/1 * * * ?", description = "创建满7天任务逾期状态更新", overwrite = false)
@RequiredArgsConstructor
public class TaskDueTimeJob implements SimpleJob {
private final TaskDueTimeService taskDueTimeService;
@Override
public void execute(ShardingContext shardingContext) {
log.info("TaskDueTimeJob__execute__start");
try {
taskDueTimeService.updateOverdueTaskStatus();
} catch (Exception e) {
log.error("TaskDueTimeJob__execute__error", e);
} finally {
log.info("TaskDueTimeJob__execute__end");
}
}
}
...@@ -58,6 +58,15 @@ public interface TaskInfoMapper { ...@@ -58,6 +58,15 @@ public interface TaskInfoMapper {
int insertTaskInfo(TaskInfo taskInfo); int insertTaskInfo(TaskInfo taskInfo);
/** /**
* 将创建满指定时间、已设置截止时间且仍待完成的任务更新为已逾期。
*/
int updateOverdueStatus(@Param("taskTypeList") List<String> taskTypeList,
@Param("deadline") Date deadline,
@Param("pendingStatus") Integer pendingStatus,
@Param("overdueStatus") Integer overdueStatus,
@Param("updatedBy") String updatedBy);
/**
* 批量新增任务关联人员(同步用) * 批量新增任务关联人员(同步用)
*/ */
int batchInsertStaffRel(@Param("list") List<TaskStaffRel> list); int batchInsertStaffRel(@Param("list") List<TaskStaffRel> list);
......
package cn.com.glsx.dj.staff.service;
import cn.com.glsx.dj.staff.mapper.TaskInfoMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 任务截止时间处理服务。
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class TaskDueTimeService {
private static final List<String> TASK_TYPE_LIST = Arrays.asList(
"traffic-optimize",
"multi-scene-expand",
"new-store-traffic"
);
private static final int OVERDUE_DAYS = 7;
private static final int PENDING_STATUS = 0;
private static final int OVERDUE_STATUS = 2;
private static final String SYSTEM_OPERATOR = "system";
private final TaskInfoMapper taskInfoMapper;
/**
* 将创建满7天、已设置截止时间且仍待完成的任务更新为已逾期。
*/
@Transactional(rollbackFor = Exception.class)
public void updateOverdueTaskStatus() {
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_MONTH, -OVERDUE_DAYS);
Date deadline = calendar.getTime();
int updateCount = taskInfoMapper.updateOverdueStatus(
TASK_TYPE_LIST, deadline, PENDING_STATUS, OVERDUE_STATUS, SYSTEM_OPERATOR);
log.info("TaskDueTimeService__updateOverdueTaskStatus, deadline={}, updateCount={}",
deadline, updateCount);
}
}
...@@ -101,8 +101,8 @@ public class TaskInfoService { ...@@ -101,8 +101,8 @@ public class TaskInfoService {
} }
/** /**
* 统计事项任务数量:待完成 / 已完成 / 全部 * 统计事项任务数量:待完成 / 已完成 / 全部。
* <p>为避免三次 SQL,全部数 = 待完成 + 已完成,由 status=0 与 status=1 两次查询汇总。</p> * <p>全部数单独按不限定状态统计,包含已逾期(status=2)任务。</p>
*/ */
public TaskCountStatDTO countTaskStat(TaskInfoQueryDTO query) { public TaskCountStatDTO countTaskStat(TaskInfoQueryDTO query) {
// 1. 统计待完成(status=0) // 1. 统计待完成(status=0)
...@@ -113,10 +113,14 @@ public class TaskInfoService { ...@@ -113,10 +113,14 @@ public class TaskInfoService {
TaskInfoQueryDTO completedQuery = copyForCount(query, 1); TaskInfoQueryDTO completedQuery = copyForCount(query, 1);
Long completedCount = taskInfoMapper.countTaskInfo(completedQuery); Long completedCount = taskInfoMapper.countTaskInfo(completedQuery);
// 3. 统计全部(包含status=2已逾期)
TaskInfoQueryDTO totalQuery = copyForCount(query, null);
Long totalCount = taskInfoMapper.countTaskInfo(totalQuery);
TaskCountStatDTO result = new TaskCountStatDTO(); TaskCountStatDTO result = new TaskCountStatDTO();
result.setPendingCount(pendingCount == null ? 0L : pendingCount); result.setPendingCount(pendingCount == null ? 0L : pendingCount);
result.setCompletedCount(completedCount == null ? 0L : completedCount); result.setCompletedCount(completedCount == null ? 0L : completedCount);
result.setTotalCount(result.getPendingCount() + result.getCompletedCount()); result.setTotalCount(totalCount == null ? 0L : totalCount);
return result; return result;
} }
...@@ -128,6 +132,7 @@ public class TaskInfoService { ...@@ -128,6 +132,7 @@ public class TaskInfoService {
if (src != null) { if (src != null) {
dest.setAssignedToId(src.getAssignedToId()); dest.setAssignedToId(src.getAssignedToId());
dest.setKeyword(src.getKeyword()); dest.setKeyword(src.getKeyword());
dest.setTaskTypeList(src.getTaskTypeList());
} }
dest.setStatus(status); dest.setStatus(status);
return dest; return dest;
...@@ -146,6 +151,9 @@ public class TaskInfoService { ...@@ -146,6 +151,9 @@ public class TaskInfoService {
if (Integer.valueOf(1).equals(taskInfo.getStatus())) { if (Integer.valueOf(1).equals(taskInfo.getStatus())) {
throw new RpcServiceException("任务已完成,请勿重复提交"); throw new RpcServiceException("任务已完成,请勿重复提交");
} }
if (Integer.valueOf(2).equals(taskInfo.getStatus())) {
throw new RpcServiceException("任务已逾期,无法提交");
}
// 2. 人员更换评估任务保存执行结果 // 2. 人员更换评估任务保存执行结果
TaskStaffReplaceResult replaceResult = null; TaskStaffReplaceResult replaceResult = null;
......
...@@ -141,6 +141,7 @@ public class TaskInfoSyncService { ...@@ -141,6 +141,7 @@ public class TaskInfoSyncService {
taskInfo.setTaskType(task.getBizType()); taskInfo.setTaskType(task.getBizType());
taskInfo.setTaskTypeName(task.getBizTypeName()); taskInfo.setTaskTypeName(task.getBizTypeName());
taskInfo.setStatus(0); taskInfo.setStatus(0);
taskInfo.setDueTime(task.getDueTime());
taskInfo.setStoreName(contextFieldMap.getOrDefault("merchant_name", "")); taskInfo.setStoreName(contextFieldMap.getOrDefault("merchant_name", ""));
taskInfo.setStoreId(contextFieldMap.get("merchant_id")); taskInfo.setStoreId(contextFieldMap.get("merchant_id"));
taskInfo.setBusinessType(parseIntOrNull(contextFieldMap.get("business_type"))); taskInfo.setBusinessType(parseIntOrNull(contextFieldMap.get("business_type")));
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
<result column="task_type" property="taskType" jdbcType="VARCHAR"/> <result column="task_type" property="taskType" jdbcType="VARCHAR"/>
<result column="task_type_name" property="taskTypeName" jdbcType="VARCHAR"/> <result column="task_type_name" property="taskTypeName" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="TINYINT"/> <result column="status" property="status" jdbcType="TINYINT"/>
<result column="due_time" property="dueTime" jdbcType="TIMESTAMP"/>
<result column="store_name" property="storeName" jdbcType="VARCHAR"/> <result column="store_name" property="storeName" jdbcType="VARCHAR"/>
<result column="store_id" property="storeId" jdbcType="VARCHAR"/> <result column="store_id" property="storeId" jdbcType="VARCHAR"/>
<result column="business_type" property="businessType" jdbcType="TINYINT"/> <result column="business_type" property="businessType" jdbcType="TINYINT"/>
...@@ -33,7 +34,7 @@ ...@@ -33,7 +34,7 @@
<select id="pageTaskInfo" parameterType="cn.com.glsx.dj.staff.dto.taskcenter.TaskInfoQueryDTO" <select id="pageTaskInfo" parameterType="cn.com.glsx.dj.staff.dto.taskcenter.TaskInfoQueryDTO"
resultMap="TaskInfoDTOMap"> resultMap="TaskInfoDTOMap">
SELECT SELECT
t.id, t.task_center_id, t.task_type, t.task_type_name, t.status, t.id, t.task_center_id, t.task_type, t.task_type_name, t.status, t.due_time,
t.store_name, t.store_id, t.business_type, t.business_type_name, t.store_name, t.store_id, t.business_type, t.business_type_name,
t.creator, t.creator_phone, t.task_description, t.reply_content, t.creator, t.creator_phone, t.task_description, t.reply_content,
t.reply_time, t.assigned_to_id, t.assigned_to_name, t.created_date t.reply_time, t.assigned_to_id, t.assigned_to_name, t.created_date
...@@ -101,7 +102,7 @@ ...@@ -101,7 +102,7 @@
<!-- 根据id查询事项详情 --> <!-- 根据id查询事项详情 -->
<select id="getTaskInfoById" resultMap="TaskInfoDTOMap"> <select id="getTaskInfoById" resultMap="TaskInfoDTOMap">
SELECT SELECT
t.id, t.task_center_id, t.task_type, t.task_type_name, t.status, t.id, t.task_center_id, t.task_type, t.task_type_name, t.status, t.due_time,
t.store_name, t.store_id, t.business_type, t.business_type_name, t.store_name, t.store_id, t.business_type, t.business_type_name,
t.creator, t.creator_phone, t.task_description, t.reply_content, t.creator, t.creator_phone, t.task_description, t.reply_content,
t.reply_time, t.assigned_to_id, t.assigned_to_name, t.created_date t.reply_time, t.assigned_to_id, t.assigned_to_name, t.created_date
...@@ -141,17 +142,33 @@ ...@@ -141,17 +142,33 @@
<!-- 新增任务信息(同步用) --> <!-- 新增任务信息(同步用) -->
<insert id="insertTaskInfo" parameterType="cn.com.glsx.dj.staff.model.TaskInfo" useGeneratedKeys="true" keyProperty="id"> <insert id="insertTaskInfo" parameterType="cn.com.glsx.dj.staff.model.TaskInfo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO dj_car_life_task_info INSERT INTO dj_car_life_task_info
(task_center_id, task_type, task_type_name, status, store_name, store_id, (task_center_id, task_type, task_type_name, status, due_time, store_name, store_id,
business_type, business_type_name, creator, creator_phone, business_type, business_type_name, creator, creator_phone,
task_description, assigned_to_id, assigned_to_name, task_description, assigned_to_id, assigned_to_name,
created_by, created_date, updated_by, updated_date, deleted_flag) created_by, created_date, updated_by, updated_date, deleted_flag)
VALUES VALUES
(#{taskCenterId}, #{taskType}, #{taskTypeName}, #{status}, #{storeName}, #{storeId}, (#{taskCenterId}, #{taskType}, #{taskTypeName}, #{status}, #{dueTime}, #{storeName}, #{storeId},
#{businessType}, #{businessTypeName}, #{creator}, #{creatorPhone}, #{businessType}, #{businessTypeName}, #{creator}, #{creatorPhone},
#{taskDescription}, #{assignedToId}, #{assignedToName}, #{taskDescription}, #{assignedToId}, #{assignedToName},
#{createdBy}, #{createdDate}, #{updatedBy}, NOW(), 'N') #{createdBy}, #{createdDate}, #{updatedBy}, NOW(), 'N')
</insert> </insert>
<!-- 将创建满7天、已设置截止时间且仍待完成的指定类型任务更新为已逾期 -->
<update id="updateOverdueStatus">
UPDATE dj_car_life_task_info
SET status = #{overdueStatus},
updated_by = #{updatedBy},
updated_date = NOW()
WHERE deleted_flag = 'N'
AND status = #{pendingStatus}
AND due_time IS NOT NULL
AND created_date &lt;= #{deadline}
AND task_type IN
<foreach collection="taskTypeList" item="taskType" open="(" separator="," close=")">
#{taskType}
</foreach>
</update>
<!-- 批量新增任务关联人员(同步用) --> <!-- 批量新增任务关联人员(同步用) -->
<insert id="batchInsertStaffRel"> <insert id="batchInsertStaffRel">
INSERT INTO dj_car_life_task_staff_rel INSERT INTO dj_car_life_task_staff_rel
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 业务逻辑歧义与命名误导(最重要)

    • 问题描述:在 TaskDueTimeService 和 TaskInfoMapper.xml 中,判定任务逾期的 SQL 逻辑为 created_date <= #{deadline},即将“当前时间减去7天”的值作为 deadline 与创建时间进行比较。同时 SQL 中仅判断了 due_time IS NOT NULL,并未比较 due_time 的具体值。
    • 影响分析:
      1. 潜在的严重Bug:通常“逾期”的业务定义是“当前时间超过了任务的截止时间(due_time)”。如果业务本意是依据 due_time 判定,当前逻辑会导致创建满7天但实际并未到截止时间的任务被错误标记为逾期。
      2. 可维护性极差:如果业务规则确实是“只要创建满7天就算逾期(且前提是设置了截止时间)”,那么 Java 代码和 SQL 中的变量命名 deadline 是极具误导性的,它实际代表的是“创建时间阈值(createTimeThreshold)”。
    • 优化建议:
      • 若逾期以 due_time 为准,需修改 SQL 逻辑:AND due_time < NOW()。
      • 若逾期确以 created_date 满7天为准,请修改变量命名以消除歧义: java // TaskDueTimeService.java Date createTimeThreshold = calendar.getTime(); int updateCount = taskInfoMapper.updateOverdueStatus( TASK_TYPE_LIST, createTimeThreshold, PENDING_STATUS, OVERDUE_STATUS, SYSTEM_OPERATOR); xml <!-- TaskInfoMapper.xml --> AND created_date &lt;= #{createTimeThreshold}

    2. 定时任务执行频率过高,存在无效资源消耗

    • 问题描述:TaskDueTimeJob 上的 Cron 表达式为 0 0/1 * * * ?(每分钟执行一次)。
    • 影响分析:该定时任务处理的是“满7天”这种大时间跨度的低时效性业务。每分钟去数据库执行一次全表扫描(或索引范围扫描)更新状态,会带来不必要的数据库连接和 CPU 资源浪费,特别是在任务表数据量逐渐增大的情况下。
    • 优化建议:建议降低任务执行频率,例如改为每小时执行一次(0 0 * * * ?),或者至少每 10/30 分钟执行一次,足以满足“7天逾期”的业务精度要求。

    3. 遗留的日期时间 API 使用(工程最佳实践)

    • 问题描述:在 TaskDueTimeService 中,使用了 java.util.Date 和 java.util.Calendar 来进行日期的加减运算。
    • 影响分析:Date 和 Calendar 是 Java 8 之前的遗留 API,存在线程不安全、易读性差等问题。主流的工程最佳实践推荐使用 java.time 包下的类。
    • 优化建议:使用 LocalDateTime 重构日期计算逻辑(如果 Mapper 接口必须接收 Date,可做最终转换): java LocalDateTime thresholdTime = LocalDateTime.now().minusDays(OVERDUE_DAYS); Date createTimeThreshold = Date.from(thresholdTime.atZone(ZoneId.systemDefault()).toInstant());

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分)
      • 理由:代码较好地处理了逾期状态的流转(如提交任务时拦截已逾期状态)。严格遵守了“Mybatis 不允许使用子查询”及“Dubbo 接口参数不危险变更”的红线规则。扣分点在于判定逾期的核心逻辑与 due_time 字段的实际关联性存在歧义(见问题1)。
    • 安全性与潜在风险(30/30分)
      • 理由:无 SQL 注入风险(使用了 #{} 和 <foreach> 标签),无越权及敏感信息泄露风险,事务注解 @Transactional 使用正确。
    • 最佳实践与可维护性(16/20分)
      • 理由:代码结构清晰,魔法值(如 status=0/2, days=7)被合理地提取为了常量。扣分点在于使用了过时的 Calendar API 以及关键变量命名(deadline)与实际比较字段(created_date)语义不符。
    • 性能与资源利用(3/5分)
      • 理由:SQL 层面没有明显的全表扫风险(假设 status 和 created_date 有合理索引)。扣分点在于定时任务 Cron 频率设置过高(每分钟1次),与业务场景不匹配。
    • 提交信息质量(5/5分)
      • 理由:Commit message (feat: 事项逾期状态定时任务) 格式规范,清晰准确地描述了本次代码变更的核心目的。

    总分:90分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!