Commit 9cc5b01e by 张富康

refactor(construction): 优化子订单列表处理逻辑

- 移除冗余库存和升级商品相关查询及计算逻辑
- 引入constructionInfoService处理子订单列表数据
- 精简listConstructionSubOrder方法,提升代码可读性和维护性
- 删除不再使用的库存描述和规格转换等辅助方法
1 parent 845beb8a
package cn.com.glsx.dj.smartcarlife.service.construction;
import cn.com.glsx.dj.smartcarlife.constant.CommonConstants;
import cn.com.glsx.dj.smartcarlife.dto.ConstructionSubOrderRelationDTO;
import cn.com.glsx.dj.smartcarlife.dto.UpgradeGoodsStockDTO;
import cn.com.glsx.dj.smartcarlife.dto.construction.ConstructionBatchDTO;
import cn.com.glsx.dj.smartcarlife.dto.construction.ConstructionInfoDTO;
import cn.com.glsx.dj.smartcarlife.dto.construction.ConstructionSearch;
import cn.com.glsx.dj.smartcarlife.enums.ProductManagerEnum;
import cn.com.glsx.dj.smartcarlife.mapper.construction.ConstructionSubOrderRelationMapper;
import cn.com.glsx.dj.smartcarlife.model.construction.ConstructionSubOrderRelation;
import cn.com.glsx.dj.smartcarlife.util.CommonUtil;
import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.metrics.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.oreframework.boot.env.OrePropertyConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.math.BigDecimal;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
......@@ -35,141 +24,15 @@ public class ConstructionSubOrderRelationService {
@Autowired
private ConstructionSubOrderRelationMapper constructionSubOrderRelationMapper;
@Autowired
private Environment env;
private static final String DIMENSION_UPGRADE_GOODS_ID = "upgrade_goods_id";
private static final String DIMENSION_TYPE = "type";
@Resource
private ConstructionInfoService constructionInfoService;
public List<ConstructionSubOrderRelationDTO> listConstructionSubOrder(ConstructionSearch request) {
List<ConstructionSubOrderRelationDTO> result = constructionSubOrderRelationMapper.listConstructionSubOrder(request);
if (CollUtil.isNotEmpty(result)) {
// 获取4s门店id、园区店id
ConstructionSubOrderRelationDTO first = CollUtil.getFirst(result);
Integer s4sMerchantId = first.getMerchantId();
Integer yqMerchantId = first.getSellerMerchantId();
List<Integer> merchantIds = new ArrayList<>();
if (ObjectUtil.isNotEmpty(s4sMerchantId)) {
merchantIds.add(s4sMerchantId);
}
if (ObjectUtil.isNotEmpty(yqMerchantId)) {
merchantIds.add(yqMerchantId);
}
String dimension = env.getProperty("inventory.type.dimension", DIMENSION_UPGRADE_GOODS_ID);
// 获取升级商品id
List<UpgradeGoodsStockDTO> upgradeGoodsStockDTOS = null;
if (ObjectUtil.equals(dimension, DIMENSION_UPGRADE_GOODS_ID)) {
List<Integer> upgradeGoodsIdList = result.stream().map(ConstructionSubOrderRelationDTO::getUpgradeGoodsId).distinct().collect(Collectors.toList());
log.info("dimension:{}, s4sMerchantId:{},yqMerchantId:{},upgradeGoodsIdList:{}", dimension, s4sMerchantId, yqMerchantId, upgradeGoodsIdList);
upgradeGoodsStockDTOS = constructionSubOrderRelationMapper.queryUpgradeGoodsStockList(merchantIds, dimension, upgradeGoodsIdList, null);
} else if (ObjectUtil.equals(dimension, DIMENSION_TYPE)) {
List<String> typeList = result.stream().map(ConstructionSubOrderRelationDTO::getUpgradeGoodsCustomCode).distinct().collect(Collectors.toList());
log.info("dimension:{}, s4sMerchantId:{},yqMerchantId:{},typeList:{}", dimension, s4sMerchantId, yqMerchantId, typeList);
upgradeGoodsStockDTOS = constructionSubOrderRelationMapper.queryUpgradeGoodsStockList(merchantIds, dimension, null, typeList);
}
if (CollUtil.isNotEmpty(upgradeGoodsStockDTOS)) {
if (CollUtil.isNotEmpty(upgradeGoodsStockDTOS)) {
Map<Integer, UpgradeGoodsStockDTO> upgradeGoodsStockMap = upgradeGoodsStockDTOS.stream()
.collect(Collectors.toMap(UpgradeGoodsStockDTO::getUpgradeGoodsId, i -> i, (k1, k2) -> k1));
// 按type分类
Map<String, List<UpgradeGoodsStockDTO>> upgradeGoodsStockMapByType = upgradeGoodsStockDTOS.stream()
.collect(Collectors.groupingBy(UpgradeGoodsStockDTO::getType));
for (ConstructionSubOrderRelationDTO listItem : result) {
if (ObjectUtil.equals(dimension, DIMENSION_UPGRADE_GOODS_ID)) {
UpgradeGoodsStockDTO upgradeGoodsStockDTO = upgradeGoodsStockMap.get(listItem.getUpgradeGoodsId());
if (ObjectUtil.isNotEmpty(upgradeGoodsStockDTO)) {
listItem.setStock(getStockNumDescription(upgradeGoodsStockDTO.getStock(), upgradeGoodsStockDTO.getGoodsType()));
}
} else if (ObjectUtil.equals(dimension, DIMENSION_TYPE)) {
UpgradeGoodsStockDTO upgradeGoodsStockDTO = upgradeGoodsStockMap.get(listItem.getUpgradeGoodsId());
// 获取相同type的list
String upgradeGoodsCustomCode = listItem.getUpgradeGoodsCustomCode();
// 判断type-商品编码是否为空
if (StrUtil.isNotBlank(upgradeGoodsCustomCode)) {
List<UpgradeGoodsStockDTO> upgradeGoodsStockDTOList = upgradeGoodsStockMapByType.get(upgradeGoodsCustomCode);
if (CollUtil.isNotEmpty(upgradeGoodsStockDTOList) && ObjectUtil.isNotEmpty(upgradeGoodsStockDTO)) {
// 获取相同type的stock加和
BigDecimal stock = upgradeGoodsStockDTOList.stream().filter(ObjectUtil::isNotEmpty).map(UpgradeGoodsStockDTO::getStock).reduce(BigDecimal.ZERO, BigDecimal::add);
listItem.setStock(getStockNumDescription(stock, upgradeGoodsStockDTO.getGoodsType()));
}
} else {
if (ObjectUtil.isNotEmpty(upgradeGoodsStockDTO)) {
listItem.setStock(getStockNumDescription(upgradeGoodsStockDTO.getStock(), upgradeGoodsStockDTO.getGoodsType()));
}
}
}
}
}
}
}
constructionInfoService.handlerSubOrderList(result);
return result;
}
public static String getStockNumDescription(BigDecimal stock, Integer goodsType){
if (Objects.isNull(stock)) {
return "";
}
if (isFilmTypes(goodsType)) {
return stock+"米";
} else {
return stock+"件";
}
}
/**
* 是否膜类订单
*/
public static boolean isFilmTypes(Integer goodsType) {
if (Objects.isNull(goodsType)) {
return false;
}
String filmCouponTypes = OrePropertyConfigurer.getProperty("dj.goods.filmCouponTypes", String.class, "");
// 如果为空就用默认值
if (StrUtil.isBlank(filmCouponTypes)) {
filmCouponTypes = CommonConstants.DEFAULT_FILM_GOOD_TYPES;
}
// 转成 List<Integer>
List<Integer> FILM_COUPON_TYPES = JSONArray.parseArray(filmCouponTypes, Integer.class);
return FILM_COUPON_TYPES.contains(goodsType);
}
public static String convertSpecification(String specificationData, String specificationTypeSequence) {
if (StringUtils.isBlank(specificationData) || !specificationData.contains(";~;")) {
return null;
}
if (StringUtils.isNotBlank(specificationTypeSequence)) {
List<Integer> specificationTypeList = CommonUtil.convert2Integer(specificationTypeSequence.split(";~;"));
List<String> specificationValueList = Arrays.asList(specificationData.split(";~;"));
if (specificationTypeList.size() == specificationValueList.size()) {
StringBuilder newSpecificationData = new StringBuilder();
for (int index = 0; index < specificationTypeList.size(); index++) {
if (newSpecificationData.length() > 0) {
newSpecificationData.append("/");
}
newSpecificationData.append(specificationValueList.get(index));
ProductManagerEnum.SpecificationType st = ProductManagerEnum.SpecificationType.getByValue(specificationTypeList.get(index));
if (st == null) {
continue;
}
switch (st) {
case KILOMETERS:
newSpecificationData.append("(km/万)");
break;
case SERVICE_YEAR:
newSpecificationData.append("年");
break;
case SUIT_CAR_PRICE:
newSpecificationData.append("万");
break;
}
}
return newSpecificationData.toString();
}
}
return specificationData.replaceAll(";~;", "/");
}
public Integer batchUpdateSubOrder(ConstructionBatchDTO request) {
Date date = new Date();
......
  • @wangxw1
    commented
    Master

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

    1. 边界情况处理:缺失对空列表的校验(健壮性)

    • 问题描述:在重构前的老代码中,对查询结果 result 进行了 CollUtil.isNotEmpty(result) 的判空校验,然后再执行后续复杂的业务逻辑。重构后,直接调用了 constructionInfoService.handlerSubOrderList(result),丢失了前置的空值/空列表判断。
    • 影响分析:如果数据库查询结果为空,将空列表传入下游服务可能会导致不必要的上下文切换、方法调用开销,甚至如果 handlerSubOrderList 内部没有做好防御性编程,可能会引发 NullPointerException 或 IndexOutOfBoundsException。
    • 优化建议:在调用下游服务处理数据前,恢复对列表的判空校验。 java public List<ConstructionSubOrderRelationDTO> listConstructionSubOrder(ConstructionSearch request) { List<ConstructionSubOrderRelationDTO> result = constructionSubOrderRelationMapper.listConstructionSubOrder(request); if (CollectionUtils.isNotEmpty(result)) { constructionInfoService.handlerSubOrderList(result); } return result; }

    2. 架构设计:潜在的循环依赖风险(可维护性)

    • 问题描述:本次修改在 ConstructionSubOrderRelationService 中引入了 ConstructionInfoService。在典型的业务系统中,主订单服务和子订单服务之间极易发生相互调用的情况。
    • 影响分析:如果 ConstructionInfoService 中也注入了 ConstructionSubOrderRelationService,将会形成 Spring 循环依赖(Circular Dependency)。虽然 Spring 默认支持单例属性注入的循环依赖(高版本 Spring Boot 默认已禁用),但这种设计会导致模块间高度耦合,增加未来拆分微服务或重构的难度。
    • 优化建议:请排查 ConstructionInfoService 是否依赖了当前类。如果存在双向依赖,建议将 handlerSubOrderList 这种组装数据的逻辑下沉到一个专门的聚合层(如 ConstructionOrderAssembler 或 Manager/Facade 层),以保持 Service 层的单向依赖。

    3. 代码规范:依赖注入注解不统一(最佳实践)

    • 问题描述:当前类中同时使用了 @Autowired(注入 Mapper)和 @Resource(注入 Service)两种不同的依赖注入注解。
    • 影响分析:虽然功能上都能实现注入,但在同一个类中混用不同的注入规范(Spring 原生规范 vs JSR-250 Java 标准规范)会降低代码的统一性和可读性。
    • 优化建议:统一项目中的依赖注入风格。建议统一使用 @Resource(按名称装配,解耦 Spring 强依赖),或者更推荐使用构造器注入(Lombok 的 @RequiredArgsConstructor配合 final 关键字),这有助于在编译期发现循环依赖问题。

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 重构将复杂的库存计算和规格转换逻辑抽离,极大地符合单一职责原则(SRP),核心功能逻辑未见明显错误。扣除 2 分是因为移除了对结果列表的判空前置校验,存在轻微的健壮性瑕疵。
    • 安全性与潜在风险(30/30分): 本次变更为纯粹的代码结构重构,未引入新的外部参数拼接或敏感操作,不存在 SQL 注入、越权等安全隐患。
    • 最佳实践与可维护性(18/20分): 代码可读性得到了质的飞跃,消除了原本长达百行的“上帝方法(God Method)”,非常值得肯定。扣除 2 分主要在于注解风格的不统一以及需要警惕的潜在循环依赖设计。
    • 性能与资源利用(5/5分): 删除了冗余的未调用方法,逻辑下推到专门的 Service 处理,对内存和 CPU 的使用无负面影响。
    • 提交信息质量(5/5分): Commit message 极其优秀。使用了标准的 Conventional Commits 格式(refactor(construction): ...),且清晰地列出了具体的修改点(移除冗余、引入新服务、精简方法、删除辅助方法),高度可追溯。

    总分:96分

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