Commit 30ac184e by 谢海旭

策略中心:调整PC端创建逻辑

1 parent 3a2ad51d
......@@ -243,7 +243,76 @@ public class StrategyInfoService {
//批量插入策略基础信息
strategyInfoMapper.insertList(strategyInfoList);
//批量插入策略人员关联信息
strategyStaffRelService.batchInsert(strategyInfoList, batchStrategyInfo.getStaffList(), SceneTypeEnum.SCENE1_0.getValue());
List<StrategyStaffRel> staffList = batchStrategyInfo.getStaffList();
if (CollectionUtil.isNotEmpty(staffList)) {
// staffList分驻店和非驻店两部分
List<StrategyStaffRel> onSiteStaffList = staffList.stream()
.filter(x -> x.getUserPosition() != null && x.getUserPosition().intValue() == 0)
.collect(Collectors.toList());
List<StrategyStaffRel> offSiteStaffList = staffList.stream()
.filter(x -> x.getUserPosition() == null || x.getUserPosition().intValue() != 0)
.collect(Collectors.toList());
// 非驻店人员:沿用原有批量插入逻辑
if (CollectionUtil.isNotEmpty(offSiteStaffList)) {
strategyStaffRelService.batchInsert(strategyInfoList, offSiteStaffList, SceneTypeEnum.SCENE1_0.getValue());
}
// 驻店人员:逐门店查询uuid后插入
if (CollectionUtil.isNotEmpty(onSiteStaffList)) {
// 业务类型 → 场景类型映射:延保/权益(1/3) → 0,轻改(2) → 1
Byte sceneType;
if (baseStrategyInfo.getBiz() != null && baseStrategyInfo.getBiz().intValue() == 2) {
sceneType = SceneTypeEnum.SCENE1_1.getValue();
} else {
sceneType = SceneTypeEnum.SCENE1_0.getValue();
}
List<String> phoneNumberList = onSiteStaffList.stream()
.map(StrategyStaffRel::getUserPhone)
.filter(StrUtil::isNotBlank)
.distinct()
.collect(Collectors.toList());
for (StrategyInfo strategyInfo : strategyInfoList) {
List<StrategyStaffRel> staffListAll = new ArrayList<>();
// 逐门店查询驻店人员信息(获取uuid)
cn.com.glsx.dj.smartcarlife.dto.StaffInfoDTO condition = new cn.com.glsx.dj.smartcarlife.dto.StaffInfoDTO();
condition.setMerchantIdList(Collections.singletonList(strategyInfo.getMerchantId()));
condition.setSceneType(sceneType);
condition.setPhoneNumberList(phoneNumberList);
try {
RpcResponse<List<cn.com.glsx.dj.smartcarlife.dto.StaffInfoDTO>> listRpcResponse =
staffInfoRemote.listStaffByPhoneAndMerchant(condition);
if (listRpcResponse.isSuccess() && CollectionUtil.isNotEmpty(listRpcResponse.getResult())) {
for (cn.com.glsx.dj.smartcarlife.dto.StaffInfoDTO dto : listRpcResponse.getResult()) {
StrategyStaffRel rel = new StrategyStaffRel();
rel.setUserUuid(dto.getStaffUuid());
rel.setUserPhone(dto.getPhoneNumber());
rel.setUserName(dto.getStaffName());
rel.setUserPosition(0);
rel.setStrategyId(strategyInfo.getId());
rel.setCreatedBy(strategyInfo.getCreatedBy());
rel.setCreatedDate(strategyInfo.getCreatedDate());
rel.setDeletedFlag(DeleteFlagEnum.N.getValue());
staffListAll.add(rel);
}
} else {
log.warn("门店无可匹配的驻店人员,merchantId:{}", strategyInfo.getMerchantId());
}
} catch (Exception e) {
log.warn("查询门店驻店人员信息异常,merchantId:{}", strategyInfo.getMerchantId(), e);
}
if (CollectionUtil.isNotEmpty(staffListAll)) {
strategyStaffRelService.singleInsert(strategyInfo, staffListAll, SceneTypeEnum.SCENE1_0.getValue());
}
}
}
}
//批量插入策略课程关联关系
strategyCourseRelService.batchInsert(strategyInfoList, batchStrategyInfo.getCourseList());
//批量插入策略-录音关联关系
......
......@@ -80,6 +80,41 @@ public class StrategyStaffRelService {
return strategyStaffRelMapper.insertList(all);
}
/**
* 单个策略插入策略人员关联关系
* @param strategyInfo
* @param staffList
* @return
*/
public int singleInsert(StrategyInfo strategyInfo, List<StrategyStaffRel> staffList, Byte sceneType) {
if (CollectionUtils.isEmpty(staffList)) return 0;
//根据手机号,查询北森人员userId
List<String> phoneList = staffList.stream().map(StrategyStaffRel::getUserPhone).collect(Collectors.toList());
List<BeiSenEmployeeListResonseDTO> employeeList = listEmployeeInfoByStaffPhone(phoneList);
Map<String, Integer> phoneUserIdMap = Collections.EMPTY_MAP;
if (CollectionUtils.isNotEmpty(employeeList)) {
//构建map
phoneUserIdMap = employeeList.stream().collect(Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone, BeiSenEmployeeListResonseDTO::getUserID));
}
//根据员工id查询 员工信息
//构建对象,遍历2个对象创建新StrategyStaffRel
List<StrategyStaffRel> all = new ArrayList<>();
for (StrategyStaffRel staff : staffList) {
StrategyStaffRel rel = new StrategyStaffRel();
rel.setUserUuid(staff.getUserUuid());
rel.setUserName(staff.getUserName());
rel.setStrategyId(strategyInfo.getId());
rel.setUserPosition(staff.getUserPosition());
rel.setUserPhone(staff.getUserPhone());
rel.setCreatedBy(strategyInfo.getCreatedBy());
rel.setCreatedDate(strategyInfo.getCreatedDate());
rel.setDeletedFlag(DeleteFlagEnum.N.getValue());
rel.setBeisenUserId(phoneUserIdMap.get(staff.getUserPhone()));
all.add(rel);
}
return strategyStaffRelMapper.insertList(all);
}
public Map<String, StaffInfoDTO> queryUserMap(List<StrategyInfo> strategyInfoList, List<StrategyStaffRel> staffList, Byte sceneType) {
//根据员工id查询 员工信息
List<String> userUuidList = staffList.stream().map(x -> x.getUserUuid()).collect(Collectors.toList());
......
  • @wangxw1
    commented
    Master

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

    1. 严重的性能瓶颈:循环内进行 RPC 调用与数据库操作(N+1 问题)

    • 问题描述:在 StrategyInfoService.java 中,针对驻店人员的处理逻辑遍历了 strategyInfoList。在 for 循环内部,不仅发起了 RPC 调用 (staffInfoRemote.listStaffByPhoneAndMerchant),还在调用 strategyStaffRelService.singleInsert 时,其内部又触发了查询北森人员信息的调用 (listEmployeeInfoByStaffPhone) 以及数据库的 insertList 操作。
    • 影响分析:这是典型的 N+1 性能陷阱。如果 strategyInfoList 数据量较大(如批量导入数十上百个门店),将导致数十上百次的网络 RPC 请求和数据库连接交互,极易引发接口响应超时、连接池耗尽,甚至拖垮下游 Dubbo 服务提供者。
    • 优化建议:将循环内的 RPC 和 DB 操作提取到循环外进行批量处理。先收集所有的 merchantId,发起一次批量 RPC 查询;在内存中按 merchantId 进行分组(Grouping);最后构建好所有的关联关系后,执行一次批量的 DB 插入。
    • 代码示例(思路): ```java // 1. 收集所有需要的 merchantId List allMerchantIds = strategyInfoList.stream().map(StrategyInfo::getMerchantId).collect(Collectors.toList()); condition.setMerchantIdList(allMerchantIds);

    // 2. 循环外一次性 RPC 调用获取所有门店的驻店人员 RpcResponse> response = staffInfoRemote.listStaffByPhoneAndMerchant(condition); // 3. 在内存中按 merchantId 分组 Map> staffMap = response.getResult().stream() .collect(Collectors.groupingBy(StaffInfoDTO::getMerchantId));

    // 4. 遍历 strategyInfoList,从内存 Map 中取值构建实体,最后在循环外统一 batchInsert

    
    **2. 健壮性风险:`Collectors.toMap` 可能引发 `IllegalStateException`**
    - **问题描述**:在 `StrategyStaffRelService.java` 的 `singleInsert` 方法中,将 `employeeList` 转换为 Map 时使用了 `Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone, BeiSenEmployeeListResonseDTO::getUserID)`。
    - **影响分析**:如果北森系统返回的人员列表中存在相同的手机号(重复数据),`Collectors.toMap` 会直接抛出 `IllegalStateException: Duplicate key` 异常,导致整个保存逻辑中断,业务报错。
    - **优化建议**:为 `toMap` 提供第三个参数(Merge Function),以处理键冲突的情况,通常保留第一个或最后一个即可。
    - **代码示例**:
    ```java
    // 增加 (v1, v2) -> v1 解决重复 key 报错问题
    phoneUserIdMap = employeeList.stream().collect(Collectors.toMap(
        BeiSenEmployeeListResonseDTO::getMobilePhone, 
        BeiSenEmployeeListResonseDTO::getUserID, 
        (v1, v2) -> v1
    ));
    

    3. 最佳实践缺陷:存在未使用的参数与不规范的空集合定义

    • 问题描述:
      1. StrategyStaffRelService.java 的 singleInsert 方法签名中定义了 Byte sceneType 参数,但方法体内完全没有使用该参数。
      2. 使用了 Collections.EMPTY_MAP 来初始化 Map。
    • 影响分析:未使用的参数会增加代码阅读的认知负担,属于典型的 Dead Code。Collections.EMPTY_MAP 是未泛型化的原生类型(Raw Type),在现代 Java 开发中不推荐使用,可能会引起编译器的 unchecked 警告。
    • 优化建议:移除 singleInsert 方法中无用的 sceneType 参数;将 Collections.EMPTY_MAP 替换为泛型安全的 Collections.emptyMap()。
    • 代码示例: ```java // 方法签名移除 sceneType public int singleInsert(StrategyInfo strategyInfo, List staffList) { ... }

    // 使用泛型安全的空集合 Map phoneUserIdMap = Collections.emptyMap();

    
    ---
    
    #### 二、评分明细
    
    1. **功能实现的正确性与健壮性(32/40分)**
       - **理由**:基础业务逻辑拆分(驻店/非驻店)正确。但在处理集合转 Map 时未考虑数据重复的边界情况(Duplicate Key 风险),且未针对 RPC 异常做足够的降级或熔断处理(仅做了 catch 和 log),可能导致部分数据丢失而业务无感知。
    
    2. **安全性与潜在风险(30/30分)**
       - **理由**:未发现 SQL 注入、越权、敏感信息泄露等明显安全漏洞。RPC 接口参数变更未涉及提供者接口修改,符合安全规范。
    
    3. **最佳实践与可维护性(15/20分)**
       - **理由**:代码结构整体尚可,使用了 Stream API 简化过滤逻辑。但存在无用参数 (`sceneType`) 传递、废弃/原生类型 (`Collections.EMPTY_MAP`) 的使用,违背了 Clean Code 原则。
    
    4. **性能与资源利用(0/5分)**
       - **理由**:出现了非常严重的 `for` 循环内 RPC 远程调用和数据库操作(N+1 问题)。这在生产环境中是极大的性能隐患,必须重构为批量处理。
    
    5. **提交信息质量(2/5分)**
       - **理由**:Commit 信息 “策略中心:调整PC端创建逻辑” 过于宽泛。无法从提交信息中得知具体的业务背景(为什么要调整)以及核心改动点(区分驻店与非驻店人员逻辑),不利于后续代码追溯。
    
    总分:79分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!