Commit 4fea436c by 谢海旭

调整推送任务中心与从任务中心同步逻辑

1 parent 222466a9
......@@ -397,6 +397,9 @@ public class StrategyCreatedConsumer extends KafkaConsumer {
// 构建业务上下文(bizContext),用于下游解析人员信息
task.setBizContext(buildBizContext(strategyInfo));
// 构建业务展示信息(bizDisplayInfo),用于页面展示归属门店/归属业务/指定区域运营/问题驻店销售
task.setBizDisplayInfo(buildBizDisplayInfo(strategyInfo));
// 构建请求
CreateTaskRequest request = new CreateTaskRequest();
request.setTask(task);
......@@ -436,44 +439,155 @@ public class StrategyCreatedConsumer extends KafkaConsumer {
/**
* 构建业务上下文(JSON数组字符串)
* 下游 {@code TaskInfoSyncService.parseStaffFromBizContext} 解析格式:
* [{"fieldId":"staff_list","fieldValue":"[{staffName,staffPhone,staffUuid,userPosition}]"}]
* 仅包含驻店人员(userPosition=0),作为"问题驻店销售"数据
* <p>组装门店名称、门店商户号、业务类型值、业务名称、问题驻店人员列表等字段,</p>
* <p>下游 {@code TaskInfoSyncService.parseStaffFromBizContext} 解析 staff_list 提取人员信息。</p>
*
* <pre>
* [{"fieldId":"merchant_name","fieldValue":"深光明店"},
* {"fieldId":"merchant_id","fieldValue":"12345"},
* {"fieldId":"business_type","fieldValue":"4"},
* {"fieldId":"business_type_name","fieldValue":"二手车"},
* {"fieldId":"staff_list","fieldValue":"[{staffName,staffPhone,staffUuid,userPosition}]"}]
* </pre>
*
* @param strategyInfo 策略信息
* @return JSON数组字符串
*/
private String buildBizContext(StrategyInfo strategyInfo) {
JSONArray contextArray = new JSONArray();
List<StrategyStaffRel> staffList = strategyInfo.getStaffList();
if (CollectionUtils.isEmpty(staffList)) {
return "[{\"fieldId\":\"staff_list\",\"fieldValue\":\"[]\"}]";
}
// 构建人员列表 JSON 数组,仅过滤驻店人员(userPosition=0)
JSONArray staffArray = new JSONArray();
for (StrategyStaffRel staff : staffList) {
if (staff == null) {
continue;
}
if (!UserPositionEnum.RESIDENT.getValue().equals(staff.getUserPosition())) {
continue;
// 1. 门店名称
JSONObject storeNameField = new JSONObject();
storeNameField.set("fieldId", "merchant_name");
storeNameField.set("fieldValue", strategyInfo.getMerchantName());
contextArray.add(storeNameField);
// 2. 门店商户号
JSONObject storeIdField = new JSONObject();
storeIdField.set("fieldId", "merchant_id");
storeIdField.set("fieldValue", strategyInfo.getMerchantId() != null
? String.valueOf(strategyInfo.getMerchantId()) : "");
contextArray.add(storeIdField);
// 3. 业务类型值
JSONObject bizTypeField = new JSONObject();
bizTypeField.set("fieldId", "business_type");
bizTypeField.set("fieldValue", strategyInfo.getBiz() != null
? String.valueOf(strategyInfo.getBiz()) : "");
contextArray.add(bizTypeField);
// 4. 业务名称
JSONObject bizNameField = new JSONObject();
bizNameField.set("fieldId", "business_type_name");
bizNameField.set("fieldValue", convertBusinessTypeName(strategyInfo.getBiz()));
contextArray.add(bizNameField);
// 5. 问题驻店人员列表(仅过滤驻店人员 userPosition=0)
JSONObject staffListField = new JSONObject();
staffListField.set("fieldId", "staff_list");
if (CollectionUtils.isEmpty(staffList)) {
staffListField.set("fieldValue", "[]");
} else {
JSONArray staffArray = new JSONArray();
for (StrategyStaffRel staff : staffList) {
if (staff == null) {
continue;
}
if (!UserPositionEnum.RESIDENT.getValue().equals(staff.getUserPosition())) {
continue;
}
JSONObject staffObj = new JSONObject();
staffObj.set("staffName", staff.getUserName());
staffObj.set("staffPhone", staff.getUserPhone());
staffObj.set("staffUuid", staff.getUserUuid());
staffObj.set("userPosition", staff.getUserPosition());
staffArray.add(staffObj);
}
JSONObject staffObj = new JSONObject();
staffObj.set("staffName", staff.getUserName());
staffObj.set("staffPhone", staff.getUserPhone());
staffObj.set("staffUuid", staff.getUserUuid());
staffObj.set("userPosition", staff.getUserPosition());
staffArray.add(staffObj);
staffListField.set("fieldValue", staffArray.toString());
}
contextArray.add(staffListField);
// 组装外层结构
JSONObject contextField = new JSONObject();
contextField.set("fieldId", "staff_list");
contextField.set("fieldValue", staffArray.toString());
return contextArray.toString();
}
JSONArray contextArray = new JSONArray();
contextArray.add(contextField);
/**
* 构建业务展示信息(JSON数组字符串)
* <p>组装格式示例:</p>
* <pre>
* [{"fieldId":"merchant_name","fieldName":"归属门店","fieldType":"text","fieldValue":"深光明店","linkAlias":null},
* {"fieldId":"business_type","fieldName":"归属业务","fieldType":"text","fieldValue":"二手车销售","linkAlias":null},
* {"fieldId":"regional_operator","fieldName":"指定区域运营","fieldType":"text","fieldValue":"李四","linkAlias":null},
* {"fieldId":"problem_staff","fieldName":"问题驻店销售","fieldType":"text","fieldValue":"张三、李四","linkAlias":null}]
* </pre>
* <ul>
* <li>归属门店、归属业务、指定区域运营为公共字段,所有协同类策略均有</li>
* <li>问题驻店销售仅「人员更换评估(9)」策略类型有,多个姓名按"、"分隔</li>
* </ul>
*
* @param strategyInfo 策略信息
* @return JSON数组字符串
*/
private String buildBizDisplayInfo(StrategyInfo strategyInfo) {
JSONArray displayInfoArray = new JSONArray();
// 1. 归属门店
JSONObject merchantObj = new JSONObject();
merchantObj.set("fieldId", "merchant_name");
merchantObj.set("fieldName", "归属门店");
merchantObj.set("fieldType", "text");
merchantObj.set("fieldValue", strategyInfo.getMerchantName());
merchantObj.set("linkAlias", null);
displayInfoArray.add(merchantObj);
// 2. 归属业务
JSONObject bizObj = new JSONObject();
bizObj.set("fieldId", "business_type");
bizObj.set("fieldName", "归属业务");
bizObj.set("fieldType", "text");
bizObj.set("fieldValue", convertBusinessTypeName(strategyInfo.getBiz()));
bizObj.set("linkAlias", null);
displayInfoArray.add(bizObj);
// 3. 指定区域运营(从staffList中取userPosition=8或9的区域运营)
List<StrategyStaffRel> staffList = strategyInfo.getStaffList();
String regionalOperatorNames = "";
if (CollectionUtils.isNotEmpty(staffList)) {
regionalOperatorNames = staffList.stream()
.filter(s -> s != null && s.getUserPosition() != null
&& (UserPositionEnum.REGIONAL_OPERATION_1_1.getValue().equals(s.getUserPosition())
|| UserPositionEnum.REGIONAL_OPERATION_2_1.getValue().equals(s.getUserPosition())))
.map(StrategyStaffRel::getUserName)
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining("、"));
}
JSONObject regionalOpObj = new JSONObject();
regionalOpObj.set("fieldId", "regional_operator");
regionalOpObj.set("fieldName", "指定区域运营");
regionalOpObj.set("fieldType", "text");
regionalOpObj.set("fieldValue", regionalOperatorNames);
regionalOpObj.set("linkAlias", null);
displayInfoArray.add(regionalOpObj);
// 4. 问题驻店销售(仅人员更换评估策略类型有,从staffList中取userPosition=0的驻店人员)
if (StrategyTypeEnum.PERSONNEL_CHANGE_EVAL.getValue().equals(strategyInfo.getStrategyType())) {
String residentStaffNames = "";
if (CollectionUtils.isNotEmpty(staffList)) {
residentStaffNames = staffList.stream()
.filter(s -> s != null && UserPositionEnum.RESIDENT.getValue().equals(s.getUserPosition()))
.map(StrategyStaffRel::getUserName)
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining("、"));
}
JSONObject residentObj = new JSONObject();
residentObj.set("fieldId", "problem_staff");
residentObj.set("fieldName", "问题驻店销售");
residentObj.set("fieldType", "text");
residentObj.set("fieldValue", residentStaffNames);
residentObj.set("linkAlias", null);
displayInfoArray.add(residentObj);
}
return contextArray.toString();
return displayInfoArray.toString();
}
}
  • @wangxw1
    commented
    Master

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

    1. 代码设计与可维护性:建议使用 DTO 替代手动拼接 JSON 对象

    • 问题描述:在 buildBizContext 和 buildBizDisplayInfo 方法中,大量使用了 JSONObject 手动 set 属性来构建 JSON 数组。
    • 影响分析:这种面向 JSONObject 编程的方式非常冗长,且缺乏类型安全(Type Safety)。后续如果需要增加字段或修改结构,极易因为拼写错误导致 Bug,且代码可读性较差,不符合现代 Java 工程的最佳实践。
    • 优化建议:建议定义专门的 DTO(Data Transfer Object)类,通过给对象赋值后,统一使用 JSON 工具类(如 Fastjson / Jackson)进行序列化。 代码示例: ```java // 1. 定义 DTO @Data @AllArgsConstructor public class BizDisplayFieldDTO { private String fieldId; private String fieldName; private String fieldType; private String fieldValue; private String linkAlias; }

    // 2. 业务逻辑中使用 List displayList = new ArrayList<>(); displayList.add(new BizDisplayFieldDTO("merchant_name", "归属门店", "text", strategyInfo.getMerchantName(), null)); // ... 其他字段 return JSON.toJSONString(displayList);

    
    **2. 代码冗余与硬编码:存在大量魔法值与重复的构建逻辑**
    - **问题描述**:代码中散落了大量的硬编码字符串(如 `"merchant_name"`、`"business_type"`、`"text"`、`"归属门店"` 等)。此外,如果不采用上述的 DTO 方案,`buildBizDisplayInfo` 中重复编写了 4 次几乎相同的 `JSONObject` 构建和赋值代码。
    - **影响分析**:魔法值(Magic Strings)在多处被使用(特别是作为上下游交互的 Key),一旦拼写错误将导致解析失败,且极难排查。重复代码则增加了类的体积,降低了可维护性。
    - **优化建议**:
      1. 将 `"merchant_name"`、`"staff_list"` 等交互 Key 提取为类的 `private static final String` 常量。
      2. 若不使用 DTO 方案,至少应抽取一个私有辅助方法来消除重复代码:
      ```java
      private JSONObject buildDisplayObj(String fieldId, String fieldName, String fieldValue) {
          JSONObject obj = new JSONObject();
          obj.set("fieldId", fieldId);
          obj.set("fieldName", fieldName);
          obj.set("fieldType", "text");
          obj.set("fieldValue", fieldValue);
          obj.set("linkAlias", null);
          return obj;
      }
    

    3. 健壮性:缺少对核心入参的防御性校验

    • 问题描述:buildBizContext 和 buildBizDisplayInfo 方法作为被调用的私有方法,直接调用了 strategyInfo.getStaffList() 和 strategyInfo.getMerchantName()。
    • 影响分析:虽然外层调用可能已经隐含了 strategyInfo 不为 null 的前提,但作为独立的逻辑封装单元,缺少对 strategyInfo 本身的 null 判断。一旦上游逻辑变更传入 null,此处将直接抛出 NullPointerException 导致消费线程异常。
    • 优化建议:在方法头部增加防御性校验。 java private String buildBizContext(StrategyInfo strategyInfo) { if (strategyInfo == null) { return "[]"; // 或根据业务需求返回默认值/抛出自定义异常 } // ... 原有逻辑 }

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 业务逻辑(如 Stream 过滤、人员身份判断)实现正确,能较好地处理集合为空或集合元素为 null 的边界情况。扣除 2 分是因为核心入参 strategyInfo 缺少顶层的防御性空指针校验。未发现 MyBatis 子查询及 Dubbo 接口变更等违规操作。

    • 安全性与潜在风险(30/30分): 代码为内部逻辑组装,不涉及直接的 SQL 拼接或外部输入执行,暂未发现 SQL 注入、XSS、越权或敏感日志打印等安全隐患。

    • 最佳实践与可维护性(12/20分): 扣分较多。主要原因是大量使用 JSONObject 手动拼接导致代码冗长,且存在较多魔法字符串(Magic Strings)和重复的模板代码。强烈建议向基于 POJO/DTO 的序列化方式重构。

    • 性能与资源利用(5/5分): Stream 的使用合理,对象创建在 Kafka 消费者的正常承受范围内,无明显性能瓶颈,资源利用合理。

    • 提交信息质量(4/5分): Commit 信息(调整推送任务中心与从任务中心同步逻辑)说明了做了“什么”,但较为宽泛,缺乏对“为什么”做此修改(如:为了支持前端展示归属门店等信息)的描述。

    总分:89分

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