Commit 72decf86 by 谢海旭

飞行督导:调整查询驻店带教列表逻辑

1 parent e97ee3b3
......@@ -202,19 +202,25 @@
<select id="queryCoachingTrainingList" resultType="cn.com.glsx.dj.supervisor.dto.TrainingListDTO">
SELECT
id AS taskId,
DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') AS completedTime
FROM supervisor_task_info
WHERE deleted_flag = 'N'
AND task_type = 5
AND store_manager_phone = #{storeManagerPhone}
<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>
ORDER BY create_time DESC
ti.id AS taskId,
DATE_FORMAT(ti.create_time, '%Y-%m-%d %H:%i:%s') AS completedTime
FROM supervisor_task_info ti
WHERE ti.deleted_flag = 'N'
AND ti.task_type = 5
AND EXISTS (
SELECT 1
FROM supervisor_task_participant tp
WHERE tp.task_id = ti.id
AND tp.participant_phone = #{storeManagerPhone}
AND tp.deleted_flag = 'N'
)
<if test="startDate != null and startDate != ''">
AND CAST(ti.create_time AS DATE) &gt;= #{startDate, jdbcType=DATE}
</if>
<if test="endDate != null and endDate != ''">
AND CAST(ti.create_time AS DATE) &lt;= #{endDate, jdbcType=DATE}
</if>
ORDER BY ti.create_time DESC
</select>
<select id="queryMerchantBySupervisorPhone" resultType="java.lang.Integer">
SELECT DISTINCT merchant_id
......
package cn.com.glsx.dj.supervisor.service;
import cn.com.glsx.dj.mindworkplate.dto.residentpoint.StaffBaseInfoDTO;
import cn.com.glsx.dj.smartcarlife.dto.ShopMerchantDirectorConfigDTO;
import cn.com.glsx.dj.smartcarlife.enums.DirectorTypeEnum;
import cn.com.glsx.dj.smartcarlife.remote.ShopMerchantDirectorConfigRemote;
......@@ -385,4 +386,28 @@ public class SupervisorTaskInfoServiceTest extends BaseServiceTest {
supervisorTaskInfoService.updateWeeklyTaskStatus(request);
System.out.println(JSON.toJSONString(request));
}
@Test
public void testPageStaffBaseInfo() {
RpcPagination<SupervisorTeamStaffQueryDTO> pagination = new RpcPagination<>();
pagination.setPageNum(1);
pagination.setPageSize(10);
SupervisorTeamStaffQueryDTO condition = new SupervisorTeamStaffQueryDTO();
condition.setSupervisorPhone("17680252939");
pagination.setCondition(condition);
Page<SupervisorTeamStaffDTO> result = supervisorTaskInfoService.pageStaffBaseInfo(pagination);
System.out.println(JSON.toJSONString(result));
}
@Test
public void testPageTeachingRecords() {
RpcPagination<SupervisorTaskQueryDTO> pagination = new RpcPagination<>();
pagination.setPageNum(1);
pagination.setPageSize(10);
SupervisorTaskQueryDTO condition = new SupervisorTaskQueryDTO();
condition.setId(217L);
pagination.setCondition(condition);
Page<SupervisorTeachingListDTO> result = supervisorTaskInfoService.pageTeachingRecords(pagination);
System.out.println(JSON.toJSONString(result));
}
}
\ No newline at end of file
  • @wangxw1
    commented
    Master

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

    1. 严重违反规范:MyBatis SQL 语句中使用了子查询

    • 问题描述:在 SupervisorTaskInfoMapper.xml 的 queryCoachingTrainingList 方法中,新增了 EXISTS (SELECT 1 FROM supervisor_task_participant ...) 子查询。这直接违反了“mybatis的sql语句不允许使用子查询”的强制性规定。
    • 影响分析:强制规则的违反会破坏团队架构的一致性;在某些数据库版本或大数据量场景下,子查询可能导致执行计划非最优,引发慢 SQL。
    • 优化建议:将 EXISTS 子查询改写为 INNER JOIN,并配合 DISTINCT 或 GROUP BY 保证数据不重复。 xml <!-- 优化后的 SQL 示例 --> SELECT DISTINCT ti.id AS taskId, DATE_FORMAT(ti.create_time, '%Y-%m-%d %H:%i:%s') AS completedTime FROM supervisor_task_info ti INNER JOIN supervisor_task_participant tp ON tp.task_id = ti.id WHERE ti.deleted_flag = 'N' AND ti.task_type = 5 AND tp.participant_phone = #{storeManagerPhone} AND tp.deleted_flag = 'N' <!-- 省略下方原有的时间判断逻辑 -->

    2. 性能隐患:在 WHERE 条件中对索引列使用函数

    • 问题描述:在 <if> 动态 SQL 中,使用了 CAST(ti.create_time AS DATE) >= #{startDate, jdbcType=DATE}。
    • 影响分析:对 create_time 字段使用 CAST 函数会导致该字段上的索引失效(即使有索引也会退化为全表扫描),随着数据量增长,这会成为明显的性能瓶颈。
    • 优化建议:在 Java 代码层面将传入的 startDate 和 endDate 处理为标准的 yyyy-MM-dd HH:mm:ss 格式字符串(如拼接 00:00:00 和 23:59:59),然后在 SQL 中直接进行大小比较。 xml <!-- 优化后的 SQL 示例 --> <if test="startDate != null and startDate != ''"> AND ti.create_time &gt;= #{startDate} </if> <if test="endDate != null and endDate != ''"> AND ti.create_time &lt;= #{endDate} </if>

    3. 工程最佳实践:单元测试缺乏断言(Assertion)

    • 问题描述:在 SupervisorTaskInfoServiceTest.java 中新增的两个单元测试 testPageStaffBaseInfo 和 testPageTeachingRecords,仅使用了 System.out.println(JSON.toJSONString(result)); 来输出结果。
    • 影响分析:仅仅打印结果需要人工肉眼核对,失去了自动化测试的意义,无法在未来的 CI/CD 流程中自动拦截错误逻辑。
    • 优化建议:移除 System.out.println,使用 JUnit 提供的断言方法来验证结果的正确性。 java // 优化代码示例 Page<SupervisorTeamStaffDTO> result = supervisorTaskInfoService.pageStaffBaseInfo(pagination); Assert.assertNotNull("返回结果不应为空", result); Assert.assertTrue("返回记录数应大于0", result.getTotal() > 0);

    二、评分明细

    • 功能实现的正确性与健壮性(20/40分): 扣分原因:直接违反了“MyBatis 不允许使用子查询”的硬性规定(扣20分)。逻辑虽然在功能上可能跑通,但不符合系统强制约束。
    • 安全性与潜在风险(30/30分): 满分。未发现 SQL 注入风险(使用了 #{}),无越权或敏感信息泄露问题。
    • 最佳实践与可维护性(15/20分): 扣分原因:单元测试代码仅做控制台打印,未使用断言验证结果(扣5分),不符合自动化测试最佳实践。
    • 性能与资源利用(2/5分): 扣分原因:SQL 中对 create_time 使用 CAST 函数导致潜在的索引失效与全表扫描风险(扣3分)。
    • 提交信息质量(4/5分): 扣分原因:“飞行督导:调整查询驻店带教列表逻辑” 简明扼要,但若能一句话补充说明“将按店长手机号查询改为按参与人关联查询”会更利于追溯(扣1分)。

    总分:71分

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