Commit 7a5a826d by 崔彦峰

refactor(StaffAccountApprovalService): 优化审批通知推送逻辑

- 移除 StringUtils 依赖
- 重构手机号去重逻辑,使用 Stream API 替代传统循环
- 将单一消息推送改为遍历配置逐个推送,支持多管理员通知
- 优化数据结构,使用 Map 存储配置信息提高查找效率
- 改进日志记录方式,增加更详细的调试信息
- 修复潜在的空指针异常问题
1 parent a6ca88b4
......@@ -21,7 +21,6 @@ import cn.com.glsx.scrm.wechat.enums.WorkWechatAppMessageTypeEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.oreframework.distribute.lock.annotation.GlsxDistributeLockable;
import org.springframework.beans.BeanUtils;
import org.springframework.core.env.Environment;
......@@ -174,14 +173,27 @@ public class StaffAccountApprovalService {
return;
}
List<String> qwUserIds = new ArrayList<>();
for (String directorPhone : directorPhones) {
qwUserIds.add(staffResponse.getResult().stream()
.filter(s -> directorPhone.equals(s.getStaffPhone()) && s.getUserId() != null)
.map(EntmicroStaffResponse::getUserId)
.findFirst().orElse(null));
List<ShopMerchantDirectorConfig> haveDirectorPhoneConfigs = shopMerchantDirectorConfigs.stream()
.filter(s -> s.getDirectorPhone() != null && !s.getDirectorPhone().isEmpty()).collect(Collectors.toList());
//haveDirectorPhoneConfigs根据手机号去重
List<ShopMerchantDirectorConfig> distinctPhoneConfigs = haveDirectorPhoneConfigs.stream()
.collect(Collectors.collectingAndThen(
Collectors.toMap(
ShopMerchantDirectorConfig::getDirectorPhone, // 以手机号作为 Map 的 Key
config -> config, // Value 是对象本身
(existing, replacement) -> existing // 如果 Key 重复,保留先出现的 (若想保留后出现的,改写为 replacement)
),
map -> new ArrayList<>(map.values()) // 将 Map 的 Values 转换回 List
));
Map<String, ShopMerchantDirectorConfig> configMap = new HashMap<>();
for (ShopMerchantDirectorConfig haveDirectorPhoneConfig : distinctPhoneConfigs) {
for (EntmicroStaffResponse entmicroStaffResponse : staffResponse.getResult()) {
if (haveDirectorPhoneConfig.getDirectorPhone().equals(entmicroStaffResponse.getStaffPhone()) && entmicroStaffResponse.getUserId() != null) {
configMap.put(entmicroStaffResponse.getUserId(), haveDirectorPhoneConfig);
}
}
}
if (CollectionUtils.isEmpty(qwUserIds)) {
if (configMap.isEmpty()) {
log.error("提交审批通知-线上运营企微用户不存在,merchantId:{},directorPhones:{}", approval.getMerchantId(), JSON.toJSONString(directorPhones));
return;
}
......@@ -192,25 +204,26 @@ public class StaffAccountApprovalService {
String staffName = approval.getStaffName() != null ? approval.getStaffName() : "";
// 发送卡片消息
String userIds = StringUtils.join(qwUserIds,"|");
SendWorkWechatMessageRequestDTO request = new SendWorkWechatMessageRequestDTO();
request.setToUser(userIds);
request.setMerchantId(merchantId);
request.setMsgType(WorkWechatAppMessageTypeEnum.TEXTCARD.getValue());
WorkWechatTextCardMessageDTO textCard = new WorkWechatTextCardMessageDTO();
textCard.setBtnTxt("去处理");
String url = env.getProperty("dj.send.work.wait.approval.notification.url");
textCard.setUrl(String.format(url, approval.getId(), approval.getStaffId(), approval.getStaffName()));
log.info("提交审批通知推送,merchantId:{},url:{}", approval.getMerchantId(), JSON.toJSONString(textCard.getUrl()));
textCard.setDescription(
String.format("你好,【%s】在【%s】发起【%s】开生意助手录单账号,请及时处理审批。",
submitterName, merchantName, staffName));
textCard.setTitle("【新员工开账号/调店审批通知】");
request.setTextCard(textCard);
log.info("提交审批通知推送,merchantId:{},request:{}", approval.getMerchantId(), JSON.toJSONString(request));
boolean success = sendWorkWechatMessageService.sendWechatMessage(request);
log.info("提交审批通知推送{},merchantId:{},request:{}", success ? "成功" : "失败", approval.getMerchantId(), JSON.toJSONString(request));
for (Map.Entry<String, ShopMerchantDirectorConfig> configEntry : configMap.entrySet()) {
request.setToUser(configEntry.getKey());
WorkWechatTextCardMessageDTO textCard = new WorkWechatTextCardMessageDTO();
textCard.setBtnTxt("去处理");
String url = env.getProperty("dj.send.work.wait.approval.notification.url");
textCard.setUrl(String.format(url, approval.getId(), configEntry.getValue().getDirectorUserId(), configEntry.getValue().getDirectorName()));
log.info("提交审批通知推送,merchantId:{},url:{}", approval.getMerchantId(), JSON.toJSONString(textCard.getUrl()));
textCard.setDescription(
String.format("你好,【%s】在【%s】发起【%s】开生意助手录单账号,请及时处理审批。",
submitterName, merchantName, staffName));
textCard.setTitle("【新员工开账号/调店审批通知】");
request.setTextCard(textCard);
log.info("提交审批通知推送,merchantId:{},request:{}", approval.getMerchantId(), JSON.toJSONString(request));
boolean success = sendWorkWechatMessageService.sendWechatMessage(request);
log.info("提交审批通知推送{},merchantId:{},request:{}", success ? "成功" : "失败", approval.getMerchantId(), JSON.toJSONString(request));
}
} catch (Exception e) {
log.error("提交审批通知推送异常,merchantId:{}", approval.getMerchantId(), e);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!