kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
src/main/java/com/nanometer/smartlab/controller/UserMngController.java
@@ -3,18 +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;
@@ -26,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;
@@ -51,6 +55,11 @@
    private BaseMetaService baseMetaService;
    @Resource
    private BaseRoleService baseRoleService;
    @Resource
    private SysProjectService sysProjectService;
    @Value("${personImgPath}")
    private String personImgPath;
    private LazyDataModel<SysUser> dataModel;
    private SysUser sysUser;
@@ -68,6 +77,10 @@
    private List<BaseMeta> codeList;
    private List<BaseRole> roleList;
    private List<Waster> wasterSelectList;
    private String projectName;
    private Long projectId;
    private String company;
    private BaseRole role;
    public List<BaseRole> getRoleList() {
        return roleList;
@@ -97,6 +110,14 @@
    public void onNewBtnClick() {
        this.sysUser = new SysUser();
        if (this.selectedList.size() > 1) {
            FacesUtils.warn("不能多选");
            return;
        }
        if (this.selectedList.size() == 1) {
            this.sysUser = this.sysUserService.getSysUser(this.selectedList.get(0).getId());
            this.sysUser.setId(null);
        }
        this.action = Constants.ACTION_ADD;
    }
@@ -128,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);
@@ -136,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) {
@@ -148,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){
@@ -208,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 {
@@ -235,30 +318,36 @@
                if(valuesList.get(12).equals(SeeFlag.LEADING.getText())){
                    sysUser.setSeeFlag(SeeFlag.LEADING);
                }
                if(valuesList.size()==14){
                    sysUser.setMemo(valuesList.get(13));
                }else {
                    sysUser.setMemo("");
                if (valuesList.size() > 13 && valuesList.get(13).trim()!=""){
                    SysProject project=sysProjectService.getProjectByName(valuesList.get(13));
                    if (project==null) {
                        errorMsg += ("课题组不存在,(" + i  + "行)");
                        continue;
                    }
                    //都存在就设置
                    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();
@@ -268,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){
@@ -375,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)){
@@ -397,8 +484,14 @@
                    return;
                }
                // 检查ARP号
                if (this.sysUserService.isSysUserExist(this.sysUser.getArp(), null, null, this.sysUser.getId())) {
                // 检查ARP号 //除自己外的arp
//                if (this.sysUserService.isSysUserExist(this.sysUser.getArp(), null, null, this.sysUser.getId())) {
//                    FacesUtils.warn("ARP已存在。");
//                    return;
//                }
                List<SysUser> list = sysUserService.getUserByArp(this.sysUser.getArp());
                assert list.size() < 2;
                if (list.size() > 0 && !list.get(0).getId().equals(this.sysUser.getId())) {
                    FacesUtils.warn("ARP已存在。");
                    return;
                }
@@ -438,9 +531,16 @@
                if(sysUser.getPassword()==null || sysUser.getPassword().equals("")){
                    sysUser.setPassword(this.editPasswor);
                }else {
                    String PW_PATTERN = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[~!@#$%^&*_.]).{8,}";
                    if (!sysUser.getPassword().matches(PW_PATTERN)){
                        FacesUtils.warn("密码必须8位以上,并且包含大小写字母、数字、特殊符号三种以上");
                        return;
                    }
                    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()");
@@ -468,6 +568,71 @@
        }
    }
    // 文件上传
    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());
        role = baseRoleService.getRoleByUserId(sysUser.getId());
    }
    public boolean isAccess(String info) {
        String permission = role.getBtnPermission();
        if(StringUtils.isEmpty(permission)){
            return  true;
        }else{
            return permission.contains(info);
        }
    }
    public LazyDataModel<SysUser> getDataModel() {
        if (this.dataModel == null) {
            this.dataModel = new LazyDataModel<SysUser>() {
@@ -475,11 +640,30 @@
                public List<SysUser> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
                    List<SysUser> list = null;
                    try {
                        int count = sysUserService.getSysUserTotalCount(arp, name,departmentName);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = sysUserService.getSysUserList(arp, name,departmentName, first, pageSize);
                        //做可见人员过滤
                        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("课题组")){
                            projectId = sysUser.getProjectId();
                            int count = sysUserService.getUserCountInProject(arp, name,departmentName,projectName,company);
                            this.setRowCount(count);
                            if (count > 0) {
                                list = sysUserService.getUserInProject(arp, name,departmentName,projectName,company, first, pageSize);
                            }
                        }else{
                            int count = sysUserService.getSysUserTotalCount(arp, name,departmentName,projectName,company);
                            this.setRowCount(count);
                            if (count > 0) {
                                list = sysUserService.getSysUserList(arp, name,departmentName,projectName,company, first, pageSize);
                            }
                        }
                        selectedList = new ArrayList<>();
                    } catch (Exception e) {
                        logger.error(e);
                    }
@@ -488,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() {
@@ -604,4 +800,29 @@
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
    public Long getProjectId() {
        return projectId;
    }
    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;
    }
    public void setCompany(String company) {
        this.company = company;
    }
}