Browse Source

优化批量添加用户提示信息。

pull/12/head
yangzhengze 7 years ago
parent
commit
e5684cb6d8
  1. 5
      src/main/java/com/ruoyi/project/system/user/controller/UserController.java
  2. 52
      src/main/java/com/ruoyi/project/system/user/service/UserServiceImpl.java
  3. 61
      src/main/resources/i18n/messages.properties
  4. BIN
      src/main/resources/static/template/用户导入模板.xlsx

5
src/main/java/com/ruoyi/project/system/user/controller/UserController.java

@ -1,5 +1,6 @@
package com.ruoyi.project.system.user.controller;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.web.controller.BaseController;
@ -186,8 +187,8 @@ public class UserController extends BaseController
return Message.success(String.valueOf(rows));
}
return Message.error();
}catch (Exception e){
log.error("批量添加用户失败 !", e);
}catch (UserException e){
log.error("批量添加用户失败 !---{}", e.getMessage());
return Message.error(e.getMessage());
}
}

52
src/main/java/com/ruoyi/project/system/user/service/UserServiceImpl.java

@ -210,23 +210,26 @@ public class UserServiceImpl implements IUserService
Workbook workbook=null;
//获取文件名
String filename=myFile.getOriginalFilename();
log.info("【ExeclfileName】{}",filename);
log.info("【ExeclfileName】={}",filename);
if(StringUtils.isNotEmpty(filename)){
//根据文件名判断文件是2003版本还是2007版本
if(ExcelImportUtils.isExcel2003(filename)){
try {
workbook=new HSSFWorkbook(myFile.getInputStream());//2003版本
}catch (IOException e){
log.error("获取Excel2003流错误"+e.getMessage());
throw new UserException("user.import.excel.fileinput.error",null);
}
}else if(ExcelImportUtils.isExcel2007(filename)){
try {
workbook=new XSSFWorkbook(myFile.getInputStream());//2007以上版本
}catch (IOException e){
log.error("获取Excel2007以上版本流错误"+e.getMessage());
throw new UserException("user.import.excel.fileinputx.error",null);
}
}else{
throw new UserException("1000",new Object[]{"文件不是Excel格式"});
throw new UserException("user.import.excel.filetype.error",null);
}
}else {
throw new UserException("user.import.excel.file.error",null);
}
//得到第一个sheet
Sheet sheet = workbook.getSheetAt(0);
@ -236,9 +239,9 @@ public class UserServiceImpl implements IUserService
//新建用户list
List<User> users=new ArrayList<User>();
List<Dept> depts;
List<Role> roles;
List<Post> posts;
List<Dept> depts=new ArrayList<Dept>();
List<Role> roles=new ArrayList<Role>();
List<Post> posts=new ArrayList<Post>();
//如果行数为空
/**
@ -247,8 +250,10 @@ public class UserServiceImpl implements IUserService
* 获取有记录的行数最后有数据的行是第n行前面有m行是空行没数据则返回n-m
*/
if((totalRows==0)&&(sheet.getPhysicalNumberOfRows()==0)){
throw new UserException("1001",new Object[]{"数据为空 请填写数据"});
}else{
throw new UserException("user.import.excel.null",null);
}else if((totalRows==0)&&(sheet.getPhysicalNumberOfRows()==1)){
throw new UserException("user.import.excel.data.null",null);
} else{
//获取全部部门信息
depts=deptService.selectDeptAll();
//获取全部角色信息
@ -270,7 +275,7 @@ public class UserServiceImpl implements IUserService
if(checkLoginNameUnique(userName).equals(UserConstants.USER_NAME_UNIQUE)){
user.setLoginName(userName);
}else {
log.error("【rows】{}行用户名已经存在",i+1);
log.error("【rows】{}行用户名{}已经存在",i+1,userName);
continue;
}
}
@ -290,8 +295,13 @@ public class UserServiceImpl implements IUserService
}
//密码
String passWord=ExcelImportUtils.getCellValue(row.getCell(3));
if(passWord.isEmpty()){
continue;
}else{
user.randomSalt();
user.setPassword(passwordService.encryptPassword(userName, passWord, user.getSalt()));
}
//部门
String dept=ExcelImportUtils.getCellValue(row.getCell(4));
if(StringUtils.isNotEmpty(dept)){
@ -308,7 +318,7 @@ public class UserServiceImpl implements IUserService
if(StringUtils.isNotEmpty(userRolesExcel)){
//Set可以去掉重复的值,
Set<Long> sets=new HashSet<Long>();
//判断是否有"," 号
//判断是否有英文的"," 号
if(userRolesExcel.contains(",")){
List<String> results= Arrays.asList(userRolesExcel.split(","));
for(String s:results){
@ -330,9 +340,9 @@ public class UserServiceImpl implements IUserService
}
for(Long longTes:sets){
log.info("username={},longTes={}",userName,longTes);
log.info("用户名={},角色ID={}",userName,longTes);
}
user.setRoleIds((Long[]) sets.toArray(new Long[sets.size()]));
user.setRoleIds(sets.toArray(new Long[sets.size()]));
}
//岗位--多个岗位以","分割
@ -340,7 +350,7 @@ public class UserServiceImpl implements IUserService
if(StringUtils.isNotEmpty(userPostExcel)){
//去掉重复的值,
Set<Long> setPosts=new HashSet<Long>();
//判断是否有"," 号
//判断是否有英文的"," 号
if(userPostExcel.contains(",")){
List<String> resultsp= Arrays.asList(userPostExcel.split(","));
for(String p:resultsp){
@ -363,9 +373,9 @@ public class UserServiceImpl implements IUserService
}
for(Long longTest:setPosts){
log.info("username={},longTest={}",userName,longTest);
log.info("用户名={},岗位ID={}",userName,longTest);
}
user.setPostIds((Long[]) setPosts.toArray(new Long[setPosts.size()]));
user.setPostIds(setPosts.toArray(new Long[setPosts.size()]));
}
//手机号
@ -394,7 +404,7 @@ public class UserServiceImpl implements IUserService
//批量插入用户
realRow=userMapper.batchAddUser(users);
}
System.out.println(realRow);
log.info("成功导入用户共{}个",realRow);
if(realRow>0){
//用户和角色关联
List<UserRole> userRoles=new ArrayList<UserRole>();
@ -407,7 +417,6 @@ public class UserServiceImpl implements IUserService
userRole.setUserId(test.getUserId());
userRole.setRoleId(test.getRoleIds()[q]);
userRoles.add(userRole);
}
for(int r=0;r<test.getPostIds().length;r++){
@ -420,12 +429,11 @@ public class UserServiceImpl implements IUserService
}
//批量添加用户-角色关联数据
userRoleMapper.batchUserRole(userRoles);
log.info("成功导入用户-角色关联数据");
//批量添加用户-岗位关联数据
userPostMapper.batchUserPost(userPosts);
;
log.info("成功导入用户-岗位关联数据");
}
return realRow;
}

61
src/main/resources/i18n/messages.properties

@ -1,31 +1,38 @@
#错误消息
not.null=* 必须填写
user.jcaptcha.error=验证码错误
user.not.exists=用户不存在/密码错误
user.password.not.match=用户不存在/密码错误
user.password.retry.limit.count=密码输入错误{0}次,{1}
user.password.retry.limit.exceed=密码输入错误{0}次,帐户锁定10分钟
user.blocked=用户已封禁,原因:{0}
role.blocked=角色已封禁,原因:{0}
user.logout.success=退出成功
#\u9519\u8BEF\u6D88\u606F
not.null=* \u5FC5\u987B\u586B\u5199
user.jcaptcha.error=\u9A8C\u8BC1\u7801\u9519\u8BEF
user.not.exists=\u7528\u6237\u4E0D\u5B58\u5728/\u5BC6\u7801\u9519\u8BEF
user.password.not.match=\u7528\u6237\u4E0D\u5B58\u5728/\u5BC6\u7801\u9519\u8BEF
user.password.retry.limit.count=\u5BC6\u7801\u8F93\u5165\u9519\u8BEF{0}\u6B21\uFF0C{1}
user.password.retry.limit.exceed=\u5BC6\u7801\u8F93\u5165\u9519\u8BEF{0}\u6B21\uFF0C\u5E10\u6237\u9501\u5B9A10\u5206\u949F
user.blocked=\u7528\u6237\u5DF2\u5C01\u7981\uFF0C\u539F\u56E0\uFF1A{0}
role.blocked=\u89D2\u8272\u5DF2\u5C01\u7981\uFF0C\u539F\u56E0\uFF1A{0}
user.logout.success=\u9000\u51FA\u6210\u529F
length.not.valid=长度必须在{min}到{max}个字符之间
length.not.valid=\u957F\u5EA6\u5FC5\u987B\u5728{min}\u5230{max}\u4E2A\u5B57\u7B26\u4E4B\u95F4
user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头
user.password.not.valid=* 5-50个字符
user.username.not.valid=* 2\u523020\u4E2A\u6C49\u5B57\u3001\u5B57\u6BCD\u3001\u6570\u5B57\u6216\u4E0B\u5212\u7EBF\u7EC4\u6210\uFF0C\u4E14\u5FC5\u987B\u4EE5\u975E\u6570\u5B57\u5F00\u5934
user.password.not.valid=* 5-50\u4E2A\u5B57\u7B26
user.email.not.valid=邮箱格式错误
user.mobile.phone.number.not.valid=手机号格式错误
user.login.success=登录成功
user.notfound=请重新登录
user.forcelogout=管理员强制退出,请重新登录
user.unknown.error=未知错误,请重新登录
jcaptcha.validate.error=验证码错误,请重新输入
jcaptcha.validate.success=验证码输入正确
user.email.not.valid=\u90AE\u7BB1\u683C\u5F0F\u9519\u8BEF
user.mobile.phone.number.not.valid=\u624B\u673A\u53F7\u683C\u5F0F\u9519\u8BEF
user.login.success=\u767B\u5F55\u6210\u529F
user.notfound=\u8BF7\u91CD\u65B0\u767B\u5F55
user.forcelogout=\u7BA1\u7406\u5458\u5F3A\u5236\u9000\u51FA\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55
user.unknown.error=\u672A\u77E5\u9519\u8BEF\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55
jcaptcha.validate.error=\u9A8C\u8BC1\u7801\u9519\u8BEF\uFF0C\u8BF7\u91CD\u65B0\u8F93\u5165
jcaptcha.validate.success=\u9A8C\u8BC1\u7801\u8F93\u5165\u6B63\u786E
##权限
no.permission=您没有数据的权限,请联系管理员添加权限 [{0}]
no.create.permission=您没有创建数据的权限,请联系管理员添加权限 [{0}]
no.update.permission=您没有修改数据的权限,请联系管理员添加权限 [{0}]
no.delete.permission=您没有删除数据的权限,请联系管理员添加权限 [{0}]
no.view.permission=您没有查看数据的权限,请联系管理员添加权限 [{0}]
#\u6279\u91CF\u63D2\u5165\u7528\u6237\u9519\u8BEF\u4FE1\u606F
user.import.excel.null=Excel\u6570\u636E\u4E3A\u7A7A\uFF0C\u8BF7\u586B\u5199\u6570\u636E
user.import.excel.data.null=Excel\u6570\u636E\u4E3A\u7A7A\uFF0C\u53EA\u6709\u6807\u9898\u884C\uFF0C\u8BF7\u586B\u5199\u6570\u636E
user.import.excel.filetype.error=\u6587\u4EF6\u4E0D\u662FExcel\u683C\u5F0F
user.import.excel.file.error=\u6587\u4EF6\u540D\u4E3A\u7A7A\uFF0C\u6587\u4EF6\u4E3A\u7A7A
user.import.excel.fileinput.error=\u83B7\u53D6Excel2003\u6D41\u9519\u8BEF
user.import.excel.fileinputx.error=\u83B7\u53D6Excel2003\u6D41\u9519\u8BEF
##\u6743\u9650
no.permission=\u60A8\u6CA1\u6709\u6570\u636E\u7684\u6743\u9650\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u6DFB\u52A0\u6743\u9650 [{0}]
no.create.permission=\u60A8\u6CA1\u6709\u521B\u5EFA\u6570\u636E\u7684\u6743\u9650\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u6DFB\u52A0\u6743\u9650 [{0}]
no.update.permission=\u60A8\u6CA1\u6709\u4FEE\u6539\u6570\u636E\u7684\u6743\u9650\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u6DFB\u52A0\u6743\u9650 [{0}]
no.delete.permission=\u60A8\u6CA1\u6709\u5220\u9664\u6570\u636E\u7684\u6743\u9650\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u6DFB\u52A0\u6743\u9650 [{0}]
no.view.permission=\u60A8\u6CA1\u6709\u67E5\u770B\u6570\u636E\u7684\u6743\u9650\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u6DFB\u52A0\u6743\u9650 [{0}]

BIN
src/main/resources/static/template/用户导入模板.xlsx

Binary file not shown.
Loading…
Cancel
Save