用户号已经存在是否覆盖解决办

@Transactional(rollbackFor = Exception.class)
    @Override
    public void importUser(List<ExcelEmployeeBasicInformation> data, Boolean isCovered) {
       log.info("员工基本信息导入data======="+data);
       if(Func.isNotEmpty(data)){
           for(int i=0;i<data.size();i++){
               //根据租户id和账号查询用户是否已经存在
               User oldUser = UserCache.getUser(data.get(i).getTenantId(), data.get(i).getAccount());
               boolean isExist=Func.isNotEmpty(oldUser);//用存在则为true
               //将实体类转换成map
               Map<String,Object>map= new HashMap<String,Object>();
               map=JSONObject.parseObject(JSONObject.toJSONString(data.get(i)),Map.class);
               //将map中的数据转换成员工基本信息表数据
               //EmployeeBasicInformation basicInfo= new EmployeeBasicInformation();
               EmployeeBasicInformation basicInfo=JSONObject.parseObject(JSONObject.toJSONString(map),EmployeeBasicInformation.class);
               //创建人名称
               basicInfo.setCreatorName(SecureUtil.getUserName());
               //创建人编号
               basicInfo.setCreatorCode(String.valueOf(AuthUtil.getUserId()));
               //创建部门名称
               basicInfo.setCreatorDeptName(deptUserEmployeeUtil.getDeptById(Long.valueOf(AuthUtil.getDeptId())).getDeptName());
               basicInfo.setTenantId(AuthUtil.getTenantId());
               //用户存在不覆盖
               if(isExist&&!isCovered){
                     continue;
               }
               if(isExist&&isCovered){//用户存在覆盖
                   basicInfo.setId(oldUser.getId());//根据已存在的用户id修改员工基本信息
                   saveOrUpdate(basicInfo);//修改
               }else if(!isExist){//用户不存在
                   saveOrUpdate(basicInfo);//保存用户信息
               }
               //将用户信息组装成用户信息的数据
               //User user=new User();
               User user=JSONObject.parseObject(JSONObject.toJSONString(map),User.class);
               user.setId(basicInfo.getId());
               //角色id 普通员工
               user.setRoleId(deptUserEmployeeUtil.getRoleId(AuthUtil.getTenantId(),"普通员工"));
               //职位id position
               user.setPostId(deptUserEmployeeUtil.getPostId(basicInfo.getPosition(),AuthUtil.getTenantId()));
               //入职部门id
               user.setDeptId(deptUserEmployeeUtil.getDeptId(AuthUtil.getTenantId(),basicInfo.getDepartment()));
               //租户id
               user.setTenantId(AuthUtil.getTenantId());
               //密码
               user.setPassword(EmployeeConstant.default_password);
               //创建人
               user.setCreateUser(basicInfo.getCreateUser());
               //创建部门
               user.setCreateDept(basicInfo.getCreateDept());
               //创建时间
               user.setCreateTime(basicInfo.getCreateTime());
               //状态值
               user.setStatus(basicInfo.getStatus());
               //是否删除
               user.setIsDeleted(basicInfo.getIsDeleted());
               if(isExist&&isCovered){//用户存在覆盖
                   userClient.updateUser(user);
               }else if(!isExist){//用户不存在
                   userClient.saveUser(user);//修改用户信息
               }
           }

       }

    }

//分解步骤实现

/**
     * 员工基本信息导入
     * @param data
     * @param isCovered
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importUser(List<ExcelEmployeeBasicInformation> data, Boolean isCovered) {
       log.info("员工基本信息导入data======="+data);
       if(Func.isNotEmpty(data)){
           //list的实体类转换成list map
           List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
           for(ExcelEmployeeBasicInformation excelInfo:data){
               Map<String,Object>map=new HashMap<String,Object>();
               map=JSONObject.parseObject(JSONObject.toJSONString(excelInfo),Map.class);
               list.add(map);
           }
           //将数据组装成员工基本信息表所需要的数据
           List<EmployeeBasicInformation> basicList=new ArrayList<EmployeeBasicInformation>();
           for(Map<String,Object>map1:list){
               EmployeeBasicInformation basicInfo=new EmployeeBasicInformation();
               basicInfo=JSONObject.parseObject(JSONObject.toJSONString(map1),EmployeeBasicInformation.class);
               //创建人名称
               basicInfo.setCreatorName(SecureUtil.getUserName());
               //创建人编号
               basicInfo.setCreatorCode(String.valueOf(AuthUtil.getUserId()));
               //创建部门名称
               basicInfo.setCreatorDeptName(deptUserEmployeeUtil.getDeptById(Long.valueOf(AuthUtil.getDeptId())).getDeptName());
               basicInfo.setTenantId(AuthUtil.getTenantId());
               saveOrUpdate(basicInfo);
               basicList.add(basicInfo);
           }
           log.info("将数据组装成员工基本信息表所需要的数据basicList====="+basicList);
           //将数据组装成用户所需要的数据进入用户表
           List<User>userList=new ArrayList<User>();
           for(Map<String,Object>map2:list){
                   User user=new User();
                user=JSONObject.parseObject(JSONObject.toJSONString(map2),User.class);

                userList.add(user);
           }
           //将用户信息需要的数据放入用户信息表
           for(int i=0;i<basicList.size();i++){
               userList.get(i).setId(basicList.get(i).getId());
               //角色id 普通员工
               userList.get(i).setRoleId(deptUserEmployeeUtil.getRoleId(AuthUtil.getTenantId(),"普通员工"));
               //职位id position
               userList.get(i).setPostId(deptUserEmployeeUtil.getPostId(basicList.get(i).getPosition(),AuthUtil.getTenantId()));
               //入职部门id
               userList.get(i).setDeptId(deptUserEmployeeUtil.getDeptId(AuthUtil.getTenantId(),basicList.get(i).getDepartment()));
               //租户id
               userList.get(i).setTenantId(AuthUtil.getTenantId());
               //密码
               userList.get(i).setPassword(EmployeeConstant.default_password);
               //创建人
               userList.get(i).setCreateUser(basicList.get(i).getCreateUser());
               //创建部门
               userList.get(i).setCreateDept(basicList.get(i).getCreateDept());
               //创建时间
               userList.get(i).setCreateTime(basicList.get(i).getCreateTime());
               //状态值
               userList.get(i).setStatus(basicList.get(i).getStatus());
               //是否删除
               userList.get(i).setIsDeleted(basicList.get(i).getIsDeleted());
               userClient.saveUser(userList.get(i));

           }
           log.info("将数据组装成用户所需要的数据进入用户表userList====="+userList);
       }

    }
原文地址:https://www.cnblogs.com/xianz666/p/13679145.html