Commit fc007318 by 崔彦峰

refactor(goods): 优化商品上架服务项目ID解析逻辑

- 修改resolveConstructionServiceItemId方法签名,增加upgradeGoodsId和businessCode参数
- 使用已解析的服务项目ID替代重复查询服务项目名称的逻辑
- 移除冗余的ConstructionServiceItmesDTO构建和分页查询代码
- 添加详细的方法调用日志记录便于调试追踪
- 优化产品成本计算流程,直接使用detailServiceItemId进行计算
1 parent 4665faea
......@@ -3005,7 +3005,7 @@ public class GoodsListingSubmitService {
String normalizedServiceItem = normalize(serviceItem);
if (isFilmGoods(upgradeGoodsDTO) && StringUtils.isNotBlank(normalizedServiceItem)) {
detailServiceItem = normalizedServiceItem;
detailServiceItemId = resolveConstructionServiceItemId(normalizedServiceItem, serviceItemIdCache);
detailServiceItemId = resolveConstructionServiceItemId(normalizedServiceItem, serviceItemIdCache,upgradeGoodsId, businessCode);
}
}
......@@ -3013,30 +3013,16 @@ public class GoodsListingSubmitService {
// 这里只要模板里配置了成本获取方式,就直接在导入阶段算出并写入明细表。
if (Objects.equals(upgradeProductCostStyle, CostStyleEnum.CUSTOM_COST.getValue())) {
upgradeProductCost = parseAmountWithRange(row.getUpgradeProductCost(), BigDecimal.ZERO, CommonConstants.DECIMAL_VALUE_ALMOST_MILLION);
} else if (Objects.equals(upgradeProductCostStyle, CostStyleEnum.DEFAULT_PRODUCT_COST.getValue()) && StringUtils.isNotBlank(serviceItem)) {
} else if (Objects.equals(upgradeProductCostStyle, CostStyleEnum.DEFAULT_PRODUCT_COST.getValue()) && detailServiceItemId != null) {
// TODO 默认带入产品成本,后续补充自动取值逻辑
RpcPagination<ConstructionServiceItmesDTO> pagination = new RpcPagination<>();
ConstructionServiceItmesDTO request = new ConstructionServiceItmesDTO();
request.setServiceItemsName(serviceItem);
request.setCategoryId(1);
pagination.setCondition(request);
pagination.setPageNum(1);
pagination.setPageSize(1);
log.info("查询服务项目入参:businessCode:{},upgradeGoodsId:{},pagination:{}",businessCode,upgradeGoodsId,JSON.toJSONString(pagination));
RpcResponse<RpcPagination<ConstructionServiceItmes>> constructionItemPage = constructionRemote.findConstructionItemPage(pagination);
log.info("查询服务项目入参:businessCode:{},upgradeGoodsId:{},pagination:{},响应结果response:{}",businessCode,upgradeGoodsId,JSON.toJSONString(pagination),JSON.toJSONString(constructionItemPage));
if (constructionItemPage.isSuccess()) {
if (constructionItemPage.getResult() != null && CollectionUtils.isNotEmpty(constructionItemPage.getResult().getList())) {
ConstructionServiceItmes constructionServiceItmes = constructionItemPage.getResult().getList().get(0);
log.info("查询产品成本入参:businessCode:{},upgradeGoodsId:{},constructionServiceItmes:{}",businessCode,upgradeGoodsId,JSON.toJSONString(constructionServiceItmes));
RpcResponse<ProductCostCalculateDTO> productCostCalculateDTORpcResponse = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, constructionServiceItmes.getId());
log.info("查询产品成本入参:businessCode:{},upgradeGoodsId:{},constructionServiceItmes:{},响应结果response:{}",
businessCode,upgradeGoodsId,JSON.toJSONString(constructionServiceItmes),JSON.toJSONString(productCostCalculateDTORpcResponse));
if (productCostCalculateDTORpcResponse.isSuccess()) {
if (productCostCalculateDTORpcResponse.getResult() != null) {
upgradeProductCost = productCostCalculateDTORpcResponse.getResult().getUpgradeCost();
}
}
log.info("查询产品成本入参:businessCode:{},upgradeGoodsId:{},detailServiceItemId:{}",businessCode,upgradeGoodsId,detailServiceItemId);
long l = System.currentTimeMillis();
RpcResponse<ProductCostCalculateDTO> productCostCalculateDTORpcResponse = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, detailServiceItemId);
log.info("查询产品成本入参:businessCode:{},upgradeGoodsId:{},detailServiceItemId:{},响应结果response:{},耗时:{}ms",
businessCode,upgradeGoodsId,detailServiceItemId,JSON.toJSONString(productCostCalculateDTORpcResponse),System.currentTimeMillis()-l);
if (productCostCalculateDTORpcResponse.isSuccess()) {
if (productCostCalculateDTORpcResponse.getResult() != null) {
upgradeProductCost = productCostCalculateDTORpcResponse.getResult().getUpgradeCost();
}
}
}
......@@ -3112,7 +3098,7 @@ public class GoodsListingSubmitService {
return filmGoodsTypeList.contains(goodsType);
}
private Integer resolveConstructionServiceItemId(String serviceItem, Map<String, Integer> serviceItemIdCache) {
private Integer resolveConstructionServiceItemId(String serviceItem, Map<String, Integer> serviceItemIdCache, Integer upgradeGoodsId, String businessCode) {
if (StringUtils.isBlank(serviceItem)) {
return null;
}
......@@ -3128,7 +3114,11 @@ public class GoodsListingSubmitService {
pagination.setCondition(request);
pagination.setPageNum(1);
pagination.setPageSize(1);
log.info("查询服务项目入参:businessCode:{},upgradeGoodsId:{},pagination:{}",businessCode,upgradeGoodsId,JSON.toJSONString(pagination));
long l = System.currentTimeMillis();
RpcResponse<RpcPagination<ConstructionServiceItmes>> response = constructionRemote.findConstructionItemPage(pagination);
log.info("查询服务项目入参:businessCode:{},upgradeGoodsId:{},pagination:{},响应结果response:{},耗时:{}ms",
businessCode,upgradeGoodsId,JSON.toJSONString(pagination),JSON.toJSONString(response),System.currentTimeMillis()-l);
if (response != null && response.isSuccess() && response.getResult() != null
&& CollectionUtils.isNotEmpty(response.getResult().getList())) {
ConstructionServiceItmes constructionServiceItmes = response.getResult().getList().get(0);
......
  • @wangxw1
    commented
    Master

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

    1. 性能与稳定性风险:日志中滥用 JSON.toJSONString 序列化 RPC 响应

    • 问题描述:在 GoodsListingSubmitService 中,新增的耗时统计日志使用了 JSON.toJSONString(response) 和 JSON.toJSONString(productCostCalculateDTORpcResponse) 来打印完整的 RPC 响应报文和分页对象。
    • 影响分析: RPC 响应对象(尤其是包含列表的分页对象 RpcPagination)通常体积较大。在主业务流程中同步进行全量 JSON 序列化,会极大地消耗 CPU 资源并产生大量临时对象,引发频繁的 GC(垃圾回收),在高并发场景下极易导致接口性能急剧下降甚至 OOM。此外,即使日志级别未开启,JSON.toJSONString 依然会被执行,造成无谓的性能损耗。
    • 优化建议:避免在生产环境打印全量 RPC 响应报文。建议仅打印关键状态码、业务主键或结果集大小。 代码示例: java // 建议修改为仅打印关键信息 log.info("查询服务项目入参:businessCode:{},upgradeGoodsId:{}, 响应状态:{}, 耗时:{}ms", businessCode, upgradeGoodsId, response.isSuccess(), System.currentTimeMillis() - l);

    2. 逻辑健壮性风险:条件判断可能导致业务逻辑被静默跳过

    • 问题描述:在成本计算逻辑中,将条件修改为了 else if (Objects.equals(upgradeProductCostStyle, CostStyleEnum.DEFAULT_PRODUCT_COST.getValue()) && detailServiceItemId != null)。
    • 影响分析: 如果商品的成本类型被配置为 DEFAULT_PRODUCT_COST,但由于某种原因 detailServiceItemId 为 null,此时该 else if 分支将被静默跳过。这会导致 upgradeProductCost 未被正确计算(可能保持为 null 或 0),从而把脏数据写入数据库,引发下游财务或利润率计算异常。
    • 优化建议:如果业务规则要求 DEFAULT_PRODUCT_COST 必须有对应的服务项目才能计算,当 detailServiceItemId 缺失时,应当抛出明确的业务异常阻断流程,而不是静默放行。 代码示例: java } else if (Objects.equals(upgradeProductCostStyle, CostStyleEnum.DEFAULT_PRODUCT_COST.getValue())) { if (detailServiceItemId == null) { throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "默认产品成本计算失败:未匹配到有效的服务项目"); } // 正常执行 RPC 调用计算成本 RpcResponse<ProductCostCalculateDTO> response = merchantGoodsInfoRemote.calculateProductCost(upgradeGoodsId, detailServiceItemId); // ... }

    3. 工程最佳实践:私有方法签名被日志参数污染

    • 问题描述:resolveConstructionServiceItemId 方法新增了 upgradeGoodsId 和 businessCode 两个参数,但这部分参数并未参与该方法的核心业务逻辑,仅仅是为了在方法内部打印日志。
    • 影响分析: 将上层业务上下文参数强行传递给底层的通用私有方法,破坏了该方法的单一职责和高内聚性,导致方法签名臃肿,降低了代码的可复用性与可维护性。
    • 优化建议:移除这两个参数。如果需要追踪业务流水号,建议在调用方外层打印日志,或者使用 SLF4J 的 MDC (Mapped Diagnostic Context) 机制隐式传递业务上下文。 代码示例: java // 恢复原有的方法签名 private Integer resolveConstructionServiceItemId(String serviceItem, Map<String, Integer> serviceItemIdCache) { // ... 核心逻辑保持不变 ... // 日志可精简,或依赖 MDC 自动附带 businessCode log.info("查询服务项目分页耗时:{}ms", System.currentTimeMillis() - l); }

    二、评分明细

    1. 功能实现的正确性与健壮性(35/40分)
      • 理由:修复了审核流程中使用错误数据源(record 替换为 oldRecord)的 Bug,且去除了冗余的 RPC 查询代码,逻辑方向正确。但 detailServiceItemId == null 时的静默跳过处理存在产生脏数据的隐患,扣除 5 分。
    2. 安全性与潜在风险(30/30分)
      • 理由:未发现 SQL 注入、越权等安全漏洞。没有变更 Dubbo 服务提供者的 Remote 接口参数,符合重要安全规则。
    3. 最佳实践与可维护性(14/20分)
      • 理由:为了打印日志而修改私有方法签名,传递无业务作用的参数,违反了软件设计的单一职责与接口隔离原则,代码整洁度下降,扣除 6 分。
    4. 性能与资源利用(2/5分)
      • 理由:在主线程的 INFO 日志中直接对大体积的 RPC 响应对象(特别是包含 List 的 Pagination 对象)执行 JSON.toJSONString,是典型的性能反模式,存在引发 OOM 或严重拖慢接口响应时间的风险,扣除 3 分。
    5. 提交信息质量(5/5分)
      • 理由:Commit message 格式规范,使用了 fix 和 refactor 前缀,分点详细说明了修改的原因和具体内容,可追溯性极强,满分。

    总分:86分

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