Commit e37d10d0 by 林永健

feat(mapper): 添加商户担保配置查询功能

- 在 EarlyWarningThresholdConfigMapper 中新增 findGuaranteeConfigByMerchantId 方法
- 添加对 dj_car_life_merchant_guarantee 表的查询支持
- 实现从新表查询兜底配置的逻辑
- 添加数据映射转换方法 mapGuaranteeToConfig
- 支持多种业务场景码和担保指标的数据转换
- 添加对 CollectionUtils 的依赖用于集合判空处理
1 parent b2e50ebe
package cn.com.glsx.dj.mindworkplate.mapper;
import java.util.List;
import java.util.Map;
import cn.com.glsx.dj.mindworkplate.model.EarlyWarningThresholdConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -9,4 +12,6 @@ import org.oreframework.datasource.mybatis.mapper.OreMapper;
public interface EarlyWarningThresholdConfigMapper extends OreMapper<EarlyWarningThresholdConfig> {
EarlyWarningThresholdConfig findByMerchantNo(@Param("merchantNo") String merchantNo);
List<Map<String, Object>> findGuaranteeConfigByMerchantId(@Param("merchantId") Integer merchantId);
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import cn.com.glsx.dj.mindworkplate.model.EarlyWarningThresholdConfig;
import cn.com.glsx.dj.mindworkplate.model.EarlyWarningThresholdConfigHistory;
import cn.com.glsx.dj.mindworkplate.model.EarlyWarningThresholdRangeConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -15,6 +16,8 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.math.BigDecimal;
import java.util.Map;
@Slf4j
@Service
......@@ -36,6 +39,28 @@ public class EarlyWarningThresholdConfigService {
* @return
*/
public EarlyWarningThresholdConfig findByMerchantNo(String merchantNo) {
// 优先从新表 dj_car_life_merchant_guarantee 查询兜底配置
try {
Integer merchantId = Integer.valueOf(merchantNo);
List<Map<String, Object>> guaranteeConfigs = earlyWarningThresholdConfigMapper.findGuaranteeConfigByMerchantId(merchantId);
if (CollectionUtils.isNotEmpty(guaranteeConfigs)) {
EarlyWarningThresholdConfig config = new EarlyWarningThresholdConfig();
config.setMerchantNo(merchantNo);
for (Map<String, Object> row : guaranteeConfigs) {
String baseSceneCode = (String) row.get("base_scene_code");
String guaranteeIndex = (String) row.get("guarantee_index");
BigDecimal standard = (BigDecimal) row.get("guarantee_standard");
mapGuaranteeToConfig(config, baseSceneCode, guaranteeIndex, standard);
}
EarlyWarningThresholdRangeConfig warningThresholdRangeConfig = findWarningThresholdRangeConfig(1);
config.setRangeValue(warningThresholdRangeConfig.getRangeValue());
return config;
}
} catch (NumberFormatException e) {
// merchantNo 非纯数字,回退到旧表查询
}
// 兜底:从旧表 early_warning_threshold_config 查询
EarlyWarningThresholdConfig byMerchantNo = earlyWarningThresholdConfigMapper.findByMerchantNo(
merchantNo);
if(byMerchantNo!=null){
......@@ -46,6 +71,49 @@ public class EarlyWarningThresholdConfigService {
}
return byMerchantNo;
}
private void mapGuaranteeToConfig(EarlyWarningThresholdConfig config, String baseSceneCode, String guaranteeIndex, BigDecimal value) {
if (value == null) {
return;
}
if ("110201".equals(baseSceneCode)) {
switch (guaranteeIndex) {
case "WR_CONV_001":
config.setEwConRate(value);
break;
case "WR_CONV_002":
config.setEwAxbConRate(value);
break;
case "WR_CONV_004":
config.setEwLtConRate(value);
break;
case "WR_CONV_003":
config.setBatteryConversionRate(value);
break;
case "WR_RATE_001":
config.setEwPenetrationRate(value);
break;
case "WR_RATE_002":
config.setEwAxbPenetrationRate(value);
break;
case "WR_RATE_004":
config.setEwLtPenetrationRate(value);
break;
case "WR_RATE_003":
config.setBatteryPenetrationRate(value);
break;
default:
break;
}
} else if ("110101".equals(baseSceneCode)) {
switch (guaranteeIndex) {
case "BE_CONV_001":
config.setRiConRate(value);
break;
default:
break;
}
}
}
/**
* 根据类型查询预警阈值
*
......
......@@ -38,4 +38,12 @@
where merchant_no = #{merchantNo}
and deleted_flag = 'N'
</select>
<select id="findGuaranteeConfigByMerchantId" resultType="map">
SELECT base_scene_code, guarantee_index, guarantee_standard
FROM dj_car_life_merchant_guarantee
WHERE merchant_id = #{merchantId, jdbcType=INTEGER}
AND deleted_flag = 'N'
AND base_scene_code IN ('110201', '110101', '100101')
</select>
</mapper>
\ No newline at end of file
  • @wangxw1
    commented
    Master

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

    1. 性能与规范:使用异常处理来进行业务逻辑控制(反模式)

    • 问题描述:在 EarlyWarningThresholdConfigService.java 中,代码通过 try { Integer.valueOf(merchantNo); } catch (NumberFormatException e) 来判断 merchantNo 是否为纯数字,以此决定是否查询新表。
    • 影响分析:在 Java 中,异常对象的创建(特别是填充堆栈跟踪信息)是非常消耗性能的操作。将异常作为常规的控制流(Control Flow)来使用属于典型的反模式。如果系统中存在大量非数字的 merchantNo 请求,将导致不必要的性能开销。
    • 优化建议:应使用正则表达式或工具类(如 Apache Commons Lang 的 StringUtils.isNumeric)进行前置校验。 java // 优化示例 if (StringUtils.isNumeric(merchantNo)) { Integer merchantId = Integer.valueOf(merchantNo); List<Map<String, Object>> guaranteeConfigs = earlyWarningThresholdConfigMapper.findGuaranteeConfigByMerchantId(merchantId); // ... 处理逻辑 } // 兜底逻辑...

    2. 健壮性隐患:Switch 语句存在潜在的 NullPointerException 风险

    • 问题描述:在 mapGuaranteeToConfig 方法中,直接对从 Map 中取出的 guaranteeIndex 使用了 switch (guaranteeIndex) 语句。
    • 影响分析:如果数据库中 guarantee_index 字段存在 null 值,或者映射时由于某些原因取到了 null,Java 中的 switch(String) 遇到 null 时会直接抛出 NullPointerException,导致整个查询接口崩溃,影响核心业务可用性。
    • 优化建议:在进行 switch 匹配前,增加对 guaranteeIndex 的非空校验。 java // 优化示例 private void mapGuaranteeToConfig(EarlyWarningThresholdConfig config, String baseSceneCode, String guaranteeIndex, BigDecimal value) { if (value == null || guaranteeIndex == null) { return; } // 后续 switch 逻辑... }

    3. 可维护性:过度使用 Map 接收结果与大量魔法值(Magic Strings)

    • 问题描述:
      1. Mapper 接口使用 List<Map<String, Object>> 接收数据库查询结果。
      2. Service 层充斥着大量硬编码的魔法字符串(如 "110201", "WR_CONV_001", "base_scene_code" 等),且 XML 中也硬编码了场景码。
    • 影响分析:使用 Map 丢失了强类型语言的优势,从 Map 中取值时极易因为拼写错误引发 Bug,且不利于后续的重构和代码阅读。大量的魔法字符串使得业务规则(如哪些指标对应哪些字段)散落在代码中,一旦后续新增或修改指标,维护成本极高,极易出错。
    • 优化建议:
      1. 新增一个 DTO(如 GuaranteeConfigDTO)来接收 MyBatis 的查询结果,替代 Map。
      2. 将场景码(SceneCode)和指标码(GuaranteeIndex)提取为常量类或枚举(Enum),统一管理映射关系。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):业务逻辑基本实现,通过了无子查询和无 Dubbo 接口变更的红线校验。但 switch 语句缺乏 null 值保护,存在引发 NPE 的隐患,扣除 5 分。
    • 安全性与潜在风险(30/30分):MyBatis 的 XML 中使用了 #{merchantId} 进行参数化查询,有效防止了 SQL 注入;未发现越权或敏感信息泄露等安全风险。
    • 最佳实践与可维护性(12/20分):使用异常做控制流、使用 Map 替代 DTO 接收数据、代码中存在大量魔法值(Magic String)和冗长的 switch-case,不符合主流工程最佳实践,扣除 8 分。
    • 性能与资源利用(3/5分):利用 try-catch 捕获 NumberFormatException 来处理常规业务分支,在非数字入参较多时会带来额外的性能损耗,扣除 2 分。
    • 提交信息质量(5/5分):Commit 信息结构清晰,分为 header 和 body,详细列出了修改点(新增方法、新表支持、集合判空等),具备良好的可追溯性。

    总分:85分

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