From 4ab942df06d2f7d6f2df1102ef5b83d6a1696763 Mon Sep 17 00:00:00 2001
From: lyfO_o <764716047@qq.com>
Date: Mon, 20 Feb 2023 17:14:14 +0800
Subject: [PATCH] 人员文件上传
---
src/main/java/com/nanometer/smartlab/controller/UserMngController.java | 145 +++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 130 insertions(+), 15 deletions(-)
diff --git a/src/main/java/com/nanometer/smartlab/controller/UserMngController.java b/src/main/java/com/nanometer/smartlab/controller/UserMngController.java
index 80e8d17..6f95ba8 100644
--- a/src/main/java/com/nanometer/smartlab/controller/UserMngController.java
+++ b/src/main/java/com/nanometer/smartlab/controller/UserMngController.java
@@ -27,11 +27,15 @@
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.primefaces.model.UploadedFile;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.InputStream;
+import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.charset.CharacterCodingException;
import java.security.NoSuchAlgorithmException;
@@ -54,6 +58,9 @@
private BaseRoleService baseRoleService;
@Resource
private SysProjectService sysProjectService;
+
+ @Value("${personImgPath}")
+ private String personImgPath;
private LazyDataModel<SysUser> dataModel;
private SysUser sysUser;
@@ -142,7 +149,7 @@
int totalRows = sheet.getPhysicalNumberOfRows();
List<SysUser> userList=new ArrayList<>();
System.out.println("totalRows=========="+totalRows);
- String errorMsg = null;
+ String errorMsg = "";
for (int i = 1; i < totalRows; i++) {
row=sheet.getRow(i);
@@ -201,18 +208,47 @@
}
SysUser sysUser=new SysUser();
- sysUser.setCompany(valuesList.get(0));//单位
- sysUser.setDepartment(valuesList.get(1));//部门
- sysUser.setArp((int)Double.parseDouble(valuesList.get(2))+"");//arp号
- sysUser.setName(valuesList.get(3));//姓名
- sysUser.setAccount(valuesList.get(4));//账号
-
- String PW_PATTERN = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[~!@#$%^&*_.]).{8,}";
- if (!valuesList.get(5).matches(PW_PATTERN)){
- errorMsg += "第"+i+"行,密码必须8位以上,并且包含大小写字母、数字、特殊符号三种以上";
+ if (valuesList.get(0) != null && !StringUtils.isEmpty(valuesList.get(0))){
+ sysUser.setCompany(valuesList.get(0));//单位
+ }else {
+ errorMsg += "第"+i+"行,单位不能为空";
continue;
}
- sysUser.setPassword(MD5Utils.encode(valuesList.get(5))+"");//密码
+ if (valuesList.get(1) != null && !StringUtils.isEmpty(valuesList.get(1))){
+ sysUser.setDepartment(valuesList.get(1));//部门
+ }else {
+ errorMsg += "第"+i+"行,部门不能为空";
+ continue;
+ }
+ if (valuesList.get(2) != null && !StringUtils.isEmpty(valuesList.get(2))){
+ sysUser.setArp((int)Double.parseDouble(valuesList.get(2))+"");//arp号
+ }else {
+ errorMsg += "第"+i+"行,arp不能为空";
+ continue;
+ }
+ if (valuesList.get(3) != null && !StringUtils.isEmpty(valuesList.get(3))){
+ sysUser.setName(valuesList.get(3));//姓名
+ }else {
+ errorMsg += "第"+i+"行,姓名不能为空";
+ continue;
+ }
+ if (valuesList.get(4) != null && !StringUtils.isEmpty(valuesList.get(4))){
+ sysUser.setAccount(valuesList.get(4));//账号
+ }else {
+ errorMsg += "第"+i+"行,账号不能为空";
+ continue;
+ }
+ if (valuesList.get(5) != null && !StringUtils.isEmpty(valuesList.get(5))){
+ String PW_PATTERN = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[~!@#$%^&*_.]).{8,}";
+ if (!valuesList.get(5).matches(PW_PATTERN)){
+ errorMsg += "第"+i+"行,密码必须8位以上,并且包含大小写字母、数字、特殊符号三种以上";
+ continue;
+ }
+ sysUser.setPassword(MD5Utils.encode(valuesList.get(5))+"");//密码
+ }else {
+ errorMsg += "第"+i+"行,密码不能为空";
+ continue;
+ }
//valuesList.get(8) id卡号检查
if (valuesList.get(8) != null && valuesList.get(8).length()>0){
@@ -232,10 +268,39 @@
continue;
}
}
+
+ // 检查ARP号
+ if (this.sysUserService.isSysUserExist(sysUser.getArp(), null, null, null)) {
+ errorMsg += ("第"+i+"行,ARP已存在");
+ continue;
+ }
+ // 检查登陆账号
+ if (this.sysUserService.isSysUserExist(null, sysUser.getAccount(), null, null)) {
+ errorMsg += ("第"+i+"行,登陆账号已存在");
+ continue;
+ }
+ if (!StringUtils.isEmpty(sysUser.getIdCard()) && this.sysUserService.isSysUserExist(null, null, sysUser.getIdCard(), null)) {
+ errorMsg += ("第"+i+"行,ID卡号已存在");
+ continue;
+ }
sysUser.setPhone(valuesList.get(6));//电话
sysUser.setEmail(valuesList.get(7));//邮箱
- sysUser.setPoint((int)Double.parseDouble(valuesList.get(9)));//分数
- sysUser.setRoleId(valuesList.get(10));//角色
+ if (valuesList.get(9) != null){
+ sysUser.setPoint((int)Double.parseDouble(valuesList.get(9)));//分数
+ }else {
+ errorMsg += "第"+i+"行,分数不能为空";
+ continue;
+ }
+ if (valuesList.get(10) != null){
+ sysUser.setRoleId(valuesList.get(10));//角色
+ }else {
+ errorMsg += "第"+i+"行,角色不能为空";
+ continue;
+ }
+ if (valuesList.get(12) == null){
+ errorMsg += "第"+i+"行,可见度不能为空";
+ continue;
+ }
if(valuesList.get(11).equals("是")){
sysUser.setApproverFlag(ApproverFlag.YES);
}else {
@@ -253,7 +318,6 @@
if(valuesList.get(12).equals(SeeFlag.LEADING.getText())){
sysUser.setSeeFlag(SeeFlag.LEADING);
}
-
if (valuesList.size() > 13 && !valuesList.get(13).replace(" ","").isEmpty()){
String[] projects = valuesList.get(13).split(",");
@@ -276,7 +340,7 @@
this.sysUserService.insertSysUserList(userList);
System.out.println("=========导入结束=====");
- if (errorMsg != null){
+ if (errorMsg != null && !StringUtils.isEmpty(errorMsg)){
FacesUtils.warn(errorMsg);
}else {
FacesUtils.info("导入成功。");
@@ -509,6 +573,57 @@
}
}
+ // 文件上传
+ public void handleImageUpload(FileUploadEvent event) {
+ UploadedFile file = event.getFile();
+ try {
+ // 1.上传文件
+ InputStream is = file.getInputstream();
+ String oldName = file.getFileName();
+ String newName = new Date().getTime() + oldName.substring(oldName.lastIndexOf("."));
+ String realPath = personImgPath + "/";
+ File dest = new File(realPath, newName);
+ if (!dest.getParentFile().exists()) {
+ boolean rel = dest.getParentFile().mkdirs();
+ if (!rel) {
+ throw new Exception("文件夹创建失败");
+ }
+ }
+
+ OutputStream os = new FileOutputStream(dest);
+ try {
+ byte[] buffer = new byte[8 * 1024];
+ int bytesRead;
+ while ((bytesRead = is.read(buffer)) != -1) {
+ os.write(buffer, 0, bytesRead);
+ }
+ // 更新用户url
+ sysUser.setUrl("upload/upload/" + newName);
+ // 清空图片文件以便再次上传
+ RequestContext.getCurrentInstance().update("dialogForm2");
+ // 更新用户表单
+ RequestContext.getCurrentInstance().update("dialogForm");
+ // 隐藏上传框
+ RequestContext.getCurrentInstance().execute("PF('imgDialog').hide()");
+
+ } catch (Exception e) {
+ FacesUtils.warn("文件上传失败。");
+ throw e;
+ } finally {
+ if (is != null) {
+ is.close();
+ }
+ if (os != null) {
+ os.close();
+ }
+ }
+
+ } catch (Exception e) {
+ FacesUtils.warn("操作失败。");
+ return;
+ }
+ }
+
public void initPage() {
sysUser = sysUserService.getSysUser(getUser().getId());
--
Gitblit v1.9.2