Commit baa6ca62 by 林永健

refactor(service): 优化门店经营品类及类型判断逻辑

- 调整单品类和多品类判断条件,增加使用多个品类字段计数判断
- 优化老店判定逻辑代码位置,避免重复计算
- 修正多品类判断逻辑中的布尔变量命名及判断方式
- 修改产值趋势查询中异常处理,确保错误时返回默认趋势图
- 调整指标查询逻辑,区分按门店聚合和非聚合指标
- 添加日志增强指标异常时的排查信息
- 优化数据查询参数设置和调用顺序,提高代码可读性
1 parent 77b9df11
...@@ -207,21 +207,20 @@ public class BusinessSceneAnalysisService { ...@@ -207,21 +207,20 @@ public class BusinessSceneAnalysisService {
// 当门店的所有记录都是老店,才为老店 // 当门店的所有记录都是老店,才为老店
// 当门店的所有记录都是单品类,才为单品类 // 当门店的所有记录都是单品类,才为单品类
groupByMerchant.forEach((merchantId, records) -> { 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,表示不进行经营品类过滤 boolean categoryCondition = true; // 默认为true,表示不进行经营品类过滤
if (request.getBusinessCategory() != null) { if (request.getBusinessCategory() != null) {
int categoryCount = 0;
categoryCount = records.stream().anyMatch(p -> p.getHasBenefits() != null && p.getHasBenefits() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasLightMod() != null && p.getHasLightMod() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasExtWarranty() != null && p.getHasExtWarranty() == 1) ? categoryCount + 1 : categoryCount;
categoryCount = records.stream().anyMatch(p -> p.getHasUsedCar() != null && p.getHasUsedCar() == 1) ? categoryCount + 1 : categoryCount;
boolean isSingle = categoryCount == 1;
boolean isMulti = categoryCount > 1;
if (request.getBusinessCategory() == 1) { if (request.getBusinessCategory() == 1) {
categoryCondition = isSingle; // 单品类 categoryCondition = isSingle; // 单品类
} else if (request.getBusinessCategory() == 2) { } else if (request.getBusinessCategory() == 2) {
categoryCondition = !isSingle; // 多品类 categoryCondition = isMulti; // 多品类
} else { } else {
categoryCondition = false; // 不满足条件 categoryCondition = false; // 不满足条件
} }
...@@ -230,6 +229,11 @@ public class BusinessSceneAnalysisService { ...@@ -230,6 +229,11 @@ public class BusinessSceneAnalysisService {
// 检查门店类型条件 // 检查门店类型条件
boolean storeTypeCondition = true; // 默认为true,表示不进行门店类型过滤 boolean storeTypeCondition = true; // 默认为true,表示不进行门店类型过滤
if (request.getStoreType() != null) { if (request.getStoreType() != null) {
// 是否属于老店
boolean isOldStore = records.stream().allMatch(p -> {
Integer storeType = p.getStoreTypeByBizs(request.getBizs());
return storeType != null && storeType == 2;
});
if (request.getStoreType() == 1) { if (request.getStoreType() == 1) {
storeTypeCondition = !isOldStore; // 新店 storeTypeCondition = !isOldStore; // 新店
} else if (request.getStoreType() == 2) { } else if (request.getStoreType() == 2) {
...@@ -301,11 +305,10 @@ public class BusinessSceneAnalysisService { ...@@ -301,11 +305,10 @@ public class BusinessSceneAnalysisService {
public List<TrendOutputResponse> trendOutput(SceneBaseReq req) { public List<TrendOutputResponse> trendOutput(SceneBaseReq req) {
List<TrendOutputResponse> res; List<TrendOutputResponse> res;
List<DataBusinessDTO> trendOutputList = new ArrayList<>();
Map<Date, BigDecimal> monthTargetOutputMap = new HashMap<>();
try { try {
req.setMerchantIds(getMerchantIdPermission(req, false)); req.setMerchantIds(getMerchantIdPermission(req, false));
}catch (Exception e){
return Collections.emptyList();
}
// 查询指标数据 // 查询指标数据
DataBizQueryParamsDTO queryParams = req.convertQueryParam(); DataBizQueryParamsDTO queryParams = req.convertQueryParam();
...@@ -321,13 +324,12 @@ public class BusinessSceneAnalysisService { ...@@ -321,13 +324,12 @@ public class BusinessSceneAnalysisService {
// 暂时只处理一个指标 // 暂时只处理一个指标
queryParams.setItemCodes(req.getItemCodeList().get(0)); queryParams.setItemCodes(req.getItemCodeList().get(0));
} }
List<DataBusinessDTO> trendOutputList = businessAnalysisService.getTotalOutputGroupByMerchant(queryParams); trendOutputList = businessAnalysisService.getTotalOutputGroupByMerchant(queryParams);
if (CollectionUtils.isEmpty(trendOutputList)) { if (CollectionUtils.isEmpty(trendOutputList)) {
log.warn("查询产值趋势及达成率返回空"); log.warn("查询产值趋势及达成率返回空");
} }
// 查询目标产值(周期=月时才展示) // 查询目标产值(周期=月时才展示)
Map<Date, BigDecimal> monthTargetOutputMap = new HashMap<>();
if (queryParams.getDateType() == 2 && req.isWithTargetRatio()) { if (queryParams.getDateType() == 2 && req.isWithTargetRatio()) {
byte businessType = req.convertToBusinessType(queryParams.getBizs()); byte businessType = req.convertToBusinessType(queryParams.getBizs());
RpcPagination<MerchantOutputTargetConfigDTO> pagination = new RpcPagination<>(); RpcPagination<MerchantOutputTargetConfigDTO> pagination = new RpcPagination<>();
...@@ -355,6 +357,9 @@ public class BusinessSceneAnalysisService { ...@@ -355,6 +357,9 @@ public class BusinessSceneAnalysisService {
}); });
log.info("查询目标产值:{}", monthTargetOutputMap); log.info("查询目标产值:{}", monthTargetOutputMap);
} }
}catch (Exception e){
log.error("获取门店产值权限失败,构建默认趋势图", e);
}
// 构建响应结果 // 构建响应结果
Map<Date, List<DataBusinessDTO>> dataGroupByStart = trendOutputList.stream().collect(Collectors.groupingBy(p -> DateUtil.parseDate(p.getStartDate()))); Map<Date, List<DataBusinessDTO>> dataGroupByStart = trendOutputList.stream().collect(Collectors.groupingBy(p -> DateUtil.parseDate(p.getStartDate())));
...@@ -1225,18 +1230,36 @@ public class BusinessSceneAnalysisService { ...@@ -1225,18 +1230,36 @@ public class BusinessSceneAnalysisService {
return res; return res;
} }
// 查询指标 // 查询指标
// 需要按照门店聚合的指标,累加计算
Map<String, List<DataBusinessDTO>> totalDataMap = new HashMap<>();
DataBizQueryParamsDTO queryParams = req.convertQueryParam(); DataBizQueryParamsDTO queryParams = req.convertQueryParam();
queryParams.setBrandIds(dataStatsConfig.defaultBrandId); queryParams.setBrandIds(dataStatsConfig.defaultBrandId);
queryParams.setExts("merchantIds");
List<String> itemCodes = Lists.newArrayList(DataStatsConstants.ITEM_CODE_A036, List<String> itemCodes = Lists.newArrayList(DataStatsConstants.ITEM_CODE_A036,
DataStatsConstants.ITEM_CODE_A064, DataStatsConstants.ITEM_CODE_A085, DataStatsConstants.ITEM_CODE_A077, DataStatsConstants.ITEM_CODE_A064, DataStatsConstants.ITEM_CODE_A085, DataStatsConstants.ITEM_CODE_A077);
DataStatsConstants.ITEM_CODE_A076, DataStatsConstants.ITEM_CODE_A095);
Map<String, List<DataBusinessDTO>> dataMap = getDataStatsService.parallelCallListMap(itemCodes, queryParams); Map<String, List<DataBusinessDTO>> dataMap = getDataStatsService.parallelCallListMap(itemCodes, queryParams);
if (MapUtils.isEmpty(dataMap)) { if (MapUtils.isEmpty(dataMap)) {
log.warn("查询指标异常. req: {}, itemCodes: {}", JSON.toJSONString(queryParams), itemCodes); log.warn("查询指标异常. req: {}, itemCodes: {}", JSON.toJSONString(queryParams), itemCodes);
} else {
totalDataMap.putAll(dataMap);
}
// 不需要进行聚合的指标
itemCodes = Lists.newArrayList(DataStatsConstants.ITEM_CODE_A076, DataStatsConstants.ITEM_CODE_A095);
queryParams.setDateType(4);
queryParams.setExts("");
dataMap = getDataStatsService.parallelCallListMap(itemCodes, queryParams);
if (MapUtils.isEmpty(dataMap)) {
log.warn("查询指标异常. req: {}, itemCodes: {}", JSON.toJSONString(queryParams), itemCodes);
} else {
totalDataMap.putAll(dataMap);
}
if (MapUtils.isEmpty(totalDataMap)) {
log.warn("查询指标异常. req: {}, itemCodes: {}", JSON.toJSONString(queryParams), itemCodes);
return res; return res;
} }
dataMap.forEach((code, dataList) -> { totalDataMap.forEach((code, dataList) -> {
if (CollectionUtils.isEmpty(dataList)) { if (CollectionUtils.isEmpty(dataList)) {
log.warn("查询指标[{}]数据为空. ", code); log.warn("查询指标[{}]数据为空. ", code);
return; return;
......
  • @wangxw1
    commented
    Master

    Auto Review Result:

    代码审查报告

    问题描述和优化建议

    1. 问题描述:
      • 单品类和多品类判断逻辑:原代码通过检查 hasBenefits、hasExtWarranty 和 hasLightMod 的值来判断是否为单品类,但新的逻辑中使用了 anyMatch 方法来增加 categoryCount,这可能导致逻辑错误。anyMatch 方法会在找到第一个匹配项后立即返回 true,因此 categoryCount 只会增加一次,而不是实际匹配的数量。

    优化建议:

    • 使用 filter 方法统计符合条件的元素数量,而不是 anyMatch。
       long categoryCount = records.stream()
           .filter(p -> p.getHasBenefits() != null && p.getHasBenefits() == 1)
           .count();
       categoryCount += records.stream()
           .filter(p -> p.getHasLightMod() != null && p.getHasLightMod() == 1)
           .count();
       categoryCount += records.stream()
           .filter(p -> p.getHasExtWarranty() != null && p.getHasExtWarranty() == 1)
           .count();
       categoryCount += records.stream()
           .filter(p -> p.getHasUsedCar() != null && p.getHasUsedCar() == 1)
           .count();
    
    1. 问题描述:
      • 异常处理:在 trendOutput 方法中,异常处理部分在查询指标数据之前,导致如果获取商户ID权限失败,不会尝试查询指标数据,直接返回空列表。这可能不是预期的行为。

    优化建议:

    • 将异常处理放在查询指标数据之后,以便在获取商户ID权限失败时仍然可以尝试查询指标数据。
       List<DataBusinessDTO> trendOutputList = new ArrayList<>();
       Map<Date, BigDecimal> monthTargetOutputMap = new HashMap<>();
       try {
           req.setMerchantIds(getMerchantIdPermission(req, false));
    
           // 查询指标数据
           DataBizQueryParamsDTO queryParams = req.convertQueryParam();
           if (queryParams.getDateType() == 4) {
               queryParams.setDateType(0);
           }
    
           queryParams.setBrandIds(dataStatsConfig.defaultBrandId);
           if (CollectionUtils.isEmpty(req.getItemCodeList())) {
               queryParams.setItemCodes(DataStatsConstants.ITEM_CODE_A036);
           } else {
               queryParams.setItemCodes(req.getItemCodeList().get(0));
           }
           trendOutputList = businessAnalysisService.getTotalOutputGroupByMerchant(queryParams);
           if (CollectionUtils.isEmpty(trendOutputList)) {
               log.warn("查询产值趋势及达成率返回空");
           }
    
           // 查询目标产值(周期=月时才展示)
           if (queryParams.getDateType() == 2 && req.isWithTargetRatio()) {
               byte businessType = req.convertToBusinessType(queryParams.getBizs());
               RpcPagination<MerchantOutputTargetConfigDTO> pagination = new RpcPagination<>();
               pagination.setPageSize(Integer.MAX_VALUE);
               pagination.setPageNum(1);
               MerchantOutputTargetConfigDTO condition = new MerchantOutputTargetConfigDTO();
               condition.setMerchantIdList(req.getMerchantIds());
               condition.setBusinessType(businessType);
               pagination.setCondition(condition);
               Date startDate = DateUtil.parseDate(queryParams.getStartDate());
               Date endDate = DateUtil.parseDate(queryParams.getEndDate());
               DateUtil.rangeToList(startDate, endDate, DateField.MONTH).forEach(begin -> {
                   String targetMonth = DateUtil.format(begin, DatePattern.NORM_MONTH_PATTERN);
                   condition.setTargetMonth(targetMonth);
                   RpcResponse<RpcPagination<MerchantOutputTargetConfigDTO>> outputTargetConfigResp = merchantOutputTargetConfigRemote.listWithPage(pagination);
                   if (outputTargetConfigResp.isFailed() || outputTargetConfigResp.getResult() == null) {
                       log.error("获取目标产值失败, pagination: {}", pagination);
                       return;
                   }
                   BigDecimal targetMonthValue = outputTargetConfigResp.getResult().getList().stream()
                           .map(MerchantOutputTargetConfig::getTargetValue)
                           .filter(Objects::nonNull)
                           .reduce(BigDecimal.ZERO, BigDecimal::add);
                   monthTargetOutputMap.put(DateUtil.parse(targetMonth, DatePattern.NORM_MONTH_PATTERN), targetMonthValue);
               });
               log.info("查询目标产值:{}", monthTargetOutputMap);
           }
       } catch (Exception e) {
           log.error("获取门店产值权限失败,构建默认趋势图", e);
       }
    
    1. 问题描述:
      • 代码冗余:在 trendOutput 方法中,两次设置了 queryParams.setBrandIds(dataStatsConfig.defaultBrandId);,这是不必要的冗余。

    优化建议:

    • 移除冗余的代码。
       queryParams.setBrandIds(dataStatsConfig.defaultBrandId);
       if (CollectionUtils.isEmpty(req.getItemCodeList())) {
           queryParams.setItemCodes(DataStatsConstants.ITEM_CODE_A036);
       } else {
           queryParams.setItemCodes(req.getItemCodeList().get(0));
       }
    

    评分明细

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

      • 代码逻辑存在错误,需要修复单品类和多品类判断逻辑(-10分)
      • 异常处理位置不合理,可能导致逻辑错误(-5分)
      • 其他功能实现基本正确(+25分)
      • 得分:25分
    2. 安全性与潜在风险(30分):

      • 代码中没有明显的安全漏洞,如SQL注入、XSS攻击等(+30分)
      • 得分:30分
    3. 是否符合最佳实践(20分):

      • 代码结构和命名规范良好(+15分)
      • 注释清晰,有助于理解代码逻辑(+5分)
      • 存在一些冗余代码(-5分)
      • 得分:20分
    4. 性能与资源利用效率(5分):

      • 代码中多次使用流操作,可能存在性能问题(-2分)
      • 其他方面性能表现良好(+3分)
      • 得分:3分
    5. Commits信息的清晰性与准确性(5分):

      • 提交信息详细且准确,描述了具体的改进点(+5分)
      • 得分:5分

    总分:73分


    以上是本次代码审查的结果,希望这些反馈能够帮助改进代码质量。如果有任何疑问或需要进一步讨论的地方,请随时联系。

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