Commit d307ba03 by 林永健

refactor(project-group): 优化项目组门店归属关系查询逻辑

- 修改 PgGroupStoreRelMapper 接口返回类型为 ProjectGroupStoreDTO
- 更新 ProjectGroupService 中商户分组映射逻辑,使用 DTO 对象替代 Map
- 在门店归属验证中显示项目组名称而非门店 ID 列表
- 调整预警配置去重校验规则,移除监控对象字段
- 更新 ORM 映射文件查询逻辑,关联项目组信息并返回完整 DTO
- 调整指标字典分组条件,移除单位字段
- 在 DTO 中添加组名字段并设置为忽略序列化
1 parent 98dd3b1e
package cn.com.glsx.dj.strategy.dto.projectgroup;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
......@@ -23,6 +24,9 @@ public class ProjectGroupStoreDTO implements Serializable {
@ApiModelProperty(notes = "项目组id")
private Integer groupId;
@JsonIgnore
private String groupName;
@ApiModelProperty(notes = "门店号")
private Integer merchantId;
......
......@@ -42,7 +42,7 @@ public interface PgGroupStoreRelMapper extends OreMapper<PgGroupStoreRel> {
* @param merchantIds 门店号列表
* @return 门店归属关系
*/
List<Map<String, Object>> listActiveGroupByMerchantIds(@Param("merchantIds") List<Integer> merchantIds);
List<ProjectGroupStoreDTO> listActiveGroupByMerchantIds(@Param("merchantIds") List<Integer> merchantIds);
/**
* 统计项目组下的有效门店数。
......
......@@ -248,21 +248,24 @@ public class ProjectGroupService {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店号不能为空");
}
Map<Integer, Integer> merchantGroupMap = Optional.ofNullable(pgGroupStoreRelMapper.listActiveGroupByMerchantIds(merchantIds))
Map<Integer, ProjectGroupStoreDTO> merchantGroupMap = Optional.ofNullable(pgGroupStoreRelMapper.listActiveGroupByMerchantIds(merchantIds))
.orElse(Collections.emptyList())
.stream()
.collect(Collectors.toMap(
item -> toInteger(item.get("merchantId")),
item -> toInteger(item.get("groupId")),
item -> toInteger(item.getMerchantId()),
item -> item,
(left, right) -> left
));
List<Integer> occupiedMerchantIds = merchantGroupMap.entrySet().stream()
.filter(item -> !Objects.equals(item.getValue(), addDTO.getGroupId()))
.map(Map.Entry::getKey)
List<ProjectGroupStoreDTO> occupiedMerchantIds = merchantGroupMap.values().stream()
.filter(projectGroupStoreDTO -> !Objects.equals(projectGroupStoreDTO.getGroupId(), addDTO.getGroupId()))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(occupiedMerchantIds)) {
List<String> occupiedGroupName = occupiedMerchantIds.stream()
.map(ProjectGroupStoreDTO::getGroupName)
.filter(StringUtils::isNotBlank)
.distinct().collect(Collectors.toList());
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"门店已归属其他项目组: " + StringUtils.join(occupiedMerchantIds, ","));
"门店已存在" + occupiedGroupName + "项目组中,无法操作");
}
return merchantIds.stream()
......@@ -581,9 +584,9 @@ public class ProjectGroupService {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "ruleJson不能为空");
}
validateRuleJson(configDTO.getRuleJson());
String configKey = configDTO.getWarningType() + "_" + configDTO.getBiz() + "_" + configDTO.getMonitorTarget();
String configKey = configDTO.getWarningType() + "_" + configDTO.getBiz();
if (!configKeySet.add(configKey)) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "同一项目组下预警类型、应用业务和监控对象不能重复");
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "同一项目组下预警类型、应用业务不能重复");
}
}
......
......@@ -40,14 +40,17 @@
ORDER BY id ASC
</select>
<select id="listActiveGroupByMerchantIds" resultType="java.util.HashMap">
<select id="listActiveGroupByMerchantIds" resultType="cn.com.glsx.dj.strategy.dto.projectgroup.ProjectGroupStoreDTO">
SELECT
merchant_id AS merchantId,
group_id AS groupId,
id AS id
FROM pg_group_store_rel
WHERE deleted_flag = 'N'
AND merchant_id IN
gs.merchant_id AS merchantId,
gs.merchant_name AS merchantName,
gs.group_id AS groupId,
gi.group_name AS groupName,
gs.id AS id
FROM pg_group_store_rel gs
left join pg_group_info gi on gs.group_id = gi.id
WHERE gs.deleted_flag = 'N'
AND gs.merchant_id IN
<foreach collection="merchantIds" item="merchantId" open="(" separator="," close=")">
#{merchantId}
</foreach>
......
......@@ -36,7 +36,7 @@
ORDER BY m.sort_order ASC, m.id ASC
</if>
<if test="queryDTO.indexName == null or queryDTO.indexName == ''">
GROUP BY m.index_name, m.data_type, m.unit
GROUP BY m.index_name, m.data_type
ORDER BY MIN(m.sort_order) ASC, MIN(m.id) ASC
</if>
</select>
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的 SQL 语法报错风险(ONLY_FULL_GROUP_BY 模式)

    • 问题描述:在 PgWarningMetricDictMapper.xml 中,GROUP BY 子句移除了 m.unit 字段(变更为 GROUP BY m.index_name, m.data_type)。
    • 影响分析:如果原 SELECT 列表中包含了 m.unit 字段,且未对其使用聚合函数(如 MAX()、MIN() 等),在 MySQL 5.7 及以上版本且开启了严格模式(ONLY_FULL_GROUP_BY)的情况下,执行该 SQL 会直接引发语法报错,导致接口不可用。
    • 优化建议:请检查该 SQL 语句的 SELECT 部分。如果需要查询 m.unit 字段,请确保对其使用聚合函数(如 MAX(m.unit)),或者将其加回 GROUP BY 子句中。

    2. 异常提示信息拼接格式不佳

    • 问题描述:在 ProjectGroupService.java 的第 266 行,直接将 List<String> 类型的 occupiedGroupName 变量拼接到字符串中:"门店已存在" + occupiedGroupName + "项目组中,无法操作"。
    • 影响分析:直接拼接 List 对象会隐式调用其 toString() 方法,最终抛出的异常信息格式为 门店已存在[组名A, 组名B]项目组中,无法操作。虽然不影响功能,但对前端展示和用户体验不够友好,不符合专业提示信息的规范。
    • 优化建议:建议使用 String.join() 或 StringUtils.join() 将列表转换为逗号分隔的字符串。 java // 优化示例 String groupNamesStr = String.join("、", occupiedGroupName); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店已存在【" + groupNamesStr + "】项目组中,无法操作");

    3. 多余的类型转换方法调用(重构遗留)

    • 问题描述:在 ProjectGroupService.java 第 254 行的 Collectors.toMap 逻辑中,使用了 toInteger(item.getMerchantId())。
    • 影响分析:在旧代码中,由于 item 是 Map<String, Object>,需要调用 toInteger 进行安全的类型转换。但经过重构后,item 已经是 ProjectGroupStoreDTO,其 getMerchantId() 方法直接返回的即为 Integer 类型,此时再调用 toInteger() 是多余的,增加了无意义的方法栈调用。
    • 优化建议:直接使用 Getter 获取值即可。 java // 优化示例 item -> item.getMerchantId(),

    二、评分明细

    1. 功能实现的正确性与健壮性(36/40分)

      • 理由:整体逻辑正确,DTO 替代 Map 的重构提升了代码的类型安全。未发现使用 Mybatis 子查询及修改 Remote 接口的情况。扣分点在于直接拼接 List 导致异常信息格式不够严谨。
    2. 安全性与潜在风险(25/30分)

      • 理由:不存在 SQL 注入、越权等常见的安全漏洞。扣分点在于 GROUP BY 逻辑变更可能在 MySQL 严格模式下触发 ONLY_FULL_GROUP_BY 异常,属于潜在的稳定性风险。
    3. 最佳实践与可维护性(18/20分)

      • 理由:代码可读性较好,将返回结果从 Map 强类型化为 DTO 是非常好的工程实践。但在重构过程中遗留了无效的 toInteger 类型转换,清理不够彻底。
    4. 性能与资源利用(5/5分)

      • 理由:SQL 查询中使用了 LEFT JOIN 替代多次查询,性能表现稳定,无明显的性能瓶颈或内存浪费。
    5. 提交信息质量(5/5分)

      • 理由:Commit 信息遵循了规范(Angular 规范风格),详尽且准确地罗列了每个文件的修改点及其业务意图,追溯性极强。

    总分:89分

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