From aad364bb323a1eaa0389ee5c6389bdc0ea7ed526 Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: Mon, 29 Jun 2026 17:09:28 +0800
Subject: [PATCH] 修改

---
 hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java |  151 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 147 insertions(+), 4 deletions(-)

diff --git a/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java b/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java
index 2d23246..aaf04b0 100644
--- a/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java
+++ b/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java
@@ -2,18 +2,18 @@
 
 import cn.hutool.core.codec.Base64;
 import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.gkhy.hazmat.common.annotation.DataScope;
 import com.gkhy.hazmat.common.api.CommonPage;
+import com.gkhy.hazmat.common.api.CommonResult;
 import com.gkhy.hazmat.common.constant.CacheConstant;
 import com.gkhy.hazmat.common.constant.UserConstant;
 import com.gkhy.hazmat.common.domain.entity.SysUser;
+import com.gkhy.hazmat.common.enums.UserStatus;
 import com.gkhy.hazmat.common.enums.UserTypeEnum;
 import com.gkhy.hazmat.common.exception.ApiException;
-import com.gkhy.hazmat.common.utils.PageUtils;
-import com.gkhy.hazmat.common.utils.RedisUtils;
-import com.gkhy.hazmat.common.utils.SecurityUtils;
-import com.gkhy.hazmat.common.utils.StringUtils;
+import com.gkhy.hazmat.common.utils.*;
 import com.gkhy.hazmat.system.domain.SysUserRole;
 import com.gkhy.hazmat.system.mapper.SysUserMapper;
 import com.gkhy.hazmat.system.mapper.SysUserRoleMapper;
@@ -21,8 +21,10 @@
 import com.gkhy.hazmat.system.service.SysUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.Validator;
+import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
@@ -47,6 +49,8 @@
 
     @Autowired
     private SysUserRoleMapper roleMapper;
+
+
 
     @Override
     public CommonPage<SysUser> selectUserList(SysUser user) {
@@ -114,6 +118,10 @@
         user.setCreateBy(SecurityUtils.getUsername());
         user.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
         int row=baseMapper.insert(user);
+        SysUserRole ur = new SysUserRole();
+        ur.setUserId(user.getId());
+        ur.setRoleId(100L);
+        roleMapper.insert(ur);
         if(row<1){
             throw new ApiException("新增用户失败");
         }
@@ -256,6 +264,141 @@
         insertUserRole(userId, roleIds);
     }
 
+    @Override
+    public Integer importUserExcel(MultipartFile file) throws IOException {
+        if(ObjectUtil.isEmpty(file)){
+            throw new ApiException("上传文件不能为空");
+        }
+
+        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
+
+        List<Map<Integer, String>> dataList = EasyExcel.read(file.getInputStream())
+                .sheet()
+                .doReadSync();
+
+        if(dataList == null || dataList.isEmpty()){
+            throw new ApiException("导入数据不能为空");
+        }
+
+        List<SysUser> userList = new ArrayList<>();
+        int rowIndex = 1;
+
+        for(Map<Integer, String> data : dataList){
+            rowIndex++;
+
+            String username = data.get(0);
+            String name = data.get(1);
+            String sexStr = data.get(2);
+            String password = data.get(3);
+            String phone = data.get(4);
+
+            validateUserData(username, name, sexStr, password, phone, rowIndex);
+
+            if(!checkUsernameUnique(new SysUser().setUsername(username))){
+                throw new ApiException("第" + rowIndex + "行登录账号[" + username + "]已存在");
+            }
+
+            if(!checkPhoneUnique(new SysUser().setPhone(phone))){
+                throw new ApiException("第" + rowIndex + "行手机号[" + phone + "]已存在");
+            }
+
+            SysUser user = new SysUser();
+            user.setUsername(username.trim());
+            user.setName(name.trim());
+            user.setPhone(phone.trim());
+
+            Integer sex = parseSex(sexStr, rowIndex);
+            user.setSex(sex);
+
+            user.setUserType(UserTypeEnum.NORMAL_USER.getCode());
+            user.setStatus(UserStatus.OK.getCode());
+
+            if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())
+                    && !currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
+                user.setCompanyId(currentUser.getCompanyId());
+            }
+
+            String encryptedPassword = SecurityUtils.encryptPassword(password.trim());
+            user.setPassword(encryptedPassword);
+            user.setCreateBy(currentUser.getUsername());
+
+            userList.add(user);
+        }
+
+        if(!userList.isEmpty()){
+            if(userList.size() > 100){
+                int pageSize = 100;
+                while (true){
+                    List<SysUser> users = userList.subList(0, Math.min(userList.size(), pageSize));
+                    saveBatch(users);
+
+                    for(SysUser savedUser : users){
+                        SysUserRole ur = new SysUserRole();
+                        ur.setUserId(savedUser.getId());
+                        ur.setRoleId(100L);
+                        roleMapper.insert(ur);
+                    }
+
+                    if(users.size() < pageSize){
+                        break;
+                    }
+                    userList = userList.subList(pageSize, userList.size());
+                    if(userList.isEmpty()){
+                        break;
+                    }
+                }
+            }else{
+                saveBatch(userList);
+
+                for(SysUser savedUser : userList){
+                    SysUserRole ur = new SysUserRole();
+                    ur.setUserId(savedUser.getId());
+                    ur.setRoleId(100L);
+                    roleMapper.insert(ur);
+                }
+            }
+        }
+
+        return userList.size();
+    }
+
+    private void validateUserData(String username, String name, String sexStr, String password, String phone, int rowIndex){
+        if(StringUtils.isBlank(username)){
+            throw new ApiException("第" + rowIndex + "行登录账号为空");
+        }
+        if(StringUtils.isBlank(name)){
+            throw new ApiException("第" + rowIndex + "用户名称为空");
+        }
+        if(StringUtils.isBlank(password)){
+            throw new ApiException("第" + rowIndex + "行密码为空");
+        }
+        if(StringUtils.isBlank(phone)){
+            throw new ApiException("第" + rowIndex + "行手机号码为空");
+        }
+
+        if(phone.length() != 11){
+            throw new ApiException("第" + rowIndex + "行手机号长度不正确");
+        }
+        if(!phone.matches("^[1][3,4,5,6,7,8,9][0-9]{9}$")){
+            throw new ApiException("第" + rowIndex + "行手机号码格式有误");
+        }
+    }
+    private Integer parseSex(String sexStr, int rowIndex){
+        if(StringUtils.isBlank(sexStr)){
+            return 2;
+        }
+
+        String trimmedSex = sexStr.trim();
+        if("男".equals(trimmedSex) || "0".equals(trimmedSex)){
+            return 0;
+        }else if("女".equals(trimmedSex) || "1".equals(trimmedSex)){
+            return 1;
+        }else{
+            return 2;
+        }
+    }
+
+
     /**
      * 新增用户角色信息
      *

--
Gitblit v1.9.2