Commit 36a41000 by 崔彦峰

feat(QWApprovalService): 添加商户企微应用配置验证和动态agentId设置

- 引入MerchantCorpProperties和NbCbCustEnum依赖
- 注入MerchantCorpConfigService服务
- 根据商户ID获取企微应用配置信息
- 验证商户是否为广联主体或优加主体
- 使用动态agentId替代默认配置
- 移除硬编码的默认agentId设置
1 parent 41898300
package cn.com.glsx.dj.financial.service;
import cn.com.glsx.dj.financial.config.MerchantCorpProperties;
import cn.com.glsx.dj.financial.enums.NbCbCustEnum;
import cn.com.glsx.dj.financial.enums.QWApprovalOrderStatusEnum;
import cn.com.glsx.dj.financial.manager.QWManager;
import cn.com.glsx.dj.financial.mapper.ApprovalRecordsMapper;
......@@ -72,6 +74,9 @@ public class QWApprovalService {
@Autowired
private NbCbCustInfoService nbCbCustInfoService;
@Autowired
private MerchantCorpConfigService merchantCorpConfigService;
/**
* 1.处理审批记录 2.记录审批记录
*
......@@ -610,10 +615,23 @@ public class QWApprovalService {
if (com.alibaba.nacos.common.utils.StringUtils.isEmpty(property)){
return;
}
Optional<MerchantCorpProperties.MerchantCorpConfig> merchantCorpConfig = merchantCorpConfigService.getConfigByMerchantId(approvalRecord.getMerchantId());
boolean present = merchantCorpConfig.isPresent();
if (!present) {
log.info("未找到商户企微应用配置信息,merchantId:{}", approvalRecord.getMerchantId());
throw new RuntimeException("未找到商户企微应用配置信息");
}
Integer custType = merchantCorpConfig.get().getCustType();
if (!Objects.equals(custType, NbCbCustEnum.GL_CUST.getCustType()) && !Objects.equals(custType, NbCbCustEnum.YJ_CUST.getCustType())) {
log.info("商户不是广联主体或者优加主体,merchantId:{},custType:{}",approvalRecord.getMerchantId(),custType);
throw new RuntimeException("商户不是广联主体或者优加主体");
}
String agentId = merchantCorpConfig.get().getAgentId();
SendWorkWechatMessageRequestDTO sendWorkWechatMessageRequestDTO = new SendWorkWechatMessageRequestDTO();
sendWorkWechatMessageRequestDTO.setMsgType("text");
sendWorkWechatMessageRequestDTO.setToUser(approvalRecord.getApplyerUserid());
sendWorkWechatMessageRequestDTO.setAgentId(environment.getProperty("default.agentid")==null?"1000008":environment.getProperty("default.agentid"));
// sendWorkWechatMessageRequestDTO.setAgentId(environment.getProperty("default.agentid")==null?"1000008":environment.getProperty("default.agentid"));
sendWorkWechatMessageRequestDTO.setAgentId(agentId);
WorkWechatTextMessageDTO workWechatTextMessageDTO = new WorkWechatTextMessageDTO();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("【").append(approvalRecord.getPayTitle()).append("】").append("推送付款信息失败\n");
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 异常处理:避免抛出通用 RuntimeException

    • 问题描述:在未找到商户配置或主体类型不符时,代码直接抛出了原生的 RuntimeException。
    • 影响分析:抛出通用的 RuntimeException 是一种反模式。它通常会被全局异常处理器捕获为未知的系统异常(通常返回 HTTP 500 和通用的系统错误提示),而不是明确的业务验证失败。这会导致前端无法获取准确的错误码,同时也增加了日志排查的难度。
    • 优化建议:建议替换为项目中现有的自定义业务异常类(如 BusinessException 或 ServiceException),并附带对应的业务错误码。

    2. 最佳实践:优化 Optional 的使用方式

    • 问题描述:代码中使用了 isPresent() 判断后接 get() 的方式来处理 Optional 对象,这与直接判空差异不大,没有充分发挥 Optional 的流式处理优势。
    • 影响分析:代码显得较为冗长,可读性有待提升。
    • 优化建议:建议使用 Optional.orElseThrow() 方法来简化代码逻辑,使代码更加优雅和紧凑。
    • 代码示例: ```java MerchantCorpProperties.MerchantCorpConfig config = merchantCorpConfigService.getConfigByMerchantId(approvalRecord.getMerchantId()) .orElseThrow(() -> { log.info("未找到商户企微应用配置信息,merchantId:{}", approvalRecord.getMerchantId()); return new BusinessException("未找到商户企微应用配置信息"); // 替换为实际的业务异常类 });

    Integer custType = config.getCustType(); if (!Objects.equals(custType, NbCbCustEnum.GL_CUST.getCustType()) && !Objects.equals(custType, NbCbCustEnum.YJ_CUST.getCustType())) { log.info("商户不是广联主体或者优加主体,merchantId:{},custType:{}", approvalRecord.getMerchantId(), custType); throw new BusinessException("商户不是广联主体或者优加主体"); }

    String agentId = config.getAgentId();

    
    **3. 代码整洁度:遗留被注释的废弃代码**
    - **问题描述**:代码中保留了被注释掉的旧逻辑 `// sendWorkWechatMessageRequestDTO.setAgentId(environment.getProperty...`。
    - **影响分析**:提交被注释的无用代码会造成代码库的“破窗效应”,降低代码的整洁度和可读性。版本控制系统(Git)已经记录了历史修改,无需在代码中保留注释版本。
    - **优化建议**:直接删除该行被注释的废弃代码。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(36/40分)**:业务逻辑实现正确,使用了 `Objects.equals` 安全地处理了 `custType` 可能为 null 的比较。扣分点在于使用了原生的 `RuntimeException` 进行业务阻断,降低了系统的健壮性和异常处理的规范性。未触发 Mybatis 子查询和 Dubbo 接口变更的红线。
    - **安全性与潜在风险(30/30分)**:未见 SQL 注入、XSS、越权等安全风险。日志打印了关键的 `merchantId` 和 `custType`,且未包含敏感信息(PII),符合安全规范。
    - **最佳实践与可维护性(15/20分)**:变量命名清晰,逻辑分明。扣分点主要在于 `Optional` 的非最佳实践用法以及未清理的注释废弃代码。
    - **性能与资源利用(5/5分)**:无明显的性能瓶颈,资源利用合理。
    - **提交信息质量(5/5分)**:Commit Message 采用了 Conventional Commits 规范(`feat(...)`),清晰地描述了修改内容、原因及具体改动点,质量极高,易于追溯。
    
    总分:91分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!