@Override
@Transactional(rollbackFor = Exception.class)
public List<TmsLoadingTime> createOrUpdate(Long tenantId, List<TmsLoadingTime> tmsLoadingTimes) {
Long userId = DetailsHelper.getUserDetails().getUserId();
List<TmsLoadingTime> resultList = new ArrayList<>();
List<TmsLoadingTime> addList = new ArrayList<>();
List<TmsLoadingTime> updateList = new ArrayList<>();
List<TmsLoadingTimeHis> tmsLoadingTimeHisList = new ArrayList<>();
//1.判断是新增还是更新
for (TmsLoadingTime tmsLoadingTime : tmsLoadingTimes) {
tmsLoadingTime.setTenantId(tenantId);
if (Objects.isNull(tmsLoadingTime.getLoadingTimeId())) {
//2.新增
//唯一性校验
TmsLoadingTime tmsLoad = new TmsLoadingTime();
tmsLoad.setTenantId(tenantId);
tmsLoad.setLoadingTime(tmsLoadingTime.getLoadingTime());
tmsLoad.setUnitId(tmsLoadingTime.getUnitId());
tmsLoad.setType(tmsLoadingTime.getType());
tmsLoad.setLength(tmsLoadingTime.getLength());
TmsLoadingTime selectOne = tmsLoadingTimeMapper.selectOne(tmsLoad);
if (Objects.nonNull(selectOne)) {
throw new MtException("FYDS_TMS_LOADINGTIME_0001", mtErrorMessageRepository.getErrorMessage(Long.valueOf(tmsLoadingTime.getLength()),
"FYDS_TMS_LOADINGTIME_0001", tmsLoadingTime.getLoadingTime().toString()));
}
addList.add(tmsLoadingTime);
resultList.add(tmsLoadingTime);
}else {
//3.更新
updateList.add(tmsLoadingTime);
resultList.add(tmsLoadingTime);
}
}
//4.插入、事件
if (CollectionUtils.isNotEmpty(addList)){
//批量插入
mtCustomDbRepository.batchInsert(addList);
//插入事件
MtEventCreateVO eventCreateVO2 = new MtEventCreateVO();
eventCreateVO2.setEventTypeCode("LOADING_TIME_INSERT");
Long eventId = mtEventRepository.eventCreate(tenantId, eventCreateVO2);
addList.stream().forEach(tmsLoadingTime -> {
TmsLoadingTimeHis tmsLoadingTimeHis = new TmsLoadingTimeHis();
BeanUtils.copyProperties(tmsLoadingTime, tmsLoadingTimeHis);
tmsLoadingTimeHis.setEventId(eventId);
tmsLoadingTimeHis.setCreatedBy(userId);
tmsLoadingTimeHis.setCreationDate(new Date());
tmsLoadingTimeHis.setLoadingTimeId(tmsLoadingTime.getLoadingTimeId());
tmsLoadingTimeHisList.add(tmsLoadingTimeHis);
});
}
//5.更新、事件
if (CollectionUtils.isNotEmpty(updateList)){
//批量更新
mtCustomDbRepository.batchUpdate(updateList);
//更新事件
MtEventCreateVO eventCreateVO2 = new MtEventCreateVO();
eventCreateVO2.setEventTypeCode("LOADING_TIME_UPDATE");
Long eventId = mtEventRepository.eventCreate(tenantId, eventCreateVO2);
updateList.stream().forEach(tmsLoadingTime -> {
TmsLoadingTimeHis tmsLoadingTimeHis = new TmsLoadingTimeHis();
BeanUtils.copyProperties(tmsLoadingTime, tmsLoadingTimeHis);
tmsLoadingTimeHis.setEventId(eventId);
tmsLoadingTimeHis.setCreatedBy(userId);
tmsLoadingTimeHis.setCreationDate(new Date());
tmsLoadingTimeHisList.add(tmsLoadingTimeHis);
});
}
//6.记录历史
if (CollectionUtils.isNotEmpty(tmsLoadingTimeHisList)){
tmsLoadingTimeHisRepository.batchInsertList(tmsLoadingTimeHisList);
}
return resultList;
}
多条数据的插入/更新/历史记录
最新推荐文章于 2023-03-27 12:43:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)