Commit 48dbf6de by 章家宝

feat:1创建专项任务的时候如果是去策略中心创建失败,逻辑删除掉记录并且报错 2 获取到区域督导的手机填充uuid

1 parent 97f454ab
......@@ -18,6 +18,7 @@ import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import cn.com.glsx.merchants.model.response.MerchantStaffResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
......@@ -49,6 +50,9 @@ public class SupervisorSpecialTaskService {
@Autowired
private TaskCenterRemote taskCenterRemote;
@Autowired
private SupervisorTaskInfoService supervisorTaskInfoService;
/**
* 发起专项任务
* 用途:需求4.1 发起专项任务
......@@ -102,14 +106,22 @@ public class SupervisorSpecialTaskService {
// 调用策略中心创建协同任务策略
Long thirdPartyId = createCollaborativeStrategy(request, task.getId());
if (thirdPartyId == null) {
// 策略中心创建失败,逻辑删除本地记录
SupervisorSpecialTask deleteTask = new SupervisorSpecialTask();
deleteTask.setId(task.getId());
deleteTask.setDeletedFlag(BaseModel.DeletedFlagEnum.D.name());
deleteTask.setUpdateTime(new Date());
supervisorSpecialTaskMapper.updateByPrimaryKeySelective(deleteTask);
log.error("调用策略中心创建协同任务策略失败,已逻辑删除本地记录,taskId={}", task.getId());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "创建协同任务策略失败,请稍后重试");
}
// 保存第三方ID
if (thirdPartyId != null) {
SupervisorSpecialTask updateTask = new SupervisorSpecialTask();
updateTask.setId(task.getId());
updateTask.setThirdPartyId(thirdPartyId);
supervisorSpecialTaskMapper.updateByPrimaryKeySelective(updateTask);
}
SupervisorSpecialTask updateTask = new SupervisorSpecialTask();
updateTask.setId(task.getId());
updateTask.setThirdPartyId(thirdPartyId);
supervisorSpecialTaskMapper.updateByPrimaryKeySelective(updateTask);
log.info("发起专项任务成功,taskId={}, thirdPartyId={}", task.getId(), thirdPartyId);
return task.getId();
......@@ -340,6 +352,9 @@ public class SupervisorSpecialTaskService {
return null;
}
// 2. 查询所有区域运营,构建手机号->UUID的映射(用于填充userUuid)
Map<String, String> phoneToUuidMap = buildRegionOperatorPhoneToUuidMap();
// 3. 构建策略信息
StrategyInfo strategyInfo = new StrategyInfo();
strategyInfo.setStrategyType(strategyType);
......@@ -369,24 +384,32 @@ public class SupervisorSpecialTaskService {
staffList.add(staff);
}
}
Integer userPosition = null;
Integer userPosition;
if (request.getBusinessType() == 2) {
userPosition = 8;
} else {
userPosition = 9;
}
// 4.2 添加区域运营(根据手机号匹配UUID)
if (CollectionUtils.isNotEmpty(request.getRegionOperatorList())) {
for (SubmitSpecialTaskDTO.StaffInfo staffInfo : request.getRegionOperatorList()) {
StrategyStaffRel staff = new StrategyStaffRel();
staff.setUserPhone(staffInfo.getPhone());
staff.setUserName(staffInfo.getName());
staff.setUserPosition(userPosition);
// 根据手机号匹配UUID
String userUuid = phoneToUuidMap.get(staffInfo.getPhone());
if (userUuid != null) {
staff.setUserUuid(userUuid);
} else {
log.warn("区域运营手机号未匹配到UUID,phone={}, taskId={}", staffInfo.getPhone(), taskId);
}
staffList.add(staff);
}
}
strategyInfo.setStaffList(staffList);
log.error("请求参数 {}", strategyInfo);
// 6. 调用远程接口
log.info("创建协同任务策略请求参数,taskId={}, strategyInfo={}", taskId, strategyInfo);
// 5. 调用远程接口
RpcResponse<Integer> response = strategyInfoRemote.create(strategyInfo);
if (response == null || !response.isSuccess()) {
log.error("创建协同任务策略失败,taskId={}, response={}", taskId, response);
......@@ -402,6 +425,30 @@ public class SupervisorSpecialTaskService {
}
/**
* 构建区域运营手机号到UUID的映射
* <p>查询所有区域运营人员,以手机号为key,UUID为value构建Map</p>
*
* @return 手机号 -> UUID 映射
*/
private Map<String, String> buildRegionOperatorPhoneToUuidMap() {
Map<String, String> phoneToUuidMap = new HashMap<>();
try {
List<MerchantStaffResponse> regionOperators = supervisorTaskInfoService.listAllRegionOperator();
if (CollectionUtils.isNotEmpty(regionOperators)) {
for (MerchantStaffResponse operator : regionOperators) {
if (operator != null && operator.getStaffPhone() != null && operator.getUuid() != null) {
phoneToUuidMap.put(operator.getStaffPhone(), operator.getUuid());
}
}
log.info("构建区域运营手机号->UUID映射成功,size={}", phoneToUuidMap.size());
}
} catch (Exception e) {
log.error("查询区域运营列表异常", e);
}
return phoneToUuidMap;
}
/**
* 映射 bizType 到策略类型
*/
private Integer mapBizTypeToStrategyType(String bizType) {
......
  • @wangxw1
    commented
    Master

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

    1. 性能瓶颈:全量加载数据到内存(性能与资源利用)

    • 问题描述:在 buildRegionOperatorPhoneToUuidMap 方法中,调用了 listAllRegionOperator() 获取所有区域运营人员的数据,仅仅为了在内存中遍历构建手机号到 UUID 的映射。
    • 影响分析:随着业务增长,区域运营人员数量增加,全量查询会导致严重的内存开销(可能引发 OOM)和网络/数据库 I/O 性能瓶颈,极大拖慢当前接口的响应速度。
    • 优化建议:应按需查询。先从 request.getRegionOperatorList() 中提取出所有的手机号集合,然后将该集合作为参数,批量查询符合条件的运营人员数据。
    • 代码示例: ```java // 提取手机号集合 List phones = request.getRegionOperatorList().stream() .map(SubmitSpecialTaskDTO.StaffInfo::getPhone) .collect(Collectors.toList());

    // 建议在 supervisorTaskInfoService 新增按手机号列表查询的接口 List operators = supervisorTaskInfoService.listRegionOperatorByPhones(phones);

    // 构建映射 Map phoneToUuidMap = operators.stream() .filter(op -> op.getStaffPhone() != null && op.getUuid() != null) .collect(Collectors.toMap(MerchantStaffResponse::getStaffPhone, MerchantStaffResponse::getUuid, (k1, k2) -> k1));

    
    **2. 事务与异常处理反模式:手动逻辑删除替代事务回滚(正确性与健壮性)**
    - **问题描述**:当 `thirdPartyId == null` 时,代码手动构建了一个 `SupervisorSpecialTask` 对象进行逻辑删除(`deletedFlag = "D"`),随后抛出了 `RpcServiceException`。
    - **影响分析**:
      1. 如果当前方法(或类)使用了 Spring 的 `@Transactional` 注解,抛出 `RpcServiceException`(通常继承自 `RuntimeException`)会自动触发事务回滚,之前的插入操作根本不会提交到数据库。此时进行的手动 `update` 逻辑删除不仅毫无意义,还会增加数据库负担。
      2. 如果没有使用事务,这种“分布式事务的弱补偿”机制也存在漏洞:如果 RPC 调用超时但远端实际创建成功,本地却删除了记录并报错,会导致两端数据不一致。
    - **优化建议**:明确事务边界。强烈建议使用 `@Transactional` 控制本地数据库的一致性,RPC 失败直接抛出异常让本地事务自动回滚。对于 RPC 可能的超时不一致问题,应通过重试机制或定时对账任务来保证最终一致性。
    
    **3. 健壮性隐患:吞没异常导致非预期的数据状态(正确性与健壮性)**
    - **问题描述**:`buildRegionOperatorPhoneToUuidMap` 方法中使用了 `try-catch(Exception e)` 捕获了所有异常,仅打印日志后返回空 Map。
    - **影响分析**:如果查询区域运营列表的服务发生故障(如数据库宕机、RPC 超时),该方法会静默失败并返回空 Map。这会导致后续组装策略人员信息时,所有人的 UUID 都为空。如果 UUID 是策略中心的核心依赖字段,这会将不完整的数据发送给下游,引发更隐蔽的业务错误。
    - **优化建议**:评估 UUID 是否为强依赖。如果是,获取 UUID 失败时应直接向上层抛出异常,阻断任务创建;如果不是强依赖,需要明确注释说明,并做好下游对空 UUID 的兼容处理。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(32/40分)**
      - **扣分理由**:手动执行逻辑删除然后抛出异常的做法违背了常规的事务处理逻辑;全层捕获异常并返回空集合掩盖了潜在的系统错误,可能导致下游业务数据不完整。扣除 8 分。
    - **安全性与潜在风险(30/30分)**
      - **评分理由**:本次变更未引入明显的 SQL 注入、XSS、越权或敏感信息泄露等安全风险。
    - **最佳实践与可维护性(17/20分)**
      - **扣分理由**:将原本错误的日志级别(`log.error` 打印请求参数)修正为 `log.info`,值得肯定。但 `buildRegionOperatorPhoneToUuidMap` 的异常吞没机制和手动回滚写法不符合主流工程最佳实践。扣除 3 分。
    - **性能与资源利用(1/5分)**
      - **扣分理由**:在循环/转换逻辑中引入了全量表数据加载(`listAllRegionOperator`),这是典型的性能反模式,在生产环境中极易引发性能瓶颈。扣除 4 分。
    - **提交信息质量(4/5分)**
      - **扣分理由**:Commit 信息说明了具体的业务变更点(逻辑删除、填充 UUID),具备可追溯性,但格式(`feat:1... 2...`)略显随意,建议遵循标准的 Conventional Commits 规范。扣除 1 分。
    
    总分:84分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!