Commit e83f4e5d by 杨卫

fix(1028423): 重构4S店信息修改功能的事务处理和异常管理

- 在MerchantEditRemoteImpl中添加@Transactional注解确保数据一致性
- 将业务逻辑分离到doUpdateMerchantInfo方法中实现事务控制
- 实现异常兜底机制,在Dubbo入口处统一捕获和处理异常
- 修复RpcServiceException构造函数调用方式确保错误码正确传递
- 优化MerchantEditController中的异常处理逻辑提高系统稳定性
1 parent 136f66d9
...@@ -25,6 +25,7 @@ import com.alibaba.dubbo.config.annotation.Service; ...@@ -25,6 +25,7 @@ import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -58,8 +59,11 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote { ...@@ -58,8 +59,11 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote {
@Autowired @Autowired
private MerchantLogService merchantLogService; private MerchantLogService merchantLogService;
@Autowired
private MerchantEditRemoteImpl self;
/** /**
* 4S店信息修改 * 4S店信息修改(Dubbo 入口,含异常兜底)
* *
* @param request 修改请求参数 * @param request 修改请求参数
* @return 修改结果 * @return 修改结果
...@@ -67,14 +71,32 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote { ...@@ -67,14 +71,32 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote {
@Override @Override
public RpcResponse<Boolean> updateMerchantInfo(MerchantEditRpcRequest request) { public RpcResponse<Boolean> updateMerchantInfo(MerchantEditRpcRequest request) {
try { try {
// 1. 参数校验 return self.doUpdateMerchantInfo(request);
} catch (RpcServiceException e) {
log.error("4S店信息修改失败,merchantNo={}", request.getMerchantNo(), e);
return RpcResponse.error(e);
} catch (Exception e) {
log.error("4S店信息修改异常,merchantNo={}", request.getMerchantNo(), e);
return RpcResponse.error(new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR.getValue(), "4S店信息修改异常"));
}
}
/**
* 4S店信息修改(事务方法,异常直接抛出触发回滚)
*
* @param request 修改请求参数
* @return 修改结果
*/
@Transactional(rollbackFor = Exception.class)
public RpcResponse<Boolean> doUpdateMerchantInfo(MerchantEditRpcRequest request) {
// 1. 参数校验
BeanValidationUtils.validateAndThrow(request); BeanValidationUtils.validateAndThrow(request);
// 2. 根据商户号查询商户详情 // 2. 根据商户号查询商户详情
Integer merchantNo = request.getMerchantNo(); Integer merchantNo = request.getMerchantNo();
Integer merchantId = merchantMapper.findIdByMerchantNo(merchantNo); Integer merchantId = merchantMapper.findIdByMerchantNo(merchantNo);
if (merchantId == null) { if (merchantId == null) {
throw new RpcServiceException(BusinessExceptionEnum.MERCHANT_NOEXIT_EXCEPTION); throw new RpcServiceException(BusinessExceptionEnum.MERCHANT_NOEXIT_EXCEPTION.getValue(), BusinessExceptionEnum.MERCHANT_NOEXIT_EXCEPTION.getDisplayName());
} }
MerchantRequest detailRequest = new MerchantRequest(); MerchantRequest detailRequest = new MerchantRequest();
detailRequest.setId(merchantId); detailRequest.setId(merchantId);
...@@ -145,13 +167,6 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote { ...@@ -145,13 +167,6 @@ public class MerchantEditRemoteImpl implements MerchantEditRemote {
merchantLogService.saveMerchantLog(merchantLog); merchantLogService.saveMerchantLog(merchantLog);
return RpcResponse.success(true); return RpcResponse.success(true);
} catch (RpcServiceException e) {
log.error("4S店信息修改失败,merchantNo={}", request.getMerchantNo(), e);
return RpcResponse.error(e);
} catch (Exception e) {
log.error("4S店信息修改异常,merchantNo={}", request.getMerchantNo(), e);
return RpcResponse.error(new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR.getValue(), "4S店信息修改异常"));
}
} }
/** /**
......
...@@ -60,6 +60,7 @@ import org.springframework.core.env.Environment; ...@@ -60,6 +60,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!