|
@@ -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;
|