kongzy
2023-11-01 dd8795a2675e32b25abe98f644f80d5f72fadb90
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;
@@ -55,6 +59,9 @@
    @Resource
    private SysProjectService sysProjectService;
    @Value("${personImgPath}")
    private String personImgPath;
    private LazyDataModel<SysUser> dataModel;
    private SysUser sysUser;
    private List<SysUser> selectedList;
@@ -73,6 +80,7 @@
    private List<Waster> wasterSelectList;
    private String project;
    private String company;
    private BaseRole role;
    public List<BaseRole> getRoleList() {
        return roleList;
@@ -102,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;
    }
@@ -133,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);
@@ -179,29 +195,60 @@
                        }
                        valuesList.add(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(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){
@@ -213,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(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 {
@@ -241,13 +319,13 @@
                    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  + "行)");
                            errorMsg += ("课题组不存在,(" + i  + "行)");
                            continue;
                        }
                    }
                    //都存在就设置
@@ -259,13 +337,14 @@
                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("导入失败。");
@@ -494,6 +573,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>() {
@@ -501,11 +645,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,project,company);
                        this.setRowCount(count);
                        if (count > 0) {
                            list = sysUserService.getSysUserList(arp, name,departmentName,project,company, 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("课题组")){
                            project = sysUser.getProject();
                            int count = sysUserService.getUserCountInProject(arp, name,departmentName,project,company);
                            this.setRowCount(count);
                            if (count > 0) {
                                list = sysUserService.getUserInProject(arp, name,departmentName,project,company, first, pageSize);
                            }
                        }else{
                            int count = sysUserService.getSysUserTotalCount(arp, name,departmentName,project,company);
                            this.setRowCount(count);
                            if (count > 0) {
                                list = sysUserService.getSysUserList(arp, name,departmentName,project,company, first, pageSize);
                            }
                        }
                        selectedList = new ArrayList<>();
                    } catch (Exception e) {
                        logger.error(e);
                    }
@@ -514,23 +677,34 @@
                @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;
//                            }
//                        }
//                    }
                    return sysUserService.getSysUser(rowKey);
                }
            };
        }
        return dataModel;
    }
    public void exportLab2Excel(){
        try {
            List<Map> list = sysUserService.getExportUserList(arp, name,departmentName,project,company);
            sysUserService.exportUser2Excel(list);
            FacesUtils.info("导出成功");
        }catch (Exception e){
            e.printStackTrace();
            FacesUtils.warn("导出失败");
        }
    }
    public SysUser getSysUser() {
        return sysUser;
    }