tao.mo

biz-customer、kafka-cdnc-server | 修改 | 处理事务不生效问题

mt
2024年12月20日18:03:18
......@@ -34,6 +34,7 @@ public enum ResponseCode {
MESSAGE_CODE_30014(30014,"business_exception_30014"),
MESSAGE_CODE_30015(30015,"business_success_30015"),
MESSAGE_CODE_30016(30016,"business_exception_30016"),
MESSAGE_CODE_30017(30017,"business_exception_30017"),
/******打个样,编写样例*****/
// MESSAGE_CODE_30001(30001,"business_exception_30001"),
......
......@@ -28,4 +28,6 @@ public interface ConsignmentExtMapper {
@Select("SELECT BC.* FROM T_BIZ_CONSIGNMENT BC WHERE BC.ID = #{query.consignmentId}")
Consignment findConsignmentInfo(@Param("query") UserConsignmentInfoQuery query);
@Select("SELECT ID FROM T_BIZ_CONSIGNMENT WHERE ID = #{id} FOR UPDATE NOWAIT")
Long lockConsignmentRow(@Param("id") Long id);
}
......
......@@ -23,4 +23,6 @@ public interface IConsignmentRepository {
Consignment saveOrUpdate(Consignment entity);
void saveOrUpdateAll(List<Consignment> list);
void lockConsignmentRow(Long id);
}
......
......@@ -9,7 +9,10 @@ import com.fedex.connect.customer.data.query.UserConsignmentInfoQuery;
import com.fedex.connect.customer.repository.base.BaseDao;
import com.fedex.connect.customer.repository.repo.IConsignmentRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.Collections;
......@@ -99,4 +102,16 @@ public class ConsignmentRepositoryImpl extends BaseDao implements IConsignmentRe
}
});
}
/**
* @Author mt
* @Description 为运单表增加行级锁,行级锁使用主键id作为标志,否则可能锁表
* @Date 2024/12/20
* @param id
* @return void
*/
@Override
public void lockConsignmentRow(Long id){
consignmentExtMapper.lockConsignmentRow(id);
}
}
......
......@@ -3,7 +3,9 @@ package com.fedex.connect.customer.service.biz.impl;
import com.fedex.connect.common.dependencies.contants.DigitConstants;
import com.fedex.connect.common.dependencies.date.vo.ResponseVo;
import com.fedex.connect.common.dependencies.exception.OpErrorException;
import com.fedex.connect.common.model.biz.*;
import com.fedex.connect.common.model.biz.Attachment;
import com.fedex.connect.common.model.biz.Consignment;
import com.fedex.connect.common.model.biz.UploadRecord;
import com.fedex.connect.common.model.sys.User;
import com.fedex.connect.customer.data.bo.AddBo;
import com.fedex.connect.customer.data.bo.ConsignmentBo;
......@@ -12,28 +14,24 @@ import com.fedex.connect.customer.enums.ResponseCode;
import com.fedex.connect.customer.service.base.BaseService;
import com.fedex.connect.customer.service.biz.IAttachmentService;
import com.fedex.connect.customer.service.biz.IConsignmentService;
import com.fedex.connect.customer.service.biz.IEmailService;
import com.fedex.connect.customer.util.service.biz.ConsignmentUtil;
import com.fedex.connect.customer.util.service.biz.PushObUtil;
import com.fedex.connect.customer.util.service.biz.UploadRecordUtil;
import com.fedex.connect.customer.util.service.biz.UserConsignmentMappingUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @Author Szl
* @Description 类说明 运单操作相关service
* @Date 2024/10/31
*/
@Slf4j
@Service
public class ConsignmentServiceImpl extends BaseService implements IConsignmentService {
@Autowired
......@@ -41,13 +39,7 @@ public class ConsignmentServiceImpl extends BaseService implements IConsignmentS
@Autowired
private UploadRecordUtil uploadRecordUtil;
@Autowired
private UserConsignmentMappingUtil userConsignmentMappingUtil;
@Autowired
private PushObUtil pushObUtil;
@Autowired
private IAttachmentService attachmentService;
@Autowired
private IEmailService emailService;
/**
* @Author Szl
......@@ -134,7 +126,7 @@ public class ConsignmentServiceImpl extends BaseService implements IConsignmentS
/**
* 保存运单相关信息
*/
this.submitInfo(consignment,attachmentList,uploadRecord,user);
consignmentUtil.submitInfo(consignment,attachmentList,uploadRecord,user);
}catch(OpErrorException ex){
/**
* 提交异常,需要删除对应已上传附件
......@@ -150,73 +142,4 @@ public class ConsignmentServiceImpl extends BaseService implements IConsignmentS
}
return responseUtils.success(ResponseCode.MESSAGE_CODE_30015);
}
/**
* @Author mt
* @Description 保存运单相关信息
* @Date 2024/11/19
* @param consignment
* @param attachmentList
* @param uploadRecord
* @param user
* @return void
*/
@Transactional(rollbackFor = Exception.class)
public void submitInfo(Consignment consignment,
List<Attachment> attachmentList,
UploadRecord uploadRecord,
User user) throws Exception{
/**
* 保存或更新运单表
*/
consignmentRepository.saveOrUpdate(consignment);
//设置上传记录表运单ID
uploadRecord.setConsignmentId(consignment.getId());
/**
* 保存上传记录表
*/
uploadRecordRepository.saveOrUpdate(uploadRecord);
//设置附件表运单id,上传记录表id
Optional.ofNullable(attachmentList).orElse(new ArrayList<>()).stream().filter(Objects::nonNull).forEach(p->{
p.setBizId(consignment.getId());
p.setUploadRecordId(uploadRecord.getId());
});
/**
* 保存附件表信息
*/
attachmentRepository.saveOrUpdateAll(attachmentList);
//用户uuid与运单uuid不一致,则需要添加用户运单中间表信息
if(!user.getUserUuid().equals(consignment.getUserUuid())){
/**
* 查找用户与运单关联信息
*/
UserConsignmentMapping userConsignmentMapping = userConsignmentMappingRepository.findByUserIdConsignmentId(user.getId(),consignment.getId());
if(Objects.isNull(userConsignmentMapping)){
/**
* 初始化插入预清关文件推送任务日志
*/
userConsignmentMapping = userConsignmentMappingUtil.initUserConsignmentMapping(user.getId(),consignment.getId());
/**
* 初始化插入预清关文件推送任务日志
*/
userConsignmentMappingRepository.saveOrUpdate(userConsignmentMapping);
}
}
/**
* Todo 初始化插入预清关文件推送邮件任务日志,szl添加
*/
/**
* 初始化插入预清关文件推送日志,mt添加
*/
PushOb pushOb = pushObUtil.initPushOb(consignment,uploadRecord,user);
/**
* 初始化插入提醒发件人邮件日志
*/
emailService.saveNotificationEmail(consignment,user);
/**
* 推送进口文件主表
*/
pushObRepository.saveOrUpdate(pushOb);
}
}
\ No newline at end of file
......
......@@ -7,27 +7,30 @@ import com.fedex.connect.common.dependencies.enums.biz.ConsignmentStatusEnum;
import com.fedex.connect.common.dependencies.util.AssignmentFieldUtils;
import com.fedex.connect.common.dependencies.util.ResponseUtils;
import com.fedex.connect.common.model.bi.DictionaryEntries;
import com.fedex.connect.common.model.biz.Consignment;
import com.fedex.connect.common.model.biz.*;
import com.fedex.connect.common.model.sys.User;
import com.fedex.connect.customer.data.bo.AddBo;
import com.fedex.connect.customer.data.bo.ConsignmentBo;
import com.fedex.connect.customer.data.dto.ConsignmentAddDto;
import com.fedex.connect.customer.enums.ResponseCode;
import com.fedex.connect.customer.repository.repo.IConsignmentRepository;
import com.fedex.connect.customer.repository.repo.*;
import com.fedex.connect.customer.service.biz.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;
import java.util.*;
/**
* @Author Szl
* @Description 类说明 运单相关util
* @Date 2024/11/5
*/
@Slf4j
@Component
public class ConsignmentUtil {
@Autowired
......@@ -36,6 +39,20 @@ public class ConsignmentUtil {
ResponseUtils responseUtils;
@Autowired
IConsignmentRepository consignmentRepository;
@Autowired
UserConsignmentMappingUtil userConsignmentMappingUtil;
@Autowired
PushObUtil pushObUtil;
@Autowired
IEmailService emailService;
@Autowired
IUploadRecordRepository uploadRecordRepository;
@Autowired
IPushObRepository pushObRepository;
@Autowired
IAttachmentRepository attachmentRepository;
@Autowired
IUserConsignmentMappingRepository userConsignmentMappingRepository;
/**
* @Author mt
......@@ -194,4 +211,78 @@ public class ConsignmentUtil {
private boolean isShipperAccountMatch(String account1, String account2) {
return StringUtils.hasText(account1) && StringUtils.hasText(account2) && account1.equals(account2);
}
/**
* @Author mt
* @Description 保存运单相关信息
* @Date 2024/11/19
* @param consignment
* @param attachmentList
* @param uploadRecord
* @param user
* @return void
*/
@Transactional(rollbackFor = Exception.class)
public void submitInfo(Consignment consignment,
List<Attachment> attachmentList,
UploadRecord uploadRecord,
User user) throws Exception{
if(Objects.nonNull(consignment) && Objects.nonNull(consignment.getId())){
try{
//增加行级锁
consignmentRepository.lockConsignmentRow(consignment.getId());
}catch(Exception ex){
responseUtils.fail(ResponseCode.MESSAGE_CODE_30017,ex);
}
}
/**
* 保存或更新运单表
*/
consignmentRepository.saveOrUpdate(consignment);
//设置上传记录表运单ID
uploadRecord.setConsignmentId(consignment.getId());
/**
* 保存上传记录表
*/
uploadRecordRepository.saveOrUpdate(uploadRecord);
//设置附件表运单id,上传记录表id
Optional.ofNullable(attachmentList).orElse(new ArrayList<>()).stream().filter(Objects::nonNull).forEach(p->{
p.setBizId(consignment.getId());
p.setUploadRecordId(uploadRecord.getId());
});
/**
* 保存附件表信息
*/
attachmentRepository.saveOrUpdateAll(attachmentList);
//用户uuid与运单uuid不一致,则需要添加用户运单中间表信息
if(!user.getUserUuid().equals(consignment.getUserUuid())){
/**
* 查找用户与运单关联信息
*/
UserConsignmentMapping userConsignmentMapping = userConsignmentMappingRepository.findByUserIdConsignmentId(user.getId(),consignment.getId());
if(Objects.isNull(userConsignmentMapping)){
/**
* 初始化插入预清关文件推送任务日志
*/
userConsignmentMapping = userConsignmentMappingUtil.initUserConsignmentMapping(user.getId(),consignment.getId());
/**
* 初始化插入预清关文件推送任务日志
*/
userConsignmentMappingRepository.saveOrUpdate(userConsignmentMapping);
}
}
/**
* 初始化插入预清关文件推送日志,mt添加
*/
PushOb pushOb = pushObUtil.initPushOb(consignment,uploadRecord,user);
/**
* 初始化插入提醒发件人邮件日志
*/
emailService.saveNotificationEmail(consignment,user);
/**
* 推送进口文件主表
*/
pushObRepository.saveOrUpdate(pushOb);
}
}
\ No newline at end of file
......
......@@ -46,3 +46,4 @@ business_exception_30013=文件名为:{0},上传失败,请检查后重试
business_exception_30014=运单信息加载失败,请稍后重试
business_success_30015=上传成功
business_exception_30016=运单提交失败
business_exception_30017=运单提交失败,该运单正在被其他用户操作,请稍后重试
\ No newline at end of file
......
......@@ -36,8 +36,8 @@ business_exception_30003={0}不正确,请重新输入
business_exception_30004=The waybill you entered is generated by another user, whether continue uploading? Confirm, Cancel. No more prompts in the future for the same situation.
business_exception_30005=The waybill you entered is generated by another account, whether continue uploading? Confirm, Cancel. No more prompts in the future for the same situation.
business_exception_30006=附件上传不符合标准
business_exception_30007=所有上传文件数量不超过50个文件
business_exception_30008=所有上传文件总大小不超过50M
business_exception_30007=所有上传文件数量不超过{}个文件
business_exception_30008=所有上传文件总大小不超过{}M
business_exception_30009=运单或发票未上传
business_exception_30010=文件类型不在:运单、发票、箱单、其他
business_exception_30011=该运单已被其他人创建,不可以重复创建
......@@ -46,3 +46,4 @@ business_exception_30013=文件名为:{0},上传失败,请检查后重试
business_exception_30014=运单信息加载失败,请稍后重试
business_success_30015=上传成功
business_exception_30016=运单提交失败
business_exception_30017=运单提交失败,该运单正在被其他用户操作,请稍后重试
\ No newline at end of file
......
......@@ -56,7 +56,10 @@ public class Dm501ServiceImpl extends BaseService implements IDm501Service {
CeInfo ceInfo = dm501Util.generateCeInfo(consignment501,kafKaTemporaryStorage.getSendTime());
//ce数据解析正常,进行后续操作
Consignment consignment = dm501Util.generateConsignment(ceInfo);
this.saveCeInfoAndConsignment(ceInfo,consignment);
/**
* 保存ce信息、以及运单表信息
*/
dm501Util.saveCeInfoAndConsignment(ceInfo,consignment);
}
}catch(Exception ex){
log.error("DM501消息处理异常:{}", ex.getMessage(),ex);
......@@ -65,19 +68,4 @@ public class Dm501ServiceImpl extends BaseService implements IDm501Service {
return processResults;
}
/**
* @Author mt
* @Description 保存ce信息、以及运单表信息
* @Date 2024/11/1
* @param ceInfo
* @param consignment
* @return void
*/
@Transactional(rollbackFor = Exception.class)
public void saveCeInfoAndConsignment(CeInfo ceInfo, Consignment consignment){
ceInfoRepository.save(ceInfo);
//绑定ceInfo表Id
consignment.setCeInfoId(ceInfo.getId());
consignmentRepository.save(consignment);
}
}
\ No newline at end of file
......
......@@ -10,12 +10,14 @@ import com.fedex.connect.common.model.sys.User;
import com.fedex.connect.kafka.constants.Constant;
import com.fedex.connect.kafka.data.dto.dm.dm501.Dm501ConAddresses;
import com.fedex.connect.kafka.data.dto.dm.dm501.Dm501Consignments;
import com.fedex.connect.kafka.repository.repo.ICeInfoRepository;
import com.fedex.connect.kafka.repository.repo.IConsignmentRepository;
import com.fedex.connect.kafka.repository.repo.IUserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
......@@ -30,6 +32,8 @@ public class Dm501Util {
@Autowired
IConsignmentRepository consignmentRepository;
@Autowired
ICeInfoRepository ceInfoRepository;
@Autowired
IUserRepository userRepository;
/**
......@@ -283,4 +287,20 @@ public class Dm501Util {
}
return address;
}
/**
* @Author mt
* @Description 保存ce信息、以及运单表信息
* @Date 2024/11/1
* @param ceInfo
* @param consignment
* @return void
*/
@Transactional(rollbackFor = Exception.class)
public void saveCeInfoAndConsignment(CeInfo ceInfo, Consignment consignment){
ceInfoRepository.save(ceInfo);
//绑定ceInfo表Id
consignment.setCeInfoId(ceInfo.getId());
consignmentRepository.save(consignment);
}
}
......