Explorar o código

用户导入功能

lizhenhao %!s(int64=2) %!d(string=hai) anos
pai
achega
73a7464622

+ 67 - 13
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/controller/UserController.java

@@ -3,6 +3,7 @@
  */
 package com.jeeplus.sys.controller;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -21,7 +22,11 @@ import com.jeeplus.core.excel.utils.EasyPoiUtil;
 import com.jeeplus.core.query.QueryWrapperGenerator;
 import com.jeeplus.security.util.SecurityUtils;
 import com.jeeplus.sys.domain.User;
+import com.jeeplus.sys.service.OfficeService;
+import com.jeeplus.sys.service.RoleService;
 import com.jeeplus.sys.service.UserService;
+import com.jeeplus.sys.service.dto.OfficeDTO;
+import com.jeeplus.sys.service.dto.RoleDTO;
 import com.jeeplus.sys.service.dto.UserDTO;
 import com.jeeplus.sys.service.mapstruct.UserWrapper;
 import com.jeeplus.sys.utils.*;
@@ -65,6 +70,12 @@ public class UserController {
     @Autowired
     protected Validator validator;
 
+    @Autowired
+    private OfficeService officeService;
+
+    @Autowired
+    private RoleService roleService;
+
     /**
      * 根据id查询用户
      *
@@ -199,6 +210,9 @@ public class UserController {
         EasyPoiUtil.exportExcel ( result, "用户数据",  options.getSheetName ( ), UserDTO.class, fileName, response );
     }
 
+    private int successNum;
+    private int failureNum;
+
     /**
      * 导入用户数据
      *
@@ -210,37 +224,66 @@ public class UserController {
     @ApiOperation(value = "导入用户excel")
     public ResponseEntity importFile(MultipartFile file) {
         try {
-            int successNum = 0;
-            int failureNum = 0;
+            this.successNum = 0;
+            this.failureNum = 0;
             StringBuilder failureMsg = new StringBuilder ( );
-            List <UserDTO> list = EasyPoiUtil.importExcel ( file, 1, 1, UserDTO.class );
+            List <UserDTO> list = EasyPoiUtil.importExcel ( file, 1, 2, UserDTO.class );
             for (UserDTO user : list) {
                 try {
-                    if ( isCheckLoginName ( "", user.getLoginName ( ) ) ) {
+                    if ( isCheckLoginNameNoCache ( "", user.getLoginName ( ) ) ) {
                         user.setPassword ( SecurityUtils.encryptPassword ( "123456" ) );
                         BeanValidators.validateWithException ( validator, user );
-                        userService.save ( UserWrapper.INSTANCE.toEntity ( user ) );
-                        successNum++;
+                        //查询人员公司信息
+                        OfficeDTO company = officeService.getOfficeByName(user.getCompanyDTO().getName(),"");
+                        if(ObjectUtil.isNotEmpty(company)){
+                            user.getCompanyDTO().setId(company.getId());
+                        }
+                        //查询人员部门信息
+                        OfficeDTO office = officeService.getOfficeByName(user.getOfficeDTO().getName(),user.getOfficeDTO().getParentName());
+                        if(ObjectUtil.isNotEmpty(office)){
+                            user.getOfficeDTO().setId(office.getId());
+                        }
+                        user.setLoginFlag("1");
+                        User user1 = UserWrapper.INSTANCE.toEntity(user);
+                        user1.setCompanyId(company.getId());
+                        user1.setOfficeId(office.getId());
+                        userService.save(user1);
+                        //人员角色添加
+                        List<RoleDTO> roleDTOList = user.getRoleDTOList();
+                        if(roleDTOList.size()!=0){
+                            if(StrUtil.isNotEmpty(roleDTOList.get(0).getName())){
+                                RoleDTO role = roleService.getRoleByName(user.getRoleDTOList().get(0).getName());
+                                if(ObjectUtil.isNotEmpty(role)){
+                                    roleDTOList.get(0).setId(role.getId());
+                                    userService.saveUserRole(user1.getId(),role.getId());
+                                }
+                            }
+                        }
+
+                        UserDTO userDTO = UserWrapper.INSTANCE.toDTO(user1);
+                        userDTO.setRoleDTOList(roleDTOList);
+                        userService.saveOrUpdate(userDTO);
+                        this.successNum++;
                     } else {
                         failureMsg.append ( "<br/>登录名 " + user.getLoginName ( ) + " 已存在; " );
-                        failureNum++;
+                        this.failureNum++;
                     }
                 } catch (ConstraintViolationException ex) {
                     failureMsg.append ( "<br/>登录名 " + user.getLoginName ( ) + " 导入失败:" );
                     List <String> messageList = BeanValidators.extractPropertyAndMessageAsList ( ex, ": " );
                     for (String message : messageList) {
                         failureMsg.append ( message + "; " );
-                        failureNum++;
+                        this.failureNum++;
                     }
                 } catch (Exception ex) {
-                    failureNum++;
+                    this.failureNum++;
                     failureMsg.append ( "<br/>登录名 " + user.getLoginName ( ) + " 导入失败:" + ex.getMessage ( ) );
                 }
             }
-            if ( failureNum > 0 ) {
-                failureMsg.insert ( 0, ",失败 " + failureNum + " 条用户,导入信息如下:" );
+            if ( this.failureNum > 0 ) {
+                failureMsg.insert ( 0, ",失败 " + this.failureNum + " 条用户,导入信息如下:" );
             }
-            return ResponseEntity.ok ( "已成功导入 " + successNum + " 条用户" + failureMsg );
+            return ResponseEntity.ok ( "已成功导入 " + this.successNum + " 条用户" + failureMsg );
         } catch (Exception e) {
             return ResponseEntity.badRequest().body ( "导入用户失败!失败信息:" + e.getMessage ( ) );
         }
@@ -264,9 +307,20 @@ public class UserController {
 
 
     private boolean isCheckLoginName(String oldLoginName, String loginName) {
+        UserDTO userByLoginName = userService.getUserByLoginName(loginName);
+        if ( loginName != null && loginName.equals ( oldLoginName ) ) {
+            return true;
+        } else if ( loginName != null && userByLoginName == null ) {
+            return true;
+        }
+        return false;
+    }
+
+    private boolean isCheckLoginNameNoCache(String oldLoginName, String loginName) {
+        UserDTO userByLoginName = userService.getUserByLoginNameNoCache(loginName);
         if ( loginName != null && loginName.equals ( oldLoginName ) ) {
             return true;
-        } else if ( loginName != null && userService.getUserByLoginName ( loginName ) == null ) {
+        } else if ( loginName != null && userByLoginName == null ) {
             return true;
         }
         return false;

+ 4 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/mapper/OfficeMapper.java

@@ -5,6 +5,8 @@ package com.jeeplus.sys.mapper;
 
 import com.jeeplus.core.domain.TreeMapper;
 import com.jeeplus.sys.domain.Office;
+import com.jeeplus.sys.service.dto.OfficeDTO;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 机构MAPPER接口
@@ -14,4 +16,6 @@ import com.jeeplus.sys.domain.Office;
  */
 public interface OfficeMapper extends TreeMapper<Office> {
 
+    OfficeDTO getOfficeByName(@Param("officeName") String officeName, @Param("parentName") String parentName);
+
 }

+ 39 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/mapper/xml/OfficeMapper.xml

@@ -2,4 +2,43 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.jeeplus.sys.mapper.OfficeMapper">
 
+    <sql id="officeColumns">
+    	a.id as "id",
+        a.parent_id as "parent.id",
+        a.parent_ids as "parentIds",
+        a.name as "name",
+        a.sort as "sort",
+        a.area_id as "areaId",
+        a.code as "code",
+        a.type as "type",
+        a.grade as "grade",
+        a.address as "address",
+        a.zip_code as "zipCode",
+        a.master as "master",
+        a.phone as "phone",
+        a.fax as "fax",
+        a.email as "email",
+        a.useable as "useable",
+        a.primary_person as "primaryPerson",
+        a.deputy_person as "deputyPerson",
+		a.create_by as "createBy.id",
+		a.create_date as "createDate",
+		a.update_by as "updateBy.id",
+		a.update_date as "updateDate",
+        a.remarks as "remarks",
+        a.del_flag as "delFlag"
+    </sql>
+
+    <select id="getOfficeByName" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
+        select
+        <include refid="officeColumns"/>
+        from sys_office a
+        <where>
+            a.name = #{officeName}
+            <if test="null != parentName and '' != parentName">
+                ${parentName}
+            </if>
+            and a.del_flag = 0
+        </where>
+    </select>
 </mapper>

+ 30 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/service/OfficeService.java

@@ -3,6 +3,7 @@
  */
 package com.jeeplus.sys.service;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.jeeplus.core.service.TreeService;
@@ -13,6 +14,7 @@ import com.jeeplus.sys.mapper.OfficeMapper;
 import com.jeeplus.sys.service.dto.OfficeDTO;
 import com.jeeplus.sys.service.mapstruct.OfficeWrapper;
 import org.apache.commons.lang.StringUtils;
+import org.checkerframework.checker.units.qual.A;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -32,6 +34,9 @@ public class OfficeService extends TreeService<OfficeMapper, Office> {
     @Autowired
     private OfficeWrapper officeWrapper;
 
+    @Autowired
+    private OfficeMapper officeMapper;
+
     public List <OfficeDTO> getRootTree(List<OfficeDTO> list, String extId, String type, String showAll) {
         List<OfficeDTO> offices = Lists.newArrayList ();
         List<OfficeDTO> rootTrees = officeWrapper.toDTO (super.getChildren (new Office (OfficeDTO.getRootId ())));
@@ -111,6 +116,31 @@ public class OfficeService extends TreeService<OfficeMapper, Office> {
                 &&(CommonConstants.YES.equals ( showAll ) || CommonConstants.YES.equals (dto.getUseable ()));
     }
 
+    /**
+     * 根据部门名称查询部门信息
+     * @param officeName
+     * @return
+     */
+    public OfficeDTO getOfficeByName(String officeName,String parentName){
+        if(StringUtils.isBlank(parentName)){
+            parentName = "";
+        }
+        String parentIdArray[] =parentName.split(",");
+        StringBuilder parentSql = new StringBuilder();
+        for (int i = 0 ; i < parentIdArray.length; i++){
+            if(StringUtils.isNotBlank(parentIdArray[i])){
+                parentSql.append(" AND parent_id = (select id from sys_office where name = '").append(parentIdArray[i]).append("' AND del_flag = 0");
+            }
+        }
+        for (int i = 0 ; i < parentIdArray.length; i++){
+            if(StringUtils.isNotBlank(parentIdArray[i])){
+                parentSql.append(")");
+            }
+        }
+        OfficeDTO office = officeMapper.getOfficeByName(officeName,parentSql.toString());
+        return office;
+    }
+
 
 
 }

+ 16 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/service/UserService.java

@@ -98,6 +98,17 @@ public class UserService  extends ServiceImpl<UserMapper, User> {
 	}
 
 	/**
+	 * 根据登录名获取用户,不使用缓存
+	 * @param loginName
+	 * @return
+	 */
+	public UserDTO getUserByLoginNameNoCache(String loginName) {
+		QueryWrapper queryWrapper = new QueryWrapper ();
+		queryWrapper.eq ("a.login_name", loginName);
+		return baseMapper.get (queryWrapper);
+	}
+
+	/**
 	 * 根据登录名获取用户id
 	 * @param loginName
 	 * @return
@@ -168,6 +179,11 @@ public class UserService  extends ServiceImpl<UserMapper, User> {
 
 	}
 
+	public void saveUserRole(String userId,String roleId){
+		if(StrUtil.isNotEmpty(userId)&&StrUtil.isNotEmpty(roleId)){
+			baseMapper.insertUserRole ( userId, roleId );
+		}
+	}
 
 	/**
 	 * 保存或者更新用户

+ 95 - 0
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/service/dto/OfficeDTO.java

@@ -5,12 +5,17 @@ package com.jeeplus.sys.service.dto;
 
 import cn.afterturn.easypoi.excel.annotation.Excel;
 import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.jeeplus.core.query.Query;
 import com.jeeplus.core.service.dto.TreeDTO;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import org.hibernate.validator.constraints.Length;
 
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -104,4 +109,94 @@ public class OfficeDTO extends TreeDTO <OfficeDTO> {
 	 */
 	private boolean disabled = false;
 
+	/**
+	 * 登录名
+	 */
+	@Length(min = 1, max = 100)
+	@Excel (name = "登录名")
+	@Query
+	private String loginName;
+
+	/**
+	 * 父级部门
+	 */
+	@Length(min = 1, max = 100)
+	@Excel (name = "父级部门")
+	@Query
+	private String parentName;
+
+	/**
+	 * 密码
+	 */
+	@JsonIgnore
+	@Length(min = 1, max = 100)
+	private String password;
+
+
+	/**
+	 * 工号
+	 */
+	@Length(min = 1, max = 100)
+	@Excel (name = "工号")
+	private String no;
+
+	/**
+	 * 手机
+	 */
+	@Length(min = 0, max = 100)
+	@Excel(name = "手机")
+	private String mobile;
+
+	/**
+	 * 最后登录IP
+	 */
+	@ApiModelProperty(hidden = true)
+	private String loginIp;
+
+	/**
+	 * 最后登录日期
+	 */
+	@ApiModelProperty(hidden = true)
+	private Date loginDate;
+
+	/**
+	 * 是否允许登录
+	 */
+	private String loginFlag;
+
+	/**
+	 * 头像
+	 */
+	private String photo;
+
+	/**
+	 * 二维码
+	 */
+	private String qrCode;
+
+	/**
+	 * 原登录名
+	 */
+	private String oldLoginName;
+
+	/**
+	 * 新密码
+	 */
+	private String newPassword;
+
+	/**
+	 * 签名
+	 */
+	private String sign;
+
+	/**
+	 * 超级管理员标志
+	 */
+	private boolean isAdmin;
+
+	/**
+	 * 是否可用
+	 */
+	private boolean typeFlag = false;
+
 }

+ 1 - 1
jeeplus-platform/jeeplus-admin/src/main/java/com/jeeplus/sys/service/dto/UserDTO.java

@@ -31,7 +31,7 @@ import java.util.stream.Collectors;
  */
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class UserDTO extends BaseDTO {
+public class  UserDTO extends BaseDTO {
 
     private static final long serialVersionUID = 1L;