更新时包含列表数据,且同时含有删除,修改,添加操作比较优雅的方式

一、需求

![在

二、代码实现

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;
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44798321/article/details/131202430