Commit ef465cf6 by zhouhaibao

[+]兜底指标否处理已经生态指标否处理

1 parent 23dfd7dd
...@@ -205,13 +205,15 @@ public class BusinessSceneAnalysisController { ...@@ -205,13 +205,15 @@ public class BusinessSceneAnalysisController {
merchantIdList = loginService.filterStoreTypeAndCategoryPermission(merchantIdList, request); merchantIdList = loginService.filterStoreTypeAndCategoryPermission(merchantIdList, request);
log.info("当前用户权限门店(过滤门店类型、经营品类):[{}]", merchantIdList); log.info("当前用户权限门店(过滤门店类型、经营品类):[{}]", merchantIdList);
List<Integer> maskMerchantIds = CommonUtil.convert2Integer(StringUtils.split(environment.getProperty("permission.mask.merchant.ids"), ",")) ;
req.setExcludeIds(maskMerchantIds);
// 添加兜底预警门店以及生态预警门店 // 添加兜底预警门店以及生态预警门店
merchantIdList = loginService.filterDdyjMerchant(merchantIdList, req); merchantIdList = loginService.filterDdyjMerchant(merchantIdList, req);
merchantIdList = loginService.filterStyjMerchant(merchantIdList, req); merchantIdList = loginService.filterStyjMerchant(merchantIdList, req);
List<Integer> maskMerchantIds = CommonUtil.convert2Integer(StringUtils.split(environment.getProperty("permission.mask.merchant.ids"), ",")) ;
req.setExcludeIds(maskMerchantIds);
if (CollectionUtils.isNotEmpty(maskMerchantIds) && CollectionUtils.isNotEmpty(merchantIdList)) { if (CollectionUtils.isNotEmpty(maskMerchantIds) && CollectionUtils.isNotEmpty(merchantIdList)) {
merchantIdList = merchantIdList.stream().filter(p -> !maskMerchantIds.contains(p)).collect(Collectors.toList()); merchantIdList = merchantIdList.stream().filter(p -> !maskMerchantIds.contains(p)).collect(Collectors.toList());
log.info("当前用户权限门店(过滤屏蔽门店):[{}]", merchantIdList); log.info("当前用户权限门店(过滤屏蔽门店):[{}]", merchantIdList);
......
...@@ -971,20 +971,32 @@ public class LoginService { ...@@ -971,20 +971,32 @@ public class LoginService {
if (flag != null && flag == 1) { if (flag != null && flag == 1) {
// 场景:1.是 (兜底预警门店) // 场景:1.是 (兜底预警门店)
// 逻辑:保留 【在】 兜底列表中的门店 (交集) // 逻辑:保留 【在】 兜底列表中的门店 (交集)
// 如果为merchantIdList为null,backupStoreSet
log.info("执行兜底预警过滤模式,保留兜底门店。原始数量: {}, 兜底库数量: {}", merchantIdList.size(), backupStoreSet.size()); log.info("执行兜底预警过滤模式,保留兜底门店。原始数量: {}, 兜底库数量: {}", merchantIdList.size(), backupStoreSet.size());
return merchantIdList.stream() if(CollectionUtils.isNotEmpty(merchantIdList)){
.filter(backupStoreSet::contains) return merchantIdList.stream()
.collect(Collectors.toList()); .filter(backupStoreSet::contains)
.collect(Collectors.toList());
}else{
return backupStoreIdList;
}
} else { } else {
// 场景:0.否 (非兜底预警门店) 或 默认情况 if (CollectionUtils.isNotEmpty(backupStoreIdList)) {
// 逻辑:保留 【不在】 兜底列表中的门店 (差集) -> 您要求的逻辑 // 【修正点 1】设置排除 IDs (只设置实际命中的,而不是整个库)
log.info("执行非兜底预警过滤模式,剔除兜底门店。原始数量: {}, 兜底库数量: {}", merchantIdList.size(), backupStoreSet.size()); List<Integer> excludeIds = request.getExcludeIds();
return merchantIdList.stream() if (CollectionUtils.isNotEmpty(excludeIds)){
.filter(storeId -> !backupStoreSet.contains(storeId)) excludeIds.addAll(backupStoreIdList);
.collect(Collectors.toList()); request.setExcludeIds(excludeIds);
}else {
request.setExcludeIds(backupStoreIdList);
}
}
} }
return merchantIdList;
} }
public List<Integer> filterStyjMerchant(List<Integer> merchantIdList, SceneBaseReq req) { public List<Integer> filterStyjMerchant(List<Integer> merchantIdList, SceneBaseReq req) {
...@@ -1036,20 +1048,25 @@ public class LoginService { ...@@ -1036,20 +1048,25 @@ public class LoginService {
merchantIdList.size(), warningStoreSet.size(), merchantIdList.size(), warningStoreSet.size(),
merchantIdList.stream().filter(warningStoreSet::contains).count()); merchantIdList.stream().filter(warningStoreSet::contains).count());
return merchantIdList.stream() if(CollectionUtils.isNotEmpty(merchantIdList)){
.filter(warningStoreSet::contains) return merchantIdList.stream()
.collect(Collectors.toList()); .filter(warningStoreSet::contains)
.collect(Collectors.toList());
}else{
return warningStoreList;
}
} else { } else {
// 场景:0.否 (不看生态预警门店 / 排除预警门店) if (CollectionUtils.isNotEmpty(warningStoreList)) {
// 逻辑:保留【不在】预警列表中的门店 (取反) List<Integer> excludeIds = req.getExcludeIds();
log.info("执行生态预警过滤[否],原始数量: {}, 预警库数量: {}, 结果数量: {}", if (CollectionUtils.isNotEmpty(excludeIds)) {
merchantIdList.size(), warningStoreSet.size(), excludeIds.addAll(warningStoreList);
merchantIdList.stream().filter(id -> !warningStoreSet.contains(id)).count()); req.setExcludeIds(excludeIds);
} else {
return merchantIdList.stream() req.setExcludeIds(warningStoreList);
.filter(storeId -> !warningStoreSet.contains(storeId)) }
.collect(Collectors.toList()); }
} }
return merchantIdList;
} }
} }
  • @wangxw1
    commented
    Master

    你好,我是资深 Java 开发工程师。针对你提供的代码变更,我进行了详细的审查。以下是核心问题与优化建议。

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

    1. 逻辑一致性与功能缺陷(高危)

    问题描述:在 LoginService 的 filterDdyjMerchant 和 filterStyjMerchant 方法中,当走 else 分支(即排除/非预警模式)时,代码逻辑发生了根本性变化。

    • 原逻辑:通过 Stream 流在内存中过滤 merchantIdList,并返回过滤后的新列表。
    • 新逻辑:将需要排除的 ID 添加到 req.excludeIds 中,但直接返回了原始未过滤的 merchantIdList。
    • 后果:Controller 层接收到的 merchantIdList 依然包含本应被排除的门店 ID。如果后续业务逻辑(如数据统计、导出等)依赖于这个 merchantIdList 变量,将会导致数据错误(包含了不该看的数据)。虽然 req 中设置了 excludeIds 可能用于后续 SQL 查询,但 Controller 中持有的内存列表状态与 req 中的条件产生了不一致。

    优化建议:

    • 恢复返回值过滤:如果业务需要内存中的列表是准确的,必须在 else 分支中同时执行 merchantIdList.removeAll(...) 或 Stream 过滤,确保返回的 List 是干净的。
    • 明确职责:如果意图改为完全依赖数据库过滤,那么 Controller 不应再接收并覆盖 merchantIdList,或者方法名应改为 appendExcludeConditions 以符合其副作用的本质。

    2. 安全性与权限放大风险(高危)

    问题描述:在 if (flag == 1) 分支中,新增了如下逻辑:

    if(CollectionUtils.isNotEmpty(merchantIdList)){
        // ... filter logic
    } else {
        return backupStoreIdList; // 直接返回所有兜底门店
    }
    

    风险:merchantIdList 通常代表“当前用户有权限的门店列表”。

    • 如果 merchantIdList 为空,通常意味着用户没有任何门店权限。
    • 修改后的代码在用户无权限时,竟然返回了整个系统所有的兜底/预警门店列表。
    • 除非系统中“空列表”明确定义为“超级管理员/查看全部”,否则这是一个严重的水平越权漏洞,会导致普通无权限用户看到所有敏感门店数据。

    优化建议:

    • 严格校验权限逻辑。如果 merchantIdList 为空,应当直接返回空列表,而不是兜底的全量列表。
    • 如果确实存在“空代表全选”的特殊约定,必须加上明确的注释或管理员身份校验代码。

    3. 代码健壮性与潜在异常(中危)

    问题描述:在 else 分支中使用了 excludeIds.addAll(backupStoreIdList)。

    • 不可变集合风险:excludeIds 源自 req.getExcludeIds(),而 Controller 中是通过 CommonUtil.convert2Integer 设置进去的。如果工具类返回的是 Arrays.asList 生成的固定大小列表,或者 Collections.unmodifiableList,调用 addAll 会直接抛出 UnsupportedOperationException,导致生产环境崩溃。
    • 副作用(Side Effects):在名为 filter... 的查询服务方法中,修改入参 req 的内部状态(Set ExcludeIds),违反了“最小惊讶原则”和阿里巴巴开发手册关于方法设计的建议。这使得代码难以维护和调试。

    优化建议:

    • 防御性编程:在添加 ID 时,创建一个新的 ArrayList,例如: java List<Integer> newExcludeIds = new ArrayList<>(); if (req.getExcludeIds() != null) newExcludeIds.addAll(req.getExcludeIds()); newExcludeIds.addAll(backupStoreIdList); req.setExcludeIds(newExcludeIds);
    • 消除副作用:尽量不要在 Service 的过滤方法中修改 req 对象,而是应该返回过滤条件,由 Controller 组装。

    二、评分明细

    维度 评分 扣分理由
    功能正确性 (40分) 15 else 分支返回了未过滤的列表,导致 Controller 层持有的数据与预期不符,破坏了原有逻辑;且 flag==1 时的空列表处理逻辑极大概率是错误的。
    安全性 (30分) 10 存在严重的越权风险(空列表返回全量数据);直接修改入参对象可能导致并发或状态污染问题。
    可维护性 (20分) 10 方法名 filter... 与实际行为(修改 Request 参数且不过滤返回值)不符;代码中存在重复的逻辑块(Copy-Paste),未提取公共方法。
    性能 (5分) 4 整体性能尚可,但 addAll 操作在列表巨大时有轻微开销,内存中保留了未过滤的大列表。
    提交信息 (5分) 3 提交信息有错别字且语焉不详,未解释为何从内存过滤改为修改 Request 参数。

    总分:42分

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