Commit f805f3aa by zhouhaibao

feat(supervisor): 优化门店员工查询和预警功能

- 在EmployeeRanking DTO中添加merchantId字段用于查询结果临时存储
- 升级rpc-dj-smartcarlife-api依赖版本从5.8.1到5.8.2
- 新增StaffAccountAssignMapper及对应XML文件实现门店员工查询功能
- 修改getSupervisorStoreAndEmployeePhoneList方法支持业务类型参数
- 将远程调用改为本地数据库查询提升性能并添加业务场景过滤
- 在StorePerformanceDTO中添加warnList字段存储门店预警信息
- 实现门店预警类型查询功能并按商户分组返回预警集合
- 调整产值显示格式化方法去除万元单位后缀
- 修复门店业绩查询中的参数传递问题
1 parent e828fc25
......@@ -57,6 +57,8 @@ public class EmployeeRanking implements Serializable {
@ApiModelProperty(notes = "个人产值预警等级:0-红,1-黄,2-绿")
private Integer outputWarningLevel;
@ApiModelProperty(notes = "门店ID(用于查询结果临时存储)")
private Integer merchantId;
private List<Integer> merchantIdList;
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package cn.com.glsx.dj.supervisor.dto;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import java.util.Set;
import lombok.Data;
/**
......@@ -33,6 +34,9 @@ public class StorePerformanceDTO extends BasePerformanceDTO {
@ApiModelProperty(notes = "门店区域")
public String region;
@ApiModelProperty(notes = "门店预警列表")
public Set<Integer> warnList;
/**
* 计算二级指标(基于一级指标求和结果计算)
*
......
......@@ -68,7 +68,7 @@
<dependency>
<groupId>cn.com.glsx.dj</groupId>
<artifactId>rpc-dj-smartcarlife-api</artifactId>
<version>5.8.1-SNAPSHOT</version>
<version>5.8.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.com.glsx.dj.peanalysis</groupId>
......
package cn.com.glsx.dj.supervisor.mapper;
import cn.com.glsx.dj.supervisor.dto.EmployeeRanking;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 员工账号分配记录Mapper
*/
@Mapper
public interface StaffAccountAssignMapper {
/**
* 查询门店的员工列表(按手机号去重)
*
* @param merchantIds 商户ID列表
* @param sceneType 场景类型:0-延保权益,1-轻改(可选)
* @return 员工列表
*/
List<EmployeeRanking> selectShopStaffList(@Param("merchantIds") List<Integer> merchantIds,
@Param("sceneType") Byte sceneType);
}
package cn.com.glsx.dj.supervisor.mapper;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -33,4 +35,15 @@ public interface WarningRecordMapper {
List<Integer> selectYellowWarningStoreIds(@Param("periodStart") String periodStart,
@Param("periodEnd") String periodEnd);
/**
* 查询商户的所有预警类型(去重),按商户号分组
*
* @param allMerchantIds 商户ID列表(可选)
* @param periodStart 周期开始时间
* @param periodEnd 周期结束时间
* @return 预警记录列表,每条记录包含 storeId 和 warningTypes
*/
List<Map<String, Object>> selectWarningListByStoreIds(@Param("allMerchantIds") List<Integer> allMerchantIds,
@Param("periodStart") String periodStart,
@Param("periodEnd") String periodEnd);
}
\ No newline at end of file
......@@ -58,7 +58,7 @@ public class SupervisorEmployeeTrainingInfoService {
List<Integer> merchantIds = supervisorDashboardService.getSupervisorMerchants(supervisorUUID,request.getBusinessType());
List<EmployeeRanking> supervisorStoreAndEmployeePhoneList = supervisorDashboardService.getSupervisorStoreAndEmployeePhoneList(
supervisorUUID, merchantIds);
supervisorUUID, merchantIds, request.getBusinessType());
if (CollectionUtils.isEmpty(supervisorStoreAndEmployeePhoneList)) {
log.warn("督导下没有员工信息,supervisorPhone={}", request.getSupervisorPhone());
return new ArrayList<>();
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.glsx.dj.supervisor.mapper.StaffAccountAssignMapper">
<resultMap id="EmployeeRankingResultMap" type="cn.com.glsx.dj.supervisor.dto.EmployeeRanking">
<result column="staff_phone" property="employeePhone" javaType="java.lang.String"/>
<result column="staff_name" property="employeeName" javaType="java.lang.String"/>
<result column="merchant_id" property="merchantId" javaType="java.lang.Integer"/>
</resultMap>
<select id="selectShopStaffList" parameterType="map" resultMap="EmployeeRankingResultMap">
select distinct t1.staff_phone, t3.staff_name, t1.merchant_id
from dj_car_life_staff_account_assign_record t1
left join dj_car_life_staff_account_assign_scene t2 on t1.id = t2.account_record_id
left join dj_car_life_staff_base_info t3 on t1.staff_phone = t3.staff_phone
where t1.deleted_flag = 'N'
<if test="sceneType != null">
and t2.scene_type = #{sceneType}
</if>
<if test="merchantIds != null and merchantIds.size() > 0">
and t1.merchant_id in
<foreach collection="merchantIds" open="(" close=")" separator="," item="item">
#{item}
</foreach>
</if>
group by t1.staff_phone
</select>
</mapper>
......@@ -25,7 +25,24 @@
AND period_end &lt;= #{periodEnd}
GROUP BY store_id
</select>
<select id="selectWarningListByStoreIds" resultType="java.util.HashMap">
SELECT
store_id,
GROUP_CONCAT(DISTINCT warning_type ORDER BY warning_type SEPARATOR ',') as warning_types
FROM warning_record
WHERE deleted_flag = 'N'
AND business_type IN (1, 3)
AND monitor_target = 2
AND period_start &gt;= #{periodStart}
AND period_end &lt;= #{periodEnd}
<if test="allMerchantIds != null and allMerchantIds.size() > 0">
AND store_id IN
<foreach collection="allMerchantIds" item="merchantId" open="(" separator="," close=")">
#{merchantId}
</foreach>
</if>
GROUP BY store_id
</select>
</mapper>
\ No newline at end of file
......@@ -32,7 +32,7 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest {
*/
@Test
public void testGetSupervisorStoreAndEmployeePhoneList() {
List<EmployeeRanking> result = supervisorDashboardService.getSupervisorStoreAndEmployeePhoneList("63c121922cba4956b2aac3f21dacd92f", null);
List<EmployeeRanking> result = supervisorDashboardService.getSupervisorStoreAndEmployeePhoneList("63c121922cba4956b2aac3f21dacd92f", null, 1);
System.out.println("获取员工列表成功,数量:" + (result != null ? result.size() : 0));
}
......@@ -76,7 +76,7 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest {
public void testGetStorePerformanceHarmony() {
SupervisionQueryReq req = new SupervisionQueryReq();
req.setSupervisorId("44c0e96ebd5f4bbb89460e02a1f7c67a");
req.setStartDate("2026-01-01");
req.setStartDate("2026-05-01");
req.setEndDate("2026-05-31");
req.setDateType(4); // 月
req.setStatDimension(2); // 问界鸿蒙--延保权益
......
  • @wangxw1
    commented
    Master

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

    1. SQL 语法隐患与业务数据丢失风险(严重)

    • 问题描述:在 StaffAccountAssignMapper.xml 的 selectShopStaffList 查询中,使用了 group by t1.staff_phone,但 SELECT 列表中包含了非聚合列 t3.staff_name 和 t1.merchant_id。
    • 影响分析:
      1. 稳定性风险:如果生产环境 MySQL 开启了 ONLY_FULL_GROUP_BY 模式(MySQL 5.7+ 默认开启),该 SQL 执行时会直接报错,导致核心业务瘫痪。
      2. 业务逻辑冲突:Java 层的 getSupervisorStoreAndEmployeePhoneList 方法中,特意编写了按手机号去重并聚合归属多个门店(merchantIdList.add(employee.getMerchantId()))的逻辑。但由于 SQL 层已经做了 GROUP BY,数据库只会随机返回该员工关联的其中一个门店 ID,导致 Java 层的门店聚合逻辑失效,数据丢失。
    • 优化建议:移除 SQL 中的 GROUP BY 子句,完全交由 Java 层的 uniqueStaffMap 逻辑进行去重和门店聚合。 xml <!-- 修改建议 --> <select id="selectShopStaffList" parameterType="map" resultMap="EmployeeRankingResultMap"> select distinct t1.staff_phone, t3.staff_name, t1.merchant_id from dj_car_life_staff_account_assign_record t1 left join dj_car_life_staff_account_assign_scene t2 on t1.id = t2.account_record_id left join dj_car_life_staff_base_info t3 on t1.staff_phone = t3.staff_phone where t1.deleted_flag = 'N' <if test="sceneType != null"> and t2.scene_type = #{sceneType} </if> <if test="merchantIds != null and merchantIds.size() > 0"> and t1.merchant_id in <foreach collection="merchantIds" open="(" close=")" separator="," item="item"> #{item} </foreach> </if> <!-- 移除 group by t1.staff_phone --> </select>

    2. 集合为空导致的慢查询与全表扫描风险(高)

    • 问题描述:在 WarningRecordMapper.xml 的 selectWarningListByStoreIds 中,如果传入的 allMerchantIds 为空集合,<if> 标签内的 IN 条件将不会生效。
    • 影响分析:当 allMerchantIds 为空时,该 SQL 会蜕变为仅按时间范围查询整个 warning_record 表,并对全表数据进行 GROUP_CONCAT 分组。这会引发严重的性能瓶颈,甚至拖垮数据库。
    • 优化建议:在 Java 层的 getWarningListByStoreIds 方法入口处进行防御性编程,如果传入的商户 ID 列表为空,直接返回空结果,避免发起无意义且高风险的数据库查询。 java // SupervisorDashboardService.java 中的修改建议 private Map<Integer, Set<Integer>> getWarningListByStoreIds(List<Integer> allMerchantIds, String startDate, String endDate) { if (allMerchantIds == null || allMerchantIds.isEmpty()) { return new HashMap<>(); } List<Map<String, Object>> result = warningRecordMapper.selectWarningListByStoreIds(allMerchantIds, startDate, endDate); // ... 后续逻辑保持不变 }

    3. 数据库 GROUP_CONCAT 结果类型强转风险(中)

    • 问题描述:在 SupervisorDashboardService.java 的 getWarningListByStoreIds 方法中,直接将 Map 中的结果强转为 String:String warningTypesStr = (String) row.get("warning_types");。
    • 影响分析:在 MyBatis + MySQL 的环境下,GROUP_CONCAT 的返回类型并不总是 String。当拼接的字符串较长或受到特定 MySQL 驱动版本影响时,它可能会返回 byte[]。此时直接 (String) 强转会触发 ClassCastException 导致运行时异常。
    • 优化建议:使用安全的数据类型转换方式,例如 String.valueOf(),或者在判断非空后进行处理。 java // 修改建议 Object warningTypesObj = row.get("warning_types"); if (warningTypesObj != null) { String warningTypesStr = warningTypesObj instanceof byte[] ? new String((byte[]) warningTypesObj) : String.valueOf(warningTypesObj); if (!warningTypesStr.isEmpty()) { // ... split 逻辑 } }

    二、评分明细

    1. 功能实现的正确性与健壮性(26/40分)

      • 理由:扣分主要由于 SQL 的 GROUP BY 导致 Java 层的多门店聚合逻辑失效,存在明显的业务逻辑冲突;且未考虑空集合传入导致的非预期全表查询。未发现 MyBatis 子查询,也未发现 Dubbo Remote 接口参数变更。
    2. 安全性与潜在风险(30/30分)

      • 理由:MyBatis 参数均正确使用了 #{} 进行预编译绑定,无 SQL 注入风险。未见越权或敏感信息泄露等安全隐患。
    3. 最佳实践与可维护性(16/20分)

      • 理由:将原本的 RPC 调用改为本地 Mapper 查询以提升性能,在单体/紧耦合架构下可行,但需要注意服务边界。Java 层对 Map<String, Object> 的处理不够健壮(直接强转),存在隐患。代码注释较清晰。
    4. 性能与资源利用(3/5分)

      • 理由:本地化查询减少了网络开销,有助于性能提升。但 WarningRecordMapper 缺乏对空参数的拦截,存在潜在的慢查询和内存消耗风险。
    5. 提交信息质量(5/5分)

      • 理由:Commit message 结构清晰,详细列出了修改点(如添加字段、版本升级、新增 Mapper、性能优化等),可追溯性极佳。

    总分:80分

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