Commit 588d30d8 by 林永健

perf(follow): 优化门店关注推送为批量查询和内存处理

- 批量查询昨日动态数,减少循环单条查询数据库次数
- 按业务类型批量查询门店配置,统一缓存避免重复查询
- 批量查询所有关注明细并在内存中分组,提高查询性能
- 替换单条处理为内存结合批量数据推送,减少数据库访问
- 修改推送逻辑,使用查询缓存数据完成负责人及关注人推送
- 添加新增批量查询接口及对应Mapper XML SQL实现
- 修正推送目标空判断逻辑,避免无效推送尝试
1 parent 80b5f4f2
...@@ -55,7 +55,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -55,7 +55,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
// 1. 分页获取所有存在关注的门店 + 业务组合 // 1. 分页获取所有存在关注的门店 + 业务组合
int pageNum = 1; int pageNum = 1;
int pageSize = 100; // 每页处理 100 条记录 int pageSize = 500; // 每页处理 500 条记录
while (true) { while (true) {
// 使用 PageHelper 进行分页查询 // 使用 PageHelper 进行分页查询
...@@ -72,12 +72,93 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -72,12 +72,93 @@ public class MerchantBiDailyPushJob implements SimpleJob {
log.info("开始处理第{}页数据,当前页记录数:{}", pageNum, follows.size()); log.info("开始处理第{}页数据,当前页记录数:{}", pageNum, follows.size());
// 2. 处理当前页的数据 // 2. 批量查询优化:提取所有 merchantId 和 followId
List<Integer> merchantIds = follows.stream()
.map(MerchantBiFollow::getMerchantId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
List<Integer> followIds = follows.stream()
.map(MerchantBiFollow::getId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
// 提取所有业务类型(同一页可能包含不同业务类型)
List<Byte> businessTypes = follows.stream()
.map(MerchantBiFollow::getBusinessType)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
// 3. 批量查询昨日动态数 (返回 Map<key:merchantId_businessType, count>)
Map<String, Integer> countMap = commentService.batchCountYesterdayCommentsByBizType(merchantIds, businessTypes);
// 4. 批量查询门店配置 (按业务类型对应的 scene 查询)
Map<String, StoreSettingDTO> settingMap = new HashMap<>();
for (Byte bizType : businessTypes) {
Integer scene = MetricsValueUtils.convertBizsToScene(bizType.intValue());
List<StoreSettingDTO> settings = serviceMerchantMapper.findStoreSetting(merchantIds, scene);
for (StoreSettingDTO setting : settings) {
if (setting != null && setting.getMerchantId() != null) {
// key: merchantId_bizType
String key = setting.getMerchantId() + "_" + bizType;
settingMap.put(key, setting);
}
}
}
// 5. 批量查询关注明细 (一次 SQL 查询,内存中分组)
List<MerchantBiFollowDetail> allDetails = followDetailMapper.batchFindByFollowId(followIds);
Map<Integer, List<MerchantBiFollowDetail>> followersMap = new HashMap<>();
if (allDetails != null && !allDetails.isEmpty()) {
followersMap = allDetails.stream()
.filter(detail -> detail != null && detail.getFollowId() != null)
.collect(java.util.stream.Collectors.groupingBy(MerchantBiFollowDetail::getFollowId));
}
// 6. 内存匹配并推送
for (MerchantBiFollow follow : follows) { for (MerchantBiFollow follow : follows) {
processMerchantFollow(follow); try {
Integer merchantId = follow.getMerchantId();
Integer followId = follow.getId();
Byte businessType = follow.getBusinessType();
// 从 Map 中获取数据,避免循环查库
String countKey = merchantId + "_" + businessType;
Integer yesterdayCount = countMap.getOrDefault(countKey, 0);
if (yesterdayCount <= 0) {
continue;
}
// 获取门店配置 (key: merchantId_bizType)
String settingKey = merchantId + "_" + businessType;
StoreSettingDTO setting = settingMap.get(settingKey);
if (setting == null) {
log.info("【{}-{}】门店无相关配置", merchantId, businessType);
continue;
}
List<MerchantBiFollowDetail> details = followersMap.get(followId);
if (details == null || details.isEmpty()) {
log.info("【{}-{}】门店无关注人,id:{}", merchantId, businessType, followId);
continue;
}
// 推送给负责人 (区域运营、线上运营)
pushToOperatorsWithData(follow, yesterdayCount, setting);
// 推送给所有关注人
pushToFollowersWithDetails(follow, yesterdayCount, details);
} catch (Exception e) {
log.error("处理门店关注记录失败,门店 ID:{}, 业务类型:{}, 错误:{}",
follow.getMerchantId(), follow.getBusinessType(), e.getMessage(), e);
}
} }
// 3. 判断是否还有下一页 // 7. 判断是否还有下一页
if (!pageInfo.isHasNextPage()) { if (!pageInfo.isHasNextPage()) {
log.info("数据处理完成,共处理{}页,总记录数:{}", pageInfo.getPages(), pageInfo.getTotal()); log.info("数据处理完成,共处理{}页,总记录数:{}", pageInfo.getPages(), pageInfo.getTotal());
break; break;
...@@ -88,29 +169,48 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -88,29 +169,48 @@ public class MerchantBiDailyPushJob implements SimpleJob {
} }
/** /**
* 处理单个门店关注记录 * 推送给负责人(使用已查询的配置数据)
*/
private void pushToOperatorsWithData(MerchantBiFollow follow, int count, StoreSettingDTO setting) {
Integer merchantId = follow.getMerchantId();
Byte businessType = follow.getBusinessType();
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 processMerchantFollow(MerchantBiFollow follow) { private void pushToFollowersWithDetails(MerchantBiFollow follow, int count, List<MerchantBiFollowDetail> details) {
try { Integer merchantId = follow.getMerchantId();
// 查询该门店 + 业务昨日的动态总数 (策略) Byte businessType = follow.getBusinessType();
MerchantBiCommentReq commentReq = MerchantBiCommentReq.builder() Integer followId = follow.getId();
.merchantIds(Collections.singletonList(follow.getMerchantId()))
.businessType(follow.getBusinessType()) List<String> followerUuids = details.stream()
.build(); .map(MerchantBiFollowDetail::getStaffUuid)
int yesterdayCount = commentService.countYesterdayComments(commentReq); .filter(StringUtils::isNotBlank)
.distinct()
if (yesterdayCount <= 0) .collect(Collectors.toList());
return;
if (CollectionUtils.isEmpty(followerUuids)) {
// 推送给负责人 (区域运营、线上运营) log.info("【{}-{}】门店无关注人,id:{}", merchantId, businessType, followId);
pushToOperators(follow, yesterdayCount); return;
// 推送给所有关注人
pushToFollowers(follow, yesterdayCount);
} catch (Exception e) {
log.error("处理门店关注记录失败,门店 ID:{}, 业务类型:{}, 错误:{}",
follow.getMerchantId(), follow.getBusinessType(), e.getMessage(), e);
} }
wechatPushService.pushDailySummaryMsg(follow.getMerchantName(), getBusinessTypeName(follow.getBusinessType()),
count, followerUuids, true);
} }
private void pushToOperators(MerchantBiFollow follow, int count) { private void pushToOperators(MerchantBiFollow follow, int count) {
......
...@@ -41,4 +41,12 @@ public interface MerchantBiFollowDetailMapper extends OreMapper<MerchantBiFollow ...@@ -41,4 +41,12 @@ public interface MerchantBiFollowDetailMapper extends OreMapper<MerchantBiFollow
* 根据关注 ID 查询所有明细 * 根据关注 ID 查询所有明细
*/ */
List<MerchantBiFollowDetail> findByFollowId(@Param("followId") Integer followId); List<MerchantBiFollowDetail> findByFollowId(@Param("followId") Integer followId);
/**
* 批量根据关注 ID 查询所有明细
*
* @param followIds 关注 ID 列表
* @return 所有关注明细列表
*/
List<MerchantBiFollowDetail> batchFindByFollowId(@Param("followIds") List<Integer> followIds);
} }
\ No newline at end of file \ No newline at end of file
...@@ -24,8 +24,8 @@ public interface ServiceMerchantMapper extends OreMapper<ServiceMerchant> { ...@@ -24,8 +24,8 @@ public interface ServiceMerchantMapper extends OreMapper<ServiceMerchant> {
/** /**
* 查询门店运营人员/督导配置 * 查询门店运营人员/督导配置
* *
* @param merchantIds 门店ID列表 * @param merchantIds 门店 ID 列表
* @param scene 场景(0=1.1,1=2.1;为空时返回两套场景字段) * @param scene 场景(0=1.1,1=2.1)
*/ */
List<StoreSettingDTO> findStoreSetting(@Param("merchantIds") List<Integer> merchantIds, @Param("scene") Integer scene); List<StoreSettingDTO> findStoreSetting(@Param("merchantIds") List<Integer> merchantIds, @Param("scene") Integer scene);
......
...@@ -8,6 +8,7 @@ import org.oreframework.datasource.mybatis.mapper.OreMapper; ...@@ -8,6 +8,7 @@ import org.oreframework.datasource.mybatis.mapper.OreMapper;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
@Mapper @Mapper
public interface StrategyInfoMapper extends OreMapper<StrategyInfo> { public interface StrategyInfoMapper extends OreMapper<StrategyInfo> {
...@@ -21,4 +22,19 @@ public interface StrategyInfoMapper extends OreMapper<StrategyInfo> { ...@@ -21,4 +22,19 @@ public interface StrategyInfoMapper extends OreMapper<StrategyInfo> {
* 统计指定时间段内的记录数 * 统计指定时间段内的记录数
*/ */
int countStrategyByTime(@Param("merchantIds") List<Integer> merchantIds, @Param("biz") Integer biz, @Param("startDate") Date startDate, @Param("endDate") Date endDate); int countStrategyByTime(@Param("merchantIds") List<Integer> merchantIds, @Param("biz") Integer biz, @Param("startDate") Date startDate, @Param("endDate") Date endDate);
/**
* 批量统计指定时间段内的记录数(按门店分组)
*
* @param merchantIds 门店 ID 列表
* @param biz 业务类型
* @param startDate 开始时间
* @param endDate 结束时间
* @return List<Map>, 每个 Map 包含 merchant_id 和 count
*/
List<Map<String, Object>> batchCountStrategyByTime(
@Param("merchantIds") List<Integer> merchantIds,
@Param("biz") Integer biz,
@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
} }
...@@ -160,6 +160,49 @@ public class MerchantBiCommentService { ...@@ -160,6 +160,49 @@ public class MerchantBiCommentService {
); );
} }
/**
* 批量统计昨日新增记录总数(按门店 ID 和业务类型分组)
*
* @param merchantIds 门店 ID 列表
* @param businessTypes 业务类型列表
* @return Map<key:merchantId_businessType, 数量>
*/
public Map<String, Integer> batchCountYesterdayCommentsByBizType(
List<Integer> merchantIds,
List<Byte> businessTypes) {
if (merchantIds == null || merchantIds.isEmpty()) {
return Collections.emptyMap();
}
long startMs = System.currentTimeMillis();
Date yesterdayStart = DateUtil.beginOfDay(DateUtil.yesterday());
Date yesterdayEnd = DateUtil.endOfDay(DateUtil.yesterday());
Map<String, Integer> finalResult = new HashMap<>();
for (Byte businessType : businessTypes) {
List<Map<String, Object>> results = strategyInfoMapper.batchCountStrategyByTime(
merchantIds,
businessType != null ? businessType.intValue() : null,
yesterdayStart,
yesterdayEnd
);
Map<Integer, Integer> resultMap = results.stream()
.filter(m -> m != null && m.get("merchant_id") != null)
.collect(java.util.stream.Collectors.toMap(
m -> (Integer) m.get("merchant_id"),
m -> ((Long) m.get("count")).intValue(),
(v1, v2) -> v1
));
resultMap.forEach((k, v) -> finalResult.put(k + "_" + businessType, v));
}
log.info("批量统计昨日动态完成 (多业务类型): 门店数={}, 返回数={}, 耗时={}ms",
merchantIds.size(), finalResult.size(), System.currentTimeMillis() - startMs);
return finalResult;
}
private String getBusinessTypeName(Byte businessType) { private String getBusinessTypeName(Byte businessType) {
if (businessType == null) return "未知业务"; if (businessType == null) return "未知业务";
switch (businessType) { switch (businessType) {
......
...@@ -606,7 +606,7 @@ public class MerchantBiFollowService { ...@@ -606,7 +606,7 @@ public class MerchantBiFollowService {
targetUuids.add(setting.getRegionalOperationId()); targetUuids.add(setting.getRegionalOperationId());
if (StringUtils.isNotBlank(setting.getOnlineOperationId())) if (StringUtils.isNotBlank(setting.getOnlineOperationId()))
targetUuids.add(setting.getOnlineOperationId()); targetUuids.add(setting.getOnlineOperationId());
if (CollectionUtils.isNotEmpty(targetUuids)) { if (CollectionUtils.isEmpty(targetUuids)) {
log.info("触发关注推送:未找到推送目标,merchantId={}, biz={}", request.getMerchantId(), request.getBusinessType()); log.info("触发关注推送:未找到推送目标,merchantId={}, biz={}", request.getMerchantId(), request.getBusinessType());
return; return;
} }
......
...@@ -77,4 +77,15 @@ ...@@ -77,4 +77,15 @@
WHERE follow_id = #{followId} WHERE follow_id = #{followId}
AND deleted_flag = 'N' AND deleted_flag = 'N'
</select> </select>
<select id="batchFindByFollowId" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM dj_merchant_bi_follow_detail
WHERE follow_id IN
<foreach collection="followIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
AND deleted_flag = 'N'
</select>
</mapper> </mapper>
...@@ -584,18 +584,6 @@ ...@@ -584,18 +584,6 @@
<select id="findStoreSetting" resultType="cn.com.glsx.dj.peanalysis.dto.StoreSettingDTO"> <select id="findStoreSetting" resultType="cn.com.glsx.dj.peanalysis.dto.StoreSettingDTO">
select select
smdc.merchant_id as merchantId, smdc.merchant_id as merchantId,
group_concat(distinct if(smdc.director_type = 7, smdc.director_user_id, null)) as onlineOperationId11,
group_concat(distinct if(smdc.director_type = 7, smdc.director_name, null) separator '/') as onlineOperationName11,
group_concat(distinct if(smdc.director_type = 9, smdc.director_user_id, null)) as regionalOperationId11,
group_concat(distinct if(smdc.director_type = 9, smdc.director_name, null) separator '/') as regionalOperationName11,
group_concat(distinct if(smdc.director_type = 3, smdc.director_user_id, null)) as sellerUserId11,
group_concat(distinct if(smdc.director_type = 3, smdc.director_name, null) separator '/') as sellerUserName11,
group_concat(distinct if(smdc.director_type = 5, smdc.director_user_id, null)) as onlineOperationId21,
group_concat(distinct if(smdc.director_type = 5, smdc.director_name, null) separator '/') as onlineOperationName21,
group_concat(distinct if(smdc.director_type = 8, smdc.director_user_id, null)) as regionalOperationId21,
group_concat(distinct if(smdc.director_type = 8, smdc.director_name, null) separator '/') as regionalOperationName21,
group_concat(distinct if(smdc.director_type = 1, smdc.director_user_id, null)) as sellerUserId21,
group_concat(distinct if(smdc.director_type = 1, smdc.director_name, null) separator '/') as sellerUserName21,
<choose> <choose>
<when test="scene == 0"> <when test="scene == 0">
group_concat(distinct if(smdc.director_type = 7, smdc.director_user_id, null)) as onlineOperationId, group_concat(distinct if(smdc.director_type = 7, smdc.director_user_id, null)) as onlineOperationId,
...@@ -614,12 +602,18 @@ ...@@ -614,12 +602,18 @@
group_concat(distinct if(smdc.director_type = 1, smdc.director_name, null) separator '/') as sellerUserName, group_concat(distinct if(smdc.director_type = 1, smdc.director_name, null) separator '/') as sellerUserName,
</when> </when>
<otherwise> <otherwise>
group_concat(distinct if(smdc.director_type in (5,7), smdc.director_user_id, null)) as onlineOperationId, group_concat(distinct if(smdc.director_type = 7, smdc.director_user_id, null)) as onlineOperationId11,
group_concat(distinct if(smdc.director_type in (5,7), smdc.director_name, null) separator '/') as onlineOperationName, group_concat(distinct if(smdc.director_type = 7, smdc.director_name, null) separator '/') as onlineOperationName11,
group_concat(distinct if(smdc.director_type in (8,9), smdc.director_user_id, null)) as regionalOperationId, group_concat(distinct if(smdc.director_type = 9, smdc.director_user_id, null)) as regionalOperationId11,
group_concat(distinct if(smdc.director_type in (8,9), smdc.director_name, null) separator '/') as regionalOperationName, group_concat(distinct if(smdc.director_type = 9, smdc.director_name, null) separator '/') as regionalOperationName11,
group_concat(distinct if(smdc.director_type in (1,3), smdc.director_user_id, null)) as sellerUserId, group_concat(distinct if(smdc.director_type = 3, smdc.director_user_id, null)) as sellerUserId11,
group_concat(distinct if(smdc.director_type in (1,3), smdc.director_name, null) separator '/') as sellerUserName, group_concat(distinct if(smdc.director_type = 3, smdc.director_name, null) separator '/') as sellerUserName11,
group_concat(distinct if(smdc.director_type = 5, smdc.director_user_id, null)) as onlineOperationId21,
group_concat(distinct if(smdc.director_type = 5, smdc.director_name, null) separator '/') as onlineOperationName21,
group_concat(distinct if(smdc.director_type = 8, smdc.director_user_id, null)) as regionalOperationId21,
group_concat(distinct if(smdc.director_type = 8, smdc.director_name, null) separator '/') as regionalOperationName21,
group_concat(distinct if(smdc.director_type = 1, smdc.director_user_id, null)) as sellerUserId21,
group_concat(distinct if(smdc.director_type = 1, smdc.director_name, null) separator '/') as sellerUserName21,
</otherwise> </otherwise>
</choose> </choose>
group_concat(distinct if(smdc.director_type = 6, smdc.director_user_id, null)) as regionalSupervisorId, group_concat(distinct if(smdc.director_type = 6, smdc.director_user_id, null)) as regionalSupervisorId,
...@@ -628,12 +622,10 @@ ...@@ -628,12 +622,10 @@
group_concat(distinct yqmc.partner_merchant_name separator '/') as shopMerchantName group_concat(distinct yqmc.partner_merchant_name separator '/') as shopMerchantName
from dj_smartcarlife.dj_car_life_shop_merchant_director_config smdc from dj_smartcarlife.dj_car_life_shop_merchant_director_config smdc
left join dj_car_life_scene_merchant_ref a on smdc.merchant_id = a.main_merchant_id and a.deleted_flag = 'N' and a.main_merchant_type = 2 and a.sub_merchant_type in (6,8) left join dj_car_life_scene_merchant_ref a on smdc.merchant_id = a.main_merchant_id and a.deleted_flag = 'N' and a.main_merchant_type = 2 and a.sub_merchant_type in (6,8)
<if test="scene != null">
and a.scene = #{scene}
</if>
left join dj_smartcarlife.dj_car_life_yqmerchant_config yqmc on yqmc.merchant_id = a.sub_merchant_id and yqmc.deleted_flag = 'N' left join dj_smartcarlife.dj_car_life_yqmerchant_config yqmc on yqmc.merchant_id = a.sub_merchant_id and yqmc.deleted_flag = 'N'
<where> <where>
smdc.deleted_flag = 'N' smdc.deleted_flag = 'N'
and a.scene = #{scene}
<if test="merchantIds != null and merchantIds.size() > 0"> <if test="merchantIds != null and merchantIds.size() > 0">
and smdc.merchant_id in and smdc.merchant_id in
<foreach item="item" index="index" collection="merchantIds" open="(" separator="," close=")"> <foreach item="item" index="index" collection="merchantIds" open="(" separator="," close=")">
......
...@@ -76,4 +76,21 @@ ...@@ -76,4 +76,21 @@
AND deleted_flag = 'N' AND deleted_flag = 'N'
AND created_date BETWEEN #{startDate} AND #{endDate} AND created_date BETWEEN #{startDate} AND #{endDate}
</select> </select>
<select id="batchCountStrategyByTime" resultType="java.util.HashMap">
SELECT
merchant_id,
COUNT(*) as count
FROM dj_smartcarlife.dj_car_life_strategy_info
WHERE merchant_id IN
<foreach collection="merchantIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>
<if test="biz != null">
AND biz = #{biz}
</if>
AND deleted_flag = 'N'
AND created_date BETWEEN #{startDate} AND #{endDate}
GROUP BY merchant_id
</select>
</mapper> </mapper>
  • @wangxw1
    commented
    Master

    你好。我是资深的软件开发工程师,很高兴为你进行代码审查。本次代码变更的主要目的是将原有的循环单条处理逻辑优化为批量查询与内存处理,方向非常正确,但在实现细节上存在几个关键问题。

    以下是针对本次提交的代码审查意见:

    一、关键问题与优化建议

    1. SQL 逻辑错误:LEFT JOIN 关联条件误移至 WHERE 子句

    • 问题描述:在 ServiceMerchantMapper.xml 中,原先在 LEFT JOIN 的 ON 之后的可选过滤条件 a.scene = #{scene} 被移动到了 <where> 子句中。
    • 影响分析:在 SQL 标准中,LEFT JOIN 的关联表过滤条件如果放在 WHERE 中,会使 LEFT JOIN 退化为 INNER JOIN。如果某个门店在 dj_car_life_scene_merchant_ref 表中没有对应 scene 的记录,整个门店的配置信息(smdc 表数据)都会被过滤掉,导致推送失败。
    • 优化建议:将 a.scene = #{scene} 重新放回 LEFT JOIN 的 ON 关联条件中,或者确保业务逻辑上该关联必须存在。 xml <!-- 优化建议 --> left join dj_car_life_scene_merchant_ref a on smdc.merchant_id = a.main_merchant_id and a.deleted_flag = 'N' and a.main_merchant_type = 2 and a.sub_merchant_type in (6,8) and a.scene = #{scene} -- 保持在 ON 子句中

    2. 性能风险:循环内重复执行大批量 IN 查询

    • 问题描述:在 MerchantBiDailyPushJob 中,pageSize 增大到了 500。但在处理逻辑中,findStoreSetting 仍放在 for (Byte bizType : businessTypes) 循环内。
    • 影响分析:
      1. 如果一页数据包含 5 种业务类型,则会执行 5 次 SQL,每次 SQL 的 IN 子句都携带 500 个 merchantId,造成数据库解析压力。
      2. 经观察,不同 bizType 可能会映射到相同的 scene(通过 convertBizsToScene),这会导致完全相同的 SQL 携带 500 个 ID 重复查询多次,浪费 IO 资源。
    • 优化建议:先在内存中计算出所有去重后的 scenes,在循环外一次性查询所有 scene 的配置,或在循环内增加 scene 维度的缓存。

    3. 健壮性隐患:硬编码 Map 结果集处理与类型强转

    • 问题描述:strategyInfoMapper.batchCountStrategyByTime 返回类型为 List<Map<String, Object>>,在 Service 层通过 (Long) m.get("count") 进行强转,并硬编码了字符串 Key。
    • 影响分析:
      1. 类型安全:不同版本的 JDBC 驱动或数据库底层实现对 COUNT(*) 的返回类型可能不同(如 Long 或 BigInteger),强转 Long 易抛出 ClassCastException。
      2. 维护性:硬编码 "merchant_id" 和 "count" 与 XML 中的别名耦合。若 SQL 调整了别名,Java 端编译期无法察觉,将导致运行时 NPE。
    • 优化建议:定义专门的 StatisticsDTO 类,利用 MyBatis 的 resultType 自动映射。 java // 建议定义 DTO public class MerchantCountDTO { private Integer merchantId; private Integer count; }

    二、评分明细

    评分维度 分数 理由
    功能实现正确性与健壮性 30/40 批量化改造方向正确,但 SQL 逻辑变更(LEFT JOIN 变 INNER JOIN)存在业务回归风险;Map 强转存在隐患。
    安全性与潜在风险 30/30 使用了 MyBatis 的 foreach 处理 IN 查询,无 SQL 注入风险;暂未发现越权或敏感信息泄露。
    最佳实践与可维护性 15/20 引入了批量处理减少了 RPC/DB 调用次数。但 Mapper 返回值建议使用 DTO 而非 Map,且 XML 别名在 otherwise 分支中不统一。
    性能与资源利用 4/5 显著减少了循环查库次数,但 findStoreSetting 在循环内对大集合进行 IN 查询仍有优化空间。
    提交信息质量 5/5 Commit 信息清晰,涵盖了主要变更点和优化动机。

    总分:84分

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