Commit 83c5d1b9 by 谢海旭

策略中心:创建策略后mq消费逻辑调整

1 parent fd38edb3
...@@ -45,6 +45,7 @@ public class StrategyInfo implements Serializable { ...@@ -45,6 +45,7 @@ public class StrategyInfo implements Serializable {
/** /**
* 业务类型:1.延保 2.轻改 3.权益 4.二手车 * 业务类型:1.延保 2.轻改 3.权益 4.二手车
*/ */
@ApiModelProperty(notes = "业务类型:1.延保 2.轻改 3.权益 4.二手车")
@Column(name = "biz") @Column(name = "biz")
private Integer biz; private Integer biz;
......
...@@ -45,7 +45,7 @@ public class StrategyStaffRel implements Serializable { ...@@ -45,7 +45,7 @@ public class StrategyStaffRel implements Serializable {
/** /**
* 岗位类型:0:驻店,6:飞行督导,9:2.1区域运营 * 岗位类型:0:驻店,6:飞行督导,9:2.1区域运营
*/ */
@ApiModelProperty(notes = "岗位名称") @ApiModelProperty(notes = "岗位类型:0:驻店,6:飞行督导,9:2.1区域运营")
@Column(name = "user_position") @Column(name = "user_position")
private Integer userPosition; private Integer userPosition;
......
...@@ -69,6 +69,11 @@ ...@@ -69,6 +69,11 @@
<artifactId>platform-merchants-api</artifactId> <artifactId>platform-merchants-api</artifactId>
<version>5.6.1-SNAPSHOT</version> <version>5.6.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>cn.com.glsx</groupId>
<artifactId>rpc-scrm-wechat-api</artifactId>
<version>4.54.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file \ No newline at end of file
...@@ -2,6 +2,8 @@ package cn.com.glsx.dj.strategy.mqlistener; ...@@ -2,6 +2,8 @@ package cn.com.glsx.dj.strategy.mqlistener;
import cn.com.glsx.dj.mindworkplate.enums.messagebox.ReminderTypeEnums; import cn.com.glsx.dj.mindworkplate.enums.messagebox.ReminderTypeEnums;
import cn.com.glsx.dj.strategy.config.KafkaTopicConfig; import cn.com.glsx.dj.strategy.config.KafkaTopicConfig;
import cn.com.glsx.dj.strategy.enums.StrategyTypeEnum;
import cn.com.glsx.dj.strategy.model.StrategyInfo;
import cn.com.glsx.dj.strategy.model.StrategyStaffRel; import cn.com.glsx.dj.strategy.model.StrategyStaffRel;
import cn.com.glsx.dj.strategy.service.StrategyInfoService; import cn.com.glsx.dj.strategy.service.StrategyInfoService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -9,6 +11,7 @@ import org.oreframework.jms.kafka.consumer.KafkaConsumer; ...@@ -9,6 +11,7 @@ import org.oreframework.jms.kafka.consumer.KafkaConsumer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
...@@ -19,6 +22,15 @@ import java.util.List; ...@@ -19,6 +22,15 @@ import java.util.List;
@Component @Component
public class StrategyPushConsumer extends KafkaConsumer { public class StrategyPushConsumer extends KafkaConsumer {
/** 新推送逻辑覆盖的策略类型 */
private static final List<Integer> NEW_PUSH_STRATEGY_TYPES = Arrays.asList(
StrategyTypeEnum.MENTORING_APPLICATION.getValue(),
StrategyTypeEnum.PERSONNEL_CHANGE_EVAL.getValue(),
StrategyTypeEnum.BUSINESS_POLICY_UPDATE_EVAL.getValue(),
StrategyTypeEnum.MULTI_CATEGORY_EXPANSION.getValue(),
StrategyTypeEnum.TRAFFIC_IMPROVEMENT.getValue()
);
@Autowired @Autowired
private StrategyInfoService strategyInfoService; private StrategyInfoService strategyInfoService;
...@@ -39,9 +51,29 @@ public class StrategyPushConsumer extends KafkaConsumer { ...@@ -39,9 +51,29 @@ public class StrategyPushConsumer extends KafkaConsumer {
return; return;
} }
try {
StrategyInfo strategyInfo = strategyInfoService.queryStrategyInfoById(strategyId);
Integer strategyType = strategyInfo.getStrategyType();
if (NEW_PUSH_STRATEGY_TYPES.contains(strategyType)) {
processNewStrategyPush(strategyInfo);
} else {
processOriginalStrategyPush(strategyId);
}
} catch (Exception e) {
log.error("策略推送处理失败 strategyId:{}", strategyId, e);
}
log.info("策略推送kafka消息消费,处理结束,msg:{}", message);
}
/**
* 原有推送逻辑:学习类(0)、优秀录音分享类(1)、任务型(3)
*/
private void processOriginalStrategyPush(Integer strategyId) {
List<StrategyStaffRel> strategyStaffRels = strategyInfoService.queryStrategyStaffRelList(strategyId); List<StrategyStaffRel> strategyStaffRels = strategyInfoService.queryStrategyStaffRelList(strategyId);
if (strategyStaffRels == null || strategyStaffRels.size() == 0) { if (strategyStaffRels == null || strategyStaffRels.isEmpty()) {
log.info("策略推送无人员,处理结束,msg:{}", message); log.info("策略推送无人员,跳过,strategyId:{}", strategyId);
return; return;
} }
...@@ -49,10 +81,47 @@ public class StrategyPushConsumer extends KafkaConsumer { ...@@ -49,10 +81,47 @@ public class StrategyPushConsumer extends KafkaConsumer {
try { try {
strategyInfoService.processStrategyPushWithLock(strategyStaffRel, ReminderTypeEnums.FIRST_REMINDER.getCode()); strategyInfoService.processStrategyPushWithLock(strategyStaffRel, ReminderTypeEnums.FIRST_REMINDER.getCode());
} catch (Exception e) { } catch (Exception e) {
log.error("策略推送处理失败 strategyId:{}", strategyId, e); log.error("原有策略推送处理失败 strategyId:{}, staffRelId:{}", strategyId, strategyStaffRel.getId(), e);
}
} }
} }
log.info("策略推送kafka消息消费,处理结束,msg:{}", message); /**
* 新推送逻辑:根据策略类型分发到不同的推送处理
*
* @param strategyInfo 策略详情(已包含人员列表)
*/
private void processNewStrategyPush(StrategyInfo strategyInfo) {
Integer strategyType = strategyInfo.getStrategyType();
log.info("进入新推送逻辑,strategyId:{}, strategyType:{}", strategyInfo.getId(), strategyType);
if (StrategyTypeEnum.MENTORING_APPLICATION.getValue().equals(strategyType)) {
// 8-带教申请
processMentoringApplication(strategyInfo);
} else if (Arrays.asList(
StrategyTypeEnum.PERSONNEL_CHANGE_EVAL.getValue(),
StrategyTypeEnum.BUSINESS_POLICY_UPDATE_EVAL.getValue(),
StrategyTypeEnum.MULTI_CATEGORY_EXPANSION.getValue(),
StrategyTypeEnum.TRAFFIC_IMPROVEMENT.getValue()).contains(strategyType)) {
// 9-人员更换评估、10-商务政策更新评估、11-多品类拓张、12-流量问题改善
processWorktoolStrategy(strategyInfo);
}
}
/**
* 8-带教申请 推送处理
*/
private void processMentoringApplication(StrategyInfo strategyInfo) {
log.info("带教申请推送处理开始,strategyId:{}", strategyInfo.getId());
strategyInfoService.sendMentoringApplicationQWMessage(strategyInfo);
}
/**
* 9-人员更换评估、10-商务政策更新评估、11-多品类拓张、12-流量问题改善 推送处理
*/
private void processWorktoolStrategy(StrategyInfo strategyInfo) {
// TODO 组装推送信息并发送 rpc-dj-smartcarlife-job项目下的cn.com.glsx.dj.smartcarlife.manager.BatchMessagingManager#sendNewReportGroupPush
strategyInfoService.processWorktoolStrategy(strategyInfo);
log.info("worktool策略推送处理,strategyId:{}, strategyType:{}", strategyInfo.getId(), strategyInfo.getStrategyType());
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -10,6 +10,11 @@ import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum; ...@@ -10,6 +10,11 @@ import cn.com.glsx.dj.smartcarlife.enums.SceneTypeEnum;
import cn.com.glsx.dj.smartcarlife.remote.StaffInfoRemote; import cn.com.glsx.dj.smartcarlife.remote.StaffInfoRemote;
import cn.com.glsx.dj.strategy.config.DataStatsConfig; import cn.com.glsx.dj.strategy.config.DataStatsConfig;
import cn.com.glsx.dj.strategy.config.KafkaTopicConfig; import cn.com.glsx.dj.strategy.config.KafkaTopicConfig;
import cn.com.glsx.dj.strategy.config.MentoringApplicationConfig;
import cn.com.glsx.scrm.wechat.dto.workwechat.message.WorkWechatTextMessageDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.message.request.SendWorkWechatMessageRequestDTO;
import cn.com.glsx.scrm.wechat.dto.workwechat.message.response.SendWorkWechatMessageResponseDTO;
import cn.com.glsx.scrm.wechat.enums.WorkWechatAppMessageTypeEnum;
import cn.com.glsx.dj.strategy.dto.BatchInsertStrategyInfo; import cn.com.glsx.dj.strategy.dto.BatchInsertStrategyInfo;
import cn.com.glsx.dj.strategy.dto.MerchantInfo; import cn.com.glsx.dj.strategy.dto.MerchantInfo;
import cn.com.glsx.dj.strategy.dto.StaffHistoryRecordDTO; import cn.com.glsx.dj.strategy.dto.StaffHistoryRecordDTO;
...@@ -34,6 +39,7 @@ import cn.hutool.core.date.DatePattern; ...@@ -34,6 +39,7 @@ import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference; import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
...@@ -93,6 +99,9 @@ public class StrategyInfoService { ...@@ -93,6 +99,9 @@ public class StrategyInfoService {
@Resource @Resource
private StaffInfoRemote staffInfoRemote; private StaffInfoRemote staffInfoRemote;
@Resource
private MentoringApplicationConfig mentoringApplicationConfig;
public List<StrategyInfo> queryStrategyInfoList(Integer strategyType) { public List<StrategyInfo> queryStrategyInfoList(Integer strategyType) {
Example example = new Example(StrategyInfo.class); Example example = new Example(StrategyInfo.class);
...@@ -640,4 +649,129 @@ public class StrategyInfoService { ...@@ -640,4 +649,129 @@ public class StrategyInfoService {
} }
return dataResp; return dataResp;
} }
/**
* 带教申请类型策略推送企微应用消息
* 遍历区域运营人员,逐个发送企微消息
*
* @param strategyInfo 策略详情
*/
public void sendMentoringApplicationQWMessage(StrategyInfo strategyInfo) {
String regionOperatorsJson = mentoringApplicationConfig.getRegionOperators();
if (StrUtil.isBlank(regionOperatorsJson) || "[]".equals(regionOperatorsJson)) {
log.warn("带教申请推送:区域运营人员配置为空,strategyId:{}", strategyInfo.getId());
return;
}
String agentId = mentoringApplicationConfig.getAgentId();
if (StrUtil.isBlank(agentId)) {
log.error("带教申请推送:企微agentId配置缺失,strategyId:{}", strategyInfo.getId());
return;
}
String wechatBasePath = mentoringApplicationConfig.getWechatBasePath();
if (StrUtil.isBlank(wechatBasePath)) {
log.error("带教申请推送:wechat.base.path配置缺失,strategyId:{}", strategyInfo.getId());
return;
}
Integer merchantId = mentoringApplicationConfig.getMerchantId();
String approvalLinkUrl = mentoringApplicationConfig.getApprovalLinkUrl();
// 解析区域运营人员配置
cn.hutool.json.JSONArray operators = JSONUtil.parseArray(regionOperatorsJson);
for (int i = 0; i < operators.size(); i++) {
cn.hutool.json.JSONObject operator = operators.getJSONObject(i);
String name = operator.getStr("name");
String phone = operator.getStr("phone");
try {
sendMentoringQWTextMessage(phone, name, strategyInfo, merchantId, agentId, approvalLinkUrl, wechatBasePath);
} catch (Exception e) {
log.error("带教申请推送企微消息异常,strategyId:{}, 区域运营:{}", strategyInfo.getId(), name, e);
}
}
}
/**
* 发送单条带教申请企微文本消息
* 参考:cn.com.glsx.dj.staff.service.recruitment.RecruitmentDemandService#sendRecruitmentQWTextMessage
*/
private void sendMentoringQWTextMessage(String phone, String operatorName, StrategyInfo strategyInfo,
Integer merchantId, String agentId, String approvalLinkUrl,
String wechatBasePath) {
if (StrUtil.isBlank(phone)) {
log.warn("带教申请推送:区域运营手机号为空,跳过,姓名:{}", operatorName);
return;
}
// 通过手机号查询企微userId
String qwUserId = getQWUserId(wechatBasePath, merchantId, phone);
if (StrUtil.isBlank(qwUserId)) {
log.error("带教申请推送:查询企微标识为空,手机号:{}", phone);
return;
}
// 组装消息内容
String content = buildMentoringMessageContent(operatorName, approvalLinkUrl);
// 构建发送请求
SendWorkWechatMessageRequestDTO requestDTO = new SendWorkWechatMessageRequestDTO();
requestDTO.setToUser(qwUserId);
requestDTO.setMerchantId(merchantId);
requestDTO.setMsgType(WorkWechatAppMessageTypeEnum.TEXT.getValue());
requestDTO.setAgentId(agentId);
WorkWechatTextMessageDTO text = new WorkWechatTextMessageDTO();
text.setContent(content);
requestDTO.setText(text);
// 发送企微消息
String sendUrl = wechatBasePath + "/wechat/message/sendMessage";
try {
String responseStr = HttpRequestUtils.postJson(sendUrl, JSONUtil.toJsonStr(requestDTO), new HashMap<>());
SendWorkWechatMessageResponseDTO response = JSONUtil.toBean(responseStr, SendWorkWechatMessageResponseDTO.class);
if (response != null && response.isSuccess()) {
log.info("带教申请推送企微消息成功,strategyId:{}, 区域运营:{}", strategyInfo.getId(), operatorName);
} else {
log.error("带教申请推送企微消息失败,strategyId:{}, 区域运营:{}, response:{}", strategyInfo.getId(), operatorName, responseStr);
}
} catch (Exception e) {
log.error("带教申请推送企微消息请求异常,strategyId:{}, 区域运营:{}", strategyInfo.getId(), operatorName, e);
}
}
/**
* 通过手机号查询企微员工标识
*/
private String getQWUserId(String wechatBasePath, Integer merchantId, String phone) {
String url = wechatBasePath + "/wechat/oa/getuserid";
cn.hutool.json.JSONObject requestBody = new cn.hutool.json.JSONObject();
requestBody.set("merchantId", merchantId);
requestBody.set("mobile", phone);
try {
String responseStr = HttpRequestUtils.postJson(url, JSONUtil.toJsonStr(requestBody), new HashMap<>());
cn.hutool.json.JSONObject response = JSONUtil.parseObj(responseStr);
if (response != null && response.getBool("success", false)) {
return response.getStr("userid");
}
} catch (Exception e) {
log.error("查询企微员工标识异常,手机号:{}", phone, e);
}
return null;
}
/**
* 组装带教申请消息内容
*/
private String buildMentoringMessageContent(String operatorName, String approvalLinkUrl) {
StringBuilder sb = new StringBuilder();
sb.append("专项带教申请通知\n\n");
sb.append("你好,").append(operatorName).append("发起了一笔专项带教申请,请及时处理审批。\n\n");
sb.append("👉点击此处直接审批:").append(approvalLinkUrl);
return sb.toString();
}
public void processWorktoolStrategy(StrategyInfo strategyInfo) {
}
} }
  • @wangxw1
    commented
    Master

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

    1. 架构规范与性能问题:引入了 RPC 依赖却使用原生 HTTP 请求,且在循环中发生网络 I/O

    • 问题描述:在 pom.xml 中新增了 rpc-scrm-wechat-api 依赖,但在 StrategyInfoService 中调用企微接口时,却使用了 HttpRequestUtils.postJson 发起原生 HTTP 请求。此外,这些网络请求(获取 userId 和发送消息)被放置在 for 循环中逐个执行。
    • 影响分析:
      • 架构反模式:引入了内部 RPC API 包却不使用对应的 Dubbo 接口,绕过了微服务治理(如负载均衡、超时重试、熔断等)。
      • 性能瓶颈:在 Kafka 消费者线程的循环中同步发起多次 HTTP 请求,会导致严重的线程阻塞。如果网络波动或接口响应慢,将直接导致 Kafka 消费积压。
    • 优化建议:
      • 废弃原生 HTTP 请求,改为通过 @DubboReference(或对应注入方式)调用 rpc-scrm-wechat-api 提供的远程接口。
      • 尽量使用批量接口获取 userId 和发送消息;若无批量接口,建议将消息发送逻辑放入线程池异步处理,避免阻塞 Kafka 消费主线程。

    2. 健壮性与稳定性隐患:配置解析缺乏异常保护与超时控制缺失

    • 问题描述:sendMentoringApplicationQWMessage 方法中使用 JSONUtil.parseArray(regionOperatorsJson) 直接解析配置中心的字符串。同时,若沿用当前的 HttpRequestUtils.postJson,代码中未见明确的超时时间设置。
    • 影响分析:
      • 若配置中心下发的 JSON 格式存在语法错误(如缺少引号、括号不匹配),parseArray 会抛出未捕获的运行时异常,导致整个 Kafka 消息消费中断并可能触发死循环重试。
      • 若 HTTP 请求未设置严格的 Connect 和 Read 超时时间,外部接口挂起将导致消费者线程永久阻塞(Thread Starvation)。
    • 优化建议:
      • 对 JSON 解析增加 try-catch 保护,并在解析失败时记录 Error 日志并安全退出。
      • 代码示例: java cn.hutool.json.JSONArray operators; try { operators = JSONUtil.parseArray(regionOperatorsJson); } catch (Exception e) { log.error("带教申请推送:区域运营人员配置JSON解析失败,strategyId:{}, config:{}", strategyInfo.getId(), regionOperatorsJson, e); return; }

    3. 代码完整性与工程规范:存在未实现的空方法与遗留 TODO

    • 问题描述:StrategyPushConsumer 中调用了 strategyInfoService.processWorktoolStrategy(strategyInfo),但在 StrategyInfoService 中该方法是一个没有任何实现的空方法。同时 Consumer 中遗留了 // TODO 注释。
    • 影响分析:
      • 消费者匹配到对应策略类型后,调用空方法相当于直接丢弃了消息。如果这是未完成的需求,提前合入主干可能会导致线上数据的静默流失。
      • 遗留的空方法和 TODO 会增加后续代码的维护成本和理解负担。
    • 优化建议:
      • 若该功能尚未开发完毕,建议在 processWorktoolStrategy 方法内部打印明确的 Warn 日志,或直接抛出 UnsupportedOperationException。
      • 若不需要处理,请添加注释说明为何为空实现,并删除 Consumer 中的 TODO。

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分):
      • 扣分理由:基础的策略分发逻辑正确,但 JSON 解析缺乏容错处理,且包含被调用的未实现空方法(可能导致消息静默丢失)。(注:本次变更未涉及 MyBatis 子查询及 Dubbo 接口参数变更,符合红线要求)。
    • 安全性与潜在风险(30/30分):
      • 评分理由:未见越权、SQL注入、XSS及敏感信息硬编码等明显的安全漏洞。
    • 最佳实践与可维护性(12/20分):
      • 扣分理由:引入了 RPC 依赖包却使用拼接 URL 的原生 HTTP 请求,违反了微服务间调用的最佳实践;代码中存在硬编码的 URL 路径和未清理的 TODO。
    • 性能与资源利用(2/5分):
      • 扣分理由:在 for 循环中串行发起多次网络 I/O 请求(N+1 问题),对 Kafka 消费者性能有显著的负面影响。
    • 提交信息质量(4/5分):
      • 评分理由:“策略中心:创建策略后mq消费逻辑调整” 描述了变更范围,但若能简要补充新增了哪些策略类型的分支会更利于追溯。

    总分:80分

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