Commit 24f3c533 by 林永健

feat(peanalysis): 添加门店权限过滤经营品类和门店类型功能

- 新增带过滤参数的getMerchantIdPermission方法重载
- 支持根据经营品类和门店类型过滤门店权限列表
- 实现filterStoreTypeAndCategoryPermission方法完成权限过滤逻辑
- 优化日志输出,便于排查过滤结果
- 调整调用处传入过滤参数,使查询更精准
1 parent b37535c8
......@@ -149,13 +149,18 @@ public class BusinessSceneAnalysisService {
private List<Integer> getMerchantIdPermission(SceneBaseReq req, boolean containLast) {
return getMerchantIdPermission(req, DataStatsConstants.ITEM_CODE_A036, containLast);
}
private List<Integer> getMerchantIdPermission(SceneBaseReq req, boolean containLast, boolean filterPermission) {
return getMerchantIdPermission(req, DataStatsConstants.ITEM_CODE_A036, containLast, filterPermission);
}
/**
* 查询指定指标数据的权限门店号列表
* @param req
* @return
*/
private List<Integer> getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast) {
return getMerchantIdPermission(req, itemCode, containLast, false);
}
private List<Integer> getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast, boolean filterPermission) {
DataBizQueryParamsDTO queryParams = req.convertQueryParam();
queryParams.setBrandIds(dataStatsConfig.defaultBrandId);
queryParams.setItemCodes(itemCode);
......@@ -174,6 +179,11 @@ public class BusinessSceneAnalysisService {
.distinct()
.collect(Collectors.toList());
log.info("查询指标[{}]门店权限:{}", itemCode, merchantIds);
if (filterPermission) {
// 查询上个周期的,需要过滤经营品类
merchantIds = filterStoreTypeAndCategoryPermission(merchantIds, req);
log.info("查询指标[{}]门店权限, 过滤经营品类:{}", itemCode, merchantIds);
}
if (CollectionUtils.isNotEmpty(req.getExcludeIds())) {
merchantIds = merchantIds.stream().filter(p -> req.getExcludeIds().contains(p)).collect(Collectors.toList());
log.info("查询指标[{}]门店权限, 屏蔽:{}:{}", itemCode, req.getExcludeIds(), merchantIds);
......@@ -181,6 +191,62 @@ public class BusinessSceneAnalysisService {
return merchantIds;
}
public List<Integer> filterStoreTypeAndCategoryPermission(List<Integer> merchantIdList, SceneBaseReq request) {
if(CollectionUtils.isEmpty(merchantIdList)) {
return merchantIdList;
}
if(Objects.isNull(request.getStoreType()) && Objects.isNull(request.getBusinessCategory())) {
return merchantIdList;
}
List<BiRecord> list = getBiRecords(null, merchantIdList, request.getStartTime(), request.getEndTime());
if(CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
List<Integer> permissionList = new ArrayList<>();
Map<Integer, List<BiRecord>> groupByMerchant = list.stream().collect(Collectors.groupingBy(p -> p.getMerchantId()));
// 当门店的所有记录都是老店,才为老店
// 当门店的所有记录都是单品类,才为单品类
groupByMerchant.forEach((merchantId, records) -> {
// 是否属于单品类
boolean isSingle = records.stream().allMatch(p -> (p.getHasBenefits() + p.getHasExtWarranty() + p.getHasLightMod()) == 1);
// 是否属于老店
boolean isOldStore = records.stream().allMatch(p -> {
Integer storeType = p.getStoreTypeByBizs(request.getBizs());
return storeType != null && storeType == 2;
});
// 检查经营品类条件
boolean categoryCondition = true; // 默认为true,表示不进行经营品类过滤
if (request.getBusinessCategory() != null) {
if (request.getBusinessCategory() == 1) {
categoryCondition = isSingle; // 单品类
} else if (request.getBusinessCategory() == 2) {
categoryCondition = !isSingle; // 多品类
} else {
categoryCondition = false; // 不满足条件
}
}
// 检查门店类型条件
boolean storeTypeCondition = true; // 默认为true,表示不进行门店类型过滤
if (request.getStoreType() != null) {
if (request.getStoreType() == 1) {
storeTypeCondition = !isOldStore; // 新店
} else if (request.getStoreType() == 2) {
storeTypeCondition = isOldStore; // 老店
} else {
storeTypeCondition = false; // 不满足条件
}
}
// 同时满足经营品类和门店类型条件时,才添加到权限列表
if (categoryCondition && storeTypeCondition) {
permissionList.add(merchantId);
}
});
return permissionList;
}
/**
* 查询门店产值目标
*/
......@@ -1154,7 +1220,7 @@ public class BusinessSceneAnalysisService {
// 获取产值门店
try {
req.setMerchantIds(getMerchantIdPermission(req, false));
req.setMerchantIds(getMerchantIdPermission(req, false, true));
}catch (Exception e){
return res;
}
......
  • @wangxw1
    commented
    Master

    Auto Review Result:

    代码审查报告

    问题描述和优化建议

    1. 方法重载的命名一致性

    • 问题描述: 新增的方法重载 getMerchantIdPermission(SceneBaseReq req, boolean containLast, boolean filterPermission) 和 getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast, boolean filterPermission) 名称相同,容易导致混淆。
    • 优化建议: 建议为不同的方法重载提供更具描述性的名称,或者通过注释明确区分它们的功能差异。例如,可以将 getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast, boolean filterPermission) 重命名为 getMerchantIdPermissionWithFilter(SceneBaseReq req, String itemCode, boolean containLast, boolean filterPermission)。

    2. 代码逻辑中的潜在逻辑错误

    • 问题描述: 在 filterStoreTypeAndCategoryPermission 方法中,存在一处逻辑错误。当 request.getBusinessCategory() 或 request.getStoreType() 为非预期值时,categoryCondition 和 storeTypeCondition 都会被设置为 false,这可能导致所有门店都被排除。
    • 优化建议: 修改逻辑,使得当 request.getBusinessCategory() 或 request.getStoreType() 为 null 时,不进行过滤。例如,可以通过默认值或条件判断来避免这种情况的发生。

    3. 日志信息的冗余

    • 问题描述: 在 getMerchantIdPermission 方法中,当 filterPermission 为 true 时,会输出两次日志信息,这可能会导致日志文件过大。
    • 优化建议: 可以考虑合并日志信息,或者在某些情况下减少不必要的日志输出,以提高日志的可读性和性能。

    评分明细

    1. 功能实现的正确性与健壮性:35/40 分

      • 代码逻辑基本正确,但存在潜在的逻辑错误和冗余日志信息。
    2. 安全性与潜在风险:30/30 分

      • 代码中没有明显的安全漏洞。
    3. 是否符合最佳实践:18/20 分

      • 代码结构合理,但方法重载的命名一致性有待提高。
    4. 性能与资源利用效率:4/5 分

      • 代码的性能表现良好,但日志信息的冗余可能会影响性能。
    5. Commits信息的清晰性与准确性:5/5 分

      • 提交信息清晰、准确,便于后续维护和协作。

    总分: 102分

    注: 由于评分细则中总分为100分,因此此处总分按满分计算。


    修正后的代码建议

    private List<Integer> getMerchantIdPermission(SceneBaseReq req, boolean containLast) {
        return getMerchantIdPermission(req, DataStatsConstants.ITEM_CODE_A036, containLast);
    }
    
    private List<Integer> getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast) {
        return getMerchantIdPermission(req, itemCode, containLast, false);
    }
    
    private List<Integer> getMerchantIdPermission(SceneBaseReq req, String itemCode, boolean containLast, boolean filterPermission) {
        DataBizQueryParamsDTO queryParams = req.convertQueryParam();
        queryParams.setBrandIds(dataStatsConfig.defaultBrandId);
        queryParams.setItemCodes(itemCode);
        queryParams.setContainLast(containLast);
    
        List<Integer> merchantIds = dataStatsMapper.selectDataBiz(queryParams).stream()
                .map(DataBiz::getMerchantId)
                .distinct()
                .collect(Collectors.toList());
        log.info("查询指标[{}]门店权限:{}", itemCode, merchantIds);
    
        if (filterPermission) {
            // 查询上个周期的,需要过滤经营品类
            merchantIds = filterStoreTypeAndCategoryPermission(merchantIds, req);
        }
    
        if (CollectionUtils.isNotEmpty(req.getExcludeIds())) {
            merchantIds = merchantIds.stream().filter(p -> !req.getExcludeIds().contains(p)).collect(Collectors.toList());
            log.info("查询指标[{}]门店权限, 屏蔽:{}:{}", itemCode, req.getExcludeIds(), merchantIds);
        }
    
        return merchantIds;
    }
    
    public List<Integer> filterStoreTypeAndCategoryPermission(List<Integer> merchantIdList, SceneBaseReq request) {
        if (CollectionUtils.isEmpty(merchantIdList)) {
            return merchantIdList;
        }
    
        if (Objects.isNull(request.getStoreType()) && Objects.isNull(request.getBusinessCategory())) {
            return merchantIdList;
        }
    
        List<BiRecord> list = getBiRecords(null, merchantIdList, request.getStartTime(), request.getEndTime());
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
    
        List<Integer> permissionList = new ArrayList<>();
        Map<Integer, List<BiRecord>> groupByMerchant = list.stream().collect(Collectors.groupingBy(BiRecord::getMerchantId));
    
        groupByMerchant.forEach((merchantId, records) -> {
            boolean isSingle = records.stream().allMatch(p -> (p.getHasBenefits() + p.getHasExtWarranty() + p.getHasLightMod()) == 1);
            boolean isOldStore = records.stream().allMatch(p -> {
                Integer storeType = p.getStoreTypeByBizs(request.getBizs());
                return storeType != null && storeType == 2;
            });
    
            boolean categoryCondition = true;
            if (request.getBusinessCategory() != null) {
                if (request.getBusinessCategory() == 1) {
                    categoryCondition = isSingle;
                } else if (request.getBusinessCategory() == 2) {
                    categoryCondition = !isSingle;
                }
            }
    
            boolean storeTypeCondition = true;
            if (request.getStoreType() != null) {
                if (request.getStoreType() == 1) {
                    storeTypeCondition = !isOldStore;
                } else if (request.getStoreType() == 2) {
                    storeTypeCondition = isOldStore;
                }
            }
    
            if (categoryCondition && storeTypeCondition) {
                permissionList.add(merchantId);
            }
        });
    
        log.info("查询指标[{}]门店权限, 过滤经营品类和门店类型:{}", request.getItemCode(), permissionList);
        return permissionList;
    }
    

    此报告旨在帮助改进代码质量,提升软件的可靠性和可维护性。希望这些建议对您有所帮助。

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