Commit 6a91ebce by chenxi

5.8.6 产品上架状态考虑采购商品上架状态

1 parent da2dd76a
......@@ -213,4 +213,7 @@ public class UpgradeGoodsDTO extends UpgradeGoods {
@ApiModelProperty(notes = "供应方编号(单选筛选)")
private String supplierCode;
@ApiModelProperty(notes = "采购商品上架记录(草稿/待审核/已上架,用于前端判断产品是否可删除)")
private List<PurchaseGoodsInfo> purchaseGoodsInfos;
}
......@@ -11,6 +11,7 @@ import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.ShelfStatus;
import cn.com.glsx.dj.smartcarlife.enums.SpecificationTypeEnum;
import cn.com.glsx.dj.smartcarlife.mapper.BaseGoodsMapper;
import cn.com.glsx.dj.smartcarlife.mapper.GoodsOperationLogMapper;
import cn.com.glsx.dj.smartcarlife.mapper.PurchaseGoodsInfoMapper;
import cn.com.glsx.dj.smartcarlife.mapper.UpgradeGoodsMapper;
import cn.com.glsx.dj.smartcarlife.model.*;
import cn.com.glsx.dj.smartcarlife.service.common.GoodsOperationLogService;
......@@ -49,6 +50,8 @@ import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import tk.mybatis.mapper.entity.Example;
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = RpcServiceException.class)
......@@ -106,6 +109,9 @@ public class BaseGoodsService {
@Autowired
private UpgradeGoodsProviderRefService upgradeGoodsProviderRefService;
@Autowired
private PurchaseGoodsInfoMapper purchaseGoodsInfoMapper;
public List<BaseGoodsDTO> list(BaseGoodsDTO record) {
RpcAssert.assertNotNull(record, DefaultErrorEnum.DATA_NULL, "BaseGoodsDTO not be null");
try {
......@@ -362,6 +368,8 @@ public class BaseGoodsService {
}
//设置sku是否已上架
setMerchantGoodsExist(upgradeGoodsDTOList);
// 一次性批量查询采购商品上架记录(草稿/待审核/已上架),避免循环查询
fillPurchaseGoodsInfos(upgradeGoodsDTOList);
record.setUpgradeGoodsList(upgradeGoodsDTOList);
UpdateOperationLog updateOperationLog = new UpdateOperationLog(1, baseGoodsId);
......@@ -410,6 +418,40 @@ public class BaseGoodsService {
}
}
/**
* 批量填充每个SKU下的采购商品上架记录(草稿/待审核/已上架,未删除),
* 用于前端结合 merchantGoodsInfos 判断产品是否可删除。一次性查询,避免循环查库。
*/
private void fillPurchaseGoodsInfos(List<UpgradeGoodsDTO> upgradeGoodsDTOList) {
if (CollectionUtils.isEmpty(upgradeGoodsDTOList)) {
return;
}
List<Integer> upgradeGoodsIds = upgradeGoodsDTOList.stream()
.map(UpgradeGoodsDTO::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(upgradeGoodsIds)) {
return;
}
Example example = new Example(PurchaseGoodsInfo.class);
example.createCriteria()
.andEqualTo("deletedFlag", BaseModel.DeletedFlagEnum.N.name())
.andIn("upgradeGoodsId", upgradeGoodsIds)
.andIn("status", Arrays.asList(
CarLifeEnum.MerchantGoodsStatus.DRAFT.getValue(),
CarLifeEnum.MerchantGoodsStatus.WAIT_AUDIT.getValue(),
CarLifeEnum.MerchantGoodsStatus.ON_SHELF.getValue()));
List<PurchaseGoodsInfo> purchaseGoodsInfos = purchaseGoodsInfoMapper.selectByExample(example);
Map<Integer, List<PurchaseGoodsInfo>> purchaseGoodsMap = purchaseGoodsInfos.stream()
.filter(pgi -> pgi.getUpgradeGoodsId() != null)
.collect(Collectors.groupingBy(PurchaseGoodsInfo::getUpgradeGoodsId));
for (UpgradeGoodsDTO dto : upgradeGoodsDTOList) {
if (dto.getId() != null) {
dto.setPurchaseGoodsInfos(purchaseGoodsMap.get(dto.getId()));
}
}
}
public GoodsMixDTO getByBaseGoodsIdPage(GoodsGetByBaseGoodsIdDTO request) {
RpcAssert.assertNotNull(request.getId(), DefaultErrorEnum.DATA_NULL, "baseGoodsId not be null");
try {
......
......@@ -1520,9 +1520,32 @@ public class UpgradeGoodsService {
UpgradeGoods upgradeGoods = upgradeGoodsMapper.selectByPrimaryKey(upgradedGoodsId);
RpcAssert.assertNotNull(upgradeGoods, DefaultErrorEnum.DATA_NULL, "升级商品不存在");
//RpcAssert.assertIsTrue(Objects.equals(upgradeGoods.getDeletedFlag(), BaseModel.DeletedFlagEnum.N.name()), DefaultErrorEnum.DATA_NULL, "升级商品已删除");
boolean b = updateUpdatedGoodsShelfStatus(new UpdateUpgradeGoodsDTO(upgradedGoodsId, condition.getStatus(), condition.getUpdatedBy()));
goodsReviewUpdateBaseGoodsStatus.updateBaseGoodsStatus(upgradeGoods.getBaseGoodsId(),condition.getUpdatedBy());
return b;
boolean skipOffShelf = false;
if (CarLifeEnum.ShelfStatus.OUT_SHELF.getValue() == condition.getStatus()) {
// 任一渠道(商户商品/采购商品)仍有上架记录时,不允许将SKU置为下架
long merchantGoodsCount = merchantGoodsInfoService.count(
MerchantGoodsInfoDTO.builder()
.upgradeGoodsId(upgradedGoodsId)
.status(CarLifeEnum.MerchantGoodsStatus.ON_SHELF.getValue())
.deletedFlag(BaseModel.DeletedFlagEnum.N.name())
.build());
long purchaseGoodsCount = purchaseGoodsInfoMapper.count(
PurchaseGoodsInfoDTO.builder()
.upgradeGoodsId(upgradedGoodsId)
.status(CarLifeEnum.MerchantGoodsStatus.ON_SHELF.getValue())
.build());
skipOffShelf = merchantGoodsCount > 0 || purchaseGoodsCount > 0;
if (skipOffShelf) {
log.info("changeShelfStatus SKU仍有上架渠道记录,跳过下架 upgradeGoodsId={}, merchantGoodsCount={}, purchaseGoodsCount={}, updatedBy={}",
upgradedGoodsId, merchantGoodsCount, purchaseGoodsCount, condition.getUpdatedBy());
}
}
if (!skipOffShelf) {
updateUpdatedGoodsShelfStatus(new UpdateUpgradeGoodsDTO(upgradedGoodsId, condition.getStatus(), condition.getUpdatedBy()));
}
// 无论是否跳过下架,都重算产品可用状态(幂等)
goodsReviewUpdateBaseGoodsStatus.updateBaseGoodsStatus(upgradeGoods.getBaseGoodsId(), condition.getUpdatedBy());
return true;
} catch (RpcServiceException e) {
throw e;
} catch (Exception e) {
......
package cn.com.glsx.dj.smartcarlife.service.purchaseGoods;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum;
import cn.com.glsx.dj.smartcarlife.model.UpgradeGoods;
import cn.com.glsx.dj.smartcarlife.model.UpgradeGoodsArea;
import org.apache.commons.collections.CollectionUtils;
......@@ -160,6 +162,16 @@ public class PurchaseGoodsInfoService {
merchantPurchaseGoodsRefService.updateBatch(record.getId(), record.getMerchantRefList(), record.getUpdatedBy());
purchaseMallService.updateOffShelfByCheckAll(record.getUpdatedBy());
thiz.insertOrUpdateAfterHandler(record,old);
// 编辑已上架记录会回到待审核,等效下架,联动更新SKU/产品上架状态
if(old != null && MerchantGoodsStatus.ON_SHELF.getValue() == old.getStatus()
&& MerchantGoodsStatus.ON_SHELF.getValue() != record.getStatus()) {
upgradeGoodsService.changeShelfStatus(
UpgradeGoodsDTO.builder()
.id(record.getUpgradeGoodsId())
.status(CarLifeEnum.ShelfStatus.OUT_SHELF.getValue())
.updatedBy(record.getUpdatedBy())
.build());
}
return record;
}catch(RpcServiceException e) {
throw e;
......@@ -279,6 +291,14 @@ public class PurchaseGoodsInfoService {
purchaseGoodsInfoMapper.updateByPrimaryKeySelective(info);
purchaseGoodsLogService.insert(PurchaseGoodsLogType.TO_OFF_SHELF,info.getId(),info.getUpdatedBy());
// 联动更新SKU/产品上架状态(changeShelfStatus 内部会校验是否仍有其他渠道在架)
upgradeGoodsService.changeShelfStatus(
UpgradeGoodsDTO.builder()
.id(old.getUpgradeGoodsId())
.status(CarLifeEnum.ShelfStatus.OUT_SHELF.getValue())
.updatedBy(record.getUpdatedBy())
.build());
if(purchaseMallOffShelfByCheck) {
purchaseMallService.updateOffShelfByCheckAll(record.getUpdatedBy());
}
......@@ -365,6 +385,17 @@ public class PurchaseGoodsInfoService {
info.setId(item);
purchaseGoodsLogService.insert(PurchaseGoodsLogType.AUDIT_TO_ON_SHELF,info.getId(),info.getUpdatedBy());
});
// 联动更新SKU/产品上架状态(同一SKU可能有多条采购记录,去重后处理)
purchaseGoodsInfos.stream()
.map(PurchaseGoodsInfo::getUpgradeGoodsId)
.filter(Objects::nonNull)
.distinct()
.forEach(ugId -> upgradeGoodsService.changeShelfStatus(
UpgradeGoodsDTO.builder()
.id(ugId)
.status(CarLifeEnum.ShelfStatus.ON_SHELF.getValue())
.updatedBy(record.getUpdatedBy())
.build()));
return true;
} catch(RpcServiceException e) {
throw e;
......@@ -453,6 +484,7 @@ public class PurchaseGoodsInfoService {
DefaultErrorEnum.SERVER_ERROR, "批量修改仅支持自定义成本的上架ID");
}
Date now = new Date();
List<Integer> offShelfUgIds = new ArrayList<>();
for (PurchaseGoodsInfo pgi : list) {
Byte oldStatus = pgi.getStatus();
pgi.setPurchasePrice(record.getPurchasePrice());
......@@ -460,6 +492,10 @@ public class PurchaseGoodsInfoService {
pgi.setUpdatedDate(now);
if (MerchantGoodsStatus.DRAFT.getValue() != oldStatus) {
pgi.setStatus(MerchantGoodsStatus.WAIT_AUDIT.getValue());
if (MerchantGoodsStatus.ON_SHELF.getValue() == oldStatus) {
// 已上架记录改价后回到待审核,等效下架,记录待联动的SKU
offShelfUgIds.add(pgi.getUpgradeGoodsId());
}
}
purchaseGoodsInfoMapper.updateByPrimaryKeySelective(pgi);
purchaseGoodsLogService.insert(PurchaseGoodsLogType.UPDATE_PURCHASE_PRICE, pgi.getId(), record.getUpdatedBy());
......@@ -467,6 +503,14 @@ public class PurchaseGoodsInfoService {
purchaseGoodsLogService.insert(PurchaseGoodsLogType.TO_AUDIT, pgi.getId(), record.getUpdatedBy());
}
}
// 联动更新SKU/产品上架状态(changeShelfStatus 内部会校验是否仍有其他渠道在架)
offShelfUgIds.stream().filter(Objects::nonNull).distinct()
.forEach(ugId -> upgradeGoodsService.changeShelfStatus(
UpgradeGoodsDTO.builder()
.id(ugId)
.status(CarLifeEnum.ShelfStatus.OUT_SHELF.getValue())
.updatedBy(record.getUpdatedBy())
.build()));
return true;
} catch(RpcServiceException e) {
throw e;
......
  • @wangxw1
    commented
    Master

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

    1. API 层架构违规与 Dubbo 接口变更风险(重要性:高)

    • 问题描述:在 rpc-dj-smartcarlife-api 模块的 UpgradeGoodsDTO 中新增了 List<PurchaseGoodsInfo> purchaseGoodsInfos 字段。根据代码推断,PurchaseGoodsInfo 是底层数据库实体(在 BaseGoodsService 中被用作 tk.mybatis 的 Example 泛型)。
    • 影响分析:
      1. 触发高危规则:修改 Dubbo Remote 接口相关的 DTO 属于已有请求参数变更,可能引发新老版本的兼容性问题或序列化异常。
      2. 架构分层破坏:将数据库实体(Entity)直接暴露在 API 层的 DTO 中是严重的反模式。实体类通常包含框架注解、软删除标识或敏感字段,直接通过 RPC 暴露存在数据泄露和反序列化失败(如存在懒加载代理)的风险。
    • 优化建议: 在 API 模块中新建一个专门的 DTO 类(如 PurchaseGoodsInfoDTO),在 Service 层将 PurchaseGoodsInfo 实体转换为该 DTO 后再赋值给 UpgradeGoodsDTO。同时评估此字段新增是否对老版本消费者产生影响。

    2. 批处理逻辑存在严重的 N+1 性能瓶颈(重要性:高)

    • 问题描述:在 PurchaseGoodsInfoService 的 auditBatch 和 updatePurchasePriceBatch 方法中,使用了 Java 8 的 .forEach() 循环,并在循环内部调用了 upgradeGoodsService.changeShelfStatus(...)。
    • 影响分析:changeShelfStatus 方法内部包含了多次数据库操作(如主键查询、两次 Count 统计查询、更新操作等)。在批量审核或批量改价的场景下,这会导致严重的 N+1 性能问题,成倍消耗数据库连接池资源,极大增加接口响应时间,甚至引发事务超时。
    • 优化建议: 重构状态联动逻辑,提供支持批量处理的方法(如 changeShelfStatusBatch(List<Integer> upgradeGoodsIds)),在方法内部使用 SQL 的 IN 语句进行一次性批量统计和批量更新。 示例伪代码: java // 收集需要处理的 ID 后,统一调用批量方法 List<Integer> ugIds = purchaseGoodsInfos.stream().map(...).distinct().collect(Collectors.toList()); upgradeGoodsService.changeShelfStatusBatch(ugIds, condition.getStatus(), updatedBy);

    3. 统计查询遗漏软删除条件导致逻辑错误(重要性:中)

    • 问题描述:在 UpgradeGoodsService.java 的 changeShelfStatus 方法中,计算 merchantGoodsCount 时加上了 deletedFlag 校验,但在计算 purchaseGoodsCount 时遗漏了该条件。
    • 影响分析:由于缺少 deletedFlag = 'N' 的过滤条件,系统会将已经逻辑删除的采购商品上架记录也统计在内。这将导致 skipOffShelf 变量被错误地置为 true,使得满足下架条件的 SKU 无法正常下架,引发业务状态混乱。
    • 优化建议: 在构建 PurchaseGoodsInfoDTO 时补充软删除过滤条件: java long purchaseGoodsCount = purchaseGoodsInfoMapper.count( PurchaseGoodsInfoDTO.builder() .upgradeGoodsId(upgradedGoodsId) .status(CarLifeEnum.MerchantGoodsStatus.ON_SHELF.getValue()) .deletedFlag(BaseModel.DeletedFlagEnum.N.name()) // 补充此行 .build());

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分): 基本实现了多渠道上架状态的联动逻辑,且在 BaseGoodsService 中运用了批量查询避免循环查库(值得肯定)。但 changeShelfStatus 方法中 purchaseGoodsCount 遗漏了软删除标识,存在业务逻辑缺陷,扣除 8 分。
    • 安全性与潜在风险(30/30分): 使用 ORM 框架及 Example 构建查询,无 SQL 注入风险;未发现越权或 XSS 等明显安全漏洞。
    • 最佳实践与可维护性(12/20分): API 层 DTO 耦合了底层数据库 Entity,破坏了系统分层架构;此外,触发了“Dubbo 接口已有请求参数变更”的危险操作规则,扣除 8 分。
    • 性能与资源利用(2/5分): 在批处理方法(auditBatch 等)的 forEach 循环中嵌套调用包含大量 DB 交互的 Service 方法,存在明显的 N+1 性能瓶颈,扣除 3 分。
    • 提交信息质量(5/5分): Commit 信息("5.8.6 产品上架状态考虑采购商品上架状态")清晰地表达了本次代码变更的业务意图和版本归属。

    总分:81分

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