Commit db4e0933 by 周立明

feat(goodsListing): add profit rate calculation status update methods

1. 新增两个更新利润率计算状态的Mapper方法及XML实现
2. 新增草稿状态校验逻辑,仅草稿状态可操作利润率计算
3. 计算开始时更新计算状态为未完成,异常时标记计算完成
1 parent a0ca291c
...@@ -23,8 +23,12 @@ public interface GoodsListingProfitRateMapper extends OreMapper<GoodsListingProd ...@@ -23,8 +23,12 @@ public interface GoodsListingProfitRateMapper extends OreMapper<GoodsListingProd
int updateProfitRate(GoodsListingProductBaseDTO record); int updateProfitRate(GoodsListingProductBaseDTO record);
GoodsListingApproval getGoodsListingApproval(@Param("businessCode") String businessCode); int updateProfitRateCalFinishFlag(GoodsListingProductBaseDTO record);
int updateProfitRateCalFinishStart(GoodsListingProductBaseDTO record);
GoodsListingApproval getGoodsListingApproval(@Param("businessCode") String businessCode);
List<GoodsListingProductBaseDTO> listGoodsListingProductBase(@Param("businessCode") String businessCode,@Param("productType") Byte productType); List<GoodsListingProductBaseDTO> listGoodsListingProductBase(@Param("businessCode") String businessCode,@Param("productType") Byte productType);
List<GoodsListingProductDetailDTO> listGoodsListingProductDetail(@Param("businessCode") String businessCode); List<GoodsListingProductDetailDTO> listGoodsListingProductDetail(@Param("businessCode") String businessCode);
......
...@@ -29,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -29,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
import cn.com.glsx.dj.goods.listing.dto.GoodsListingProductBaseDTO; import cn.com.glsx.dj.goods.listing.dto.GoodsListingProductBaseDTO;
import cn.com.glsx.dj.goods.listing.dto.GoodsListingProductDetailDTO; import cn.com.glsx.dj.goods.listing.dto.GoodsListingProductDetailDTO;
import cn.com.glsx.dj.goods.listing.enums.GoodsListingEnum;
import cn.com.glsx.dj.goods.listing.model.GoodsListingApproval; import cn.com.glsx.dj.goods.listing.model.GoodsListingApproval;
import cn.com.glsx.dj.goods.listing.model.GoodsListingBaseRule; import cn.com.glsx.dj.goods.listing.model.GoodsListingBaseRule;
import cn.com.glsx.dj.goods.listing.model.GoodsListingDistributionRule; import cn.com.glsx.dj.goods.listing.model.GoodsListingDistributionRule;
...@@ -92,6 +93,7 @@ import cn.com.glsx.dj.smartcarlife.service.goods.MerchantGoodsRefService; ...@@ -92,6 +93,7 @@ import cn.com.glsx.dj.smartcarlife.service.goods.MerchantGoodsRefService;
import cn.com.glsx.dj.smartcarlife.service.goods.MerchantPackageGoodsInfoService; import cn.com.glsx.dj.smartcarlife.service.goods.MerchantPackageGoodsInfoService;
import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum; import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum; import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.enums.StatusEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert; import cn.com.glsx.framework.core.util.RpcAssert;
import cn.com.glsx.scrm.shopmall.enums.FlagEnum; import cn.com.glsx.scrm.shopmall.enums.FlagEnum;
...@@ -139,6 +141,7 @@ public class GoodsListingProfitRateService { ...@@ -139,6 +141,7 @@ public class GoodsListingProfitRateService {
RpcAssert.assertNotNull(businessCode, DefaultErrorEnum.PARAMETER_NULL, "businessCode not be null"); RpcAssert.assertNotNull(businessCode, DefaultErrorEnum.PARAMETER_NULL, "businessCode not be null");
try{ try{
GoodsListingApproval goodsListingApproval = goodsListingProfitRateMapper.getGoodsListingApproval(businessCode); GoodsListingApproval goodsListingApproval = goodsListingProfitRateMapper.getGoodsListingApproval(businessCode);
RpcAssert.assertIsTrue(Objects.equals(goodsListingApproval.getSystemStatus(), GoodsListingEnum.SystemStatus.DRAFT.getValue()), DefaultErrorEnum.SERVER_ERROR,"仅状态=“草稿”的记录可操作");
List<GoodsListingProductBaseDTO> productList = goodsListingProfitRateMapper.listGoodsListingProductBase(businessCode,null); List<GoodsListingProductBaseDTO> productList = goodsListingProfitRateMapper.listGoodsListingProductBase(businessCode,null);
List<GoodsListingProductDetailDTO> productDetailList = goodsListingProfitRateMapper.listGoodsListingProductDetail(businessCode); List<GoodsListingProductDetailDTO> productDetailList = goodsListingProfitRateMapper.listGoodsListingProductDetail(businessCode);
GoodsListingMerchantRef merchantDTO = goodsListingProfitRateMapper.getFirstOneMerchant(businessCode); GoodsListingMerchantRef merchantDTO = goodsListingProfitRateMapper.getFirstOneMerchant(businessCode);
...@@ -171,6 +174,9 @@ public class GoodsListingProfitRateService { ...@@ -171,6 +174,9 @@ public class GoodsListingProfitRateService {
ExecutorService executorService = Executors.newFixedThreadPool(OrePropertyConfigurer.getProperty("update.create.business.product.profit.thread.count", Integer.class,4)); ExecutorService executorService = Executors.newFixedThreadPool(OrePropertyConfigurer.getProperty("update.create.business.product.profit.thread.count", Integer.class,4));
try { try {
List<CompletableFuture<Void>> futures = new ArrayList<>(); List<CompletableFuture<Void>> futures = new ArrayList<>();
goodsListingProfitRateMapper.updateProfitRateCalFinishStart(GoodsListingProductBaseDTO.builder()
.businessCode(businessCode)
.build());
for (GoodsListingProductBaseDTO productDTO : productList) { for (GoodsListingProductBaseDTO productDTO : productList) {
futures.add(CompletableFuture.runAsync(() -> { futures.add(CompletableFuture.runAsync(() -> {
try { try {
...@@ -196,7 +202,10 @@ public class GoodsListingProfitRateService { ...@@ -196,7 +202,10 @@ public class GoodsListingProfitRateService {
goodsListingProfitRateMapper.updateProfitRate(productDTO); goodsListingProfitRateMapper.updateProfitRate(productDTO);
} catch (Exception e) { } catch (Exception e) {
log.error("updateCreateBusinessProductProfit product compute error, businessCode:{}, productDTO:{}, e:{}", businessCode, productDTO, e); log.error("updateCreateBusinessProductProfit product compute error, businessCode:{}, productDTO:{}, e:{}", businessCode, productDTO, e);
throw new RuntimeException(e); goodsListingProfitRateMapper.updateProfitRateCalFinishFlag(GoodsListingProductBaseDTO.builder()
.id(productDTO.getId()).profitRateCalFinish(StatusEnum.YES.getValue())
.updatedBy(CommonConstants.DEFAULT_UPDATE_BY)
.build());
} }
}, executorService)); }, executorService));
} }
......
...@@ -47,6 +47,30 @@ ...@@ -47,6 +47,30 @@
</set> </set>
where deleted_flag ='N' and id = #{id} where deleted_flag ='N' and id = #{id}
</update> </update>
<update id="updateProfitRateCalFinishFlag" parameterType="cn.com.glsx.dj.goods.listing.dto.GoodsListingProductBaseDTO">
update dj_car_life_goods_listing_product_base
<set>
profit_rate_cal_finish = #{profitRateCalFinish},updated_date = NOW(),updated_by = #{updatedBy}
</set>
where deleted_flag ='N' and id = #{id}
</update>
<update id="updateProfitRateCalFinishStart" parameterType="cn.com.glsx.dj.goods.listing.dto.GoodsListingProductBaseDTO">
update dj_car_life_goods_listing_product_base
<set>
revenue = #{revenue},traffic_share_profit = #{trafficShareProfit},service_provider_profit = #{serviceProviderProfit},
dj_revenue = #{djRevenue},dj_cost = #{djCost},manager_market_fee = #{managerMarketFee},
manager_market_fee1 = #{managerMarketFee1},manager_market_fee2 = #{managerMarketFee2},
first_line_sunshine_incentive_fee = #{firstLineSunshineIncentiveFee},
first_line_sunshine2_incentive_fee = #{firstLineSunshine2IncentiveFee},
resource_fee = #{resourceFee},labor_cost = #{laborCost},
dj_gross_profit = #{djGrossProfit},profit_rate = #{profitRate},
profit_rate_cal_finish = 'N',updated_date = NOW()
</set>
where deleted_flag ='N' and business_code = #{businessCode}
</update>
<select id="getGoodsListingApproval" resultType="cn.com.glsx.dj.goods.listing.model.GoodsListingApproval"> <select id="getGoodsListingApproval" resultType="cn.com.glsx.dj.goods.listing.model.GoodsListingApproval">
select select
id, id,
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常 (NPE) 风险

    • 问题描述:在 GoodsListingProfitRateService.java 中,新增的草稿状态校验逻辑直接调用了 goodsListingApproval.getSystemStatus()。若 getGoodsListingApproval 方法未查询到数据并返回 null,此处将直接抛出 NullPointerException。
    • 影响分析:当传入无效的 businessCode 时,系统会抛出未捕获的 NPE 异常,导致接口直接 500 报错,缺乏友好的错误提示,降低了系统的健壮性。
    • 优化建议:在获取状态前,先对 goodsListingApproval 对象进行非空断言校验。 java GoodsListingApproval goodsListingApproval = goodsListingProfitRateMapper.getGoodsListingApproval(businessCode); // 新增非空校验 RpcAssert.assertNotNull(goodsListingApproval, DefaultErrorEnum.SERVER_ERROR, "审批记录不存在"); RpcAssert.assertIsTrue(Objects.equals(goodsListingApproval.getSystemStatus(), GoodsListingEnum.SystemStatus.DRAFT.getValue()), DefaultErrorEnum.SERVER_ERROR,"仅状态=“草稿”的记录可操作");

    2. 异步任务异常被吞没且错误标记业务状态

    • 问题描述:在异步任务的 catch 块中,原有的 throw new RuntimeException(e); 被移除,改为将 profitRateCalFinish 状态更新为 StatusEnum.YES.getValue()(表示计算完成),且未将异常抛出。
    • 影响分析:
      1. 状态误导:当计算发生异常失败时,反而将状态标记为“计算完成”,这会导致后续业务流程误以为该产品利润率已正确计算,从而使用错误的空数据或旧数据。
      2. 异常吞没:异常被捕获后未重新抛出,如果外层使用了类似 CompletableFuture.allOf().join() 的逻辑等待任务完成,主线程将无法感知到子任务失败,产生“虚假成功”的现象。
    • 优化建议:异常时应将状态标记为“计算失败”(建议引入如 FAIL 的状态),并重新抛出异常让上层感知。 java catch (Exception e) { log.error("updateCreateBusinessProductProfit product compute error, businessCode:{}, productDTO:{}, e:{}", businessCode, productDTO, e); goodsListingProfitRateMapper.updateProfitRateCalFinishFlag(GoodsListingProductBaseDTO.builder() .id(productDTO.getId()) // 建议使用表示失败的状态,而不是 YES .profitRateCalFinish(StatusEnum.NO.getValue()) .updatedBy(CommonConstants.DEFAULT_UPDATE_BY) .build()); // 重新抛出异常,中断外层流程 throw new RuntimeException(e); }

    3. MyBatis Update 语句未进行空值判断,存在隐式置空风险

    • 问题描述:updateProfitRateCalFinishStart 在 XML 中直接更新了 revenue、traffic_share_profit 等十几个金额字段。但在 Java 层调用该方法时,仅 build 传入了 businessCode,其他字段均为默认的 null。
    • 影响分析:由于 XML 中没有使用 <if test="revenue != null"> 等条件标签,MyBatis 会将未赋值的属性作为 null 传入数据库。如果数据库对应字段不允许为 NULL,会触发 SQL 报错;如果允许为 NULL,则会将这些字段全部清空。如果业务本意是“清空重置”,这种依赖 Java 对象隐式 null 值的做法意图不明显,极易在未来维护中被误改。
    • 优化建议:如果意图是重置数据,建议在 SQL 中明确写死为 NULL(或 0);如果意图是按需更新,则必须加上 <if> 标签。 xml <!-- 如果业务意图是重置数据,建议直接在 SQL 中明确赋值,去除对入参的隐式依赖 --> <update id="updateProfitRateCalFinishStart" parameterType="cn.com.glsx.dj.goods.listing.dto.GoodsListingProductBaseDTO"> update dj_car_life_goods_listing_product_base <set> revenue = NULL, traffic_share_profit = NULL, service_provider_profit = NULL, <!-- ... 其他字段 ... --> profit_rate_cal_finish = 'N', updated_date = NOW() </set> where deleted_flag ='N' and business_code = #{businessCode} </update>

    二、评分明细

    • 功能实现的正确性与健壮性(25/40分):存在明显的 NPE 风险;异常处理逻辑中存在业务状态误导(失败标记为成功),可能引发严重的数据一致性问题。
    • 安全性与潜在风险(30/30分):SQL 语句规范使用了 #{} 防止 SQL 注入;未发现越权、敏感信息泄露等安全问题;未违反 Dubbo 接口变更规则。
    • 最佳实践与可维护性(14/20分):吞没异常不符合并发编程最佳实践;SQL 隐式全量置空降低了代码可读性和可维护性。
    • 性能与资源利用(3/5分):变更代码无明显性能瓶颈。(注:审查发现上下文代码中 Executors.newFixedThreadPool 局部创建了线程池,若方法结束未执行 shutdown() 或未改为全局复用,将存在极高的 OOM 内存泄漏风险,请务必排查)。
    • 提交信息质量(5/5分):Commit 信息结构清晰,准确描述了新增功能与修改意图,可追溯性良好。

    总分:77分

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