From 47a751cb301d05276ae5d75145d57b2d090fe4e1 Mon Sep 17 00:00:00 2001 From: kongzy <kongzy> Date: 星期一, 01 七月 2024 10:58:35 +0800 Subject: [PATCH] change --- src/main/java/com/nanometer/smartlab/controller/UserMngController.java | 307 +++++++++++++++++++++++++++++++++++++------------- 1 files changed, 225 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/nanometer/smartlab/controller/UserMngController.java b/src/main/java/com/nanometer/smartlab/controller/UserMngController.java index 0408681..8fd8c58 100644 --- a/src/main/java/com/nanometer/smartlab/controller/UserMngController.java +++ b/src/main/java/com/nanometer/smartlab/controller/UserMngController.java @@ -3,19 +3,18 @@ import com.alibaba.druid.util.StringUtils; import com.nanometer.smartlab.entity.BaseMeta; import com.nanometer.smartlab.entity.BaseRole; +import com.nanometer.smartlab.entity.SysProject; import com.nanometer.smartlab.entity.SysUser; import com.nanometer.smartlab.entity.enumtype.ApproverFlag; import com.nanometer.smartlab.entity.enumtype.SeeFlag; import com.nanometer.smartlab.entity.enumtype.ValidFlag; import com.nanometer.smartlab.entity.enumtype.Waster; +import com.nanometer.smartlab.realm.ShiroDbRealm; import com.nanometer.smartlab.service.BaseMetaService; import com.nanometer.smartlab.service.BaseRoleService; import com.nanometer.smartlab.service.SysProjectService; import com.nanometer.smartlab.service.SysUserService; -import com.nanometer.smartlab.util.Constants; -import com.nanometer.smartlab.util.FacesUtils; -import com.nanometer.smartlab.util.IDUtils; -import com.nanometer.smartlab.util.MD5Utils; +import com.nanometer.smartlab.util.*; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; @@ -27,11 +26,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; @@ -55,6 +58,9 @@ @Resource private SysProjectService sysProjectService; + @Value("${personImgPath}") + private String personImgPath; + private LazyDataModel<SysUser> dataModel; private SysUser sysUser; private List<SysUser> selectedList; @@ -71,7 +77,8 @@ private List<BaseMeta> codeList; private List<BaseRole> roleList; private List<Waster> wasterSelectList; - private String project; + private String projectName; + private Long projectId; private String company; private BaseRole role; @@ -109,6 +116,7 @@ } if (this.selectedList.size() == 1) { this.sysUser = this.sysUserService.getSysUser(this.selectedList.get(0).getId()); + this.sysUser.setId(null); } this.action = Constants.ACTION_ADD; } @@ -141,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); @@ -149,7 +157,7 @@ List<String> valuesList=new ArrayList<String>(); for (int t = 0; t < totalCells; t++) { String cellInfo=""; - String groupId = ""; + String groupCode = ""; if (row.getCell(t) != null) { @@ -161,55 +169,86 @@ } if(t == 0 || t == 1){ if( t == 0){ - groupId = "user_company"; + groupCode = "user_company"; }else if(t == 1){ - groupId = "user_department"; + groupCode = "user_department"; } - String id=getId(groupId,cellInfo); - if(id == "" && cellInfo!=null && cellInfo!="" ) { + Long id=getId(groupCode,cellInfo); + if(id == null && cellInfo!=null && cellInfo!="" ) { BaseMeta baseMeta = new BaseMeta(); - baseMeta.setId(IDUtils.uuid()); - baseMeta.setGroupId(groupId); + // baseMeta.setId(IDUtils.uuid()); + baseMeta.setGroupCode(groupCode); baseMeta.setMetaValue(cellInfo); baseMeta.setMetaKey(cellInfo); baseMetaService.insertBaseMeta(baseMeta); id = baseMeta.getId(); this.codeList.add(baseMeta); } - valuesList.add(id); + valuesList.add(String.valueOf(id)); }else if(t==10){ - String id=getRoleId(cellInfo); - if(id == "" && cellInfo!=null && cellInfo!="" ){ + Long id=getRoleId(cellInfo); + if(id == null && cellInfo!=null && cellInfo!="" ){ BaseRole role=new BaseRole(); - role.setId(IDUtils.uuid()); + //role.setId(IDUtils.uuid()); role.setName(cellInfo); role.setValidFlag(ValidFlag.VALID); } - valuesList.add(id); + valuesList.add(String.valueOf(id)); }else if(t==6){ - cellInfo=new BigDecimal(""+cellInfo).toString(); - valuesList.add(cellInfo); + if (!StringUtils.isEmpty(cellInfo)){ + cellInfo=new BigDecimal(""+cellInfo).toString(); + valuesList.add(cellInfo); + }else{ + valuesList.add(cellInfo); + } } else { valuesList.add(cellInfo); } - } - System.out.println("size : "+valuesList.size()); - System.out.println("value : "+valuesList); + 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(Long.valueOf(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(Long.valueOf(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){ @@ -221,16 +260,47 @@ //8位补齐20位,否则不做操作 String idCard = "E0E01E000A" + idCardUpper + "01"; sysUser.setIdCard(idCard);//id卡号 + }else { + sysUser.setIdCard(idCardUpper);//id卡号 } }else{ - FacesUtils.warn("第"+i+"行,id卡号需要8位或者20位"); - return; + errorMsg += ("第"+i+"行,id卡号需要8位或者20位"); + 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(Long.valueOf(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 { @@ -249,39 +319,35 @@ sysUser.setSeeFlag(SeeFlag.LEADING); } - - if (valuesList.size() > 13 && !valuesList.get(13).replace(" ","").isEmpty()){ - String[] projects = valuesList.get(13).split(","); - for (String project : projects) { - //不存再就直接推出 - if (!sysProjectService.isExistProject(project)) { - throw new Exception("课题组不存在,(" + i + "行)"); - } + if (valuesList.size() > 13 && valuesList.get(13).trim()!=""){ + SysProject project=sysProjectService.getProjectByName(valuesList.get(13)); + if (project==null) { + errorMsg += ("课题组不存在,(" + i + "行)"); + continue; } //都存在就设置 - sysUser.setProject(valuesList.get(13).replace(" ","")); - }else{ - sysUser.setProject(null); + sysUser.setProjectId(project.getId()); } sysUser.setValidFlag(ValidFlag.VALID); userList.add(sysUser); } - if (errorMsg != null){ - FacesUtils.warn(errorMsg); - } this.sysUserService.insertSysUserList(userList); System.out.println("=========导入结束====="); - FacesUtils.info("导入成功。"); + if (errorMsg != null && !StringUtils.isEmpty(errorMsg)){ + FacesUtils.warn(errorMsg); + }else { + FacesUtils.info("导入成功。"); + } }catch (Exception ex) { ex.printStackTrace(); FacesUtils.info("导入失败。"); } } - public String getRoleId(String name){ - String id=""; + public Long getRoleId(String name){ + Long id=null; for (int i=0;i<this.roleList.size();i++){ if(this.roleList.get(i).getName().equals(name)){ id=this.roleList.get(i).getId(); @@ -291,8 +357,8 @@ } //判断字典表里是否存在这组键值对 - public String getId(String groupId,String metaValue){ - String id=""; + public Long getId(String groupId,String metaValue){ + Long id= null; if(this.codeList.size()>0){ for (BaseMeta bm:this.codeList){ @@ -398,9 +464,7 @@ FacesUtils.warn("ID卡号已存在。"); return; } - if(!org.apache.commons.lang.StringUtils.isNotBlank(sysUser.getProject())){ - sysUser.setProject(null); - } + String PW_PATTERN = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[~!@#$%^&*_.]).{8,}"; if (!sysUser.getPassword().matches(PW_PATTERN)){ @@ -475,6 +539,8 @@ sysUser.setPassword(MD5Utils.encode(sysUser.getPassword())); } this.sysUserService.updateSysUser(sysUser); + ShiroDbRealm shiroDbRealm= SpringUtil.getBean("authorizationRealm",ShiroDbRealm.class); + shiroDbRealm.removeUserCache(sysUser.getAccount()); FacesUtils.info("修改成功。"); RequestContext.getCurrentInstance().execute("PF('dialog').hide()"); @@ -502,6 +568,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()); @@ -509,7 +626,11 @@ } public boolean isAccess(String info) { String permission = role.getBtnPermission(); - return permission.contains(info); + if(StringUtils.isEmpty(permission)){ + return true; + }else{ + return permission.contains(info); + } } public LazyDataModel<SysUser> getDataModel() { @@ -521,27 +642,28 @@ try { //做可见人员过滤 String userPermission = role.getUserPermission(); + userPermission="11"; if (userPermission.contains("自己")) { list = Collections.singletonList(sysUserService.getSysUser(UserMngController.this.sysUser.getId())); this.setRowCount(1); }else if (userPermission.contains("课题组")){ - project = sysUser.getProject(); - int count = sysUserService.getUserCountInProject(arp, name,departmentName,project,company); + projectId = sysUser.getProjectId(); + int count = sysUserService.getUserCountInProject(arp, name,departmentName,projectName,company); this.setRowCount(count); if (count > 0) { - list = sysUserService.getUserInProject(arp, name,departmentName,project,company, first, pageSize); + list = sysUserService.getUserInProject(arp, name,departmentName,projectName,company, first, pageSize); } }else{ - int count = sysUserService.getSysUserTotalCount(arp, name,departmentName,project,company); + int count = sysUserService.getSysUserTotalCount(arp, name,departmentName,projectName,company); this.setRowCount(count); if (count > 0) { - list = sysUserService.getSysUserList(arp, name,departmentName,project,company, first, pageSize); + list = sysUserService.getSysUserList(arp, name,departmentName,projectName,company, first, pageSize); } } - + selectedList = new ArrayList<>(); } catch (Exception e) { logger.error(e); } @@ -550,21 +672,33 @@ @Override public SysUser getRowData(String rowKey) { - Iterator<SysUser> iterator = this.iterator(); - if (iterator != null) { - SysUser su = null; - while (iterator.hasNext()) { - su = iterator.next(); - if (rowKey.equals(su.getId())) { - return su; - } - } - } - return null; +// Iterator<SysUser> iterator = this.iterator(); +// if (iterator != null) { +// SysUser su = null; +// while (iterator.hasNext()) { +// su = iterator.next(); +// if (rowKey.equals(su.getId())) { +// return su; +// } +// } +// } + Long id=Long.valueOf(rowKey); + return sysUserService.getSysUser(id); } }; } return dataModel; + } + + public void exportLab2Excel(){ + try { + List<Map> list = sysUserService.getExportUserList(arp, name,departmentName,projectId,company); + sysUserService.exportUser2Excel(list); + FacesUtils.info("导出成功"); + }catch (Exception e){ + e.printStackTrace(); + FacesUtils.warn("导出失败"); + } } public SysUser getSysUser() { @@ -667,14 +801,23 @@ this.departmentName = departmentName; } - public String getProject() { - return project; + public Long getProjectId() { + return projectId; } - public void setProject(String project) { - this.project = project; + public void setProject(Long projectId) { + this.projectId = projectId; } + public String getProjectName(){ + return this.projectName; + } + + public void setProjectName(String projectName){ + this.projectName=projectName; + } + + public String getCompany() { return company; } -- Gitblit v1.9.2