田源
2025-01-16 a13255b4129ee8a7a7b7e1ecd8e02dd2c78f7c17
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/OsRevisionRuleServiceImpl.java
@@ -1,22 +1,33 @@
package com.vci.web.service.impl;
import com.vci.client.mw.ClientSessionUtility;
import com.vci.corba.common.PLException;
import com.vci.corba.omd.vrm.VersionRule;
import com.vci.dto.OsRevisionRuleDTO;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.pagemodel.OsRevisionRuleVO;
import com.vci.po.OsRevisionRulePO;
import com.vci.starter.poi.bo.ReadExcelOption;
import com.vci.starter.poi.bo.WriteExcelData;
import com.vci.starter.poi.bo.WriteExcelOption;
import com.vci.starter.poi.constant.ExcelLangCodeConstant;
import com.vci.starter.poi.util.ExcelUtil;
import com.vci.starter.web.annotation.log.VciUnLog;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.util.*;
import com.vci.web.service.OsRevisionRuleServiceI;
import com.vci.web.util.Func;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.swing.*;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
@@ -40,6 +51,16 @@
    @Autowired(required = false)
    @Lazy
    private OsRevisionRuleServiceI self;
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     *  必填列
     */
    private List<Integer> ColumnNameisRed = new ArrayList<Integer>();
    /**
     * 查询所有的版本规则
@@ -73,20 +94,130 @@
     */
    @Override
    public boolean addVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException {
        VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName());
        //判空
        VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称");
        //版本规则合规检验
        this.checkVersionRule(osRevisionRuleDTO);
        //查重
        VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getId());
        //name不为空
        if(Func.isNotEmpty(vr) && !"".equals(vr.name)){
            throw new PLException("500",new String[]{"名称重复请更换名称!"});
        }
        platformClientUtil.getVersionService().addVersionRule(this.dto2VersionRule(osRevisionRuleDTO));
        return false;
        return platformClientUtil.getVersionService().addVersionRule(this.dto2VersionRule(osRevisionRuleDTO));
    }
    /**
     * 修改版本规则
     * @param osRevisionRuleDTO
     * @return
     */
    @Override
    public boolean updateVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) {
    public boolean updateVersionRule(OsRevisionRuleDTO osRevisionRuleDTO) throws PLException {
        //判空
        VciBaseUtil.alertNotNull(osRevisionRuleDTO,"版本规则对象",osRevisionRuleDTO.getId(),"版本规则名称");
        //判断是否在系统中存在
        VersionRule vr = platformClientUtil.getVersionService().getVersionRule(osRevisionRuleDTO.getName());
        //版本规则合规检验
        this.checkVersionRule(osRevisionRuleDTO);
        //name不为空
        if(Func.isEmpty(vr) && !"".equals(vr.name)){
            throw new PLException("500",new String[]{"修改的版本规则在系统中不存在!"});
        }
        return platformClientUtil.getVersionService().modifyVersionRule(this.dto2VersionRule(osRevisionRuleDTO));
    }
        return false;
    /**
     * 删除版本规则
     * @param osRevisionRuleDTOS
     * @return
     */
    @Override
    public boolean deleteVersionRule(List<OsRevisionRuleDTO> osRevisionRuleDTOS) throws PLException {
        VciBaseUtil.alertNotNull(osRevisionRuleDTOS,"待删除的属性列表");
        //判断要删除的版本规则是否有被引用
        osRevisionRuleDTOS.stream().forEach(item->{
            String vrName = item.getId();
            try {
                List<Map<String, String>> usedVersionRuleList = this.getUsedVersionRuleList(vrName);
                if(Func.isNotEmpty(usedVersionRuleList)){
                    throw new VciBaseException("该版本已被使用不允许删除");
                }
            } catch (PLException e) {
                logger.error(e.getMessage());
                e.printStackTrace();
                throw new VciBaseException(e.getMessage());
            }
        });
        //平台的deleteStatus方法必传三个参数,oid、name和ts
        List<VersionRule> vrList = new ArrayList<>();
        for(OsRevisionRuleDTO vrDTO : osRevisionRuleDTOS){
            //oid和ts判空
            String oid = vrDTO.getOid();
            //id主要用来对缓存数据删除
            String id = vrDTO.getId();
            //后台会用ts进行数据一致性校验
            Date ts = vrDTO.getTs();
            if(Func.isBlank(oid) || Func.isBlank(id) || Func.isEmpty(ts)){
                throw new PLException("500",new String[]{"待删除的版本规则列表中主键【oid】、调整时间【ts】、状态名称【name】不能为空!"});
            }
            VersionRule vr = new VersionRule();
            vr.oid = oid;
            vr.name = id;
            vr.ts = Func.format(ts,VciDateUtil.DateTimeMillFormat);
            vrList.add(vr);
        }
        return platformClientUtil.getVersionService().deleteVersionRules(vrList.toArray(new VersionRule[vrList.size()]));
    }
    /**
     * 检查版本规则设置的是否合理
     * @param dto
     */
    private void checkVersionRule(OsRevisionRuleDTO dto) throws PLException {
        //版本规则名称只能为英文字母
        String regex = "[a-z A-Z]*";
        if (!dto.getId().matches(regex)) {
            throw new PLException("500",new String[]{"名称只能为英文!"});
        }
        //跳跃字符只能为数字或者字母
        String regex0 = "^[a-zA-Z0-9,]+$";
        if(Func.isNotBlank(dto.getJumpCharacter()) && (!(dto.getJumpCharacter().matches(regex0)))){
            throw new PLException("500",new String[]{"跳跃字符只能为数字或者字母!"});
        }
        //初始值不能为空且只能为数字或者字母或英文状态下的符号
        String regex1 = "[A-Za-z0-9!@#$%^&*()-_=+{}':|;,.?/]+$";
        if(Func.isBlank(dto.getInitialValue()) || !dto.getInitialValue().matches(regex1)){
            throw new PLException("500",new String[]{"初始值不能为空且只能为数字或者字母或英文状态下的符号!"});
        }
        if(dto.getInitialValue().length() + dto.getInitialValue().length() > 32) {
            throw new PLException("500",new String[]{"初始值不能超过32个字符!"});
        }
        //步长不能为空且必须为1-9的正整数
        String regex2 = "[1-9]";
        if(Func.isBlank(dto.getStepLength()) || (!dto.getStepLength().matches(regex2))){
            throw new PLException("500",new String[]{"步长不能为空且必须为1-9的正整数"});
        }
        //前缀相关判断
        String regex3 = "^\\s+.*";
        if(Func.isNotBlank(dto.getPrefixion()) && (dto.getPrefixion().matches(regex3))){
            throw new PLException("500",new String[]{"前缀不能以空格开头"});
        }
        if (Func.isNotBlank(dto.getPrefixion()) && dto.getPrefixion().length() > 32) {
            throw new PLException("500",new String[]{"前缀不能超过32个字符"});
        }
        //后缀相关判断
        String regex4 = "^*.\\s+$";
        if(Func.isNotBlank(dto.getSuffix()) && (dto.getSuffix().matches(regex4))){
            throw new PLException("500",new String[]{"后缀不能以空格结尾"});
        }
        if (Func.isNotBlank(dto.getSuffix()) && dto.getSuffix().length() > 32) {
            throw new PLException("500",new String[]{"后缀不能超过32个字符"});
        }
        if (dto.getId().length() > 255) {
            throw new PLException("500",new String[]{"名称不能超过255个字符"});
        }
    }
    /**
@@ -95,17 +226,22 @@
     */
    private VersionRule dto2VersionRule(OsRevisionRuleDTO osRevisionRuleDTO){
        VersionRule newVR = new VersionRule();
        newVR.oid = osRevisionRuleDTO.getOid();
        newVR.name = osRevisionRuleDTO.getId();
        newVR.tag = osRevisionRuleDTO.getName();
        newVR.jumpCharacter = osRevisionRuleDTO.getPrefixCode();
        /*newVR.initialValue = jtf3.getText();
        newVR.stepLength = jtf4.getText();
        newVR.prefixion = jtf5.getText();
        newVR.suffix = jtf6.getText();
        newVR.description = jta.getText();*/
        String userName = ClientSessionUtility.getCurUserName();
        newVR.creator = userName;
        newVR.label = osRevisionRuleDTO.getName();
        newVR.description = osRevisionRuleDTO.getDescription();
        newVR.jumpCharacter = osRevisionRuleDTO.getJumpCharacter();
        newVR.initialValue = osRevisionRuleDTO.getInitialValue();
        newVR.stepLength = osRevisionRuleDTO.getStepLength();
        newVR.prefixion = osRevisionRuleDTO.getPrefixion();
        newVR.suffix = osRevisionRuleDTO.getSuffix();
        String userName = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
        long timeMillis = System.currentTimeMillis();
        newVR.ts = Func.format((Func.isNotEmpty(osRevisionRuleDTO.getTs()) ? osRevisionRuleDTO.getTs():new Date()),VciDateUtil.DateTimeMillFormat);
        newVR.creator = Func.isBlank(osRevisionRuleDTO.getCreator()) ? userName:osRevisionRuleDTO.getCreator();
        newVR.createTime = Func.isEmpty(osRevisionRuleDTO.getCreateTime()) ? timeMillis:osRevisionRuleDTO.getCreateTime().getTime();
        newVR.modifier = userName;
        newVR.modifyTime = timeMillis;
        return newVR;
    }
@@ -141,18 +277,18 @@
            try {
                ruleVO.setCreateTime(VciDateUtil.long2Date(versionRule.createTime));
                ruleVO.setLastModifyTime(VciDateUtil.long2Date(versionRule.modifyTime));
                ruleVO.setTs(VciDateUtil.str2Date(versionRule.ts,VciDateUtil.DateTimeFormat));
                ruleVO.setTs(VciDateUtil.str2Date(versionRule.ts,VciDateUtil.DateTimeMillFormat));
            } catch (Exception e) {
                e.printStackTrace();
            }
            ruleVO.setDescription(versionRule.description);
            ruleVO.setId(versionRule.name);
            ruleVO.setName(versionRule.tag);
            ruleVO.setSerialStep(WebUtil.getInt(versionRule.stepLength));
            ruleVO.setSkipCode(versionRule.jumpCharacter);
            ruleVO.setPrefixCode(versionRule.prefixion);
            ruleVO.setSuffixCode(versionRule.suffix);
            ruleVO.setStartCode(versionRule.initialValue);
            ruleVO.setName(versionRule.label);
            ruleVO.setStepLength(WebUtil.getInt(versionRule.stepLength));
            ruleVO.setJumpCharacter(versionRule.jumpCharacter);
            ruleVO.setPrefixion(versionRule.prefixion);
            ruleVO.setSuffix(versionRule.suffix);
            ruleVO.setInitialValue(versionRule.initialValue);
            //associated暂时没有使用
        }
        return ruleVO;
@@ -170,6 +306,27 @@
            return self.selectAllRevisionMap().getOrDefault(id.toLowerCase().trim(),null);
        }
        return null;
    }
    /**
     * 使用多个编号获取规则的值
     * @param vrIdList 编号
     * @return 显示对象
     */
    @Override
    public List<OsRevisionRuleVO> getRevisionRuleByIds(Collection<String> vrIdList) {
        if(Func.isEmpty(vrIdList)){
            return null;
        }
        Map<String, OsRevisionRuleVO> revisionRuleVOMap = self.selectAllRevisionMap();
        List<OsRevisionRuleVO> versionRuleList = new ArrayList<>();
        vrIdList.stream().forEach(vrId->{
            OsRevisionRuleVO versionRuleVO = revisionRuleVOMap.getOrDefault(vrId.toLowerCase(Locale.ROOT),null);
            if(versionRuleVO!=null){
                versionRuleList.add(versionRuleVO);
            }
        });
        return versionRuleList;
    }
    /**
@@ -197,6 +354,210 @@
    }
    /**
     * 导出选中的版本规则
     * @param exportFileName 导出的文件名
     * @param vrNames 需要导出的版本规则名称
     * @param flag 控制导出的列名是否和导入模板一致
     * @return
     */
    @Override
    public String exportVersionRule(String exportFileName, String vrNames, boolean flag) throws PLException {
        if(Func.isBlank(vrNames)){
            throw new PLException("500",new String[]{"请勾选要导出的版本规则!"});
        }
        //界面没传名称,使用默认导出名称
        exportFileName = Func.isBlank(exportFileName) ?  "版本规则导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName;
        //设置列名
        List<String> columns = this.getCloumns(flag);
        //写excel
        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
        try {
            new File(excelPath).createNewFile();
        } catch (Throwable e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
        }
        //设置列
        List<WriteExcelData> excelDataList = new ArrayList<>();
        //设置列头
        for (int index = 0; index < columns.size(); index++) {
            excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
        }
        //按照版本规则名查询,然后处理版本规则导出
        List<String> vrIdList = Func.toStrList(vrNames);
        List<OsRevisionRuleVO> revisionRuleVOList = this.getRevisionRuleByIds(vrIdList);
        if(Func.isEmpty(revisionRuleVOList)){
            excelDataList.add(new WriteExcelData(1,1, "根据名称未查询到版本规则信息,请刷新后尝试重新导出!"));
        }else{
            for (int i = 0; i < revisionRuleVOList.size(); i++) {
                OsRevisionRuleVO revisionRuleVO = revisionRuleVOList.get(i);
                excelDataList.add(new WriteExcelData(i+1,0, revisionRuleVO.getId()));
                excelDataList.add(new WriteExcelData(i+1,1, revisionRuleVO.getName()));
                excelDataList.add(new WriteExcelData(i+1,2, revisionRuleVO.getJumpCharacter()));
                excelDataList.add(new WriteExcelData(i+1,3, revisionRuleVO.getInitialValue()));
                excelDataList.add(new WriteExcelData(i+1,4, revisionRuleVO.getStepLength()));
                excelDataList.add(new WriteExcelData(i+1,5, revisionRuleVO.getPrefixion()));
                excelDataList.add(new WriteExcelData(i+1,6, revisionRuleVO.getSuffix()));
                excelDataList.add(new WriteExcelData(i+1,7, revisionRuleVO.getDescription()));
                if(!flag){
                    excelDataList.add(new WriteExcelData(i+1,8, Func.format(revisionRuleVO.getCreateTime(),"yyyy年MM月dd日 hh:mm:ss")));
                }
            }
        }
        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
        ExcelUtil.writeDataToFile(excelPath, excelOption);
        return excelPath;
    }
    /**
     * 下载版本规则导入模板
     * @param exportFileName
     * @return
     * @throws PLException
     */
    @Override
    public String downloadVersionRuleTemplate(String exportFileName) throws Exception {
        //界面没传名称,使用默认导出名称
        exportFileName = Func.isBlank(exportFileName) ?  "版本规则导入模板_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"):exportFileName;
        //设置列名
        List<String> columns = this.getCloumns(true);
        //设置必填列
        ColumnNameisRed.clear();
        ColumnNameisRed.add(0);
        ColumnNameisRed.add(3);
        ColumnNameisRed.add(4);
        //写excel
        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
        try {
            new File(excelPath).createNewFile();
        } catch (Throwable e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
        }
        //设置列
        List<WriteExcelData> excelDataList = new ArrayList<>();
        //设置列头
        for (int index = 0; index < columns.size(); index++) {
            //判断是否为必填列,给必填列设置颜色
            if(ColumnNameisRed.contains(index)){
                WriteExcelData excelData = new WriteExcelData(0, index, columns.get(index));
                excelData.setFontColor(String.valueOf(HSSFColor.HSSFColorPredefined.RED.getIndex()));
                excelDataList.add(excelData);
            }else{
                excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
            }
        }
        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
        ExcelUtil.writeDataToFile(excelPath, excelOption);
        return excelPath;
    }
    /**
     * 导入版本规则
     * @param file
     * @return
     */
    @Override
    public BaseResult importVersionRules(File file) throws Exception {
        VciBaseUtil.alertNotNull(file,"excel文件");
        if(!file.exists()){
            throw new VciBaseException("导入的excel文件不存在,{0}",new String[]{file.getPath()});
        }
        try{
            //1、读取excel中的数据,组成对象
            ReadExcelOption excelOption = new ReadExcelOption();
            List<OsRevisionRulePO> poList = ExcelUtil.readDataObjectFromExcel(file, OsRevisionRulePO.class,excelOption,(value, po, fieldName)->{});
            //去除都是空的情况
            if(CollectionUtils.isEmpty(poList)){
                return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{});
            }
            //excel判重,数据校验,dto对象转换,存储对象转换,执行保存
            List<OsRevisionRuleDTO> dtoList = new ArrayList<>();
            //当前excel中是否重复用的判重Map:(key:判重版本规则名,value:行号)
            Map<String, String> excelReapeat = new HashMap<>();
            //数据库查询是否有已存在的枚举名,方便后续做判重处理
            List<OsRevisionRuleVO> versionRuleVOList = this.getRevisionRuleByIds(poList.stream().map(OsRevisionRulePO::getId).collect(Collectors.toSet()));
            List<String> repeatVrId = new ArrayList<>();
            if(Func.isNotEmpty(versionRuleVOList)){
                repeatVrId = versionRuleVOList.stream().map(OsRevisionRuleVO::getId).collect(Collectors.toList());
            }
            //判断必填属性是否为空和其他校验
            List<String> finalRepeatVrId = repeatVrId;
            poList.stream().forEach(vrPO -> {
                if(Func.isBlank(vrPO.getId())){//版本规则名判空
                    throw new VciBaseException("第【"+vrPO.getRowIndex()+"】行,versionrulenameerror");
                }else if(excelReapeat.containsKey(vrPO.getId())){//版本规则名表格中判重
                    throw new VciBaseException("第【"+excelReapeat.get(vrPO.getId())+"】行和第【"+vrPO.getRowIndex()+"】行数据,名称重复");
                }else if (Func.isNotEmpty(finalRepeatVrId) && finalRepeatVrId.contains(vrPO.getId())){//判断名称是否与系统中重复
                    throw new VciBaseException("第【"+vrPO.getRowIndex()+"】行,名称在系统中已经存在,请修改!");
                }
                //版本规则名excel中判重处理
                excelReapeat.put(vrPO.getId(),vrPO.getRowIndex());
                OsRevisionRuleDTO revisionRuleDTO = new OsRevisionRuleDTO();
                //revisionRuleDTO.setOid(VciBaseUtil.getPk().toUpperCase(Locale.ROOT));
                revisionRuleDTO.setId(vrPO.getId());
                revisionRuleDTO.setName(vrPO.getName());
                revisionRuleDTO.setJumpCharacter(vrPO.getJumpCharacter());
                revisionRuleDTO.setInitialValue(vrPO.getInitialValue());
                revisionRuleDTO.setStepLength(vrPO.getStepLength());
                revisionRuleDTO.setPrefixion(vrPO.getPrefixion());
                revisionRuleDTO.setSuffix(vrPO.getSuffix());
                revisionRuleDTO.setDescription(vrPO.getDescription());
                try {
                    //检查版本规则是否合规
                    checkVersionRule(revisionRuleDTO);
                } catch (PLException e) {
                    e.printStackTrace();
                    throw new VciBaseException(VciBaseUtil.getExceptionMessage(e));
                }
                dtoList.add(revisionRuleDTO);
            });
            //执行保存操作
            dtoList.stream().forEach(dto->{
                try {
                    boolean b = platformClientUtil.getVersionService().addVersionRule(dto2VersionRule(dto));
                    if(!b){
                        throw new VciBaseException("save and return false");
                    }
                } catch (PLException e) {
                    e.printStackTrace();
                    throw new VciBaseException("执行保存时出现错误,错误版本规则对象名为:【" + dto.getId() + "】,原因:"+VciBaseUtil.getExceptionMessage(e));
                }
            });
        }catch (Exception e){
            if(logger.isErrorEnabled()){
                logger.error("读取excel内容时或保存版本规则时出现了错误,具体原因:",VciBaseUtil.getExceptionMessage(e));
            }
            e.printStackTrace();
            return BaseResult.fail(VciBaseUtil.getExceptionMessage(e),new String[]{},e);
        }
        return BaseResult.success("枚举导入成功!");
    }
    /**
     * 获取导出或导入模板的列名
     * @param flag 是否获取导入模板列名
     * @return
     */
    private List<String> getCloumns(boolean flag){
        if(flag){
            return new ArrayList<>(
                    Arrays.asList(
                            "名称", "标签", "跳跃字符(以逗号分隔)",
                            "初始值", "步长", "前缀", "后缀", "描述"
                    )
            );
        }
        return new ArrayList<>(
                Arrays.asList(
                        "名称", "标签", "跳跃字符", "初始值", "步长",
                        "前缀", "后缀", "描述", "创建时间"
                )
        );
    }
    /**
     * 清除缓存
     */
    @Override