“djh”
2 days ago aad364bb323a1eaa0389ee5c6389bdc0ea7ed526
修改
6 files modified
179 ■■■■■ changed files
hazmat-admin/src/main/java/com/gkhy/hazmat/admin/controller/system/SysUserController.java 13 ●●●●● patch | view | raw | blame | history
hazmat-admin/src/main/resources/application-prod.yml 8 ●●●● patch | view | raw | blame | history
hazmat-admin/src/main/resources/application.yml 4 ●●● patch | view | raw | blame | history
hazmat-common/src/main/java/com/gkhy/hazmat/common/domain/entity/SysUser.java 4 ●●●● patch | view | raw | blame | history
hazmat-system/src/main/java/com/gkhy/hazmat/system/service/SysUserService.java 5 ●●●●● patch | view | raw | blame | history
hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/SysUserServiceImpl.java 145 ●●●●● patch | view | raw | blame | history
hazmat-admin/src/main/java/com/gkhy/hazmat/admin/controller/system/SysUserController.java
@@ -18,7 +18,9 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -147,4 +149,15 @@
        return success(deptService.selectDeptTreeList(dept));
    }
    @RepeatSubmit
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "用户Excel批量导入")
    @PostMapping("/importExcel")
    public CommonResult importExcel(MultipartFile file) throws IOException {
        return CommonResult.success(sysUserService.importUserExcel(file));
    }
}
hazmat-admin/src/main/resources/application-prod.yml
@@ -5,9 +5,9 @@
    druid:
      # 主库数据源
      master:
        url: jdbc:mysql://127.0.0.1:23306/hazmat_manage_new?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
        username: hazmat_manage
        password: scrBiEFAPCF5HneK
        url: jdbc:mysql://127.0.0.1:7006/hazmat_manage_zkd?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
        username: root
        password: 2farwL3yPXfbH2AP
      # 从库数据源
      slave:
        enabled: false
@@ -41,7 +41,7 @@
        login-password: druid
  #redis 配置
  redis:
    database: 1
    database: 7
    host: 127.0.0.1
    port: 6379
    password: akj78avauba789a
hazmat-admin/src/main/resources/application.yml
@@ -15,7 +15,7 @@
      matching-strategy: ant_path_matcher
server:
  port: 8083
  port: 8005
  servlet:
    context-path: /api
  tomcat:
@@ -50,3 +50,5 @@
  # 匹配链接
  urlPatterns: /system/*,/manage/*
third-party:
  token: "c73da3ba-1cf4-4afa-bde9-b7769130d92d"
hazmat-common/src/main/java/com/gkhy/hazmat/common/domain/entity/SysUser.java
@@ -119,15 +119,19 @@
    /** 角色对象 */
    @TableField(exist = false)
    private List<SysRole> roles;
    /** 角色组 */
    @TableField(exist = false)
    private Long[] roleIds;
    /** 岗位组 */
    @TableField(exist = false)
    private Long[] postIds;
    /** 角色ID */
    @TableField(exist = false)
    private Long roleId;
    @JsonIgnore
hazmat-system/src/main/java/com/gkhy/hazmat/system/service/SysUserService.java
@@ -3,8 +3,11 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.api.CommonResult;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
@@ -127,4 +130,6 @@
    public List<SysUser> selectUnallocatedList(SysUser user);
    void insertUserAuth(Long userId, Long[] roleIds);
    Integer importUserExcel(MultipartFile file) throws IOException;
}
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;
@@ -262,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;
        }
    }
    /**
     * 新增用户角色信息
     *