场景
公司有十个人,有可能离职还有新来员工,工作任务需要在职员工轮循去工作,有的工作需要几个人去完成
/*** 获取审批人员* @param userIdList 全部的员工* @param userRoleType 角色类型 (业务需要) * @param userType 人员类型 (业务需要) * @param shType 审核类型(业务需要) * @param num 需要返回几个人去工作* @return*/public List<String> getUserIdList(List<String> userIdList, String userRoleType, String userType, String shType,int num) {//全部已经干活人员List<YwRoundUser> list = this.baseMapper.selectList(new LambdaQueryWrapper<YwRoundUser>().eq(YwRoundUser::getUserRoleType, userRoleType).eq(YwRoundUser::getUserType, userType).eq(YwRoundUser::getShType, shType).eq(YwRoundUser::getDelFlag, YesNo.NO.getValue()));List<String> idList = list.stream().map(YwRoundUser::getUserId).collect(Collectors.toList());//求差集 需要插入的(新入职的)Set<String> difference = userIdList.stream().filter(e -> !idList.contains(e)).collect(Collectors.toSet());//求差集 需要删除的(离职的)Set<String> difference2 = idList.stream().filter(e -> !userIdList.contains(e)).collect(Collectors.toSet());//删除if(difference2.size()!=0){YwRoundUser user = new YwRoundUser();user.setDelFlag("1");this.baseMapper.update(user,new LambdaQueryWrapper<YwRoundUser>().in(YwRoundUser::getUserId,difference2));}//需要增加的for (String userId : difference){YwRoundUser ywRoundUser = new YwRoundUser();ywRoundUser.setTimeStamp(BigDecimal.valueOf(System.currentTimeMillis()));ywRoundUser.setUserId(userId);ywRoundUser.setUserType(userType);ywRoundUser.setUserRoleType(userRoleType);ywRoundUser.setDelFlag(YesNo.NO.getValue());ywRoundUser.setShType(shType);this.baseMapper.insert(ywRoundUser);}//按照时间戳查询需要的个数List<YwRoundUser> resultList = this.baseMapper.selectList(new LambdaQueryWrapper<YwRoundUser>().eq(YwRoundUser::getUserRoleType, userRoleType).eq(YwRoundUser::getUserType, userType).eq(YwRoundUser::getShType, shType).eq(YwRoundUser::getDelFlag, YesNo.NO.getValue()).orderByAsc(YwRoundUser::getTimeStamp).last(" limit "+ num));//获取IdList<String> resultIdList = resultList.stream().map(YwRoundUser::getUserId).collect(Collectors.toList());//更新选中人的时间戳for (YwRoundUser user : resultList){user.setTimeStamp(BigDecimal.valueOf(System.currentTimeMillis()));this.baseMapper.updateById(user);}return resultIdList;}