一、需求
二、代码实现
public Boolean updateMapping(List<Integer> deleteIds, List<ArticleContentMappingUpdateReq> articleContentMappingUpdateReqs) {
//如果有删除的视频 就先删除原视频与该文章关系
if (CollectionUtil.isNotEmpty(deleteIds)){
if (!this.removeByIds(deleteIds)){
throw new BizException("删除原视频失败");
}
}
Date localDate = new Date();
if (CollectionUtil.isNotEmpty(articleContentMappingUpdateReqs)){
//MappingId 不是空就更新 是空就添加
List<ArticleContentMappingUpdateReq> updateReqList = articleContentMappingUpdateReqs.stream()
.filter(x -> x.getMappingId() != null).collect(Collectors.toList());
List<ArticleContentMappingUpdateReq> saveRepList = articleContentMappingUpdateReqs.stream()
.filter(x -> x.getMappingId() == null).collect(Collectors.toList());
List<ArticleContentMappingEntity> updateList = PropertyCopier.copy(updateReqList, ArticleContentMappingEntity.class);
List<ArticleContentMappingEntity> saveList = PropertyCopier.copy(saveRepList, ArticleContentMappingEntity.class);
saveList.forEach(x -> {
x.setMappingStatus(CommonStatusEnum.valid.getCode());
x.setCreateTime(localDate);
x.setUpdateTime(localDate);
});
//更新
if (CollectionUtil.isNotEmpty(updateList)){
if (!this.updateBatchById(updateList)){
throw new BizException("更新视频关系失败");
}
}
if (CollectionUtil.isNotEmpty(saveList)){
//添加
if (!this.saveBatch(saveList)){
throw new BizException("添加视频关系失败");
}
}
}
return true;
}