|
|
@@ -1,10 +1,13 @@
|
|
|
package org.springblade.system.user.service.impl;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.read.metadata.ReadSheet;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.nacos.common.utils.StringUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
@@ -35,6 +38,7 @@ import org.springblade.core.tool.utils.*;
|
|
|
import org.springblade.manager.dto.SaveUserInfoByProjectDTO;
|
|
|
import org.springblade.manager.entity.*;
|
|
|
import org.springblade.manager.feign.ContractClient;
|
|
|
+import org.springblade.manager.feign.ProjectAssignmentUserClient;
|
|
|
import org.springblade.manager.vo.PrivateTreeVO;
|
|
|
import org.springblade.manager.vo.PrivateTreeVO2;
|
|
|
import org.springblade.manager.vo.WbsTreeContractLazyQueryInfoVO;
|
|
|
@@ -45,12 +49,16 @@ import org.springblade.system.cache.DictCache;
|
|
|
import org.springblade.system.cache.ParamCache;
|
|
|
import org.springblade.system.cache.SysCache;
|
|
|
import org.springblade.system.entity.Dept;
|
|
|
+import org.springblade.system.entity.Role;
|
|
|
import org.springblade.system.entity.Tenant;
|
|
|
import org.springblade.system.enums.DictEnum;
|
|
|
import org.springblade.system.feign.ISysClient;
|
|
|
import org.springblade.system.user.bean.NodeVO;
|
|
|
import org.springblade.system.user.cache.UserCache;
|
|
|
+import org.springblade.system.user.dto.ExcelUserTemp;
|
|
|
+import org.springblade.system.user.dto.ImportUserLog;
|
|
|
import org.springblade.system.user.dto.UserDTO;
|
|
|
+import org.springblade.system.user.dto.UserImporterNew;
|
|
|
import org.springblade.system.user.entity.*;
|
|
|
import org.springblade.system.user.enums.UserEnum;
|
|
|
import org.springblade.system.user.excel.UserExcel;
|
|
|
@@ -75,9 +83,14 @@ import org.springframework.jdbc.core.SingleColumnRowMapper;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
@@ -85,6 +98,7 @@ import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
import java.util.function.Function;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
@@ -2366,4 +2380,662 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
|
|
|
return R.data(information);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public R importUserNew(MultipartFile file) {
|
|
|
+ List<UserImporterNew> finalUserList = new ArrayList<>();
|
|
|
+ List<ImportUserLog>logs=new ArrayList<>();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ String importTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ Long importId= SnowFlakeUtil.getId();
|
|
|
+ Long importUser=AuthUtil.getUserId();
|
|
|
+ List<User> insertUserList=new ArrayList<>();
|
|
|
+ List<UserDept> insertUserDeptList=new ArrayList<>();
|
|
|
+ List<SaveUserInfoByProjectDTO> insertUserInfoByProjectList=new ArrayList<>();
|
|
|
+ List<User>updateUserList=new ArrayList<>();
|
|
|
+ List<SaveUserInfoByProjectDTO>updateUserInfoByProjectList=new ArrayList<>();
|
|
|
+ try {
|
|
|
+ // 1. 获取所有sheet元数据
|
|
|
+ List<ReadSheet> allSheets = EasyExcel.read(file.getInputStream())
|
|
|
+ .head(ExcelUserTemp.class)
|
|
|
+ .build()
|
|
|
+ .excelExecutor()
|
|
|
+ .sheetList();
|
|
|
+
|
|
|
+ // 2. 遍历每个sheet处理
|
|
|
+ for (ReadSheet sheet : allSheets) {
|
|
|
+ System.out.println("正在读取sheet:" + sheet.getSheetName() + "(索引:" + sheet.getSheetNo() + ")");
|
|
|
+
|
|
|
+ // 读取当前sheet数据(从第3行开始,跳过前2行表头)
|
|
|
+ List<ExcelUserTemp> sheetData = EasyExcel.read(file.getInputStream())
|
|
|
+ .head(ExcelUserTemp.class)
|
|
|
+ .sheet(sheet.getSheetNo())
|
|
|
+ .headRowNumber(2)
|
|
|
+ .doReadSync();
|
|
|
+
|
|
|
+ // 3. 核心逻辑:收集同一用户的多个合同段,用逗号拼接
|
|
|
+ ExcelUserTemp currentUser = null; // 记录当前用户核心信息
|
|
|
+ List<String> contractList = new ArrayList<>(); // 收集当前用户的所有合同段
|
|
|
+
|
|
|
+ for (ExcelUserTemp row : sheetData) {
|
|
|
+ // 判断当前行是否是“新用户行”(核心字段有值)
|
|
|
+ if (!isAllCoreFieldsNull(row)) {
|
|
|
+ // 先处理上一个用户(若存在)
|
|
|
+ if (currentUser != null) {
|
|
|
+ generateSingleUserRecord(currentUser, contractList, finalUserList);
|
|
|
+ }
|
|
|
+ // 初始化新用户信息
|
|
|
+ currentUser = row;
|
|
|
+ contractList = new ArrayList<>();
|
|
|
+ // 收集当前行的合同段(首行可能带合同段)
|
|
|
+ addValidContract(row.getContractInfo(), contractList);
|
|
|
+ } else {
|
|
|
+ // 当前行是“合同段补充行”(核心字段空,仅合同段有值)
|
|
|
+ if (currentUser != null) {
|
|
|
+ addValidContract(row.getContractInfo(), contractList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 处理最后一个用户
|
|
|
+ if (currentUser != null) {
|
|
|
+ generateSingleUserRecord(currentUser, contractList, finalUserList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 5. 后续业务逻辑(批量保存等)
|
|
|
+ if (!finalUserList.isEmpty()) {
|
|
|
+ //查出所有的部门名
|
|
|
+ List<Dept>deptNames=jdbcTemplate.query("select id,dept_name from blade_dept where is_deleted=0",new BeanPropertyRowMapper<>(Dept.class));
|
|
|
+ //查出所有的角色名
|
|
|
+ List<Role>roleNamesSG=jdbcTemplate.query("select id,role_name from blade_role where parent_id=(select id from blade_role where role_name='施工方' and is_deleted=0 limit 1)",new BeanPropertyRowMapper<>(Role.class));
|
|
|
+ List<Role>roleNamesJL=jdbcTemplate.query("select id,role_name from blade_role where parent_id=(select id from blade_role where role_name='监理方' and is_deleted=0 limit 1)",new BeanPropertyRowMapper<>(Role.class));
|
|
|
+ List<Role>roleNamesYZ=jdbcTemplate.query("select id,role_name from blade_role where parent_id=(select id from blade_role where role_name='业主方' and is_deleted=0 limit 1)",new BeanPropertyRowMapper<>(Role.class));
|
|
|
+ Map<String,List<Role>> roleMap= new HashMap<>();
|
|
|
+ roleMap.put("3",roleNamesSG);
|
|
|
+ roleMap.put("2",roleNamesJL);
|
|
|
+ roleMap.put("1",roleNamesYZ);
|
|
|
+ //查出所有的项目的合同段名
|
|
|
+ // 查出所有的项目的合同段名
|
|
|
+ Set<String> projectIdData = finalUserList.stream()
|
|
|
+ .filter(userImporterNew -> !StringUtil.isEmpty(userImporterNew.getProjectId()))
|
|
|
+ .flatMap(userImporterNew -> Arrays.stream(userImporterNew.getProjectId().split(",")))
|
|
|
+ .map(String::trim)
|
|
|
+ .filter(projectId -> !projectId.isEmpty())
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ List<String>projectIds=contractClient.queryProjectIds(projectIdData);
|
|
|
+ //根据项目id查询合同段
|
|
|
+ Map<String,List<ContractInfo>> contractNamesMap=new HashMap<>();
|
|
|
+ if(!projectIds.isEmpty()){
|
|
|
+ for (String projectId : projectIds) {
|
|
|
+ List<ContractInfo>contractNames=contractClient.queryContractNamesListByProjectId(projectId);
|
|
|
+ contractNamesMap.put(projectId,contractNames);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (UserImporterNew userImporterNew : finalUserList) {
|
|
|
+ //首先 根据账号查询用户
|
|
|
+ //账号为null
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getAccount())){
|
|
|
+ //姓名不为null
|
|
|
+ if(!StringUtil.isEmpty(userImporterNew.getRealName())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),userImporterNew.getRealName()+"的账号为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }else {
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"账号姓名都为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ User user=this.getOne(new QueryWrapper<User>().lambda().eq(User::getAccount,userImporterNew.getAccount()));
|
|
|
+ //账号不存在 则新增
|
|
|
+ if(user==null){
|
|
|
+ //前置条件判断
|
|
|
+ /**
|
|
|
+ * 验证姓名
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getRealName())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"姓名为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证手机号
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getPhone())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"手机号为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ boolean b = isValidChinesePhoneNumber(userImporterNew.getPhone());
|
|
|
+ if(!b){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"手机号格式错误",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证身份证号
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getIdNumber())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"身份证号为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ boolean b = isValidIdCard(userImporterNew.getIdNumber());
|
|
|
+ if(!b){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"身份证号格式错误",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证用户平台
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getUserType())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"用户平台为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ String userType = userImporterNew.getUserType();
|
|
|
+ List<String> types = new ArrayList<>();
|
|
|
+ if (userType != null && !userType.isEmpty()) {
|
|
|
+ Pattern pattern = Pattern.compile("\\d+");
|
|
|
+ java.util.regex.Matcher matcher = pattern.matcher(userType);
|
|
|
+ while (matcher.find()) {
|
|
|
+ types.add(matcher.group());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断是否只包含1,2,3,7
|
|
|
+ Set<String> allowedTypes = new HashSet<>(Arrays.asList("1", "2", "3", "7"));
|
|
|
+ boolean isValid = types.stream().allMatch(allowedTypes::contains);
|
|
|
+ if(!isValid){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"用户平台格式错误",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ userImporterNew.setUserType(String.join(",", types));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证部门
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getDeptName())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属部门为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ Optional<Dept> dept = deptNames.stream().filter(deptName -> !StringUtil.isEmpty(deptName) && deptName.equals(userImporterNew.getDeptName())).findFirst();
|
|
|
+ if(!dept.isPresent()){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属部门不存在",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ userImporterNew.setDeptId(dept.get().getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果部门信息全都为null 就跳过验证
|
|
|
+ if(!(StringUtil.isEmpty(userImporterNew.getProjectId())&&StringUtil.isEmpty(userImporterNew.getContractInfo())&&StringUtil.isEmpty(userImporterNew.getOwner())&&StringUtil.isEmpty(userImporterNew.getRoleName()))){
|
|
|
+ /**
|
|
|
+ * 验证项目信息
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getProjectId())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属项目为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ if(!projectIds.contains(userImporterNew.getProjectId())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属项目Id没找到对应的项目",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证合同段
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getContractInfo())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"合同段名称/编号为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ StringBuilder contractId=new StringBuilder();
|
|
|
+ String contractInfo = userImporterNew.getContractInfo();
|
|
|
+ String[] contractInfoData = contractInfo.split(",");
|
|
|
+ //根据名称或编号来查找
|
|
|
+ List<ContractInfo> contractInfos = contractNamesMap.get(userImporterNew.getProjectId());
|
|
|
+ for (String info : contractInfoData) {
|
|
|
+ Optional<ContractInfo> nameOptional = contractInfos.stream().filter(c -> !StringUtil.isEmpty(c.getContractName()) && c.getContractName().equals(info)).findFirst();
|
|
|
+ Optional<ContractInfo> numberOptional = contractInfos.stream().filter(c -> !StringUtil.isEmpty(c.getContractNumber()) && c.getContractNumber().equals(info)).findFirst();
|
|
|
+ if(!(nameOptional.isPresent()||numberOptional.isPresent())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"合同段名称/编号不存在",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }else {
|
|
|
+ if(nameOptional.isPresent()){
|
|
|
+ contractId.append(",").append(nameOptional.get().getId());
|
|
|
+ }else {
|
|
|
+ contractId.append(",").append(numberOptional.get().getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userImporterNew.setContractId(contractId.substring(1));
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证所属方
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getOwner())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属方为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ String owner = userImporterNew.getOwner();
|
|
|
+ if(!(owner.equals("1")||owner.equals("2")||owner.equals("3"))){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属方格式错误",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证岗位
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getRoleName())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"角色为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }else {
|
|
|
+ List<Role> roles = roleMap.get(userImporterNew.getOwner());
|
|
|
+ Optional<Role> role = roles.stream().filter(r -> r.getRoleName().equals(userImporterNew.getRoleName())).findFirst();
|
|
|
+ if (role.isPresent()) {
|
|
|
+ userImporterNew.setRoleId(role.get().getId());
|
|
|
+ } else {
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"角色不存在",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //验证完毕 开始新增
|
|
|
+ /**
|
|
|
+ * 用户账号表
|
|
|
+ */
|
|
|
+ user.setId(SnowFlakeUtil.getId());
|
|
|
+ user.setTenantId(AuthUtil.getTenantId());
|
|
|
+ user.setAccount(userImporterNew.getAccount());
|
|
|
+ user.setUserType(userImporterNew.getUserType());
|
|
|
+ user.setPassword(DigestUtil.encrypt(userImporterNew.getPassword()));
|
|
|
+ user.setPlaintextPassword(userImporterNew.getPassword());
|
|
|
+ user.setName(userImporterNew.getRealName());
|
|
|
+ user.setRealName(userImporterNew.getRealName());
|
|
|
+ user.setIdNumber(userImporterNew.getIdNumber());
|
|
|
+ user.setPhone(userImporterNew.getPhone());
|
|
|
+ user.setRoleId(userImporterNew.getRoleId()+"");
|
|
|
+ user.setDeptId(userImporterNew.getDeptId()+"");
|
|
|
+ user.setStatus("是".equals(userImporterNew.getStatus()) ? 1 : 0);
|
|
|
+ user.setAccCode(userImporterNew.getAccCode());
|
|
|
+ insertUserList.add(user);
|
|
|
+ /**
|
|
|
+ * 用户部门表
|
|
|
+ */
|
|
|
+ UserDept userDept = new UserDept();
|
|
|
+ userDept.setUserId(user.getId());
|
|
|
+ userDept.setDeptId(userImporterNew.getDeptId());
|
|
|
+ insertUserDeptList.add(userDept);
|
|
|
+ /**
|
|
|
+ * 项目分配用户关系表
|
|
|
+ */
|
|
|
+ if(!StringUtil.isEmpty(userImporterNew.getProjectId())&&!StringUtil.isEmpty(userImporterNew.getContractId())){
|
|
|
+ String contractIds = userImporterNew.getContractId();
|
|
|
+ String[] contractList = contractIds.split(",");
|
|
|
+ for (String contractId : contractList) {
|
|
|
+ SaveUserInfoByProjectDTO dto = new SaveUserInfoByProjectDTO();
|
|
|
+ dto.setId(SnowFlakeUtil.getId());
|
|
|
+ dto.setProjectId(userImporterNew.getProjectId());
|
|
|
+ dto.setContractId(contractId);
|
|
|
+ dto.setRoleId(user.getRoleId());
|
|
|
+ dto.setUserId(user.getId()+"");
|
|
|
+ dto.setStatus(1);
|
|
|
+ insertUserInfoByProjectList.add(dto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //账号存在 则更新
|
|
|
+ //模板上姓名和数据库的姓名相同则修改
|
|
|
+ if(!StringUtil.isEmpty(userImporterNew.getRealName())){
|
|
|
+ if(user.getRealName().equals(userImporterNew.getRealName())){
|
|
|
+ //拿到项目和合同段
|
|
|
+ //如果部门信息全都为null 就跳过验证
|
|
|
+ if(!(StringUtil.isEmpty(userImporterNew.getProjectId())&&StringUtil.isEmpty(userImporterNew.getContractInfo())&&StringUtil.isEmpty(userImporterNew.getOwner())&&StringUtil.isEmpty(userImporterNew.getRoleName()))){
|
|
|
+ /**
|
|
|
+ * 验证项目信息
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getProjectId())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属项目为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ if(!projectIds.contains(userImporterNew.getProjectId())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属项目Id没找到对应的项目",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证合同段
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getContractInfo())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"合同段名称/编号为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ StringBuilder contractId=new StringBuilder();
|
|
|
+ String contractInfo = userImporterNew.getContractInfo();
|
|
|
+ String[] contractInfoData = contractInfo.split(",");
|
|
|
+ //根据名称或编号来查找
|
|
|
+ List<ContractInfo> contractInfos = contractNamesMap.get(userImporterNew.getProjectId());
|
|
|
+ for (String info : contractInfoData) {
|
|
|
+ Optional<ContractInfo> nameOptional = contractInfos.stream().filter(c -> !StringUtil.isEmpty(c.getContractName()) && c.getContractName().equals(info)).findFirst();
|
|
|
+ Optional<ContractInfo> numberOptional = contractInfos.stream().filter(c -> !StringUtil.isEmpty(c.getContractNumber()) && c.getContractNumber().equals(info)).findFirst();
|
|
|
+ if(!(nameOptional.isPresent()||numberOptional.isPresent())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"合同段名称/编号不存在",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }else {
|
|
|
+ if(nameOptional.isPresent()){
|
|
|
+ contractId.append(",").append(nameOptional.get().getId());
|
|
|
+ }else {
|
|
|
+ contractId.append(",").append(numberOptional.get().getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userImporterNew.setContractId(contractId.substring(1));
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证所属方
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getOwner())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属方为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ String owner = userImporterNew.getOwner();
|
|
|
+ if(!(owner.equals("1")||owner.equals("2")||owner.equals("3"))){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"所属方格式错误",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 验证岗位
|
|
|
+ */
|
|
|
+ if(StringUtil.isEmpty(userImporterNew.getRoleName())){
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"角色为空",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }else {
|
|
|
+ List<Role> roles = roleMap.get(userImporterNew.getOwner());
|
|
|
+ Optional<Role> role = roles.stream().filter(r -> r.getRoleName().equals(userImporterNew.getRoleName())).findFirst();
|
|
|
+ if (role.isPresent()) {
|
|
|
+ userImporterNew.setRoleId(role.get().getId());
|
|
|
+ } else {
|
|
|
+ ImportUserLog log=new ImportUserLog(SnowFlakeUtil.getId(),userImporterNew.getAccount(),userImporterNew.getRealName(),"角色不存在",importTime,importId,importUser,file.getOriginalFilename());
|
|
|
+ logs.add(log);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将当前的项目以及合同段做比较
|
|
|
+ //查出当前用户分配的项目和合同段
|
|
|
+ String sql="select * from m_project_assignment_user where user_id="+user.getId()+" and is_deleted=0 and project_id="+userImporterNew.getProjectId();
|
|
|
+ List<SaveUserInfoByProjectDTO> projectAssignmentUsers = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SaveUserInfoByProjectDTO.class));
|
|
|
+ //说明该项目是新增的
|
|
|
+ if(projectAssignmentUsers.isEmpty()){
|
|
|
+ user.setRoleId(user.getRoleId()+","+userImporterNew.getRoleId());
|
|
|
+ user.setStatus("是".equals(userImporterNew.getStatus()) ? 1 : 0);
|
|
|
+ updateUserList.add(user);
|
|
|
+ if(!StringUtil.isEmpty(userImporterNew.getProjectId())&&!StringUtil.isEmpty(userImporterNew.getContractId())){
|
|
|
+ String contractIds = userImporterNew.getContractId();
|
|
|
+ String[] contractList = contractIds.split(",");
|
|
|
+ for (String contractId : contractList) {
|
|
|
+ SaveUserInfoByProjectDTO dto = new SaveUserInfoByProjectDTO();
|
|
|
+ dto.setId(SnowFlakeUtil.getId());
|
|
|
+ dto.setProjectId(userImporterNew.getProjectId());
|
|
|
+ dto.setContractId(contractId);
|
|
|
+ dto.setRoleId(user.getRoleId());
|
|
|
+ dto.setUserId(user.getId()+"");
|
|
|
+ dto.setStatus(1);
|
|
|
+ insertUserInfoByProjectList.add(dto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //不是新增的项目 但是是新增的合同段
|
|
|
+
|
|
|
+ //不是新增的项目 但是新增了合同段其中的老合同段
|
|
|
+
|
|
|
+ //不是新增的项目 也没有新增的合同段
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return R.fail("导入失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.success("导入成功,共生成" + finalUserList.size() + "条有效记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验并添加有效合同段(过滤空值和纯空格)
|
|
|
+ */
|
|
|
+ private void addValidContract(String contract, List<String> contractList) {
|
|
|
+ if (Objects.nonNull(contract) && !contract.trim().isEmpty()) {
|
|
|
+ contractList.add(contract.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成单个用户记录(多个合同段用逗号拼接)
|
|
|
+ */
|
|
|
+ private void generateSingleUserRecord(ExcelUserTemp currentUser, List<String> contractList, List<UserImporterNew> finalList) {
|
|
|
+ // 处理默认值
|
|
|
+ String defaultPwd = Objects.isNull(currentUser.getPassword()) ? "user123456" : currentUser.getPassword();
|
|
|
+ String defaultStatus = Objects.isNull(currentUser.getStatus()) ? "是" : currentUser.getStatus();
|
|
|
+
|
|
|
+ // 拼接合同段(空列表则存空字符串)
|
|
|
+ String joinedContract = contractList.isEmpty() ? "" : String.join(",", contractList);
|
|
|
+
|
|
|
+ // 构建用户记录(仅一条)
|
|
|
+ UserImporterNew user = new UserImporterNew();
|
|
|
+ user.setAccount(currentUser.getAccount());
|
|
|
+ user.setPassword(defaultPwd);
|
|
|
+ user.setRealName(currentUser.getRealName());
|
|
|
+ user.setUserType(currentUser.getUserType());
|
|
|
+ user.setPhone(currentUser.getPhone());
|
|
|
+ user.setIdNumber(currentUser.getIdNumber());
|
|
|
+ user.setDeptName(currentUser.getDeptName());
|
|
|
+ user.setAccCode(currentUser.getAccCode());
|
|
|
+ user.setProjectId(currentUser.getProjectName());
|
|
|
+ user.setContractInfo(joinedContract); // 逗号拼接后的合同段
|
|
|
+ user.setOwner(currentUser.getOwner());
|
|
|
+ user.setRoleName(currentUser.getRoleName());
|
|
|
+ user.setStatus(defaultStatus);
|
|
|
+
|
|
|
+ finalList.add(user);
|
|
|
+ System.out.println("用户" + currentUser.getRealName() + "的合同段:" + joinedContract);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断核心字段是否全为空(识别新用户行)
|
|
|
+ */
|
|
|
+ private boolean isAllCoreFieldsNull(ExcelUserTemp temp) {
|
|
|
+ return (Objects.isNull(temp.getAccount()) || temp.getAccount().trim().isEmpty())
|
|
|
+ && (Objects.isNull(temp.getRealName()) || temp.getRealName().trim().isEmpty())
|
|
|
+ && (Objects.isNull(temp.getPhone()) || temp.getPhone().trim().isEmpty())
|
|
|
+ && (Objects.isNull(temp.getIdNumber()) || temp.getIdNumber().trim().isEmpty());
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 判断中国大陆手机号格式是否正确
|
|
|
+ *
|
|
|
+ * @param phoneNumber 手机号
|
|
|
+ * @return true表示格式正确,false表示格式错误
|
|
|
+ */
|
|
|
+ public static boolean isValidChinesePhoneNumber(String phoneNumber) {
|
|
|
+ if (phoneNumber == null || phoneNumber.length() != 11) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用正则表达式匹配手机号格式
|
|
|
+ String regex = "^1[3-9]\\d{9}$";
|
|
|
+ return phoneNumber.matches(regex);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断身份证格式是否正确
|
|
|
+ *
|
|
|
+ * @param idCard 身份证号码
|
|
|
+ * @return true表示格式正确,false表示格式错误
|
|
|
+ */
|
|
|
+ public static boolean isValidIdCard(String idCard) {
|
|
|
+ if (idCard == null || idCard.trim().isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ idCard = idCard.trim();
|
|
|
+
|
|
|
+ // 长度验证
|
|
|
+ if (idCard.length() == 15) {
|
|
|
+ return isValidIdCard15(idCard);
|
|
|
+ } else if (idCard.length() == 18) {
|
|
|
+ return isValidIdCard18(idCard);
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证15位身份证号码
|
|
|
+ *
|
|
|
+ * @param idCard 15位身份证号码
|
|
|
+ * @return true表示格式正确,false表示格式错误
|
|
|
+ */
|
|
|
+ private static boolean isValidIdCard15(String idCard) {
|
|
|
+ // 15位身份证号码为纯数字
|
|
|
+ if (!Pattern.matches("\\d{15}", idCard)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证出生日期
|
|
|
+ String birthDay = "19" + idCard.substring(6, 12); // 15位身份证年份默认为19XX年
|
|
|
+ return isValidDate(birthDay, "yyyyMMdd");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证18位身份证号码
|
|
|
+ *
|
|
|
+ * @param idCard 18位身份证号码
|
|
|
+ * @return true表示格式正确,false表示格式错误
|
|
|
+ */
|
|
|
+ private static boolean isValidIdCard18(String idCard) {
|
|
|
+ // 18位身份证号码前17位为数字,最后一位为数字或X
|
|
|
+ if (!Pattern.matches("\\d{17}[\\dXx]", idCard)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证地址码(简单验证前两位)
|
|
|
+ String areaCode = idCard.substring(0, 2);
|
|
|
+ if (!AREA_CODES.contains(areaCode)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证出生日期
|
|
|
+ String birthDay = idCard.substring(6, 14);
|
|
|
+ if (!isValidDate(birthDay, "yyyyMMdd")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证校验码
|
|
|
+ return checkIdCard18(idCard);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证日期格式是否正确
|
|
|
+ *
|
|
|
+ * @param date 日期字符串
|
|
|
+ * @param format 日期格式
|
|
|
+ * @return true表示日期格式正确,false表示日期格式错误
|
|
|
+ */
|
|
|
+ private static boolean isValidDate(String date, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ sdf.setLenient(false); // 严格模式,不允许不合理的日期
|
|
|
+ try {
|
|
|
+ sdf.parse(date);
|
|
|
+ // 检查年份范围(例如:1900年至今)
|
|
|
+ Calendar calendar = new GregorianCalendar();
|
|
|
+ calendar.setTime(sdf.parse(date));
|
|
|
+ int year = calendar.get(Calendar.YEAR);
|
|
|
+ int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
|
|
+ return year >= 1900 && year <= currentYear;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证18位身份证号码的校验码
|
|
|
+ *
|
|
|
+ * @param idCard 18位身份证号码
|
|
|
+ * @return true表示校验码正确,false表示校验码错误
|
|
|
+ */
|
|
|
+ private static boolean checkIdCard18(String idCard) {
|
|
|
+ char[] idCardArray = idCard.toCharArray();
|
|
|
+ int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
|
|
+ char[] validate = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
|
|
|
+ int sum = 0;
|
|
|
+
|
|
|
+ for (int i = 0; i < 17; i++) {
|
|
|
+ sum += (idCardArray[i] - '0') * weight[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ int mod = sum % 11;
|
|
|
+ char validateCode = validate[mod];
|
|
|
+
|
|
|
+ return Character.toUpperCase(idCardArray[17]) == validateCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 简化的省份代码列表(前两位)
|
|
|
+ private static final java.util.Set<String> AREA_CODES = new java.util.HashSet<>();
|
|
|
+ static {
|
|
|
+ AREA_CODES.add("11"); // 北京
|
|
|
+ AREA_CODES.add("12"); // 天津
|
|
|
+ AREA_CODES.add("13"); // 河北
|
|
|
+ AREA_CODES.add("14"); // 山西
|
|
|
+ AREA_CODES.add("15"); // 内蒙古
|
|
|
+ AREA_CODES.add("21"); // 辽宁
|
|
|
+ AREA_CODES.add("22"); // 吉林
|
|
|
+ AREA_CODES.add("23"); // 黑龙江
|
|
|
+ AREA_CODES.add("31"); // 上海
|
|
|
+ AREA_CODES.add("32"); // 江苏
|
|
|
+ AREA_CODES.add("33"); // 浙江
|
|
|
+ AREA_CODES.add("34"); // 安徽
|
|
|
+ AREA_CODES.add("35"); // 福建
|
|
|
+ AREA_CODES.add("36"); // 江西
|
|
|
+ AREA_CODES.add("37"); // 山东
|
|
|
+ AREA_CODES.add("41"); // 河南
|
|
|
+ AREA_CODES.add("42"); // 湖北
|
|
|
+ AREA_CODES.add("43"); // 湖南
|
|
|
+ AREA_CODES.add("44"); // 广东
|
|
|
+ AREA_CODES.add("45"); // 广西
|
|
|
+ AREA_CODES.add("46"); // 海南
|
|
|
+ AREA_CODES.add("50"); // 重庆
|
|
|
+ AREA_CODES.add("51"); // 四川
|
|
|
+ AREA_CODES.add("52"); // 贵州
|
|
|
+ AREA_CODES.add("53"); // 云南
|
|
|
+ AREA_CODES.add("54"); // 西藏
|
|
|
+ AREA_CODES.add("61"); // 陕西
|
|
|
+ AREA_CODES.add("62"); // 甘肃
|
|
|
+ AREA_CODES.add("63"); // 青海
|
|
|
+ AREA_CODES.add("64"); // 宁夏
|
|
|
+ AREA_CODES.add("65"); // 新疆
|
|
|
+ AREA_CODES.add("71"); // 台湾
|
|
|
+ AREA_CODES.add("81"); // 香港
|
|
|
+ AREA_CODES.add("82"); // 澳门
|
|
|
+ AREA_CODES.add("91"); // 国外
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|