Commit 65e5203c by 叶立庆

Merge branch 'v5.8.6.2-bak' into v5.8.6.2

# Conflicts:
#	rpc-dj-smartcarlife-api/pom.xml
#	rpc-dj-smartcarlife/pom.xml
2 parents e02d0eed 9f4b5044
......@@ -12,7 +12,7 @@
<name>rpc-dj-smartcarlife-api</name>
<properties>
<deploy.scanPackage>cn.com.glsx.dj.smartcarlife.remote</deploy.scanPackage>
<smartcarlife.api.version>5.8.6-SNAPSHOT</smartcarlife.api.version>
<smartcarlife.api.version>5.8.6.2-SNAPSHOT</smartcarlife.api.version>
<shopamll.api.version>5.6.4-SNAPSHOT</shopamll.api.version>
</properties>
<dependencies>
......
package cn.com.glsx.dj.smartcarlife.remote;
import cn.com.glsx.dj.smartcarlife.dto.merchant.MerchantDetailBatchQueryDTO;
import cn.com.glsx.dj.smartcarlife.dto.merchant.MerchantDetailQueryDTO;
import cn.com.glsx.dj.smartcarlife.dto.merchant.MerchantDetailResponseDTO;
import cn.com.glsx.dj.smartcarlife.dto.merchant.MerchantEditDTO;
import cn.com.glsx.dj.smartcarlife.model.SceneMerchantRefUpd;
import cn.com.glsx.framework.core.annotation.ApiMethod;
import cn.com.glsx.framework.core.annotation.ApiParam;
import cn.com.glsx.framework.core.annotation.ApiResponse;
import cn.com.glsx.framework.core.annotation.ApiService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.merchants.model.request.MerchantRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 商户门店信息同步服务接口
*
* @author yelq
* @since 2026-09-24
*/
@ApiService(value = "商户门店信息同步服务接口", owner = "yelq", protocol = ApiService.ProtocolEnum.dubbo, version = "1.0.0")
public interface SyncMerchantInfoRemote {
/**
* 同步门店名称
*
*/
@ApiMethod("同步门店名称")
@ApiParam(name = "merchantRequest", notes = "更新参数", dataType = ApiParam.DataTypeEnum.OBJECT, required = true)
@ApiResponse("是否成功")
RpcResponse<Boolean> updateMerchantName(@NotNull MerchantRequest merchantRequest);
}
\ No newline at end of file
......@@ -16,7 +16,7 @@
<skipTests>true</skipTests>
<shopmall.api.version>5.8.2-SNAPSHOT</shopmall.api.version>
<stoassistant.api.version>5.6.4-SNAPSHOT</stoassistant.api.version>
<smartcarlife.api.version>5.8.6-SNAPSHOT</smartcarlife.api.version>
<smartcarlife.api.version>5.8.6.2-SNAPSHOT</smartcarlife.api.version>
<sale.api.version>5.8.4-SNAPSHOT</sale.api.version>
<mindworkplate.api.version>5.8.3-SNAPSHOT</mindworkplate.api.version>
</properties>
......@@ -34,7 +34,7 @@
<dependency>
<groupId>cn.com.glsx.merchants</groupId>
<artifactId>platform-merchants-api</artifactId>
<version>5.8.0-SNAPSHOT</version>
<version>5.8.4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.com.glsx</groupId>
......
package cn.com.glsx.dj.smartcarlife.remote;
import cn.com.glsx.dj.smartcarlife.service.merchant.SyncMerchantInformationService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.merchants.model.request.MerchantRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 商户门店信息同步服务接口
*/
@Slf4j
@DubboService(version = "1.0.0", validation = "true")
public class SyncMerchantInfoRemoteImpl implements SyncMerchantInfoRemote {
@Autowired
private SyncMerchantInformationService syncMerchantInformationService;
/**
* 同步门店名称
*
* @param merchantRequest
* @return
*/
@Override
public RpcResponse<Boolean> updateMerchantName(MerchantRequest merchantRequest) {
Boolean b = syncMerchantInformationService.updateMerchantName(merchantRequest);
if (b) return RpcResponse.success(b);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, "同步门店名称更新失败!");
}
}
package cn.com.glsx.dj.smartcarlife.service.merchant;
import cn.com.glsx.dj.smartcarlife.mapper.SceneMerchantRefNewMapper;
import cn.com.glsx.dj.smartcarlife.mapper.ServiceMerchantMapper;
import cn.com.glsx.dj.smartcarlife.mapper.YqMerchantInfoMapper;
import cn.com.glsx.dj.smartcarlife.model.SceneMerchantRefNew;
import cn.com.glsx.dj.smartcarlife.model.ServiceMerchant;
import cn.com.glsx.dj.smartcarlife.model.YqMerchantInfo;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.merchants.model.request.MerchantRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
/**
* 门店信息同步
*/
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = RpcServiceException.class)
public class SyncMerchantInformationService {
@Autowired
private YqMerchantInfoMapper yqMerchantInfoMapper;
@Autowired
private ServiceMerchantMapper serviceMerchantMapper;
@Autowired
private SceneMerchantRefNewMapper sceneMerchantRefNewMapper;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public Boolean updateMerchantName(MerchantRequest merchantRequest) {
log.info("门店信息:{}", JSON.toJSONString(merchantRequest));
if (merchantRequest == null || merchantRequest.getMerchantNo() == null || merchantRequest.getMerchantName() == null) {
log.error("门店信息不全,无法更新!!");
return false;
}
try {
// 更新园区店名称
YqMerchantInfo updateYq = new YqMerchantInfo();
updateYq.setUpdatedDate(new Date());
updateYq.setUpdatedBy("system");
updateYq.setMerchantName(merchantRequest.getMerchantName());
Example exyq = new Example(YqMerchantInfo.class);
exyq.createCriteria().andEqualTo("merchantId",merchantRequest.getMerchantNo());
yqMerchantInfoMapper.updateByExampleSelective(updateYq,exyq);
// 更新服务门店名称
ServiceMerchant updateService = new ServiceMerchant();
updateService.setUpdatedDate(new Date());
updateService.setUpdatedBy("system");
updateService.setMerchantName(merchantRequest.getMerchantName());
Example exService = new Example(ServiceMerchant.class);
exService.createCriteria().andEqualTo("merchantId",merchantRequest.getMerchantNo());
serviceMerchantMapper.updateByExampleSelective(updateService,exService);
// 更新场景门店名称,主关联
SceneMerchantRefNew updateScene = new SceneMerchantRefNew();
updateScene.setUpdatedDate(new Date());
updateScene.setUpdatedBy("system");
updateScene.setMainMerchantName(merchantRequest.getMerchantName());
Example exScene = new Example(SceneMerchantRefNew.class);
exScene.createCriteria().andEqualTo("mainMerchantId",merchantRequest.getMerchantNo());
sceneMerchantRefNewMapper.updateByExampleSelective(updateScene,exScene);
// 更新场景门店名称,子关联
SceneMerchantRefNew updateScene1 = new SceneMerchantRefNew();
updateScene1.setUpdatedDate(new Date());
updateScene1.setUpdatedBy("system");
updateScene1.setSubMerchantName(merchantRequest.getMerchantName());
Example exScene1 = new Example(SceneMerchantRefNew.class);
exScene.createCriteria().andEqualTo("subMerchantId",merchantRequest.getMerchantNo());
sceneMerchantRefNewMapper.updateByExampleSelective(updateScene1,exScene1);
return true;
} catch (Exception e) {
log.error(e.getMessage(),e);
return false;
}
}
}
  • @wangxw1
    commented
    Master

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

    1. 【严重缺陷】复制粘贴导致的“全表更新”灾难(功能正确性)

    • 问题描述:在 SyncMerchantInformationService.java 的第 73-75 行,存在明显的复制粘贴错误。在构建更新场景门店名称(子关联)的条件时,错误地使用了上一个变量 exScene 来创建条件,导致 exScene1 实际上没有任何查询条件(WHERE 子句)。
    • 影响分析:极其危险。由于 exScene1 没有条件,执行 sceneMerchantRefNewMapper.updateByExampleSelective(updateScene1, exScene1); 时,将会把 dj_car_life_scene_merchant_ref_new 表中所有记录的 sub_merchant_name 字段全部更新为当前请求的商户名称,导致严重的生产数据污染。
    • 优化建议:修正变量名,将 exScene 改为 exScene1。
    • 代码示例:

      // 错误代码
      Example exScene1 = new Example(SceneMerchantRefNew.class);
      exScene.createCriteria().andEqualTo("subMerchantId",merchantRequest.getMerchantNo()); // 这里写错了变量名
      sceneMerchantRefNewMapper.updateByExampleSelective(updateScene1,exScene1);
      
      // 修复代码
      Example exScene1 = new Example(SceneMerchantRefNew.class);
      exScene1.createCriteria().andEqualTo("subMerchantId",merchantRequest.getMerchantNo()); 
      sceneMerchantRefNewMapper.updateByExampleSelective(updateScene1,exScene1);
      

    2. 【逻辑缺陷】异常被吞噬导致 @Transactional 事务失效(功能正确性与稳定性)

    • 问题描述:SyncMerchantInformationService.updateMerchantName 方法上声明了 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)。但在方法内部使用了 try...catch (Exception e) 捕获了所有异常并返回 false。
    • 影响分析:Spring 的声明式事务依赖于异常的抛出。由于异常在方法内部被 catch 且没有重新抛出,当这 4 个表更新过程中(例如第 3 个表更新时)发生数据库异常,事务管理器会认为方法执行成功,不会触发回滚。这将导致部分表更新成功、部分失败,产生数据不一致(脏数据)。
    • 优化建议:在 catch 块中记录日志后,必须抛出 RpcServiceException 以触发事务回滚;或者直接移除 try-catch,让全局异常处理器和 Dubbo 过滤器来统一处理异常。
    • 代码示例: java } catch (Exception e) { log.error("更新门店名称失败, merchantNo: {}", merchantRequest.getMerchantNo(), e); // 必须抛出异常以触发回滚 throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR.getCode(), "同步门店名称更新失败"); }

    3. 【工程实践】代码冗余与硬编码问题(最佳实践)

    • 问题描述:SyncMerchantInformationService 中连续 4 次手动创建实体、设置 updatedDate 和 updatedBy,存在大量重复的模板代码;且 updatedBy 被硬编码为 "system"。此外,参数校验中 merchantRequest == null 是多余的,因为 Dubbo 实现类已经开启了 @DubboService(validation = "true") 且接口参数加了 @NotNull。
    • 影响分析:硬编码降低了代码的可维护性和扩展性;冗余代码增加了阅读成本。如果未来需要增加审计字段或修改更新人逻辑,需要修改多处。
    • 优化建议:提取公共的构建更新对象的方法;使用 StringUtils.isAnyBlank() 优化字符串校验;将 "system" 提取为常量。

    二、评分明细

    • 功能实现的正确性与健壮性(15/40分):扣分较多。代码中存在致命的“全表更新” Bug(复制粘贴导致条件丢失),且 try-catch 吞噬异常导致事务 @Transactional 完全失效,无法保证数据一致性。
    • 安全性与潜在风险(30/30分):满分。使用了 MyBatis 的 Example 对象进行条件组装,底层使用预编译 SQL,不存在 SQL 注入风险;未发现越权或敏感信息泄露问题。
    • 最佳实践与可维护性(12/20分):扣 8 分。存在明显的硬编码("system")、大量重复的 Example 构建样板代码,以及无意义的局部变量声明(如 Boolean b)。
    • 性能与资源利用(3/5分):扣 2 分。虽然分 4 次单表更新在业务上可接受,但由于 Bug 导致的全表更新会引发严重的锁表和性能灾难。
    • 提交信息质量(4/5分):扣 1 分。Commit 信息清晰指出了更新的表名和字段,但包含了 Merge 冲突的无用信息(# Conflicts: 等),不够整洁。

    总分:64分

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