原创

Java-代码段-list列表数据参数-新增、删除、保留的判断示例代码

private Integer doSaveOrUpdateByDto(EduTrainUserDto dto) {

    log.info("{}新增或编辑接口入参:dto:{}", TAG, dto);

    //查询原来的人
    EduTrainUserDto eduTrainUserDto = new EduTrainUserDto();
    eduTrainUserDto.setContainerId(UserInfoUtil.getCurContainerId());
    eduTrainUserDto.setImplementId(dto.getImplementId());
    List<EduTrainUserVo> userOldTrainUserVoList = eduTrainUserMapper.getTrainUserIdList(eduTrainUserDto);
    List<String> userOldTrainUserIdList = userOldTrainUserVoList.stream().map(EduTrainUserVo::getUserId).collect(Collectors.toList());
    log.info("userOldTrainUserIdList:{}", userOldTrainUserIdList);

    //查询最新的人
    List<String> newHandledUserIdList = getUserIdListByImplementId(dto.getImplementId());
    log.info("updateUserIdList:{}", newHandledUserIdList);

    //新增的
    List<String> newAddUserIdList = newHandledUserIdList.stream().filter(item -> !userOldTrainUserIdList.contains(item)).collect(Collectors.toList());
    log.info("newAddUserIdList:{}", newAddUserIdList);
    //删除的
    List<String> deleteUserIdList = userOldTrainUserIdList.stream().filter(item -> !newHandledUserIdList.contains(item)).collect(Collectors.toList());
    log.info("deleteUserIdList:{}", deleteUserIdList);
    //不变的人 (也查出来,更新一下edu_train_user表的user_intermediate_id,保持跟随最新的状态)
    //先复制1份,retainAll() 影响 retainUserIdList,但是不影响userOldTrainUserIdList
    List<String> retainUserIdList = Lists.newArrayList(newHandledUserIdList);
    retainUserIdList.retainAll(userOldTrainUserIdList);
    //特别注意:业务上不变的人可能要执行update操作去更新一些字段

    return newHandledUserIdList.size();//返回总的人数
}


@Transactional(timeout = 50, isolation = Isolation.READ_UNCOMMITTED)
public synchronized void batchAddCourseAssociatedUser(boolean isOnlyInsert, String containerId, Integer courseId, List<CourseAssociatedUser> courseAssociatedUserList) {

    log.info("开始处理课程分配人员,入参:isOnlyInsert:{},containerId:{},courseId:{},courseAssociatedUserList:{}"
            , isOnlyInsert, containerId, courseId, JSON.toJSONString(courseAssociatedUserList));

    //查询原来的人
    List<CourseAssociatedUser> courseAssociatedUserList = getCourseAssociatedUserListFromDb(containerId, courseId, learningRequirement12, null);
    List<String> oldCourseUserIdList = courseAssociatedUsersDb.stream().map(CourseAssociatedUser::getUserId).distinct().collect(Collectors.toList());
    log.info("oldCourseUserIdList:{}", oldCourseUserIdList);

    //查询最新的人
    List<String> newCourseUserIdList = courseAssociatedUserList.stream().map(CourseAssociatedUser::getUserId).distinct().collect(Collectors.toList());
    log.info("newCourseUserIdList:{}", newCourseUserIdList);

    //新增的
    List<String> newAddCourseUserIdList = newCourseUserIdList.stream().filter(item -> !oldCourseUserIdList.contains(item)).collect(Collectors.toList());
    log.info("newAddCourseUserIdList:{}", newAddCourseUserIdList);
    //删除的
    //如果新增的里面也有自选的,也要先删除掉
    List<String> deleteRequirement3CourseUserIdList = Collections.emptyList();
    if (CollectionUtils.isNotEmpty(newAddCourseUserIdList)) {
        List<Integer> learningRequirement3 = Lists.newArrayList(Constants.LEARNING_REQUIREMENT_SELF_CHOOSE);
        List<CourseAssociatedUser> courseAssociatedUsersDb3 = getCourseAssociatedUserListFromDb(containerId, courseId, learningRequirement3, newAddCourseUserIdList);
        if (CollectionUtils.isNotEmpty(courseAssociatedUsersDb3)) {
            deleteRequirement3CourseUserIdList = courseAssociatedUsersDb3.stream().filter(
                    (Predicate<CourseAssociatedUser>) courseAssociatedUser -> newAddCourseUserIdList.contains(courseAssociatedUser.getUserId()))
                    .map(CourseAssociatedUser::getUserId).distinct().collect(Collectors.toList());
        }
    }
    List<String> deleteCourseUserIdList = oldCourseUserIdList.stream().filter(item -> !newCourseUserIdList.contains(item)).collect(Collectors.toList());
    log.info("deleteCourseUserIdList:{}", deleteCourseUserIdList);
    log.info("deleteRequirement3CourseUserIdList:{}", deleteRequirement3CourseUserIdList);
    deleteCourseUserIdList.addAll(deleteRequirement3CourseUserIdList);
    log.info("deleteCourseUserIdList加上删除有自选:{}", deleteCourseUserIdList);
    //不变的人 (也查出来,更新一下edu_train_user表的user_intermediate_id,保持跟随最新的状态)
    //先复制1份,retainAll() 影响 retainUserIdList,但是不影响oldCourseUserIdList
    List<String> retainUserIdList = Lists.newArrayList(newCourseUserIdList);
    retainUserIdList.retainAll(oldCourseUserIdList);
     //特别注意:业务上不变的人可能要执行update操作去更新一些字段
    //保存不变的人可能会更新了必修选修
    if (CollectionUtils.isNotEmpty(retainUserIdList)) {
        Map<String, CourseAssociatedUser> courseAssociatedUsersDbMap = Maps.uniqueIndex(
                courseAssociatedUsersDb.stream().filter(associatedUser -> retainUserIdList.contains(associatedUser.getUserId())).collect(Collectors.toList()),
                CourseAssociatedUser::getUserId);
                // 特别注意,这里的courseAssociatedUserList这里是来自数据库的,主要是因为可能更新了必修选修一个字段,另外需要使用到原id
                // 其他的场景,需要更新多个最新数据在字段时,courseAssociatedUserList应该替换为前端最新提交,且注意id的处理 劳保代码代码如下
                //这里还是需要拿到record id的(从最新的laborReceiveDetailListVos 拿到更新数据,getUniqueTag是用来确定唯一对象的 )
                //List<LaborReceiveDetailListVo> retainProductList = //laborReceiveDetailListVos.stream().filter(oldOne -> //retainProductTagList.contains(oldOne.getUniqueTag())).collect(Collectors.toList());
                //retainProductList = ListUtils.getInstance().getUniqueKeyList(retainProductList, //LaborReceiveDetailListVo::getUniqueTag);
               // Map<String, LaborReceiveDetailListVo> dbMap = Maps.uniqueIndex(dbList,LaborReceiveDetailListVo::getUniqueTag);
                //应该已经多余
                //retainProductList.forEach(retain -> {
                   // LaborReceiveDetailListVo dbOne = dbMap.get(retain.getUniqueTag());
                    //if(retain.getRequisitionMasterId()==null){
                       // retain.setRequisitionMasterId(requisitionMasterId);
                   // }
                    //if (dbOne != null && dbOne.getId() != null) {
                   //     retain.setId(dbOne.getId());
                     //   retain.setOwner(dbOne.getOwner());
                    //} else {
                     //   log.error("注意,没有拿到领用清单中的数据,这是不太正常的:retain:{},uniqueTag://{}", JSON.toJSONString(retain), retain.getUniqueTag());
                   // }
                //});
        List<CourseAssociatedUser> retainCourseAssociatedUserList = courseAssociatedUserList.stream().filter(associatedUser -> retainUserIdList.contains(associatedUser.getUserId())).collect(Collectors.toList());

        List<CourseAssociatedUser> retainChangeLearningRequirementCourseAssociatedUserList = retainCourseAssociatedUserList.stream().filter((Predicate<CourseAssociatedUser>) courseAssociatedUser -> {
            CourseAssociatedUser courseAssociatedUserDb = courseAssociatedUsersDbMap.get(courseAssociatedUser.getUserId());
            return courseAssociatedUserDb.getCourseId().equals(courseAssociatedUser.getCourseId()) &&
                    !courseAssociatedUserDb.getLearningRequirement().equals(courseAssociatedUser.getLearningRequirement());
        }).distinct().collect(Collectors.toList());


        if (CollectionUtils.isNotEmpty(retainChangeLearningRequirementCourseAssociatedUserList)) {
            log.info("找到只改变必修选修的数据, retainChangeLearningRequirementCourseAssociatedUserList:{}", JSON.toJSONString(retainChangeLearningRequirementCourseAssociatedUserList));
            //执行update
            retainChangeLearningRequirementCourseAssociatedUserList.forEach(change -> {
                CourseAssociatedUser db = courseAssociatedUsersDbMap.get(change.getUserId());
                if (db != null) {
                    change.setId(db.getId());
                    courseAssociatedUserMapper.updateByPrimaryKeySelective(change);
                }
            });
        }
    }

    if (CollectionUtils.isNotEmpty(deleteCourseUserIdList)) {
        //从这里找到删除的
        List<CourseAssociatedUser> delAddCourseUserList = courseAssociatedUsersDb.stream().filter(associatedUser -> deleteCourseUserIdList.contains(associatedUser.getUserId())).collect(Collectors.toList());
        Example example = new Example(CourseAssociatedUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("containerId", containerId);
        criteria.andEqualTo("courseId", courseId);
        criteria.andIn("userId", deleteCourseUserIdList);
        //BaoXianFuWu-4835-admin_51 ,唯一索引
        //criteria.andNotEqualTo("learningRequirement", Constants.LEARNING_REQUIREMENT_SELF_CHOOSE);
        courseAssociatedUserMapper.deleteByExample(example);
        log.info("删除课程关联人员成功,containerId:{},courseId:{},deleteCourseUserIdList:{}", containerId, courseId, deleteCourseUserIdList);
        afterCommitExecutor.execute(() -> {
            //发减人通知
            sendDistributeCourseMsgByQiLiao(course, containerId, Constants.COURSE_DISTRIBUTE_ACTION_DEL_USER, delAddCourseUserList);
        });
    }

    if (CollectionUtils.isNotEmpty(newAddCourseUserIdList)) {
        //从这里找到新增的
        List<CourseAssociatedUser> newAddCourseUserList = courseAssociatedUserList.stream().filter(associatedUser -> newAddCourseUserIdList.contains(associatedUser.getUserId())).collect(Collectors.toList());
        courseAssociatedUserMapper.batchInsert(newAddCourseUserList);
        afterCommitExecutor.execute(() -> {
            //发加人通知
            sendDistributeCourseMsgByQiLiao(course, containerId, Constants.COURSE_DISTRIBUTE_ACTION_ADD_USER, newAddCourseUserList);
        });
    }

  }
正文到此结束
本文目录