Commit 47880f42 by 周立明

利润率测算 嘀加毛利计算逻辑修改。优加门店:嘀加毛利 = 嘀加收入 − 嘀加成本

1 parent c3d0afed
......@@ -64,6 +64,7 @@ import cn.com.glsx.dj.smartcarlife.service.goods.MerchantGoodsInfoService;
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.merchantSettleDistribution.MerchantDivisionRuleService;
import cn.com.glsx.dj.smartcarlife.service.YqMerchantConfigService;
import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.enums.StatusEnum;
......@@ -101,6 +102,8 @@ public class GoodsListingProfitRateService {
private MerchantGoodsRefService merchantGoodsRefService;
@Autowired
private MerchantDivisionRuleService merchantDivisionRuleService;
@Autowired
private YqMerchantConfigService yqMerchantConfigService;
public void refreshRuleTest(String businessCode) {
try{
......@@ -793,34 +796,67 @@ public class GoodsListingProfitRateService {
GoodsListingProductBaseDTO updateRecord) {
GoodsListingMerchantRef merchant = param.getMerchant();
// ((嘀加收入-嘀加成本-管理层激励费-一线阳光激励费-一线阳光2激励费-资源费-人力成本)/嘀加收入)*100%
if (updateRecord.getDjRevenue() == null || updateRecord.getDjCost() == null
|| updateRecord.getManagerMarketFee1() == null || updateRecord.getManagerMarketFee2() == null || updateRecord.getFirstLineSunshineIncentiveFee() == null
|| updateRecord.getFirstLineSunshine2IncentiveFee() == null
|| ((Objects.equals(OperationModeEnum.DIRECT_SALES.getValue(), merchant.getOperationMode()))
&& updateRecord.getLaborCost() == null)) {
log.error("compileProfitRate 存在计算字段为空, skip, updateRecord:{}", updateRecord);
// 优加门店判断:主服务商商户号 == 配置项 youjia.merchant.id(深圳优加服务商商户号)
boolean isYoujia = isYoujiaStore(merchant);
if (updateRecord.getDjRevenue() == null || updateRecord.getDjCost() == null) {
log.error("compileProfitRate 嘀加收入/嘀加成本为空, skip, updateRecord:{}", updateRecord);
return;
}
if(updateRecord.getRevenue().compareTo(BigDecimal.ZERO) == 0){
if (updateRecord.getRevenue() == null || updateRecord.getRevenue().compareTo(BigDecimal.ZERO) == 0) {
log.error("compileProfitRate revenue is 0, skip, updateRecord:{}", updateRecord);
return;
}
BigDecimal djGrossProfit = updateRecord.getDjRevenue()
.subtract(updateRecord.getManagerMarketFee())
.subtract(updateRecord.getFirstLineSunshineIncentiveFee())
.subtract(updateRecord.getFirstLineSunshine2IncentiveFee());
//.subtract(Optional.ofNullable(updateRecord.getResourceFee()).orElse(BigDecimal.ZERO));
if (Objects.equals(OperationModeEnum.DIRECT_SALES.getValue(), merchant.getOperationMode())) {
djGrossProfit = djGrossProfit.subtract(updateRecord.getLaborCost());
}
//
if(!Objects.equals(param.getSceneType(),SceneTypeEnum.SCENE1_1.getValue())){
djGrossProfit = djGrossProfit.subtract(updateRecord.getDjCost());
BigDecimal djGrossProfit;
if (isYoujia) {
// 优加门店:嘀加毛利 = 嘀加收入 − 嘀加成本
djGrossProfit = updateRecord.getDjRevenue().subtract(updateRecord.getDjCost());
} else {
if (updateRecord.getManagerMarketFee1() == null || updateRecord.getManagerMarketFee2() == null || updateRecord.getFirstLineSunshineIncentiveFee() == null
|| updateRecord.getFirstLineSunshine2IncentiveFee() == null
|| ((Objects.equals(OperationModeEnum.DIRECT_SALES.getValue(), merchant.getOperationMode()))
&& updateRecord.getLaborCost() == null)) {
log.error("compileProfitRate 存在计算字段为空, skip, updateRecord:{}", updateRecord);
return;
}
djGrossProfit = updateRecord.getDjRevenue()
.subtract(updateRecord.getManagerMarketFee())
.subtract(updateRecord.getFirstLineSunshineIncentiveFee())
.subtract(updateRecord.getFirstLineSunshine2IncentiveFee());
//.subtract(Optional.ofNullable(updateRecord.getResourceFee()).orElse(BigDecimal.ZERO));
if (Objects.equals(OperationModeEnum.DIRECT_SALES.getValue(), merchant.getOperationMode())) {
djGrossProfit = djGrossProfit.subtract(updateRecord.getLaborCost());
}
//
if(!Objects.equals(param.getSceneType(),SceneTypeEnum.SCENE1_1.getValue())){
djGrossProfit = djGrossProfit.subtract(updateRecord.getDjCost());
}
}
updateRecord.setDjGrossProfit(djGrossProfit);
updateRecord.setProfitRate(djGrossProfit.multiply(new BigDecimal(100)).divide(updateRecord.getRevenue(), 2, RoundingMode.HALF_UP));
}
/**
* 判断当前计算门店是否为优加门店。
* 调用 YqMerchantConfigService.getByMerchantId 取主服务商商户号(partnerMerchantId),
* 与配置项 youjia.merchant.id(深圳优加服务商商户号)比较。
* 配置项未配置或门店无园区店配置时,按非优加门店处理(保持原计算逻辑)。
*/
private boolean isYoujiaStore(GoodsListingMerchantRef merchant) {
if (merchant == null || merchant.getYqMerchantId() == null) {
return false;
}
Integer youjiaMerchantId = OrePropertyConfigurer.getProperty("youjia.merchant.id", Integer.class, 44217696);
if (youjiaMerchantId == null || youjiaMerchantId == 0) {
log.warn("配置项 youjia.merchant.id(深圳优加服务商商户号)未配置,按非优加门店处理");
return false;
}
YqMerchantConfigDTO yqConfig = yqMerchantConfigService.getByMerchantId(merchant.getYqMerchantId());
if (yqConfig == null) {
return false;
}
return Objects.equals(yqConfig.getPartnerMerchantId(), youjiaMerchantId);
}
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public String getAverageProfitGrade(CreateBusinessApproveDTO record) {
String businessCode = record.getBusinessCode();
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 潜在的 N+1 查询/RPC调用性能问题 (性能与资源利用)

    • 问题描述:在 isYoujiaStore 方法中,每次调用都会通过 yqMerchantConfigService.getByMerchantId() 获取商户配置。如果 compileProfitRate 方法是在一个针对商品列表或订单列表的 for 循环中被调用的,这会导致严重的 N+1 查询问题或频繁的 RPC 调用,极大地拖慢接口响应速度。
    • 影响分析:随着批量处理的数据量增加,数据库或下游服务的连接数和网络开销会急剧上升,容易引发性能瓶颈甚至服务超时。
    • 优化建议:建议在调用 compileProfitRate 之前的外层逻辑中,根据传入的商品/订单列表,批量查询出对应的 YqMerchantConfigDTO 并组装成 Map<Integer, YqMerchantConfigDTO>,然后将其作为参数传入,或者在当前 Service 中引入适当的本地缓存(如 Caffeine/Guava)。

    2. 配置获取方式不符合 Spring 最佳实践 (最佳实践与可维护性)

    • 问题描述:在 isYoujiaStore 方法内部使用了硬编码的工具类 OrePropertyConfigurer.getProperty(...) 来动态获取配置,并且带有一个魔法值 44217696 作为默认值。
    • 影响分析:每次方法调用都去解析配置项不仅存在微小的性能损耗,而且隐藏了类的外部依赖,降低了代码的可测试性(单元测试时难以 Mock 配置项),不符合 Spring 依赖注入的最佳实践。
    • 优化建议:建议使用 Spring 的 @Value 注解在类初始化时将配置注入为成员变量。

    代码示例:

    // 1. 在类级别注入配置
    @Value("${youjia.merchant.id:44217696}")
    private Integer youjiaMerchantId;
    
    // 2. 优化后的 isYoujiaStore 方法
    private boolean isYoujiaStore(GoodsListingMerchantRef merchant) {
        if (merchant == null || merchant.getYqMerchantId() == null) {
            return false;
        }
        if (youjiaMerchantId == null || youjiaMerchantId == 0) {
            log.warn("配置项 youjia.merchant.id(深圳优加服务商商户号)未配置,按非优加门店处理");
            return false;
        }
        // 注意:这里的 getByMerchantId 仍建议在上层循环外做批量查询优化
        YqMerchantConfigDTO yqConfig = yqMerchantConfigService.getByMerchantId(merchant.getYqMerchantId());
        if (yqConfig == null) {
            return false;
        }
        return Objects.equals(yqConfig.getPartnerMerchantId(), youjiaMerchantId);
    }
    

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 理由:核心业务逻辑修改正确,且在 updateRecord.getRevenue() 处增加了 null 判断,有效避免了原代码潜在的空指针异常(NPE),健壮性有所提升。扣除2分是因为在计算逻辑中直接引入外部查询,在批量场景下设计略欠考虑。

    • 安全性与潜在风险(30/30分): 理由:本次变更主要为算力逻辑和内部配置查询,未引入 SQL 注入、越权等安全风险。Objects.equals 的使用也安全地处理了潜在的 Null 比较。

    • 最佳实践与可维护性(16/20分): 理由:代码可读性良好,注释清晰地解释了优加门店的判断逻辑。扣除4分主要是因为使用了硬编码的工具类获取配置项,未采用 Spring 推荐的 @Value 或 @ConfigurationProperties 注入方式。

    • 性能与资源利用(3/5分): 理由:在计算方法内部直接调用 Service 查询数据库/RPC,若处于循环调用场景下存在 N+1 性能隐患,建议改为批量查询或增加本地缓存。

    • 提交信息质量(5/5分): 理由:Commit 信息("利润率测算 嘀加毛利计算逻辑修改。优加门店:嘀加毛利 = 嘀加收入 − 嘀加成本")清晰准确地描述了修改的业务背景和核心逻辑,具有很好的可追溯性。

    总分:92分

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