Commit ce8c4036 by zhoulm

feat: add goods info integrity check for reconciliation rule list

1. 新增GoodsListingGoodsTypeGroupDTO的integrity字段用于标识信息是否完整
2. 新增listBaseOfIntegrity方法查询品类组并校验规则完整性
3. 调整批量上架逻辑中人力成本规则的生成逻辑,适配非直销模式
4. 修复人力成本计算逻辑的空值处理问题
1 parent ce418dbc
...@@ -25,6 +25,9 @@ public class GoodsListingGoodsTypeGroupDTO extends GoodsListingGoodsTypeGroup{ ...@@ -25,6 +25,9 @@ public class GoodsListingGoodsTypeGroupDTO extends GoodsListingGoodsTypeGroup{
*/ */
private static final long serialVersionUID = -5726238364638899927L; private static final long serialVersionUID = -5726238364638899927L;
@ApiModelProperty(name = "integrity", notes = "信息是否填写完整(Y:是,N:否)")
private String integrity;
@ApiModelProperty(notes = "品类下商品列表") @ApiModelProperty(notes = "品类下商品列表")
private List<GoodsListingGoodsTypeGroupRelation> goodsTypeGroupRelationList; private List<GoodsListingGoodsTypeGroupRelation> goodsTypeGroupRelationList;
......
...@@ -31,7 +31,7 @@ public class GoodsListingReconciliationRuleRemoteImpl implements GoodsListingRec ...@@ -31,7 +31,7 @@ public class GoodsListingReconciliationRuleRemoteImpl implements GoodsListingRec
@Override @Override
public RpcResponse<List<GoodsListingGoodsTypeGroupDTO>> listGoodsTypeGroupBase(@NotBlank String businessCode) { public RpcResponse<List<GoodsListingGoodsTypeGroupDTO>> listGoodsTypeGroupBase(@NotBlank String businessCode) {
try{ try{
return RpcResponse.success(goodsListingGoodsTypeGroupService.listBase(businessCode,RuleTypeEnum.RECONCILIATION_RULE.getValue())); return RpcResponse.success(goodsListingGoodsTypeGroupService.listBaseOfIntegrity(businessCode,RuleTypeEnum.RECONCILIATION_RULE.getValue()));
} catch(Exception e) { } catch(Exception e) {
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
return RpcResponse.error(e); return RpcResponse.error(e);
......
...@@ -4,8 +4,10 @@ import java.util.ArrayList; ...@@ -4,8 +4,10 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
...@@ -26,14 +28,20 @@ import cn.com.glsx.dj.goods.listing.model.GoodsListingDistributionRule; ...@@ -26,14 +28,20 @@ import cn.com.glsx.dj.goods.listing.model.GoodsListingDistributionRule;
import cn.com.glsx.dj.goods.listing.model.GoodsListingExpenseCategoryRule; import cn.com.glsx.dj.goods.listing.model.GoodsListingExpenseCategoryRule;
import cn.com.glsx.dj.goods.listing.model.GoodsListingGoodsTypeGroup; import cn.com.glsx.dj.goods.listing.model.GoodsListingGoodsTypeGroup;
import cn.com.glsx.dj.goods.listing.model.GoodsListingGoodsTypeGroupRelation; import cn.com.glsx.dj.goods.listing.model.GoodsListingGoodsTypeGroupRelation;
import cn.com.glsx.dj.goods.listing.model.GoodsListingMerchantRef;
import cn.com.glsx.dj.smartcarlife.dto.MerchantExpenseCategoryRuleDTO;
import cn.com.glsx.dj.smartcarlife.dto.MerchantGoodsTypeGroupReconciliationRuleDTO;
import cn.com.glsx.dj.smartcarlife.enums.BaseSceneEnum; import cn.com.glsx.dj.smartcarlife.enums.BaseSceneEnum;
import cn.com.glsx.dj.smartcarlife.enums.DistributionRuleTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.DistributionRuleTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.FeeCategoryEnum;
import cn.com.glsx.dj.smartcarlife.enums.GoodsGroupCategoryTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.GoodsGroupCategoryTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.GoodsTypeGroupTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.GoodsTypeGroupTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.RuleTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.RuleTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.vd.OperationModeEnum;
import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum; import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination; import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
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 lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -55,6 +63,8 @@ public class GoodsListingGoodsTypeGroupService { ...@@ -55,6 +63,8 @@ public class GoodsListingGoodsTypeGroupService {
private GoodsListingExpenseCategoryRuleService goodsListingExpenseCategoryRuleService; private GoodsListingExpenseCategoryRuleService goodsListingExpenseCategoryRuleService;
@Autowired @Autowired
private GoodsListingApprovalService goodsListingApprovalService; private GoodsListingApprovalService goodsListingApprovalService;
@Autowired
private GoodsListingMerchantRefService goodsListingMerchantRefService;
public Page<GoodsListingGoodsTypeGroupDTO> listBaseWithPage(RpcPagination<GoodsListingGoodsTypeGroupDTO> pagination) { public Page<GoodsListingGoodsTypeGroupDTO> listBaseWithPage(RpcPagination<GoodsListingGoodsTypeGroupDTO> pagination) {
try{ try{
...@@ -80,6 +90,47 @@ public class GoodsListingGoodsTypeGroupService { ...@@ -80,6 +90,47 @@ public class GoodsListingGoodsTypeGroupService {
} }
} }
public List<GoodsListingGoodsTypeGroupDTO> listBaseOfIntegrity(String businessCode,Byte ruleType) {
RpcAssert.assertNotEmpty(businessCode, DefaultErrorEnum.DATA_NULL, "businessCode不能为空");
RpcAssert.assertNotNull(ruleType, DefaultErrorEnum.DATA_NULL, "ruleType不能为空");
try{
List<GoodsListingGoodsTypeGroupDTO> goodsTypeGroupList = goodsListingGoodsTypeGroupMapper.listBase(GoodsListingGoodsTypeGroupDTO.builder()
.businessCode(businessCode).ruleType(ruleType)
.build());
if(CollectionUtils.isNotEmpty(goodsTypeGroupList)) {
GoodsListingMerchantRef merchant = goodsListingMerchantRefService.getFirstOne(businessCode);
List<GoodsListingExpenseCategoryRule> expenseCategoryRuleList = goodsListingExpenseCategoryRuleService.listByBusinessCode(businessCode);
Map<Integer,List<GoodsListingExpenseCategoryRule>> expenseCategoryRuleMap = expenseCategoryRuleList.stream()
.collect(Collectors.groupingBy(GoodsListingExpenseCategoryRule::getGoodsTypeGroupId));
for(GoodsListingGoodsTypeGroupDTO group:goodsTypeGroupList) {
group.setIntegrity(isRuleNotIntegrity(expenseCategoryRuleMap.get(group.getId()),merchant.getOperationMode()) ? StatusEnum.NO.getValue():StatusEnum.YES.getValue());
}
}
return goodsTypeGroupList;
}catch (Exception e){
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR);
}
}
//校验单品类规则信息是否未填写完整
public boolean isRuleNotIntegrity(List<GoodsListingExpenseCategoryRule> expenseCategoryRules,Byte operationMode) {
if(CollectionUtils.isEmpty(expenseCategoryRules)) {
return true;
}
Integer laborCostCategoryId = FeeCategoryEnum.LABOR_COST.getValue();
for(GoodsListingExpenseCategoryRule r:expenseCategoryRules) {
if(Objects.equals(r.getExpenseCategoryId(), laborCostCategoryId) && Objects.equals(operationMode, OperationModeEnum.NOT_DIRECT_SALES.getValue())){
continue;
}
if(r.getCalculateMode() == null || r.getExpenseCategoryCalculateType() == null || r.getExpenseCategoryValue() == null) {
log.info("GoodsListingExpenseCategoryRule:{}",r);
return true;
}
}
return false;
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class) @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public List<GoodsListingGoodsTypeGroup> insertBatch(List<GoodsListingGoodsTypeGroup> list, String updatedBy) { public List<GoodsListingGoodsTypeGroup> insertBatch(List<GoodsListingGoodsTypeGroup> list, String updatedBy) {
RpcAssert.assertNotNull(list, DefaultErrorEnum.DATA_NULL, "list is null"); RpcAssert.assertNotNull(list, DefaultErrorEnum.DATA_NULL, "list is null");
......
...@@ -21,6 +21,7 @@ import cn.com.glsx.dj.smartcarlife.enums.*; ...@@ -21,6 +21,7 @@ import cn.com.glsx.dj.smartcarlife.enums.*;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.DivisionType; import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.DivisionType;
import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.PositionEnum; import cn.com.glsx.dj.smartcarlife.enums.CarLifeEnum.PositionEnum;
import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.MerchantDivideTypeEnum; import cn.com.glsx.dj.smartcarlife.enums.createBusinessApprove.MerchantDivideTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.vd.OperationModeEnum;
import cn.com.glsx.dj.smartcarlife.model.YqMerchantServiceDetails; import cn.com.glsx.dj.smartcarlife.model.YqMerchantServiceDetails;
import cn.com.glsx.dj.smartcarlife.remote.MerchantSettleDistributeRuleRemote; import cn.com.glsx.dj.smartcarlife.remote.MerchantSettleDistributeRuleRemote;
import cn.com.glsx.dj.smartcarlife.remote.MerchantStoreInfoRemote; import cn.com.glsx.dj.smartcarlife.remote.MerchantStoreInfoRemote;
...@@ -404,6 +405,7 @@ public class GoodsListingSubmitService { ...@@ -404,6 +405,7 @@ public class GoodsListingSubmitService {
return new ArrayList<>(); return new ArrayList<>();
} }
CreateBusinessApproveMerchantDTO merchant = buildMerchantDTO(submitDTO, submitDTO.getMerchantId(), submitDTO.getMerchantName(), firstLine); CreateBusinessApproveMerchantDTO merchant = buildMerchantDTO(submitDTO, submitDTO.getMerchantId(), submitDTO.getMerchantName(), firstLine);
context.setOperationMode(merchant.getOperationMode());
return merchant == null ? new ArrayList<>() : Collections.singletonList(merchant); return merchant == null ? new ArrayList<>() : Collections.singletonList(merchant);
} }
// 批量模式:按Excel逐行构建门店商户 // 批量模式:按Excel逐行构建门店商户
...@@ -456,17 +458,18 @@ public class GoodsListingSubmitService { ...@@ -456,17 +458,18 @@ public class GoodsListingSubmitService {
merchantList.add(merchant); merchantList.add(merchant);
operationModeRowMap.put(i, merchant.getOperationMode()); operationModeRowMap.put(i, merchant.getOperationMode());
} }
validateBatchMerchantOperationMode(operationModeRowMap, errorList); Byte operationMode = validateBatchMerchantOperationMode(operationModeRowMap, errorList);
context.setOperationMode(operationMode);
return merchantList; return merchantList;
} }
/** /**
* 校验批量上架商户运营模式是否一致,错误写回对应商户行。 * 校验批量上架商户运营模式是否一致,错误写回对应商户行。
*/ */
private void validateBatchMerchantOperationMode(Map<Integer, Byte> operationModeRowMap, private Byte validateBatchMerchantOperationMode(Map<Integer, Byte> operationModeRowMap,
List<StringBuilder> errorList) { List<StringBuilder> errorList) {
if (MapUtil.isEmpty(operationModeRowMap)) { if (MapUtil.isEmpty(operationModeRowMap)) {
return; return null;
} }
Byte baseOperationMode = null; Byte baseOperationMode = null;
// 批量上架最终会按同一批规则写入旧链路。 // 批量上架最终会按同一批规则写入旧链路。
...@@ -485,6 +488,7 @@ public class GoodsListingSubmitService { ...@@ -485,6 +488,7 @@ public class GoodsListingSubmitService {
setLineError(errorList, "批量上架商户运营模式必须一致", entry.getKey()); setLineError(errorList, "批量上架商户运营模式必须一致", entry.getKey());
} }
} }
return baseOperationMode;
} }
private CreateBusinessApproveMerchantDTO buildMerchantDTO(GoodsListingSubmitDTO submitDTO, Integer merchantId, private CreateBusinessApproveMerchantDTO buildMerchantDTO(GoodsListingSubmitDTO submitDTO, Integer merchantId,
...@@ -2290,15 +2294,20 @@ public class GoodsListingSubmitService { ...@@ -2290,15 +2294,20 @@ public class GoodsListingSubmitService {
MerchantDivideTypeEnum.getValueByDisplayName(ruleRow == null ? null : ruleRow.getFirstLineSunshine2IncentiveFeeCalType()), MerchantDivideTypeEnum.getValueByDisplayName(ruleRow == null ? null : ruleRow.getFirstLineSunshine2IncentiveFeeCalType()),
parseDecimal(ruleRow == null ? null : ruleRow.getFirstLineSunshine2IncentiveFee(), true)); parseDecimal(ruleRow == null ? null : ruleRow.getFirstLineSunshine2IncentiveFee(), true));
if(Objects.equals(OperationModeEnum.DIRECT_SALES.getValue(), globalContext.getOperationMode())) {
addExpenseRuleItem(items, businessCode, groupId, ExpenseRuleSource.LABOR_COST, FeeCategoryEnum.LABOR_COST, addExpenseRuleItem(items, businessCode, groupId, ExpenseRuleSource.LABOR_COST, FeeCategoryEnum.LABOR_COST,
defaultCalculateMode(), ExpenseCategoryCalculateTypeEnum.INCOME_PROPORTION.getValue(), getLaborCostValue(globalContext)); defaultCalculateMode(), ExpenseCategoryCalculateTypeEnum.INCOME_PROPORTION.getValue(), getLaborCostValue(globalContext));
}else {
addExpenseRuleItem(items, businessCode, groupId, ExpenseRuleSource.LABOR_COST, FeeCategoryEnum.LABOR_COST,
defaultCalculateMode(), ExpenseCategoryCalculateTypeEnum.AMOUNT.getValue(), BigDecimal.ZERO);
}
return toExpenseRules(deduplicateExpenseRuleItems(items)); return toExpenseRules(deduplicateExpenseRuleItems(items));
} }
private BigDecimal getLaborCostValue(CreateBusinessApproveImportExcelContext globalContext) { private BigDecimal getLaborCostValue(CreateBusinessApproveImportExcelContext globalContext) {
Map<Integer, MerchantDetailResponseDTO> merchantInfoDTOMap = globalContext.getMerchantInfoDTOMap(); Map<Integer, MerchantDetailResponseDTO> merchantInfoDTOMap = globalContext.getMerchantInfoDTOMap();
Iterator<Integer> merchantIter = merchantInfoDTOMap.keySet().iterator(); Iterator<Integer> merchantIter = merchantInfoDTOMap.keySet().iterator();
BigDecimal totalValue = BigDecimal.ZERO; BigDecimal totalValue = null;
int count = 0; int count = 0;
while(merchantIter.hasNext()) { while(merchantIter.hasNext()) {
Integer merchantId = merchantIter.next(); Integer merchantId = merchantIter.next();
...@@ -2310,14 +2319,21 @@ public class GoodsListingSubmitService { ...@@ -2310,14 +2319,21 @@ public class GoodsListingSubmitService {
.map(ProductDetailDTO::getEstimatedRevenue) .map(ProductDetailDTO::getEstimatedRevenue)
.filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add); .filter(Objects::nonNull).reduce(BigDecimal.ZERO,BigDecimal::add);
} }
BigDecimal value = BigDecimal.ZERO; BigDecimal personnelCost = data.getMerchantInfo().getPersonnelCost();
if(estimatedRevenueTotal.compareTo(BigDecimal.ZERO) != 0 && data.getMerchantInfo().getPersonnelCost() != null) { if(estimatedRevenueTotal.compareTo(BigDecimal.ZERO) == 0 || personnelCost == null) {
value = data.getMerchantInfo().getPersonnelCost().multiply(CommonConstants.ONEHUNDRED_BIGDECIMAL) //存在商户的产值或人员成本没有的情况 无法计算人力成本比例
.divide(estimatedRevenueTotal,2, BigDecimal.ROUND_HALF_UP); totalValue = null;
break;
} }
totalValue = totalValue == null ? BigDecimal.ZERO:totalValue;
BigDecimal value = data.getMerchantInfo().getPersonnelCost().multiply(CommonConstants.ONEHUNDRED_BIGDECIMAL)
.divide(estimatedRevenueTotal,2, BigDecimal.ROUND_HALF_UP);
totalValue = totalValue.add(value); totalValue = totalValue.add(value);
} }
return count == 0 ? BigDecimal.ZERO:totalValue.divide(BigDecimal.valueOf(count),2,BigDecimal.ROUND_HALF_UP); if(totalValue != null) {
return totalValue.divide(BigDecimal.valueOf(count),2,BigDecimal.ROUND_HALF_UP).min(CommonConstants.ONEHUNDRED_BIGDECIMAL);
}
return totalValue;
} }
private boolean hasPartnerMerchant2SettleRule(CreateBusinessApproveAllImportVo ruleRow) { private boolean hasPartnerMerchant2SettleRule(CreateBusinessApproveAllImportVo ruleRow) {
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常(NPE)风险:未校验商户对象是否为空

    • 问题描述:在 GoodsListingGoodsTypeGroupService.java 的 listBaseOfIntegrity 方法中,通过 goodsListingMerchantRefService.getFirstOne(businessCode) 获取 merchant 对象后,未进行判空处理,直接在后续的循环中调用了 merchant.getOperationMode()。
    • 影响分析:如果数据库中由于脏数据、并发删除或历史遗留数据导致该 businessCode 下没有对应的商户记录,getFirstOne 返回 null,系统将抛出 NullPointerException,导致整个查询接口崩溃,影响上层业务。
    • 优化建议:在使用 merchant 对象前增加判空逻辑。如果商户不存在,应根据业务逻辑提前返回或抛出明确的业务异常。 java GoodsListingMerchantRef merchant = goodsListingMerchantRefService.getFirstOne(businessCode); if (merchant == null) { log.warn("未查询到业务单号对应的商户信息, businessCode: {}", businessCode); // 根据业务诉求,直接返回 goodsTypeGroupList 或抛出业务异常 return goodsTypeGroupList; }

    2. 算术异常(Division by Zero)及边界防御缺失

    • 问题描述:在 GoodsListingSubmitService.java 的 getLaborCostValue 方法中,原有的 count == 0 安全校验被移除。虽然新逻辑将 totalValue 初始设为了 null 并在最后判断 if(totalValue != null),但如果循环体中 totalValue 被赋值且由于某种异常逻辑导致 count 最终为 0(注:Diff中未体现 count++ 的位置,假设其存在),BigDecimal.valueOf(count) 作为除数将引发 ArithmeticException: Division by zero。
    • 影响分析:除零异常属于严重的 RuntimeException,会导致整个批量上架或提交流程中断。
    • 优化建议:为了代码的绝对健壮性,建议在执行除法前恢复对被除数(count)的安全校验。 java if(totalValue != null && count > 0) { return totalValue.divide(BigDecimal.valueOf(count), 2, BigDecimal.ROUND_HALF_UP) .min(CommonConstants.ONEHUNDRED_BIGDECIMAL); } return totalValue;

    3. 循环内部日志打印缺乏上下文且级别不当

    • 问题描述:在 GoodsListingGoodsTypeGroupService.java 的 isRuleNotIntegrity 方法中,当规则不完整时使用了 log.info("GoodsListingExpenseCategoryRule:{}",r);。
    • 影响分析:
      1. 该方法在双层循环/集合遍历中被调用,如果“规则未填写完整”是正常的业务中间状态,使用 info 级别会导致生产环境日志大量刷屏(Log Spam)。
      2. 仅打印了 Rule 对象本身,缺失了关键的关联上下文(如当前的 businessCode 或 goodsTypeGroupId),在排查问题时难以快速定位。
    • 优化建议:调整日志级别为 debug(如果是正常态)或 warn(如果是异常态),并补充上下文信息。 java log.debug("品类规则信息未填写完整, goodsTypeGroupId:{}, ruleId:{}", r.getGoodsTypeGroupId(), r.getId());

    二、评分明细

    • 1. 功能实现的正确性与健壮性(32/40分)
      • 理由:核心业务逻辑(直销与非直销模式的人力成本计算、信息完整性校验)实现正确,且严格遵守了“Dubbo已有请求参数不得变更”和“MyBatis禁用子查询”的红线规则。但存在明显的 NPE 隐患(商户对象未判空)以及除法运算的边界防御减弱,扣除 8 分。
    • 2. 安全性与潜在风险(30/30分)
      • 理由:未引入新的 SQL 注入、XSS、越权等安全漏洞,数据处理逻辑在安全层面表现良好。
    • 3. 最佳实践与可维护性(17/20分)
      • 理由:熟练使用了 Java 8 Stream API 进行内存分组,有效避免了 N+1 查询问题,代码结构清晰。但在日志打印的最佳实践上稍有欠缺(循环内 info 打印且缺乏上下文),扣除 3 分。
    • 4. 性能与资源利用(5/5分)
      • 理由:通过 Collectors.groupingBy 在内存中完成数据匹配,避免了在 for 循环中调用数据库,资源利用合理。
    • 5. 提交信息质量(5/5分)
      • 理由:Commit message 格式规范,分点清晰地描述了新增功能与修复点,可追溯性极强。

    总分:89分

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