Commit b99deb54 by 周立明

商品上架-批量上架情况下判断是否全为优加会非优加判断

1 parent b77cf351
......@@ -2,6 +2,9 @@ package cn.com.glsx.dj.smartcarlife.remote;
import javax.validation.constraints.NotBlank;
import java.util.List;
import cn.com.glsx.dj.smartcarlife.dto.CreateBusinessApproveMerchantDTO;
import cn.com.glsx.framework.core.annotation.ApiMethod;
import cn.com.glsx.framework.core.annotation.ApiParam;
import cn.com.glsx.framework.core.annotation.ApiResponse;
......@@ -15,14 +18,29 @@ public interface GoodsListingRemote {
@ApiParam(name="businessCode",notes="单据编号",dataType= ApiParam.DataTypeEnum.STRING,required=true)
@ApiResponse("返回更新结果")
RpcResponse<Boolean> updateCreateBusinessProductProfit(@NotBlank String businessCode);
@ApiMethod("下架满足条件的套餐商户")
@ApiParam(name="businessCode",notes="单据编号",dataType= ApiParam.DataTypeEnum.STRING,required=true)
@ApiResponse("返回更新结果")
RpcResponse<Boolean> offShelfSomeMerchantPackage(@NotBlank String businessCode);
@ApiMethod("刷新分账测试")
@ApiParam(name="businessCode",notes="单据编号",dataType= ApiParam.DataTypeEnum.STRING,required=true)
@ApiResponse("返回更新结果")
RpcResponse<Boolean> refreshRuleTest(@NotBlank String businessCode);
/**
* 商品上架提交环节:门店优加一致性校验。
*
* <p>提交时单据尚未落库,无法按 businessCode 查审批记录,故直接用入参门店列表判断。
* 调用方(rpc-dj-goods-listing)已在本地按上架类型过滤,仅项目制批量上架才调用本方法,
* 因此本方法只做混批判断,不再重复判断上架类型。</p>
*
* @param merchants 提交入参的门店列表
* @return 校验通过返回 true;混批返回 false,错误信息取 errorMessage
*/
@ApiMethod("提交校验门店优加一致性")
@ApiParam(name="merchants",notes="提交入参的门店列表",dataType= ApiParam.DataTypeEnum.OBJECT,required=true)
@ApiResponse("校验通过返回true,混批返回false")
RpcResponse<Boolean> validateSubmitMerchantsYoujiaUniform(List<CreateBusinessApproveMerchantDTO> merchants);
}
......@@ -2,10 +2,13 @@ package cn.com.glsx.dj.smartcarlife.remote;
import javax.validation.constraints.NotBlank;
import java.util.List;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import cn.com.glsx.dj.smartcarlife.dto.CreateBusinessApproveMerchantDTO;
import cn.com.glsx.dj.smartcarlife.service.goodsListing.GoodsListingProfitRateService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
......@@ -52,4 +55,21 @@ public class GoodsListingRemoteImpl implements GoodsListingRemote {
return RpcResponse.error(e);
}
}
/**
* 商品上架提交环节的门店优加一致性校验。
*
* <p>提交时单据尚未落库,因此直接用入参门店列表判断,不查审批记录。
* 上架类型由调用方判断,本方法只负责混批检测。</p>
*/
@Override
public RpcResponse<Boolean> validateSubmitMerchantsYoujiaUniform(List<CreateBusinessApproveMerchantDTO> merchants) {
try{
goodsListingProfitRateService.validateSubmitMerchantsYoujiaUniform(merchants);
return RpcResponse.success(true);
}catch(RpcServiceException e){
log.error(e.getMessage(), e);
return RpcResponse.error(e);
}
}
}
......@@ -842,7 +842,38 @@ public class GoodsListingProfitRateService {
* 配置项未配置或门店无园区店配置时,按非优加门店处理(保持原计算逻辑)。
*/
private boolean isYoujiaStore(GoodsListingMerchantRef merchant) {
if (merchant == null || merchant.getYqMerchantId() == null) {
if (merchant == null) {
return false;
}
return isYoujiaStore(merchant.getYqMerchantId());
}
/**
* 判断园区店配置的主服务商是否为优加(深圳优加服务商)。
*
* <p>供库表门店({@link GoodsListingMerchantRef})与入参门店共用同一口径,
* 保证提交环节与审批后利润测算环节的判断结果一致。</p>
*
* @param yqMerchantId 园区店(云中鹤)商户ID,为空视为非优加
*/
private boolean isYoujiaStore(Integer yqMerchantId) {
return isYoujiaStore(yqMerchantId, null);
}
/**
* 判断园区店配置的主服务商是否为优加(深圳优加服务商)。
*
* <p>传入 {@code yqMerchantPartnerMap} 时按「先查缓存、未命中再查库、查到后写回缓存」处理,
* 使同一园区店在整批中只查询一次;map 为 {@code null} 时直接查库。</p>
*
* <p>缓存中不存在的园区店有两种含义:尚未查询过,或该园区店确实没有配置。
* 两者都返回 {@code false}(非优加),对判断结果无影响,因此无需额外区分。</p>
*
* @param yqMerchantId 园区店(云中鹤)商户ID,为空视为非优加
* @param yqMerchantPartnerMap 园区店ID → 主服务商ID 缓存,为 null 时不使用缓存
*/
private boolean isYoujiaStore(Integer yqMerchantId, Map<Integer, Integer> yqMerchantPartnerMap) {
if (yqMerchantId == null) {
return false;
}
Integer youjiaMerchantId = OrePropertyConfigurer.getProperty("youjia.merchant.id", Integer.class, 44217696);
......@@ -850,11 +881,66 @@ public class GoodsListingProfitRateService {
log.warn("配置项 youjia.merchant.id(深圳优加服务商商户号)未配置,按非优加门店处理");
return false;
}
YqMerchantConfigDTO yqConfig = yqMerchantConfigService.getByMerchantId(merchant.getYqMerchantId());
if (yqConfig == null) {
return false;
Integer partnerMerchantId;
if (yqMerchantPartnerMap == null) {
partnerMerchantId = queryYqPartnerMerchantId(yqMerchantId);
} else if (yqMerchantPartnerMap.containsKey(yqMerchantId)) {
// 命中缓存(同园区店的重复门店),不再查库
partnerMerchantId = yqMerchantPartnerMap.get(yqMerchantId);
} else {
// 首次出现该园区店,查库并写入缓存
partnerMerchantId = queryYqPartnerMerchantId(yqMerchantId);
yqMerchantPartnerMap.put(yqMerchantId, partnerMerchantId);
}
return Objects.equals(partnerMerchantId, youjiaMerchantId);
}
/**
* 查询园区店配置的主服务商ID。
*
* <p>查询异常时返回 {@code null} 并记录日志,不向上抛出,避免园区店配置表异常阻断流程。</p>
*/
private Integer queryYqPartnerMerchantId(Integer yqMerchantId) {
try {
YqMerchantConfigDTO yqConfig = yqMerchantConfigService.getByMerchantId(yqMerchantId);
return yqConfig == null ? null : yqConfig.getPartnerMerchantId();
} catch (Exception e) {
log.warn("查询园区店配置失败,按非优加门店处理, yqMerchantId={}, error={}", yqMerchantId, e.getMessage(), e);
return null;
}
}
/**
* 商品上架提交环节的门店优加一致性校验(入参门店版)。
*
* <p>用于商品上架提交环节:此时单据尚未落库(businessCode 查不到审批记录),
* 因此直接用提交入参中的门店列表判断,校验口径与
* {@link #validateProjectBatchYoujiaUniform(String)} 完全一致。</p>
*
* <p>上架类型(是否项目制批量上架)已由调用方在本地判断,只有项目制批量上架才会调用本方法,
* 因此此处不再重复判断上架类型,只做混批检测。</p>
*
* @param merchants 提交入参的门店列表
* @throws RpcServiceException 存在优加与非优加混批时抛出
*/
public void validateSubmitMerchantsYoujiaUniform(List<CreateBusinessApproveMerchantDTO> merchants) {
if (CollectionUtils.isEmpty(merchants)) {
return;
}
// 逐个查询园区店主服务商,查询结果按园区店ID 缓存:
// 同一园区店只查一次,后续门店直接命中缓存,避免重复查询;
// 不做全量拉取,避免为少量门店加载整张园区店配置表。
Map<Integer, Integer> yqMerchantPartnerMap = new HashMap<>();
Boolean uniformYoujia = null;
for (CreateBusinessApproveMerchantDTO merchant : merchants) {
boolean youjia = isYoujiaStore(merchant == null ? null : merchant.getYqMerchantId(), yqMerchantPartnerMap);
if (uniformYoujia == null) {
uniformYoujia = youjia;
} else if (uniformYoujia != youjia) {
throw new RpcServiceException(DefaultErrorEnum.DATA_INVALID,
"项目制批量上架的门店必须全部为优加门店或全部为非优加门店,当前存在优加与非优加混批,请检查门店配置");
}
}
return Objects.equals(yqConfig.getPartnerMerchantId(), youjiaMerchantId);
}
/**
......
  • @wangxw1
    commented
    Master

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

    1. Dubbo 接口入参直接使用泛型集合存在序列化风险(最佳实践与健壮性)

    • 问题描述:在 GoodsListingRemote 接口中,新增的方法 validateSubmitMerchantsYoujiaUniform 直接使用 List<CreateBusinessApproveMerchantDTO> 作为顶层入参。
    • 影响分析:在 Dubbo RPC 调用中,如果使用 Hessian2 等常见序列化协议,直接将泛型集合作为顶层参数传递,极易发生泛型类型擦除。在服务提供者(Provider)端反序列化时,可能会被解析为 List<Map> 或 List<Object>,导致后续代码在迭代强转时抛出 ClassCastException。
    • 优化建议:将集合参数封装为专门的 Request 对象(如 ValidateMerchantsReq),在该对象内部定义 List 属性,以确保泛型信息得到完整保留。 java // 建议的参数封装类 public class ValidateMerchantsReq implements Serializable { private List<CreateBusinessApproveMerchantDTO> merchants; // getter & setter } // 建议的接口定义 RpcResponse<Boolean> validateSubmitMerchantsYoujiaUniform(ValidateMerchantsReq request);

    2. Dubbo 服务提供者未进行全局异常兜底(健壮性)

    • 问题描述:在 GoodsListingRemoteImpl.validateSubmitMerchantsYoujiaUniform 方法中,仅捕获了 RpcServiceException。
    • 影响分析:如果下层服务 goodsListingProfitRateService 抛出了其他未预期的运行时异常(如 NullPointerException、数据库连接异常等),异常会直接抛给 Dubbo 框架。这不仅会导致消费端收到难以处理的底层异常堆栈,还可能因为具体的异常类在消费端不存在而导致反序列化失败。
    • 优化建议:增加对 Exception 的兜底捕获,统一转化为 RpcResponse 错误返回。 java @Override public RpcResponse<Boolean> validateSubmitMerchantsYoujiaUniform(List<CreateBusinessApproveMerchantDTO> merchants) { try { goodsListingProfitRateService.validateSubmitMerchantsYoujiaUniform(merchants); return RpcResponse.success(true); } catch (RpcServiceException e) { log.error(e.getMessage(), e); return RpcResponse.error(e); } catch (Exception e) { log.error("校验门店优加一致性发生未知异常", e); return RpcResponse.error(DefaultErrorEnum.SYSTEM_ERROR); // 或其他通用错误枚举 } }

    3. 循环内部重复读取系统全局配置(性能与资源利用)

    • 问题描述:在 validateSubmitMerchantsYoujiaUniform 的循环中,每次遍历门店并调用 isYoujiaStore 时,都会执行 OrePropertyConfigurer.getProperty("youjia.merchant.id", ...)。
    • 影响分析:如果在批量上架场景下门店数量较多,且 getProperty 方法底层涉及复杂的解析或没有进行高效的本地缓存,这种循环内的重复调用会导致不必要的性能损耗。
    • 优化建议:将配置读取操作提取到循环外部,获取一次后作为参数传递给 isYoujiaStore 方法,或者在 Service 层做类级别的缓存。 java // 优化思路:在循环外获取 youjiaMerchantId Integer youjiaMerchantId = OrePropertyConfigurer.getProperty("youjia.merchant.id", Integer.class, 44217696); for (CreateBusinessApproveMerchantDTO merchant : merchants) { boolean youjia = isYoujiaStore(merchant, yqMerchantPartnerMap, youjiaMerchantId); // ... }

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分): 整体业务逻辑清晰,使用了局部 Map 进行缓存避免重复查库,对入参和中间变量进行了完善的 null 值校验。扣分项在于 Dubbo 接口实现缺少全局异常兜底,存在潜在的健壮性隐患。

    • 安全性与潜在风险(30/30分): 代码不涉及 SQL 拼接,无越权及敏感信息泄露等安全风险。异常处理中虽然吞掉了查库异常(返回 null 降级处理),但有相应的 warn 日志记录,符合业务容错预期。

    • 最佳实践与可维护性(16/20分): 代码具备良好的注释,说明了业务背景和设计意图(如缓存复用逻辑),可读性较好。扣分项在于 Dubbo 接口直接使用泛型集合作为入参,不符合 RPC 接口设计的最佳实践规范。

    • 性能与资源利用(3/5分): 利用 yqMerchantPartnerMap 在单次请求中有效减少了数据库 I/O 次数,是很好的实践。扣分项在于循环内部重复获取全局配置项,存在一定的优化空间。

    • 提交信息质量(3/5分): Commit 信息为:“商品上架-批量上架情况下判断是否全为优加会非优加判断”,存在错别字(“会非优加”应为“或非优加”),且语句表达不够通顺,略显随意。

    总分:88分

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