Commit cab7dcbe by 杨卫

fix(1028805): 更新日报记录统计查询逻辑

- 修改countByStaffUuidsAndDateRange方法以支持两表联合统计
- 添加dj_car_life_staff_daily_report表的数据统计
- 优化SQL查询结构以实现日常报表和员工日报数据合并计算
- 更新注释说明以明确双表统计功能
- 保持原有查询参数和返回值兼容性
1 parent 96d3d352
...@@ -18,17 +18,29 @@ ...@@ -18,17 +18,29 @@
AND report_date <= #{endDate} AND report_date <= #{endDate}
</select> </select>
<!-- 统计指定人员在时间范围内的日报记录数 --> <!-- 统计指定人员在时间范围内的日报记录数(日常报表 + 员工日报两张表之和) -->
<select id="countByStaffUuidsAndDateRange" resultType="int"> <select id="countByStaffUuidsAndDateRange" resultType="int">
SELECT COUNT(1) SELECT
FROM dj_car_life_daily_report_record (SELECT COUNT(1)
WHERE merchant_id = #{merchantId} FROM dj_car_life_daily_report_record
AND staff_uuid IN WHERE merchant_id = #{merchantId}
<foreach collection="staffUuids" item="uuid" open="(" separator="," close=")"> AND staff_uuid IN
#{uuid} <foreach collection="staffUuids" item="uuid" open="(" separator="," close=")">
</foreach> #{uuid}
AND scene_type = #{sceneType} </foreach>
AND report_date &gt;= #{startDate} AND scene_type = #{sceneType}
AND report_date &lt;= #{endDate} AND report_date &gt;= #{startDate}
AND report_date &lt;= #{endDate})
+
(SELECT COUNT(1)
FROM dj_car_life_staff_daily_report
WHERE merchant_id = #{merchantId}
AND staff_uuid IN
<foreach collection="staffUuids" item="uuid" open="(" separator="," close=")">
#{uuid}
</foreach>
AND scene_type = #{sceneType}
AND report_date &gt;= #{startDate}
AND report_date &lt;= #{endDate}) AS total_sum
</select> </select>
</mapper> </mapper>
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 违反强制规范:Mybatis SQL 中使用了子查询(最高优先级)

    • 问题描述:在修改后的 countByStaffUuidsAndDateRange SQL 中,使用了 SELECT (SELECT ...) + (SELECT ...) AS total_sum 的语法结构,这明确违反了“mybatis的sql语句不允许使用子查询”的重要规则。
    • 影响分析:子查询在部分 MySQL 版本中可能会导致执行计划不佳,产生临时表或无法有效利用索引,从而引发性能瓶颈。此外,将两张无直接关联的表的统计逻辑在数据库层强行拼接,增加了数据库的计算压力。
    • 优化建议:将该复杂查询拆分为两个独立的单表查询,并在 Java 业务层(Service 层)进行结果的相加。这不仅符合无子查询的规范,还能将计算压力分摊到应用层,提升数据库性能和代码的可维护性。

    代码示例: Mapper.xml (拆分为两个查询)

      <!-- 统计日常报表记录数 -->
      <select id="countDailyReportByStaffUuidsAndDateRange" resultType="int">
        SELECT COUNT(1)
        FROM dj_car_life_daily_report_record
        WHERE merchant_id = #{merchantId}
          AND staff_uuid IN
          <foreach collection="staffUuids" item="uuid" open="(" separator="," close=")">
            #{uuid}
          </foreach>
          AND scene_type = #{sceneType}
          AND report_date &gt;= #{startDate}
          AND report_date &lt;= #{endDate}
      </select>
    
      <!-- 统计员工日报记录数 -->
      <select id="countStaffDailyReportByStaffUuidsAndDateRange" resultType="int">
        SELECT COUNT(1)
        FROM dj_car_life_staff_daily_report
        WHERE merchant_id = #{merchantId}
          AND staff_uuid IN
          <foreach collection="staffUuids" item="uuid" open="(" separator="," close=")">
            #{uuid}
          </foreach>
          AND scene_type = #{sceneType}
          AND report_date &gt;= #{startDate}
          AND report_date &lt;= #{endDate}
      </select>
    

    Java Service 层 (聚合数据)

    int dailyReportCount = mapper.countDailyReportByStaffUuidsAndDateRange(merchantId, staffUuids, sceneType, startDate, endDate);
    int staffReportCount = mapper.countStaffDailyReportByStaffUuidsAndDateRange(merchantId, staffUuids, sceneType, startDate, endDate);
    int totalSum = dailyReportCount + staffReportCount;
    

    (注:本次审查仅发现上述 1 个核心问题,按实际数量输出)


    二、评分明细

    • 功能实现的正确性与健壮性(25/40分):业务逻辑上实现了两表数据的求和,但直接违反了“不允许使用子查询”的硬性红线规则,存在潜在的健壮性隐患,故扣除15分。
    • 安全性与潜在风险(30/30分):SQL 参数传递均规范使用了 #{} 以及 <foreach> 标签,有效防止了 SQL 注入风险,未见越权及敏感信息泄露问题。
    • 最佳实践与可维护性(12/20分):将多表聚合逻辑下沉到数据库层不是良好的架构实践。推荐将计算逻辑上浮到 Java 业务层,保持 SQL 的简单纯粹,有利于后续的单表扩展、分库分表及缓存优化。
    • 性能与资源利用(3/5分):子查询的执行效率通常不如单表简单查询,在数据量较大的情况下,可能会造成数据库 CPU 资源的额外消耗。
    • 提交信息质量(5/5分):Commit message 采用了规范的 fix(issue_id) 格式,清晰地描述了修改内容、背景(两表联合统计)以及兼容性说明,质量很高。

    总分:75分

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