kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
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;
@@ -150,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) {
@@ -162,31 +169,31 @@
                    }
                    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){
                        if (!StringUtils.isEmpty(cellInfo)){
                            cellInfo=new BigDecimal(""+cellInfo).toString();
@@ -202,13 +209,13 @@
                SysUser sysUser=new SysUser();
                if (valuesList.get(0) != null && !StringUtils.isEmpty(valuesList.get(0))){
                    sysUser.setCompany(valuesList.get(0));//单位
                    sysUser.setCompany(Long.valueOf(valuesList.get(0)));//单位
                }else {
                    errorMsg += "第"+i+"行,单位不能为空";
                    continue;
                }
                if (valuesList.get(1) != null && !StringUtils.isEmpty(valuesList.get(1))){
                    sysUser.setDepartment(valuesList.get(1));//部门
                    sysUser.setDepartment(Long.valueOf(valuesList.get(1)));//部门
                }else {
                    errorMsg += "第"+i+"行,部门不能为空";
                    continue;
@@ -285,7 +292,7 @@
                    continue;
                }
                if (valuesList.get(10) != null){
                    sysUser.setRoleId(valuesList.get(10));//角色
                    sysUser.setRoleId(Long.valueOf(valuesList.get(10)));//角色
                }else {
                    errorMsg += "第"+i+"行,角色不能为空";
                    continue;
@@ -312,19 +319,14 @@
                    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)) {
                            errorMsg += ("课题组不存在,(" + i  + "行)");
                            continue;
                        }
                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);
@@ -344,8 +346,8 @@
        }
    }
    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();
@@ -355,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){
@@ -462,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)){
@@ -539,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()");
@@ -566,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());
@@ -573,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() {
@@ -585,23 +642,24 @@
                    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);
                            }
                        }
@@ -624,7 +682,8 @@
//                            }
//                        }
//                    }
                    return sysUserService.getSysUser(rowKey);
                    Long id=Long.valueOf(rowKey);
                    return sysUserService.getSysUser(id);
                }
            };
        }
@@ -633,7 +692,7 @@
    public void exportLab2Excel(){
        try {
            List<Map> list = sysUserService.getExportUserList(arp, name,departmentName,project,company);
            List<Map> list = sysUserService.getExportUserList(arp, name,departmentName,projectId,company);
            sysUserService.exportUser2Excel(list);
            FacesUtils.info("导出成功");
        }catch (Exception e){
@@ -742,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;
    }