Commit c3fbc781 by 叶立庆

Merge branch 'v5.8.5' into 'master'

上线合并

See merge request !6
2 parents 9ce4cb0e 96812388
package cn.com.glsx.dj.goods.listing.mapper;
import cn.com.glsx.dj.smartcarlife.model.ServiceMerchant;
import org.apache.ibatis.annotations.Mapper;
import org.oreframework.datasource.mybatis.mapper.OreMapper;
@Mapper
public interface ServiceMerchantMapper extends OreMapper<ServiceMerchant> {
}
......@@ -202,6 +202,8 @@ public class GoodsListingApprovalService {
@Autowired
private GoodsInfoMapper goodsInfoMapper;
@Autowired
private ServiceMerchantService serviceMerchantService;
public Page<GoodsListingApprovalDTO> listWithPage(RpcPagination<GoodsListingApprovalDTO> pagination) {
......@@ -575,6 +577,7 @@ public class GoodsListingApprovalService {
log.error(e.getMessage(), e);
}
publishApprovedRulesByMerchant(context);
openScene(context);
goodsListingApprovalMapper.updateByPrimaryKeySelective(GoodsListingApproval.builder()
.id(context.approval.getId()).systemStatus(SystemStatus.APPROVED.getValue())
.updatedBy(updatedBy).updatedDate(new Date())
......@@ -620,6 +623,20 @@ public class GoodsListingApprovalService {
}
}
public void openScene(ApprovalPublishContext context) {
try {
for (GoodsListingMerchantRef merchantRef : context.merchantRefs) {
log.info("updateServiceMerchantOpenDate start,merchantId:{},businessCode:{}", merchantRef.getMerchantId(), context.businessCode);
if (Objects.equals(SceneTypeEnum.SCENE1_1.getValue(), context.sceneType)) {
log.info("updateServiceMerchantOpenDate start,merchantId:{},businessCode:{}, sceneType:{}", merchantRef.getMerchantId(), context.businessCode, context.sceneType);
serviceMerchantService.updateServiceMerchantOpenDate(merchantRef.getMerchantId(), new Date());
}
}
} catch (Exception e) {
log.error("updateServiceMerchantOpenDate error:{}", e.getMessage(), e);
}
}
private ApprovalPublishContext buildApprovalPublishContextOrdinary(String businessCode, String updatedBy) {
GoodsListingApproval approval = thiz.getByBusinessCode(businessCode);
RpcAssert.assertNotNull(approval, DefaultErrorEnum.DATA_NOT_EXIST, "未找到审核单,businessCode:" + businessCode);
......
package cn.com.glsx.dj.goods.listing.service;
import cn.com.glsx.dj.goods.listing.mapper.ServiceMerchantMapper;
import cn.com.glsx.dj.smartcarlife.constant.CommonConstants;
import cn.com.glsx.dj.smartcarlife.model.ServiceMerchant;
import cn.com.glsx.framework.core.beans.BaseModel;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
@Slf4j
@Service
@Transactional(propagation= Propagation.SUPPORTS,rollbackFor= RpcServiceException.class)
public class ServiceMerchantService {
@Lazy
@Autowired
private ServiceMerchantService thiz;
@Autowired
private ServiceMerchantMapper serviceMerchantMapper;
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public void updateServiceMerchantOpenDate(Integer merchantId, Date openDate){
ServiceMerchant sm = thiz.getBaseByMerchantId(merchantId);
if(sm != null && sm.getOpenDate() == null) {
log.info("updateServiceMerchantOpenDate start merchantId:{} openDate:{}", merchantId, openDate);
ServiceMerchant updatedRecord = ServiceMerchant.builder().id(sm.getId()).openDate(openDate)
.updatedDate(new Date()).updatedBy(CommonConstants.DEFAULT_UPDATE_BY).build();
serviceMerchantMapper.updateByPrimaryKeySelective(updatedRecord);
log.info("updateServiceMerchantOpenDate end merchantId:{} openDate:{}", merchantId, openDate);
}
}
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public ServiceMerchant getBaseByMerchantId(Integer merchantId){
RpcAssert.assertNotNull(merchantId, DefaultErrorEnum.DATA_NULL,"merchantId not be null");
try{
ServiceMerchant condition = new ServiceMerchant();
condition.setMerchantId(merchantId);
condition.setDeletedFlag(BaseModel.DeletedFlagEnum.N.name());
return serviceMerchantMapper.selectOne(condition);
}catch(Exception e){
log.info(e.getMessage(),e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR,e.getMessage());
}
}
}
  • @wangxw1
    commented
    Master

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

    1. 异常处理不当与潜在的空指针风险(功能实现的正确性与健壮性)

    • 问题描述:在 GoodsListingApprovalService.java 的 openScene 方法中,直接对 context.merchantRefs 进行了 for 循环遍历,缺少对 merchantRefs 的判空处理。其次,整个方法使用了 try-catch(Exception e) 吞掉了所有异常且仅做日志记录。
    • 影响分析:
      1. 如果 context.merchantRefs 为 null,会抛出 NullPointerException,虽然被 catch 住了不会阻断主流程,但会导致后续正常的商家无法被处理。
      2. 吞掉异常意味着如果数据库更新失败(如网络抖动、死锁等),主审批流程依然会继续并提交(systemStatus 被更新为 APPROVED),这将导致业务数据不一致(审批通过了,但商户开通时间未更新)。
    • 优化建议:增加集合判空逻辑;根据业务强弱依赖关系决定是否抛出异常。如果要求强一致性,应将异常抛出以触发事务回滚。 java public void openScene(ApprovalPublishContext context) { if (context == null || CollectionUtils.isEmpty(context.merchantRefs)) { return; } // 如果业务要求审批和开通时间强一致,建议不要吞掉异常,或者抛出自定义 RpcServiceException for (GoodsListingMerchantRef merchantRef : context.merchantRefs) { if (Objects.equals(SceneTypeEnum.SCENE1_1.getValue(), context.sceneType)) { serviceMerchantService.updateServiceMerchantOpenDate(merchantRef.getMerchantId(), new Date()); } } }

    2. Spring AOP 自调用反模式(最佳实践与可维护性)

    • 问题描述:在 ServiceMerchantService.java 中,使用了 @Lazy @Autowired private ServiceMerchantService thiz; 来解决 Spring AOP 的内部方法调用事务失效问题。
    • 影响分析:这种写法虽然能解决事务失效问题,但属于典型的代码坏味道(Code Smell),增加了类的耦合度和理解成本。实际上,updateServiceMerchantOpenDate 方法本身已经被 @Transactional(Propagation.REQUIRED) 修饰,当外部调用该方法时,已经开启了事务上下文。此时在方法内部直接调用 this.getBaseByMerchantId(),由于都在同一个线程上下文中,查询操作自然会加入到当前事务中,完全不需要通过代理对象 thiz 来调用。
    • 优化建议:移除 thiz 注入,直接使用 this 调用内部方法。 ```java // 移除 @Lazy @Autowired private ServiceMerchantService thiz;

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class) public void updateServiceMerchantOpenDate(Integer merchantId, Date openDate){ // 直接调用即可,无需使用 thiz ServiceMerchant sm = this.getBaseByMerchantId(merchantId); // ... 后续逻辑 }

    
    **3. 循环内操作数据库与非原子更新(性能与最佳实践)**
    - **问题描述**:
      1. `openScene` 方法在 `for` 循环中逐个调用 `updateServiceMerchantOpenDate`,存在 N+1 数据库交互问题。
      2. `updateServiceMerchantOpenDate` 内部采用了“先查询 (`selectOne`),再判断,后更新 (`updateByPrimaryKeySelective`)”的模式。
    - **影响分析**:在循环中频繁建立数据库连接和执行 SQL 会造成不必要的性能损耗。同时,“先查后改”在并发场景下存在竞态条件(Check-Then-Act),虽然审批场景并发可能不高,但并非严谨的工程实践。
    - **优化建议**:建议利用 SQL 的原子性直接进行更新,并尽量采用批量更新。如果受限于 MyBatis Plus 的单表操作,至少应将查询和更新合并为一条带条件的原子 Update 语句。
    ```java
    // 建议在 ServiceMerchantMapper 中自定义 SQL 或使用 UpdateWrapper 直接更新:
    // UPDATE service_merchant SET open_date = #{openDate}, updated_date = NOW() 
    // WHERE merchant_id = #{merchantId} AND open_date IS NULL AND deleted_flag = 'N'
    

    二、评分明细

    • 功能实现的正确性与健壮性 (32/40分): 扣分原因:openScene 方法缺少对集合的判空处理,存在隐藏的 NPE 风险;同时,直接吞咽异常可能破坏审批业务与商户状态更新的数据一致性。
    • 安全性与潜在风险 (30/30分): 得分原因:未发现 SQL 注入、越权等明显安全漏洞;没有违反 MyBatis 子查询和 Dubbo 接口参数变更的红线规则。使用了 MyBatis Plus 提供的方法,安全性有保障。
    • 最佳实践与可维护性 (14/20分): 扣分原因:使用了 @Lazy @Autowired 注入自身的反模式,且在当前事务传播级别下属于画蛇添足;循环内调用数据库更新不符合最佳实践。
    • 性能与资源利用 (3/5分): 扣分原因:采用了“先查后改”的两次数据库 IO 操作,且置于外部 for 循环中,增加了数据库的连接开销和资源浪费。
    • 提交信息质量 (4/5分): 得分原因:Commit 信息“嘀加上架审核通过后,开通4s店1.1业务场景”清楚地描述了业务背景和目的,具备一定的可追溯性,但格式可以进一步规范化(如采用 feat: xxx 格式)。

    总分:83分

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