Commit a9de5c49 by 崔彦峰

feat(goods): 添加精品升级成本校验功能

- 新增精品升级场景的成本验证逻辑
- 校验商品列表产品详情中的升级成本字段
- 针对非会员权益场景增加升级成本必填检查
- 当升级成本为空时抛出服务器错误异常
1 parent a00497aa
...@@ -415,6 +415,16 @@ public class GoodsListingApprovalService { ...@@ -415,6 +415,16 @@ public class GoodsListingApprovalService {
verifyVirtualInventory(record.getBusinessCode()); verifyVirtualInventory(record.getBusinessCode());
//校验参数服务项目 //校验参数服务项目
verifyParams(record.getBusinessCode()); verifyParams(record.getBusinessCode());
//精品升级校验升级成本
//是否会员权益
boolean isScene0 = Objects.equals(SceneTypeEnum.SCENE1_0.getValue(),BaseSceneEnum.fromCode(oldRecord.getBaseSceneCode()).getSceneType());
if (!isScene0) {
List<GoodsListingProductDetailDTO> goodsListingProductDetailDTOS = goodsListingProductDetailService.listByBusinessCode(record.getBusinessCode());
for (GoodsListingProductDetailDTO goodsListingProductDetailDTO : goodsListingProductDetailDTOS) {
RpcAssert.assertNotNull(goodsListingProductDetailDTO.getUpgradeProductCost(), DefaultErrorEnum.SERVER_ERROR, "升级成本 is null");
}
}
if (Objects.equals(oldRecord.getShelfModel(), ShelfModelEnum.SPECIAL_SHELF.getValue()) if (Objects.equals(oldRecord.getShelfModel(), ShelfModelEnum.SPECIAL_SHELF.getValue())
|| (Objects.equals(oldRecord.getShelfModel(), ShelfModelEnum.ORDINARY_SHELF.getValue()) && Objects.equals(oldRecord.getShelfType(), ShelfTypeEnum.SINGLE_STORE.getValue()))) { || (Objects.equals(oldRecord.getShelfModel(), ShelfModelEnum.ORDINARY_SHELF.getValue()) && Objects.equals(oldRecord.getShelfType(), ShelfTypeEnum.SINGLE_STORE.getValue()))) {
boolean hasNotFinish = products.stream().anyMatch(o -> !Objects.equals(o.getProfitRateCalFinish() ,StatusEnum.YES.getValue())); boolean hasNotFinish = products.stream().anyMatch(o -> !Objects.equals(o.getProfitRateCalFinish() ,StatusEnum.YES.getValue()));
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常(NPE)风险(功能实现与健壮性)

    • 问题描述:在判断 isScene0 时,代码直接调用了 BaseSceneEnum.fromCode(oldRecord.getBaseSceneCode()).getSceneType()。如果 oldRecord 为 null,或者 getBaseSceneCode() 为 null,亦或者 fromCode 方法未匹配到枚举从而返回 null,直接调用 .getSceneType() 将会引发 NullPointerException。
    • 影响分析:一旦触发空指针异常,整个审批流程将被中断,导致核心业务不可用,严重影响系统稳定性。
    • 优化建议:增加防御性编程逻辑,进行必要的判空处理。

    2. 异常错误码使用不当(最佳实践与可维护性)

    • 问题描述:在校验升级成本为空时,使用了 DefaultErrorEnum.SERVER_ERROR(通常对应 HTTP 500 或系统级异常)。
    • 影响分析:升级成本为空属于业务规则校验失败或参数缺失,抛出服务器内部错误会干扰日志监控系统的告警准确性,也不利于前端或调用方根据错误码进行针对性的业务提示处理。
    • 优化建议:建议将 SERVER_ERROR 替换为代表参数错误(如 PARAM_ERROR)或具体的业务校验错误码,同时提示信息建议更加面向用户(如 "升级成本不能为空")。

    3. 集合遍历前未做判空保护(功能实现与健壮性)

    • 问题描述:调用 goodsListingProductDetailService.listByBusinessCode(...) 获取集合后,直接使用了增强 for 循环进行遍历。
    • 影响分析:虽然规范上要求返回空集合(Collections.emptyList()),但如果底层服务实现不严谨返回了 null,此处直接遍历会抛出 NullPointerException。
    • 优化建议:在遍历前使用 CollectionUtils.isNotEmpty() 进行判空保护。

    综合优化代码示例:

    // 精品升级校验升级成本
    // 获取枚举并进行判空保护
    BaseSceneEnum baseScene = (oldRecord != null) ? BaseSceneEnum.fromCode(oldRecord.getBaseSceneCode()) : null;
    boolean isScene0 = (baseScene != null) && Objects.equals(SceneTypeEnum.SCENE1_0.getValue(), baseScene.getSceneType());
    
    if (!isScene0) {
        List<GoodsListingProductDetailDTO> detailList = goodsListingProductDetailService.listByBusinessCode(record.getBusinessCode());
        // 集合判空保护
        if (detailList != null && !detailList.isEmpty()) {
            for (GoodsListingProductDetailDTO detail : detailList) {
                // 使用更合适的业务错误码
                RpcAssert.assertNotNull(detail.getUpgradeProductCost(), DefaultErrorEnum.PARAM_ERROR, "升级成本不能为空");
            }
        }
    }
    

    二、评分明细

    • 功能实现的正确性与健壮性(30/40分):业务逻辑方向正确,但缺乏防御性编程意识,存在两处较为明显的 NullPointerException 风险(特别是枚举解析处),扣 10 分。
    • 安全性与潜在风险(30/30分):当前提交为纯业务逻辑校验,未见 SQL 注入、越权等安全漏洞,符合安全要求。
    • 最佳实践与可维护性(15/20分):代码结构尚可,但断言校验中使用了系统级错误码(SERVER_ERROR)来代替业务/参数错误码,不符合异常处理最佳实践,扣 5 分。
    • 性能与资源利用(5/5分):在业务流程中进行常规的数据库查询和内存校验,无明显的性能瓶颈及资源浪费。
    • 提交信息质量(5/5分):Commit message 采用了符合规范的 Angular 格式(feat(goods): ...),标题清晰,正文详细说明了修改背景和具体内容,可追溯性极佳。

    总分:85分

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