Commit 448d8af2 by 范富荣

优化查询督导员工列表逻辑

1 parent 01b51efd
...@@ -1167,7 +1167,7 @@ public class StaffBaseInfoService { ...@@ -1167,7 +1167,7 @@ public class StaffBaseInfoService {
List<StaffBaseInfoDTO> list = staffBaseInfoMapper.listShopStaffInfoGroupByUUid(condition); List<StaffBaseInfoDTO> list = staffBaseInfoMapper.listShopStaffInfoGroupByUUid(condition);
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
//过滤角色,只要驻店人员 //过滤角色,只要驻店人员
return filterStaffRole(list, 1); return filterStaffRole(list, 0);
} }
return null; return null;
} }
...@@ -1179,9 +1179,14 @@ public class StaffBaseInfoService { ...@@ -1179,9 +1179,14 @@ public class StaffBaseInfoService {
* @return * @return
*/ */
public List<StaffBaseInfoDTO> filterStaffRole(List<StaffBaseInfoDTO> list, int flag) { public List<StaffBaseInfoDTO> filterStaffRole(List<StaffBaseInfoDTO> list, int flag) {
//获取园区店集合
List<Integer> merchantIdList = list.stream().filter(x -> x.getMerchantId() != null).map(StaffBaseInfoDTO::getMerchantId).distinct().collect(Collectors.toList());
if (CollectionUtils.isEmpty(merchantIdList)) return null;
MerchantStaffRequest staffRequest = new MerchantStaffRequest(); MerchantStaffRequest staffRequest = new MerchantStaffRequest();
staffRequest.setRoleName(env.getProperty("glsk_staff_role_name", String.class, "驻店人员")); staffRequest.setRoleName(env.getProperty("glsk_staff_role_name", String.class, "驻店人员"));
staffRequest.setDelFlag((byte) 0); staffRequest.setDelFlag((byte) 0);
//设置门店列表
staffRequest.setMerchantNoList(merchantIdList);
if (flag == 0) { if (flag == 0) {
List<String> uuidList = list.stream().filter(x -> StringUtils.isNotBlank(x.getLoginUuid())).map(StaffBaseInfoDTO::getLoginUuid).collect(Collectors.toList()); List<String> uuidList = list.stream().filter(x -> StringUtils.isNotBlank(x.getLoginUuid())).map(StaffBaseInfoDTO::getLoginUuid).collect(Collectors.toList());
if (CollectionUtils.isEmpty(uuidList)) return null; if (CollectionUtils.isEmpty(uuidList)) return null;
...@@ -1204,7 +1209,7 @@ public class StaffBaseInfoService { ...@@ -1204,7 +1209,7 @@ public class StaffBaseInfoService {
} }
} else { } else {
log.error("查询督导人员列表失败,请求参数={}, message={}", JSON.toJSONString(staffRequest), response.getMessage()); log.error("查询督导人员列表失败,请求参数={}, message={}", JSON.toJSONString(staffRequest), response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.RPC_ERROR, response.getMessage()); return null;
} }
} }
......
...@@ -271,7 +271,7 @@ ...@@ -271,7 +271,7 @@
</select> </select>
<select id="listShopStaffBaseInfo" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap"> <select id="listShopStaffBaseInfo" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap">
select si.staff_name, si.phone_number as staff_phone select si.staff_name, si.phone_number as staff_phone,smf.sub_merchant_id as merchant_id
from dj_car_life_scene_merchant_ref smf from dj_car_life_scene_merchant_ref smf
inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id
where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N' where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N'
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
<select id="listShopStaffInfoGroupByUUid" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap"> <select id="listShopStaffInfoGroupByUUid" parameterType="cn.com.glsx.dj.smartcarlife.dto.merchantPerformanceConfig.StaffBaseInfoDTO" resultMap="DTOResultMap">
select si.staff_uuid, si.staff_name, si.phone_number as staff_phone select si.staff_uuid, si.staff_name, si.phone_number as staff_phone,smf.sub_merchant_id as merchant_id
from dj_car_life_scene_merchant_ref smf from dj_car_life_scene_merchant_ref smf
inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id inner join dj_car_life_staff_info si on smf.sub_merchant_id = si.merchant_id
where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N' where smf.deleted_flag = 'N' and smf.sub_merchant_type in(6,8) and si.deleted_flag = 'N'
......
...@@ -32,7 +32,7 @@ public class StaffBaseInfoServiceTest extends BaseServiceTest{ ...@@ -32,7 +32,7 @@ public class StaffBaseInfoServiceTest extends BaseServiceTest{
@Test @Test
public void listShopStaffBaseInfoTest() throws Exception { public void listShopStaffBaseInfoTest() throws Exception {
StaffBaseInfoDTO condition = new StaffBaseInfoDTO(); StaffBaseInfoDTO condition = new StaffBaseInfoDTO();
condition.setMerchantIdList(Arrays.asList(44209390)); condition.setMerchantIdList(Arrays.asList(44211547));
List<StaffBaseInfoDTO> list = staffBaseInfoService.listShopStaffBaseInfo(condition); List<StaffBaseInfoDTO> list = staffBaseInfoService.listShopStaffBaseInfo(condition);
System.out.println( list); System.out.println( list);
List<StaffBaseInfoDTO> listByUUid = staffBaseInfoService.listShopStaffInfoGroupByUUid(condition); List<StaffBaseInfoDTO> listByUUid = staffBaseInfoService.listShopStaffInfoGroupByUUid(condition);
......
  • @wangxw1
    commented
    Master

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

    1. 异常吞没与潜在的空指针风险(功能与健壮性)

    • 问题描述:在 StaffBaseInfoService.java 第 1212 行,当查询督导人员列表的 RPC 接口返回失败时,原逻辑是抛出 RpcServiceException 异常,修改后改为了 return null;。
    • 影响分析:直接 return null 会吞没底层的 RPC 错误,导致上层调用方无法感知到系统异常(误以为是查询结果为空)。同时,如果上层调用方没有对返回值进行严格的 null 判断,极易引发 NullPointerException。
    • 优化建议:建议恢复抛出业务异常,让上层统一进行异常拦截处理;如果业务场景明确要求“RPC失败时降级为无数据”,也应该返回空集合而不是 null。 java // 建议恢复异常抛出,或返回空集合 log.error("查询督导人员列表失败,请求参数={}, message={}", JSON.toJSONString(staffRequest), response.getMessage()); // 方案A:抛出异常(推荐) throw new RpcServiceException(DefaultErrorEnum.RPC_ERROR, response.getMessage()); // 方案B:业务降级返回空集合 // return Collections.emptyList();

    2. 集合类型返回值不规范(最佳实践与可维护性)

    • 问题描述:在 StaffBaseInfoService.java 中新增的逻辑 if (CollectionUtils.isEmpty(merchantIdList)) return null;,以及异常分支的 return null;,对于返回类型为 List 的方法直接返回了 null。
    • 影响分析:返回 null 代替空集合是 Java 开发中的反模式(Anti-pattern)。这会强制所有调用该方法的客户端都必须编写防御性的 null 检查代码,增加了代码的冗余度并提高了出错概率。
    • 优化建议:对于返回集合(List、Set、Map等)的方法,当没有数据或提前返回时,应始终返回空集合。 java if (CollectionUtils.isEmpty(merchantIdList)) { return Collections.emptyList(); }

    3. “魔法值”的使用降低代码可读性(最佳实践与可维护性)

    • 问题描述:在 StaffBaseInfoService.java 第 1170 行,调用方法由 filterStaffRole(list, 1) 改为了 filterStaffRole(list, 0)。同时 filterStaffRole 方法内部依赖 if (flag == 0) 进行逻辑分支判断。
    • 影响分析:0 和 1 是典型的“魔法值”(Magic Number),缺乏语义。维护者在阅读代码时,无法直观理解 0 代表什么过滤逻辑,1 又代表什么,增加了代码的理解成本和后续维护的风险。
    • 优化建议:使用具有明确语义的常量或枚举来代替魔法值。 ```java private static final int FILTER_BY_UUID = 0; // private static final int FILTER_BY_OTHER = 1;

    // 调用时 return filterStaffRole(list, FILTER_BY_UUID);

    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(34/40分)**:
      SQL 变更符合要求,未使用子查询,Stream API 提取 ID 逻辑正确。但将 RPC 异常处理改为返回 `null` 严重降低了代码的健壮性,存在吞没错误和引发 NPE 的隐患,扣除 6 分。
    - **安全性与潜在风险(30/30分)**:
      MyBatis 使用了标准的配置,未引入新的 SQL 注入风险;无越权、XSS 或敏感信息泄露等安全问题,得满分。
    - **最佳实践与可维护性(15/20分)**:
      方法签名中的入参和内部逻辑强依赖缺乏语义的“魔法值”,且多处对于 List 类型的返回值直接 `return null` 不符合 Java 编码规范,扣除 5 分。
    - **性能与资源利用(5/5分)**:
      SQL 仅增加了查询字段,未改变索引命中逻辑;Java 内存操作使用了 `Stream` 的 `distinct()` 进行去重,资源利用合理,无性能瓶颈,得满分。
    - **提交信息质量(3/5分)**:
      Commit message "优化查询督导员工列表逻辑" 过于简略,仅说明了“做了什么(What)”,未说明“为什么做(Why)”(例如为何要提取 merchantId,为何要吞没异常),不利于后续追溯,扣除 2 分。
    
    总分:87分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!