Commit 072c830f by 范富荣

督导-新增统计各个任务未完成数量接口

1 parent c2029a66
package cn.com.glsx.dj.supervisor.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class UnfinishedTaskCountDTO implements Serializable {
@ApiModelProperty(notes = "每日任务未完成数量(日报、录音、带教)")
private Integer dailyCount;
@ApiModelProperty(notes = "每周任务未完成数量(优秀案例录音、带教要求)")
private Integer weeklyCount;
@ApiModelProperty(notes = "每月任务未完成数量(月报)")
private Integer monthlyCount;
@ApiModelProperty(notes = "专项任务未完成数量")
private Integer specialCount;
}
package cn.com.glsx.dj.supervisor.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class UnfinishedTaskCountReqDTO implements Serializable {
@ApiModelProperty(notes = "督导手机号", required = true)
private String supervisorPhone;
@ApiModelProperty(notes = "开始日期(yyyy-MM-dd,可为空)")
private String startDate;
@ApiModelProperty(notes = "结束日期(yyyy-MM-dd,可为空)")
private String endDate;
}
...@@ -166,4 +166,12 @@ public interface SupervisorTaskInfoRemote { ...@@ -166,4 +166,12 @@ public interface SupervisorTaskInfoRemote {
@ApiParams({@ApiParam(name = "request", notes = "请求参数(任务类型、创建日期、督导UUID)", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)}) @ApiParams({@ApiParam(name = "request", notes = "请求参数(任务类型、创建日期、督导UUID)", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)})
@ApiResponse("任务ID") @ApiResponse("任务ID")
RpcResponse<Long> updateWeeklyTaskStatus(WeeklyTaskStatusReqDTO request); RpcResponse<Long> updateWeeklyTaskStatus(WeeklyTaskStatusReqDTO request);
/**
* 统计各任务类型未完成任务数量
*/
@ApiMethod("统计各任务类型未完成任务数量")
@ApiParams({@ApiParam(name = "request", notes = "查询参数(督导手机号、起止日期)", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)})
@ApiResponse("各类型未完成任务数量")
RpcResponse<UnfinishedTaskCountDTO> getUnfinishedTaskCount(UnfinishedTaskCountReqDTO request);
} }
\ No newline at end of file \ No newline at end of file
...@@ -80,4 +80,18 @@ public interface SupervisorTaskInfoMapper extends OreMapper<SupervisorTaskInfo> ...@@ -80,4 +80,18 @@ public interface SupervisorTaskInfoMapper extends OreMapper<SupervisorTaskInfo>
@Param("startDate") String startDate, @Param("startDate") String startDate,
@Param("endDate") String endDate); @Param("endDate") String endDate);
/**
* 按任务类型分组,统计未完成任务数量
*
* @param supervisorPhone 督导手机号
* @param taskTypes 任务类型列表
* @param startDate 开始日期(yyyy-MM-dd)
* @param endDate 结束日期(yyyy-MM-dd)
* @return 每条记录包含 taskType 和 count
*/
List<Map<String, Object>> countUnfinishedByTaskTypes(@Param("supervisorPhone") String supervisorPhone,
@Param("taskTypes") List<Integer> taskTypes,
@Param("startDate") String startDate,
@Param("endDate") String endDate);
} }
\ No newline at end of file \ No newline at end of file
...@@ -299,4 +299,20 @@ public class SupervisorTaskInfoRemoteImpl implements SupervisorTaskInfoRemote { ...@@ -299,4 +299,20 @@ public class SupervisorTaskInfoRemoteImpl implements SupervisorTaskInfoRemote {
return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
@Override
public RpcResponse<UnfinishedTaskCountDTO> getUnfinishedTaskCount(UnfinishedTaskCountReqDTO request) {
try {
log.info("统计未完成任务数量 - 开始,request={}", request);
UnfinishedTaskCountDTO result = supervisorTaskInfoService.getUnfinishedTaskCount(request);
log.info("统计未完成任务数量 - 完成,result={}", result);
return RpcResponse.success(result);
} catch (RpcServiceException e) {
log.error("cn.com.glsx.dj.supervisor.remote.SupervisorTaskInfoRemoteImpl.getUnfinishedTaskCount error {}", e);
return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) {
log.error("cn.com.glsx.dj.supervisor.remote.SupervisorTaskInfoRemoteImpl.getUnfinishedTaskCount system error {}", e);
return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
}
}
} }
...@@ -1042,4 +1042,119 @@ public class SupervisorTaskInfoService { ...@@ -1042,4 +1042,119 @@ public class SupervisorTaskInfoService {
return taskInfo.getId(); return taskInfo.getId();
} }
/**
* 统计各任务类型未完成任务数量
*
* @param request 查询参数(督导手机号、起止日期)
* @return 各类型未完成任务数量
*/
public UnfinishedTaskCountDTO getUnfinishedTaskCount(UnfinishedTaskCountReqDTO request) {
String supervisorPhone = request.getSupervisorPhone();
String startDate = request.getStartDate();
String endDate = request.getEndDate();
UnfinishedTaskCountDTO result = new UnfinishedTaskCountDTO();
result.setDailyCount(0);
result.setWeeklyCount(0);
result.setMonthlyCount(0);
result.setSpecialCount(0);
boolean useCustomDate = StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate);
if (useCustomDate) {
// 使用自定义时间范围,一次查询所有类型
List<Integer> allTypes = Arrays.asList(
TaskTypeEnum.DAILY_REPORT.getCode(),
TaskTypeEnum.AUDIO_RECORDING.getCode(),
TaskTypeEnum.TRAINING.getCode(),
TaskTypeEnum.EXCELLENT_CASE_AUDIO.getCode(),
TaskTypeEnum.COACHING_REQUIREMENT.getCode(),
TaskTypeEnum.MONTHLY_REPORT.getCode(),
TaskTypeEnum.SPECIAL_RECORDING.getCode()
);
Map<Integer, Integer> countMap = toCountMap(
supervisorTaskInfoMapper.countUnfinishedByTaskTypes(supervisorPhone, allTypes, startDate, endDate));
result.setDailyCount(sumCounts(countMap,
TaskTypeEnum.DAILY_REPORT.getCode(),
TaskTypeEnum.AUDIO_RECORDING.getCode(),
TaskTypeEnum.TRAINING.getCode()));
result.setWeeklyCount(sumCounts(countMap,
TaskTypeEnum.EXCELLENT_CASE_AUDIO.getCode(),
TaskTypeEnum.COACHING_REQUIREMENT.getCode()));
result.setMonthlyCount(sumCounts(countMap, TaskTypeEnum.MONTHLY_REPORT.getCode()));
result.setSpecialCount(sumCounts(countMap, TaskTypeEnum.SPECIAL_RECORDING.getCode()));
} else {
// 默认时间范围:每日=今天,每周=本周,每月=本月,专项=今天
String today = cn.com.glsx.framework.core.util.DateUtils.formatDate(new Date());
String weekStart = cn.com.glsx.framework.core.util.DateUtils.formatDate(
cn.com.glsx.dj.supervisor.utils.DateUtils.getMondayOfWeek(new Date()));
String weekEnd = cn.com.glsx.framework.core.util.DateUtils.formatDate(
cn.com.glsx.dj.supervisor.utils.DateUtils.getSundayOfWeek(new Date()));
String monthStart = DateUtil.beginOfMonth(new Date()).toDateStr();
String monthEnd = DateUtil.endOfMonth(new Date()).toDateStr();
// 每日任务(日报、录音、带教)
List<Integer> dailyTypes = Arrays.asList(
TaskTypeEnum.DAILY_REPORT.getCode(),
TaskTypeEnum.AUDIO_RECORDING.getCode(),
TaskTypeEnum.TRAINING.getCode());
Map<Integer, Integer> dailyMap = toCountMap(
supervisorTaskInfoMapper.countUnfinishedByTaskTypes(supervisorPhone, dailyTypes, today, today));
result.setDailyCount(sumAllValues(dailyMap));
// 每周任务(优秀案例录音、带教要求)
List<Integer> weeklyTypes = Arrays.asList(
TaskTypeEnum.EXCELLENT_CASE_AUDIO.getCode(),
TaskTypeEnum.COACHING_REQUIREMENT.getCode());
Map<Integer, Integer> weeklyMap = toCountMap(
supervisorTaskInfoMapper.countUnfinishedByTaskTypes(supervisorPhone, weeklyTypes, weekStart, weekEnd));
result.setWeeklyCount(sumAllValues(weeklyMap));
// 每月任务(月报)
List<Integer> monthlyTypes = Collections.singletonList(TaskTypeEnum.MONTHLY_REPORT.getCode());
Map<Integer, Integer> monthlyMap = toCountMap(
supervisorTaskInfoMapper.countUnfinishedByTaskTypes(supervisorPhone, monthlyTypes, monthStart, monthEnd));
result.setMonthlyCount(sumAllValues(monthlyMap));
// 专项任务(专项,按每日统计)
List<Integer> specialTypes = Collections.singletonList(TaskTypeEnum.SPECIAL_RECORDING.getCode());
Map<Integer, Integer> specialMap = toCountMap(
supervisorTaskInfoMapper.countUnfinishedByTaskTypes(supervisorPhone, specialTypes, today, today));
result.setSpecialCount(sumAllValues(specialMap));
}
return result;
}
private Map<Integer, Integer> toCountMap(List<Map<String, Object>> list) {
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyMap();
}
Map<Integer, Integer> map = new HashMap<>();
for (Map<String, Object> row : list) {
Integer taskType = (Integer) row.get("taskType");
Long count = (Long) row.get("count");
if (taskType != null) {
map.put(taskType, count != null ? count.intValue() : 0);
}
}
return map;
}
private int sumCounts(Map<Integer, Integer> countMap, Integer... types) {
int sum = 0;
for (Integer type : types) {
sum += countMap.getOrDefault(type, 0);
}
return sum;
}
private int sumAllValues(Map<Integer, Integer> countMap) {
int sum = 0;
for (Integer val : countMap.values()) {
sum += val;
}
return sum;
}
} }
...@@ -279,4 +279,23 @@ ...@@ -279,4 +279,23 @@
AND CAST(create_time AS DATE) &lt;= #{endDate, jdbcType=DATE} AND CAST(create_time AS DATE) &lt;= #{endDate, jdbcType=DATE}
</select> </select>
<select id="countUnfinishedByTaskTypes" resultType="java.util.HashMap">
SELECT task_type AS taskType, COUNT(*) AS count
FROM supervisor_task_info
WHERE deleted_flag = 'N'
AND supervisor_phone = #{supervisorPhone}
AND task_status != 2
AND task_type IN
<foreach collection="taskTypes" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
<if test="startDate != null and startDate != ''">
AND CAST(create_time AS DATE) &gt;= #{startDate, jdbcType=DATE}
</if>
<if test="endDate != null and endDate != ''">
AND CAST(create_time AS DATE) &lt;= #{endDate, jdbcType=DATE}
</if>
GROUP BY task_type
</select>
</mapper> </mapper>
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

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

    1. 【性能与资源】MyBatis XML 中对时间字段使用函数导致索引失效(重要程度:高)

    • 问题描述:在 ExcellentCaseReviewMapper.xml 和 SupervisorTaskInfoMapper.xml 的新增查询(如 countBySubmitterAndDateRange、countUnfinishedByTaskTypes 等)中,使用了 CAST(create_time AS DATE) >= #{startDate} 或类似的函数转换。
    • 影响分析:在 SQL 的 WHERE 条件中对数据库字段(create_time)使用 CAST() 或 DATE() 等函数,会导致数据库无法使用该字段上的 B-Tree 索引,从而引发全表扫描(Full Table Scan)。随着数据量增长,这将成为严重的性能瓶颈,甚至拖垮数据库。
    • 优化建议:在代码层或 SQL 层将传入的日期字符串拼接为标准的时间范围,直接与原字段进行比较。 ```xml <!-- 优化前 --> AND CAST(create_time AS DATE) >= #{startDate, jdbcType=DATE} AND CAST(create_time AS DATE) <= #{endDate, jdbcType=DATE}

    <!-- 优化后代码示例 --> AND create_time >= CONCAT(#{startDate}, ' 00:00:00') AND create_time <= CONCAT(#{endDate}, ' 23:59:59')

    
    #### 2. 【健壮性】`selectOne` 存在抛出 `TooManyResultsException` 的隐患(重要程度:中)
    - **问题描述**:在 `SupervisorTaskInfoService.updateWeeklyTaskStatus` 方法中,使用了 `SupervisorTaskInfo taskInfo = supervisorTaskInfoMapper.selectOne(condition);` 来查询周任务。
    - **影响分析**:`selectOne` 要求查询结果绝对唯一(0 或 1 条)。在实际生产环境中,由于并发操作、历史脏数据或系统 Bug,极有可能出现同一督导在同一周内生成了两条相同类型的任务记录。此时 `selectOne` 会直接抛出 `TooManyResultsException` 异常,导致整个状态更新流程阻断。
    - **优化建议**:建议使用 `select` 查询列表并取第一条,或在查询条件中加入分页限制(Limit 1)。
      ```java
      // 优化代码示例
      PageHelper.startPage(1, 1);
      List<SupervisorTaskInfo> taskInfos = supervisorTaskInfoMapper.select(condition);
      if (CollectionUtils.isEmpty(taskInfos)) {
          log.warn("未找到对应周任务...");
          return null;
      }
      SupervisorTaskInfo taskInfo = taskInfos.get(0);
    

    3. 【健壮性】RPC 接口层面缺少参数校验触发机制(重要程度:中)

    • 问题描述:新增的 DTO WeeklyTaskStatusReqDTO 中使用了 javax.validation 注解(如 @NotNull, @NotBlank)。但在 Dubbo RPC 的实现类 SupervisorTaskInfoRemoteImpl.updateWeeklyTaskStatus 中,并未见触发校验的机制(如 @Valid 注解或手动校验工具类调用)。
    • 影响分析:如果消费端传入了缺失必填字段的对象(例如 createDate 为空),校验注解不会生效,程序将继续向下执行,最终在 DateUtils.parseDate(createDate) 时引发 NullPointerException 或其他不可控异常,不够优雅且排查成本高。
    • 优化建议:确保 Dubbo 开启了 validation 过滤器,或者在实现类入口处手动调用校验工具类;若接口层无法保障,需在 Service 层加入显式的防御性编程(if (createDate == null) 等)。

    二、评分明细

    评分维度 分数 扣分/得分理由说明
    1. 功能实现与健壮性 (40分) 34分 业务逻辑基本正确且完整。严格遵守了“未变更已有请求参数”的规则。扣分点在于 selectOne 可能引发的异常风险以及 RPC 参数校验未闭环,存在空指针隐患。
    2. 安全性与潜在风险 (30分) 30分 未发现 SQL 注入风险(均使用了 #{}),严格遵守了“未使用子查询”的红线规则,无明显越权或敏感信息泄露风险。满分。
    3. 最佳实践与可维护性 (20分) 18分 代码结构清晰,分层合理,DTO 运用规范,注释较为详尽。少量扣分在于部分统计逻辑(如 getUnfinishedTaskCount)在 Service 层存在较多的魔术拼接,可进一步提炼为专用查询。
    4. 性能与资源利用 (5分) 2分 扣分严重。在 MyBatis 的 Mapper XML 中大范围使用 CAST(create_time AS DATE),这是典型的会导致索引失效、引发慢 SQL 的反模式。
    5. 提交信息质量 (5分) 5分 Commit 信息(如“督导-周报:新增提报优秀案例...”)清晰描述了业务模块、动作和内容,具有很好的可追溯性。满分。

    总分:89分

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