Commit 80b5f4f2 by 林永健

feat(bi-analysis): 新增每日门店业务动态汇总推送任务

- 新增 MerchantBiDailyPushJob,每日上午9点汇总门店+业务昨日动态并推送
- 分页处理所有活跃关注的门店业务组合,提升处理效率
- 通过微信服务分别推送给负责人和所有关注人
- MerchantBiFollowService新增指定人员推送关注消息功能
- 优化触发关注推送逻辑,仅对配置的人员进行推送
- WechatPushService更新推送消息格式,新增消息标题和跳转链接
- 改造推送接口,调用biAnalysisRemote的新接口支持更灵活模版
- 升级mindworkplate.api版本以支持新推送功能
1 parent 164f3072
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<properties> <properties>
<platform-merchants.api.version>5.5.1-SNAPSHOT</platform-merchants.api.version> <platform-merchants.api.version>5.5.1-SNAPSHOT</platform-merchants.api.version>
<smartcarlife.api.version>5.6.4-SNAPSHOT</smartcarlife.api.version> <smartcarlife.api.version>5.6.4-SNAPSHOT</smartcarlife.api.version>
<mindworkplate.api.version>5.6.1-SNAPSHOT</mindworkplate.api.version> <mindworkplate.api.version>5.7.0-SNAPSHOT</mindworkplate.api.version>
<pe-analysis.api.version>5.7.0-SNAPSHOT</pe-analysis.api.version> <pe-analysis.api.version>5.7.0-SNAPSHOT</pe-analysis.api.version>
<strategy.api.version>5.6.0-SNAPSHOT</strategy.api.version> <strategy.api.version>5.6.0-SNAPSHOT</strategy.api.version>
</properties> </properties>
......
package cn.com.glsx.dj.peanalysis.job;
import cn.com.glsx.dj.peanalysis.dto.analysis.MerchantBiCommentReq;
import cn.com.glsx.dj.peanalysis.mapper.MerchantBiFollowDetailMapper;
import cn.com.glsx.dj.peanalysis.mapper.MerchantBiFollowMapper;
import cn.com.glsx.dj.peanalysis.mapper.ServiceMerchantMapper;
import cn.com.glsx.dj.peanalysis.model.MerchantBiFollow;
import cn.com.glsx.dj.peanalysis.model.MerchantBiFollowDetail;
import cn.com.glsx.dj.peanalysis.service.MerchantBiCommentService;
import cn.com.glsx.dj.peanalysis.service.WechatPushService;
import cn.com.glsx.dj.peanalysis.dto.StoreSettingDTO;
import cn.com.glsx.dj.peanalysis.utils.MetricsValueUtils;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* 每日上午9点,汇总门店+业务的昨日动态推送
*
* @author lyj
*/
@Slf4j
@Component
@ElasticJobConf(name = "MerchantBiDailyPushJob", cron = "0 0 9 * * ?", description = "每日BI动态汇总推送", overwrite = false)
public class MerchantBiDailyPushJob implements SimpleJob {
@Resource
private MerchantBiCommentService commentService;
@Resource
private WechatPushService wechatPushService;
@Resource
private MerchantBiFollowMapper followMapper;
@Resource
private MerchantBiFollowDetailMapper followDetailMapper;
@Resource
private ServiceMerchantMapper serviceMerchantMapper;
@Override
public void execute(ShardingContext shardingContext) {
log.info("开始执行每日 BI 动态汇总推送任务");
// 1. 分页获取所有存在关注的门店 + 业务组合
int pageNum = 1;
int pageSize = 100; // 每页处理 100 条记录
while (true) {
// 使用 PageHelper 进行分页查询
PageHelper.startPage(pageNum, pageSize);
List<MerchantBiFollow> follows = followMapper.findAllActiveFollows();
// 转换为 PageInfo 以获取分页信息
PageInfo<MerchantBiFollow> pageInfo = new PageInfo<>(follows);
if (CollectionUtils.isEmpty(follows)) {
log.info("分页查询完成,共处理{}页", pageNum - 1);
break;
}
log.info("开始处理第{}页数据,当前页记录数:{}", pageNum, follows.size());
// 2. 处理当前页的数据
for (MerchantBiFollow follow : follows) {
processMerchantFollow(follow);
}
// 3. 判断是否还有下一页
if (!pageInfo.isHasNextPage()) {
log.info("数据处理完成,共处理{}页,总记录数:{}", pageInfo.getPages(), pageInfo.getTotal());
break;
}
pageNum++;
}
}
/**
* 处理单个门店关注记录
*/
private void processMerchantFollow(MerchantBiFollow follow) {
try {
// 查询该门店 + 业务昨日的动态总数 (策略)
MerchantBiCommentReq commentReq = MerchantBiCommentReq.builder()
.merchantIds(Collections.singletonList(follow.getMerchantId()))
.businessType(follow.getBusinessType())
.build();
int yesterdayCount = commentService.countYesterdayComments(commentReq);
if (yesterdayCount <= 0)
return;
// 推送给负责人 (区域运营、线上运营)
pushToOperators(follow, yesterdayCount);
// 推送给所有关注人
pushToFollowers(follow, yesterdayCount);
} catch (Exception e) {
log.error("处理门店关注记录失败,门店 ID:{}, 业务类型:{}, 错误:{}",
follow.getMerchantId(), follow.getBusinessType(), e.getMessage(), e);
}
}
private void pushToOperators(MerchantBiFollow follow, int count) {
Integer merchantId = follow.getMerchantId();
Byte businessType = follow.getBusinessType();
Integer scene = MetricsValueUtils.convertBizsToScene(businessType.intValue());
List<StoreSettingDTO> settings = serviceMerchantMapper.findStoreSetting(Collections.singletonList(follow.getMerchantId()), scene);
if (CollectionUtils.isEmpty(settings)) {
log.info("【{}-{}】门店无相关配置", merchantId, businessType);
return;
}
StoreSettingDTO setting = settings.get(0);
List<String> targetUuids = new ArrayList<>();
if (StringUtils.isNotBlank(setting.getRegionalOperationId()))
targetUuids.add(setting.getRegionalOperationId());
if (StringUtils.isNotBlank(setting.getOnlineOperationId()))
targetUuids.add(setting.getOnlineOperationId());
if (CollectionUtils.isEmpty(targetUuids)) {
log.info("【{}-{}】门店配置无相应人员", merchantId, businessType);
return;
}
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()),
count, targetUuids, false);
}
private void pushToFollowers(MerchantBiFollow follow, int count) {
List<MerchantBiFollowDetail> details = followDetailMapper.findByFollowId(follow.getId());
if (CollectionUtils.isEmpty(details)) {
log.info("【{}-{}】门店无关注人, id:{}", follow.getMerchantId(), follow.getBusinessType(), follow.getId());
return;
}
List<String> followerUuids = details.stream()
.map(MerchantBiFollowDetail::getStaffUuid)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(followerUuids)) {
log.info("【{}-{}】门店无关注人, id:{}", follow.getMerchantId(), follow.getBusinessType(), follow.getId());
return;
}
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()),
count, followerUuids, true);
}
private String getBusinessTypeName(Byte businessType) {
if (businessType == null) return "未知业务";
switch (businessType) {
case 1:
return "延保";
case 2:
return "轻改";
case 3:
return "权益";
default:
return "未知业务";
}
}
}
...@@ -29,6 +29,7 @@ import org.apache.commons.collections.CollectionUtils; ...@@ -29,6 +29,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -82,6 +83,8 @@ public class MerchantBiFollowService { ...@@ -82,6 +83,8 @@ public class MerchantBiFollowService {
*/ */
@Autowired @Autowired
private WechatPushService wechatPushService; private WechatPushService wechatPushService;
@Autowired
private Environment environment;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean followMerchant(MerchantBiFollowReq request) { public Boolean followMerchant(MerchantBiFollowReq request) {
...@@ -579,26 +582,38 @@ public class MerchantBiFollowService { ...@@ -579,26 +582,38 @@ public class MerchantBiFollowService {
private void triggerFollowPush(MerchantBiFollowReq request) { private void triggerFollowPush(MerchantBiFollowReq request) {
try { try {
// 指定人员才推送关注消息
String pushStaffPhone = environment.getProperty("dj.pe.analysis.follow.push.staff", String.class);
if (StringUtils.isBlank(pushStaffPhone)) {
log.info("未配置推送人员");
return;
}
if (!Arrays.asList(pushStaffPhone.split(",")).contains(request.getStaffPhone())) {
log.info("未匹配到指定推送人员, config:{}, current:{}", pushStaffPhone, request.getStaffPhone());
return;
}
// 获取门店负责人信息 (区域运营和线上运营) // 获取门店负责人信息 (区域运营和线上运营)
Integer scene = MetricsValueUtils.convertBizsToScene(request.getBusinessType().intValue()); Integer scene = MetricsValueUtils.convertBizsToScene(request.getBusinessType().intValue());
List<StoreSettingDTO> settings = serviceMerchantMapper.findStoreSetting(Collections.singletonList(request.getMerchantId()), scene); List<StoreSettingDTO> settings = serviceMerchantMapper.findStoreSetting(Collections.singletonList(request.getMerchantId()), scene);
if (CollectionUtils.isNotEmpty(settings)) { if (CollectionUtils.isEmpty(settings)) {
StoreSettingDTO setting = settings.get(0); log.info("未找到关联门店配置");
List<String> targetUuids = new ArrayList<>(); return;
if (StringUtils.isNotBlank(setting.getRegionalOperationId())) }
targetUuids.add(setting.getRegionalOperationId()); StoreSettingDTO setting = settings.get(0);
if (StringUtils.isNotBlank(setting.getOnlineOperationId())) List<String> targetUuids = new ArrayList<>();
targetUuids.add(setting.getOnlineOperationId()); if (StringUtils.isNotBlank(setting.getRegionalOperationId()))
targetUuids.add(setting.getRegionalOperationId());
if (CollectionUtils.isNotEmpty(targetUuids)) { if (StringUtils.isNotBlank(setting.getOnlineOperationId()))
log.info("触发关注推送:merchantId={}, biz={}, 触发人={}, 推送目标人数={}", targetUuids.add(setting.getOnlineOperationId());
request.getMerchantId(), request.getBusinessType(), request.getStaffName(), targetUuids.size()); if (CollectionUtils.isNotEmpty(targetUuids)) {
wechatPushService.pushFollowMsg(request.getStaffName(), request.getMerchantName(), log.info("触发关注推送:未找到推送目标,merchantId={}, biz={}", request.getMerchantId(), request.getBusinessType());
getBusinessTypeName(request.getBusinessType()), targetUuids); return;
} else {
log.info("触发关注推送:未找到推送目标,merchantId={}, biz={}", request.getMerchantId(), request.getBusinessType());
}
} }
log.info("触发关注推送:merchantId={}, biz={}, 触发人={}, 推送目标={}",
request.getMerchantId(), request.getBusinessType(), request.getStaffName(), targetUuids);
wechatPushService.pushFollowMsg(request.getStaffName(), request.getMerchantName(),
getBusinessTypeName(request.getBusinessType()), targetUuids);
} catch (Exception e) { } catch (Exception e) {
log.error("触发关注推送异常", e); log.error("触发关注推送异常", e);
} }
......
package cn.com.glsx.dj.peanalysis.service; package cn.com.glsx.dj.peanalysis.service;
import cn.com.glsx.dj.mindworkplate.dto.bianalyse.MatterAddPushWeChatMsgDTO; import cn.com.glsx.dj.mindworkplate.dto.bianalyse.CommonPushWeChatMsgDTO;
import cn.com.glsx.dj.mindworkplate.enums.CommonErrorEnum;
import cn.com.glsx.dj.mindworkplate.remote.BiAnalysisRemote; import cn.com.glsx.dj.mindworkplate.remote.BiAnalysisRemote;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse; import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.wechat.enums.WorkWechatAppMessageTypeEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Arrays; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* 企业微信推送服务 * 企业微信推送服务
...@@ -22,26 +27,23 @@ public class WechatPushService { ...@@ -22,26 +27,23 @@ public class WechatPushService {
@Autowired @Autowired
private BiAnalysisRemote biAnalysisRemote; private BiAnalysisRemote biAnalysisRemote;
private static final List<String> SPECIAL_FOLLOWERS = Arrays.asList("赵展", "普俊鹏", "高军", "王强"); @Autowired
private Environment environment;
/** /**
* 关注行为推送 * 关注行为推送
*/ */
public void pushFollowMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) { public void pushFollowMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) {
if (!SPECIAL_FOLLOWERS.contains(staffName)) {
return;
}
String content = String.format("「%s」关注了你负责的「%s」门店:「%s」,请及时查看。", staffName, businessType, merchantName); String content = String.format("「%s」关注了你负责的「%s」门店:「%s」,请及时查看。", staffName, businessType, merchantName);
sendPush(content, targetUuids); sendPush("重要提醒", content, targetUuids);
} }
/** /**
* @ 提醒推送 * @ 提醒推送
*/ */
public void pushMentionMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) { public void pushMentionMsg(String staffName, String merchantName, String businessType, List<String> targetUuids) {
String content = String.format("「%s」在「%s」门店:「%s」添加了留言并@你,请及时查看。", staffName, businessType, merchantName); String content = String.format("「%s」关注了你负责的「%s」门店:「%s」 添加了留言并@你,请及时查看。", staffName, businessType, merchantName);
sendPush(content, targetUuids); sendPush("重要提醒", content, targetUuids);
} }
/** /**
...@@ -50,18 +52,23 @@ public class WechatPushService { ...@@ -50,18 +52,23 @@ public class WechatPushService {
public void pushDailySummaryMsg(String merchantName, String businessType, int count, List<String> targetUuids, boolean isFollower) { public void pushDailySummaryMsg(String merchantName, String businessType, int count, List<String> targetUuids, boolean isFollower) {
String prefix = isFollower ? "你关注的" : "你负责的"; String prefix = isFollower ? "你关注的" : "你负责的";
String content = String.format("%s「%s」门店:「%s」昨日共有「%d」条动态,请及时查看。", prefix, businessType, merchantName, count); String content = String.format("%s「%s」门店:「%s」昨日共有「%d」条动态,请及时查看。", prefix, businessType, merchantName, count);
sendPush(content, targetUuids); sendPush("日常跟进提醒", content, targetUuids);
} }
private void sendPush(String content, List<String> uuids) { private void sendPush(String title, String content, List<String> uuids) {
log.info("发送企微推送: {}, 目标用户: {}", content, uuids);
try { try {
// 这里复用已有的 MatterAddPushWeChatMsgDTO,如果需要更灵活的文案,可能需要扩展该 DTO 或 Remote 接口 String url = Optional.ofNullable(environment.getProperty("dj.pe.analysis.follow.work.wechat.message.text.card.url"))
MatterAddPushWeChatMsgDTO msg = MatterAddPushWeChatMsgDTO.builder() .orElseThrow(() -> new RpcServiceException(CommonErrorEnum.NOT_FOUND_CONFIG, "未找到url配置项"));
.pushUuids(uuids) CommonPushWeChatMsgDTO msg = CommonPushWeChatMsgDTO.builder()
// .content(content) // 假设 DTO 支持 content 字段,如果不支持,则需要联系用户扩展 .uuids(new HashSet<>(uuids))
.url(url)
.title(title)
.btnTxt("查看详情")
.msgType(WorkWechatAppMessageTypeEnum.TEXTCARD.getValue())
.description(content)
.build(); .build();
RpcResponse<Boolean> response = biAnalysisRemote.matterAddPushWeChatMsg(msg); log.info("发送企微推送: msg:{}", msg);
RpcResponse<Boolean> response = biAnalysisRemote.pushWeChatMsg(msg);
if (response == null || !response.isSuccess()) { if (response == null || !response.isSuccess()) {
log.error("企微推送失败: {}", response != null ? response.getMessage() : "null"); log.error("企微推送失败: {}", response != null ? response.getMessage() : "null");
} }
......
  • @wangxw1
    commented
    Master

    你好,我是负责此次代码审查的资深软件开发工程师。我已仔细评估了你提交的关于“新增每日门店业务动态汇总推送任务”的代码变更。

    总体来看,本次提交实现了预期的业务功能,Commit 信息非常规范清晰。但在核心逻辑的正确性和批量处理的性能方面存在严重问题,需要立即修复。

    以下是本次审查的 Top 3 关键问题与优化建议:

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

    1. 逻辑判断反转,导致推送功能完全失效(Blocker / 阻断性问题)

    • 问题描述:在 MerchantBiFollowService.java 的 triggerFollowPush 方法中,判断推送目标列表 targetUuids 是否为空的逻辑写反了。 java // 当前代码 if (CollectionUtils.isNotEmpty(targetUuids)) { // 如果列表不为空(即找到了人) log.info("触发关注推送:未找到推送目标..."); return; // 直接返回,不推送 } // 如果列表为空,反而继续往下执行 wechatPushService.pushFollowMsg,会导致发送空列表或报错
    • 影响分析:该逻辑会导致所有正常的关注行为都无法触发推送,而没有负责人的门店反而会尝试推送(并记录错误的日志),业务功能完全不可用。
    • 优化建议:修正判断条件。 java // 修改为: if (CollectionUtils.isEmpty(targetUuids)) { log.info("触发关注推送:未找到推送目标,merchantId={}, biz={}", request.getMerchantId(), request.getBusinessType()); return; }

    2. 定时任务存在严重的 N+1 性能问题(Critical / 严重问题)

    • 问题描述:在 MerchantBiDailyPushJob.java 中,采用了“分页查询关注列表 -> 循环处理每一条”的模式。在 processMerchantFollow 循环内部,又分别调用了 commentService(查动态数)、serviceMerchantMapper(查配置)、followDetailMapper(查关注人)以及 wechatPushService(RPC调用)。
    • 影响分析:
      • 假设有 1000 个关注关系,将产生 1000 次 RPC 调用和 3000+ 次数据库查询。
      • 这不仅会导致任务执行时间过长(可能超时),还会对数据库和下游 RPC 服务造成瞬间高并发压力(“惊群效应”),尤其是在早上 9 点业务高峰期。
    • 优化建议:将循环内的单条查询改为批量查询。

      • 取出当前页的 follow 列表后,提取所有的 merchantId 和 id。
      • 批量查询动态数、批量查询门店配置、批量查询关注明细,组装成 Map。
      • 在内存中匹配数据,最后再调用推送。 ```java // 伪代码示例 List follows = ...; // 当前页数据 List merchantIds = follows.stream().map(MerchantBiFollow::getMerchantId).collect(Collectors.toList());

      // 1. 批量查询昨日动态数 (返回 Map) Map countMap = commentService.batchCountYesterdayComments(merchantIds, ...);

      // 2. 批量查询门店配置 (返回 Map) Map settingMap = serviceMerchantMapper.batchFindSettings(merchantIds);

      // 3. 内存匹配并推送 for (MerchantBiFollow follow : follows) { // 从 Map 中获取数据,避免循环查库 // ... }

    3. 推送文案存在复制粘贴错误,语义混淆(Major / 主要问题)

    • 问题描述:在 WechatPushService.java 的 pushMentionMsg(@提醒推送)方法中,文案格式化字符串似乎复制自关注推送,包含了“关注了你负责的”字样。 java // 当前代码 String content = String.format("「%s」关注了你负责的「%s」门店:「%s」 添加了留言并@你,请及时查看。", staffName, businessType, merchantName);
    • 影响分析:用户收到 @ 提醒时,文案显示“某某关注了你负责的...添加了留言”,语义不通顺且容易引起误解(以为是新增关注提醒),降低用户体验。
    • 优化建议:恢复或修正为正确的 @ 提醒文案。 java // 修正建议 String content = String.format("「%s」在「%s」门店:「%s」添加了留言并@你,请及时查看。", staffName, getBusinessTypeName(Byte.valueOf(businessType)), merchantName); // 注意:原代码中 businessType 传入的是 String,这里可能需要确认 getBusinessTypeName 的调用方式,或者确保传入中文名称。

    二、评分明细

    评分维度 评分 理由
    功能实现的正确性与健壮性 20/40 存在阻断性的逻辑反转 Bug(Issue 1),导致核心功能失效;定时任务未考虑大数据量场景下的健壮性。
    安全性与潜在风险 28/30 使用了 MyBatis 避免 SQL 注入,日志记录适当,未见明显安全漏洞。
    最佳实践与可维护性 12/20 定时任务中出现了典型的 N+1 查询反模式;Config 配置项直接硬编码在业务逻辑中(建议封装到配置类);代码复用性尚可。
    性能与资源利用 2/5 循环查库+RPC 极其消耗资源,不可接受。
    提交信息质量 5/5 Commit Message 结构清晰,能够准确描述变更内容和目的,值得表扬。

    总分:67分

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