TechnologyVersionSaveAction.java

package com.mycim.webapp.actions.process;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.file.excel.im.ExcelImport;
import com.mycim.framework.file.excel.im.ExcelParser;
import com.mycim.framework.file.excel.im.ExcelRow;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.NamedSpaceList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.bas.ObjectVersion;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.consts.VersionStatus;
import com.mycim.valueobject.prp.Ecn;
import com.mycim.valueobject.prp.FlowExcelDataForm;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.webapp.Constants;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.TechnologyVersionInfoForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author Johnson.Wang
 * @version 6.0.0
 * @date 2019/9/1
 **/
public class TechnologyVersionSaveAction extends PrpSetupAction {

    public static final String ROUTE_OPERATION_KEY = "#$#OPERATIONID";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute(SessionNames.FACILITY_INFO_KEY, sysService.getFacility(LocalContext.getFacilityRrn()));

        TechnologyVersionInfoForm infoForm = (TechnologyVersionInfoForm) form;

        ProcessPlanning processPlanning = new ProcessPlanning(infoForm.getInstanceId(),
                                                              getNamedSpace(ObjectList.WFL_KEY,
                                                                            LocalContext.getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) getInstance(processPlanning);

        checkPermissionForOperateProcess(processPlanning.getInstanceRrn());

        List<ProcessVersion> versions = prpService.getProcessVersions(processPlanning);

        if (CollectionUtils.isEmpty(versions)) {
            request.setAttribute("editaction", "/technologyversionsave.do");
            request.setAttribute("initaction", "/technologysave.do");
            return mapping.findForward(Constants.ADDVERSION_KEY);
        }

        ObjectVersion currentVersion = versions.get(0);
        if (StringUtils.isNotEmpty(infoForm.getVersionId())) {
            currentVersion = prpService
                    .getProcessVersion(processPlanning.getInstanceRrn(), Integer.parseInt(infoForm.getVersionId()));
        }

        currentVersion.copyNamedObject(processPlanning);

        PropertyUtils.copyProperties(infoForm, currentVersion);
        infoForm.setEcnFlag(getEcnFlag());
        infoForm.setTransId(Constants.MODIFY_KEY);
        infoForm.setNpwType(processPlanning.getAttribute3());
        infoForm.setVersionEditEnable(String.valueOf(checkVersionEditEnable(currentVersion)));
        infoForm.setObjectDeleteEnable(checkObjectDeleteEnable(versions));
        String dataPermissionFlag = LocalContext.get(DATA_PERMISSION_KEY);
        String workflowUrl =
                "jnlp://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +
                        "/workflowJnlp.jsp?editorType=1&workflowVersion=" + currentVersion.getInstanceVersion() +
                        "&facilityRrn=" + LocalContext.getFacilityRrn() + "&versionEditEnable=" +
                        infoForm.getVersionEditEnable() + "&workflowRrn=" + currentVersion.getInstanceRrn() +
                        "&fromWorkflowRrn=0";
        //如果flag为1 则拼接processRrn 在workflowEditor使用copy功能时 仅能查询出有权限查看的process 。 已经改为拼接 userRrn 了。
        // if (StringUtils.equals(dataPermissionFlag, "1")) {
        //     workflowUrl = workflowUrl + "&processRrns=" + LocalContext.get(PROCESS_RRN_KEY);
        // }
        request.setAttribute("dataPermissionFlag", dataPermissionFlag);
        request.setAttribute("workflowUrl", workflowUrl);
        request.setAttribute(SessionNames.PROCESS_VERSION_KEY, currentVersion);
        if (StringUtils.equalsIgnoreCase(currentVersion.getVersionStatus(), VersionStatus.UNFROZEN_KEY)) {
            request.setAttribute("importFlag", true);
        }
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward versionInfo(ActionMapping mapping, TechnologyVersionInfoForm form, HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {
        String rrn = request.getParameter("versionRrn");
        String version = request.getParameter("version");

        int ver = Integer.parseInt(version);
        long versionRrn = Long.parseLong(rrn);

        NamedObject namedObject = new NamedObject();
        namedObject.setInstanceRrn(versionRrn);
        namedObject = baseService.getNamedObject(namedObject);

        ProcessVersion pv = prpService.getProcessVersion(versionRrn, ver);
        pv.copyNamedObject(namedObject);

        request.setAttribute("historys",
                             baseService.getObjectVersionHistoryList(pv.getInstanceRrn(), pv.getInstanceVersion()));
        PropertyUtils.copyProperties(form, pv);

        form.setTransId(Constants.MODIFY_KEY);
        request.setAttribute("actionedit", "/technologyversionsave");

        return (mapping.findForward("detail"));
    }

    public ActionForward addVersionWithEcn(ActionMapping mapping, TechnologyVersionInfoForm form,
                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
        ProcessPlanning processPlanning = new ProcessPlanning(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                  LocalContext
                                                                                                          .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        List<ProcessVersion> versions = prpService.getProcessVersions(processPlanning);

        boolean versionEdit = checkVersionEditEnable(versions);
        Assert.isFalse(versionEdit,
                       Errors.create().key(MessageIdList.VERSION_HAS_NOT_ACTIVE_VERSION).content("版本未激活").build());

        ProcessVersion processVersion = new ProcessVersion();
        processVersion.copyNamedObject(processPlanning);
        processVersion.setInstanceVersion(processPlanning.getCurrentVersion() + 1);
        processVersion.setVersionId(processPlanning.getCurrentVersion() + 1 + "");
        processVersion.setVersionDesc("Automatic schema generation");
        processVersion.setVersionStatus(VersionStatus.UNFROZEN_KEY);
        processVersion.setTransId(Constants.CREATE_KEY);
        processVersion.setTransPerformedby(LocalContext.getUserId());

        prpService.createProcessVersion(processVersion, processPlanning.getCurrentVersion());

        form.setTransId(Constants.MODIFY_KEY);
        form.setEcnFlag(getEcnFlag());
        form.setVersionEditEnable(String.valueOf(checkVersionEditEnable(processVersion)));
        request.setAttribute(SessionNames.PROCESS_VERSION_KEY, processVersion);
        return init(mapping, form, request, response);
    }

    public ActionForward delete(ActionMapping mapping, TechnologyVersionInfoForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ProcessPlanning processPlanning = new ProcessPlanning(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                  LocalContext
                                                                                                          .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        ProcessVersion pv = new ProcessVersion();
        pv.setInstanceRrn(processPlanning.getInstanceRrn());
        pv.setInstanceVersion(Integer.parseInt(form.getVersionId()));
        pv = prpService.getProcessVersion(processPlanning.getInstanceRrn(), Integer.parseInt(form.getVersionId()));

        pv.setTransId(Constants.DELETE_KEY);
        pv.setTransPerformedby(LocalContext.getUserId());

        prpService.deleteProcessVersion(pv);

        WebUtils.setSuccessMsg(request);
        return detail(mapping, form, request, response);
    }

    public ActionForward activate(ActionMapping mapping, TechnologyVersionInfoForm form, HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        ProcessPlanning processPlanning = new ProcessPlanning(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                  LocalContext
                                                                                                          .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        // 添加卡控,冻结状态才能激活
        int version = Integer.valueOf(form.getVersionId());
        String versionStatus = baseService.getVersionStatus(processPlanning.getInstanceRrn(), version);
        Assert.isTrue(StringUtils.equalsIgnoreCase(versionStatus, TransactionNames.FROZEN_KEY),
                      Errors.create().content("当前流程状态为 {},无法执行该操作!").args(versionStatus).build());

        ProcessVersion processVersion = prpService
                .getProcessVersion(processPlanning.getInstanceRrn(), Integer.parseInt(form.getVersionId()));

        Ecn ecn = ecnService.getEcn(processVersion.getEcnRrn());

        validateProcessPlan((List) ecn.getObjects());

        prpService.checkReworkIsInProcess(processVersion.getInstanceRrn(), processVersion.getInstanceVersion());
        prpService.checkLoopIsInProcess(processVersion.getInstanceRrn(), processVersion.getInstanceVersion());
        processVersion.setVersionStatus(VersionStatus.ACTIVE_KEY);
        processVersion.setTransPerformedby(LocalContext.getUserId());
        prpService.updateProcessVersionByEcnRrn(processVersion);
        if (processVersion.getInstanceVersion() > 1) {
            prpService.copyLastProcessVersionQTimeWhenAddNewProcessVersion(processVersion.getInstanceRrn(),
                                                                           processVersion.getInstanceVersion());
        }

        form.setVersionStatus(VersionStatus.ACTIVE_KEY);
        List<ObjectVersion> versions = baseService.getObjectVersions(processPlanning);

        processPlanning.setVersions(versions);

        request.setAttribute("version", versions);

        form.setTransId(Constants.MODIFY_KEY);
        form.setVersionEditEnable("false");
        return init(mapping, form, request, response);
    }

    public ActionForward detail(ActionMapping mapping, TechnologyVersionInfoForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        request.setAttribute(SessionNames.FACILITY_INFO_KEY, sysService.getFacility(LocalContext.getFacilityRrn()));
        ProcessPlanning processPlanning = new ProcessPlanning(form.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                  LocalContext
                                                                                                          .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) getInstance(processPlanning);
        List<ProcessVersion> versions = prpService.getProcessVersions(processPlanning);
        if (CollectionUtils.isEmpty(versions)) {
            versions = new ArrayList<>();
        }
        request.setAttribute("version", versions);
        request.setAttribute("editaction", "/technologyversionsave.do");
        request.setAttribute("initaction", "/technologysave.do");
        request.setAttribute("objectform", "technologyInfoForm");
        request.setAttribute("versionedit", checkVersionEditEnable(versions) + "");

        return (mapping.findForward("version"));
    }

    public ActionForward frozen(ActionMapping mapping, HttpServletRequest request, TechnologyVersionInfoForm theform,
                                HttpServletResponse response) throws Exception {
        ProcessPlanning processPlanning = new ProcessPlanning(theform.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                     LocalContext
                                                                                                             .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        // 添加卡控,解冻状态才能冻结
        int version = Integer.valueOf(theform.getVersionId());
        String versionStatus = baseService.getVersionStatus(processPlanning.getInstanceRrn(), version);
        Assert.isTrue(StringUtils.equalsIgnoreCase(versionStatus, TransactionNames.UNFROZEN_KEY),
                      Errors.create().content("当前流程状态为 {},无法执行该操作!").args(versionStatus).build());

        ProcessVersion processVersion = prpService
                .getProcessVersion(processPlanning.getInstanceRrn(), Integer.parseInt(theform.getVersionId()));

        Ecn ecn = ecnService.getEcn(processVersion.getEcnRrn());


        ecn.setEcnStatus(VersionStatus.FROZEN_KEY);
        ecn.setTransId(TransactionNames.FROZEN_KEY);
        ecn.setTransPerformedby(LocalContext.getUserId());
        if (ecn.getObjectSubtype() == null) {
            ecn.setObjectSubtype(ecn.getObjectType());
        }
        long workflowRrn = ecnService.getEcnSignOffRrn(ecn.getObjectSubtype(),
                                                       getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                     LocalContext.getFacilityRrn()),
                                                       getNamedSpace(ObjectList.WFL_KEY,
                                                                     LocalContext.getFacilityRrn()));

        Assert.isFalse(workflowRrn == 0, Errors.create().key(MessageIdList.WORKFLOW_MISSING_SIGNOFF)
                                               .content("Not foud SignOff ecn Workflow").build());


        List list = (List) ecn.getObjects();
        validateProcessPlan(list);

        //check multipath
        prpService.checkWflIsInProcess(processVersion.getInstanceRrn(), processVersion.getInstanceVersion());

        //todo ecn提交审核
        //        getProcessManager().invokeWorkflow(execRrn, map);

        processVersion.setVersionStatus(VersionStatus.FROZEN_KEY);
        prpService.updateProcessVersionByEcnRrn(processVersion);

        return init(mapping, theform, request, response);
    }

    public ActionForward unfrozen(ActionMapping mapping, HttpServletRequest request, TechnologyVersionInfoForm theform,
                                  HttpServletResponse response) throws Exception {

        ProcessPlanning processPlanning = new ProcessPlanning(theform.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                     LocalContext
                                                                                                             .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        // 添加卡控,冻结状态才能解冻
        int version = Integer.valueOf(theform.getVersionId());
        String versionStatus = baseService.getVersionStatus(processPlanning.getInstanceRrn(), version);
        Assert.isTrue(StringUtils.equalsIgnoreCase(versionStatus, TransactionNames.FROZEN_KEY),
                      Errors.create().content("当前流程状态为 {},无法执行该操作!").args(versionStatus).build());

        ProcessVersion processVersion = prpService
                .getProcessVersion(processPlanning.getInstanceRrn(), Integer.parseInt(theform.getVersionId()));

        Ecn ecn = ecnService.getEcn(processVersion.getEcnRrn());
        ecn.setEcnStatus(VersionStatus.UNFROZEN_KEY);
        ecn.setTransId(TransactionNames.UNFROZEN_KEY);
        ecn.setTransPerformedby(LocalContext.getUserId());

        processVersion.setVersionStatus(VersionStatus.UNFROZEN_KEY);
        prpService.updateProcessVersionByEcnRrn(processVersion);

        // fixme cleanECNExecutionRrn
        //        getProcessManager().cleanECNExecutionRrn(ecn.getInstanceRrn());

        processVersion = (ProcessVersion) this.getInstance(processVersion);
        processVersion.copyNamedObject(processPlanning);

        List<ProcessVersion> versions = prpService.getProcessVersions(processPlanning);
        request.setAttribute(SessionNames.PROCESSVERSION_KEY, processVersion);
        processPlanning.setVersions(versions);

        request.setAttribute("version", versions);
        request.setAttribute(SessionNames.PROCESSVERSION_KEY, processVersion);

        theform.setVersionStatus(VersionStatus.UNFROZEN_KEY);
        theform.setTransId(Constants.MODIFY_KEY);
        theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(processVersion)));
        theform.setObjectDeleteEnable(checkObjectDeleteEnable(versions));
        theform.setObjectType(ecn.getObjectType());
        return init(mapping, theform, request, response);
    }

    public ActionForward version(ActionMapping mapping, HttpServletRequest request, TechnologyVersionInfoForm theform,
                                 HttpServletResponse response) throws Exception {
        ProcessPlanning processPlanning = new ProcessPlanning(theform.getInstanceId(), getNamedSpace(ObjectList.WFL_KEY,
                                                                                                     LocalContext
                                                                                                             .getFacilityRrn()),
                                                              ObjectList.WFL_KEY);
        processPlanning = (ProcessPlanning) this.getInstance(processPlanning);

        ProcessVersion processVersion = prpService
                .getProcessVersion(processPlanning.getInstanceRrn(), theform.getInstanceVersion());

        List<ProcessVersion> versions = prpService.getProcessVersions(processPlanning);

        processVersion.copyNamedObject(processPlanning);

        PropertyUtils.copyProperties(theform, processVersion);
        theform.setNpwType(processPlanning.getAttribute3());
        theform.setEcnFlag(getEcnFlag());
        theform.setTransId(Constants.MODIFY_KEY);
        theform.setVersionEditEnable(String.valueOf(checkVersionEditEnable(processVersion)));
        theform.setObjectDeleteEnable(checkObjectDeleteEnable(versions));
        request.setAttribute(SessionNames.PROCESS_VERSION_KEY, processVersion);
        if (request.getAttribute("workflowUrl") == null) {
            String workflowUrl =
                    "jnlp://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +
                            "/workflowJnlp.jsp?editorType=1&workflowVersion=" + processVersion.getInstanceVersion() +
                            "&facilityRrn=" + LocalContext.getFacilityRrn() + "&versionEditEnable=" +
                            theform.getVersionEditEnable() + "&workflowRrn=" + processVersion.getInstanceRrn() +
                            "&fromWorkflowRrn=0";
            request.setAttribute("workflowUrl", workflowUrl);
        }
        request.setAttribute(SessionNames.PROCESS_VERSION_KEY, processVersion);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public void downloadTemplate(HttpServletResponse response) throws Exception {
        String fileName = "flowImport.xls";
        WebUtils.exportExcel(fileName, new HashMap<>(), new ArrayList(), TemplateLocation.FLOW_IMPORT, 1, 1, response);
    }

    public ActionForward doImportExcel(ActionMapping mapping, TechnologyVersionInfoForm theform,
                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        FormFile upFile = theform.getFormFile();
        String fileName = upFile.getFileName();
        String processId = theform.getInstanceId();
        Assert.isTrue(StringUtils.isNotEmpty(fileName),
                      Errors.create().key("spec.please_select_excel").content("Please select the Excel file to import!")
                            .build());

        //校验数据
        Map<String, Object> results = checkAndAssemblingData(getImpData(upFile),
                                                             StringUtils.trimToUpperCase(processId));

        List<FlowExcelDataForm> flowDatas = (List<FlowExcelDataForm>) results.get("flowExcelDataFormList");
        theform.setFlowDatas(flowDatas);
        boolean isImportFlag = MapUtils.getBoolean(results, "isImportFlag");
        if (isImportFlag) {
            //导入flow
            prpService.importWorkflow(results);
            WebUtils.setSuccessMsg(request);
        }
        return init(mapping, theform, request, response);
    }

    private List<Map> getImpData(FormFile upFile) throws IOException {

        List<Map> results = new ExcelImport().attributeFrom(0).attributeTo(0).tableFrom(6)
                                             .mapper(new ExcelParser<Object, Map>() {

                                                 @Override
                                                 public Object attributeMapper(List<ExcelRow> attributeRows,
                                                                               int attributeFrom, int attributeTo) {
                                                     return null;
                                                 }

                                                 @Override
                                                 public Map tableRowMapper(ExcelRow excelRow, int rowNum,
                                                                           Object attributeDatas) {

                                                     if (rowNum < 6 || (StringUtils.isBlank(excelRow.getString(0)) &&
                                                             StringUtils.isBlank(excelRow.getString(1)) &&
                                                             StringUtils.isBlank(excelRow.getString(2)) &&
                                                             StringUtils.isBlank(excelRow.getString(3)))) {
                                                         return null;
                                                     }
                                                     int i = 0;
                                                     Map map = new HashMap();
                                                     //序号
                                                     map.put("seq", excelRow.getString(i++));
                                                     //程序号
                                                     map.put("processId", excelRow.getString(i++));
                                                     //路径号
                                                     map.put("routeId", excelRow.getString(i++));
                                                     //步骤号
                                                     map.put("operationId", excelRow.getString(i++));
                                                     return map;
                                                 }
                                             }).file(upFile.getInputStream()).getTableDataList();
        return results;
    }

    private Map<String, Object> checkAndAssemblingData(List<Map> results, String flowId) {
        Map<String, Object> resultMap = new HashMap();
        Set<String> routeIdSet = new LinkedHashSet<>();
        List<String> operationIdList = new ArrayList<>();
        String message;
        boolean isImportFlag;
        Integer tempSeq = -1;
        List<FlowExcelDataForm> flowExcelDataFormList = new ArrayList<>();
        long flowRrn = getInstanceRrn(flowId, LocalContext.getFacilityRrn(), ObjectList.WFL_KEY);
        // 校验当前流程是否可以导入(只有未激活状态流程可以导入)
        if (flowRrn > 0) {
            String workflowStatus = baseService.getVersionStatus(flowRrn, baseService.getActiveVersion(flowRrn, false));
            isImportFlag = StringUtils.equalsIgnoreCase("UNFROZEN", workflowStatus);
            Assert.isTrue(isImportFlag, Errors.create().key(MessageIdList.FLOWIMP_FLOW_STATE_VALID)
                                              .content("Only unfrozen processes can be imported!").build());
        } else {
            isImportFlag = false;
            Assert.isTrue(isImportFlag,
                          Errors.create().key("process.process_missing").content("The process id:{} does not exist!")
                                .args(flowId).build());
        }

        // 循环校验模板中的每行数据
        for (int i = 0; i < results.size(); i++) {
            boolean flag;
            boolean valid = true;
            FlowExcelDataForm itemData = new FlowExcelDataForm();
            String seqString = StringUtils.trim(MapUtils.getString(results.get(i), "seq"));
            String processId = StringUtils.trimToUpperCase(MapUtils.getString(results.get(i), "processId"));
            String routeId = StringUtils.trimToUpperCase(MapUtils.getString(results.get(i), "routeId"));
            String operationId = StringUtils.trimToUpperCase(MapUtils.getString(results.get(i), "operationId"));

            // 1.校验必填
            flag = seqString == null || StringUtils.isBlank(processId) || StringUtils.isBlank(routeId) ||
                    StringUtils.isBlank(operationId);
            message = I18nUtils.getMessage(MessageIdList.FLOWIMP_EXCEL_DATA_EMPTY,
                                           "seq/processId/routeId/operationId can not be empty!");
            valid = isValid(itemData, valid, !flag, message);

            // 2.校验seq顺序
            message = I18nUtils
                    .getMessage(MessageIdList.FLOWIMP_SEQ_MUST_BE_NUMBER, "The seq: {} must be number!", seqString);
            valid = isValid(itemData, valid, StringUtils.isNumeric(seqString), message);

            Integer seq = NumberUtils.toInt(seqString);

            message = I18nUtils.getMessage(MessageIdList.FLOWIMP_SEQ_OUT_OF_ORDER, "The seq: {} out of order!", seq);
            valid = isValid(itemData, valid, seq > tempSeq, message);
            tempSeq = seq;

            //2.1 判断rrn 是否为其他类型数据 保证 数据不重复,如果rrn 存在 则判断数据是否为 合法类型
            long routeRrn=  baseService.getNamedObjectRrn(routeId, NamedSpaceList.DEFAULT_KEY, ObjectList.WFL_KEY);
            if(routeRrn>0){
                //通过rrn 找到路径实体
                NamedObject routeObject=  baseService.getNamedObject(routeRrn);
                //防止 process 中存在相同id 的 sub plan
                //开始判断 路径号是否合法。。
                if("PROCESS".equalsIgnoreCase(StringUtils.trim(routeObject.getObjectSubtype()))){
                    message = I18nUtils.getMessage(MessageIdList.FLOWIMP_INVALIDATE_SUBPLANID, "The sub plan id :{} already exists in the process!",routeId );
                    valid = isValid(itemData, valid, false, message);
                }
            }

            // 3.校验operation
            long operationRrn = getInstanceRrn(operationId, LocalContext.getFacilityRrn(), ObjectList.OPERATION_KEY);
            message = I18nUtils
                    .getMessage(MessageIdList.FLOWIMP_OPERATION_MISSING, "The operation id:{} does not exist!",
                                operationId);
            valid = isValid(itemData, valid, operationRrn > 0, message);


            // 4.校验导入模板中的流程和页面flow是否一致
            message = I18nUtils.getMessage(MessageIdList.FLOWIMP_PROCESS_DIFFERENT_PAGE,
                                           "The process id:{} is inconsistent with the page flow!", processId);
            valid = isValid(itemData, valid, StringUtils.equalsIgnoreCase(processId, flowId), message);

            // 5.获取导入的route、operation
            routeIdSet.add(routeId);
            //存储导入route底下的步骤信息
            Object obj = resultMap.get(routeId);
            //存储导入的步骤ID,用于校验route下面是否有重复的步骤
            Object obj2 = resultMap.get(routeId + ROUTE_OPERATION_KEY);
            List<Map<String, Object>> operationList;
            if (obj == null) {
                operationList = new ArrayList<>();
            } else {
                operationList = (List<Map<String, Object>>) obj;
            }
            if (obj2 == null) {
                operationIdList = new ArrayList<>();
            } else {
                operationIdList = (List<String>) obj2;
            }
            Map<String, Object> map = new HashMap();
            map.put("stepId", operationId);
            map.put("stepRrn", operationRrn);
            operationList.add(map);
            resultMap.put(routeId, operationList);
            resultMap.put(routeId + ROUTE_OPERATION_KEY, operationIdList);
            message = I18nUtils.getMessage(MessageIdList.FLOWIMP_ROUTE_HAVE_REPEAT_STEP,
                                           "There are the same steps: {} below this route: {}", operationId, routeId);
            valid = isValid(itemData, valid, !operationIdList.contains(operationId), message);
            operationIdList.add(operationId);

            // 6.填充模板对象数据
            itemData.setSeq(seq);
            itemData.setProcessId(processId);
            itemData.setRouteId(routeId);
            itemData.setOperationId(operationId);
            itemData.setValidateData(valid);
            flowExcelDataFormList.add(itemData);

            // 7.flow是否导入标记
            if (isImportFlag) {
                isImportFlag = valid;
            }
        }

        resultMap.put("isImportFlag", isImportFlag);
        resultMap.put("routeIdSet", routeIdSet);
        resultMap.put("flowId", flowId);
        resultMap.put("flowRrn", flowRrn);
        resultMap.put("flowVersion", baseService.getActiveVersion(flowRrn, false));
        resultMap.put("flowExcelDataFormList", flowExcelDataFormList);

        return resultMap;
    }

    /**
     * @param item:待检验数据对象
     * @param initValue:数据初始是否合法
     * @param checkValue:true:返回initValue;false:返回false,并添加message
     * @param message:返回消息
     * @return
     */
    private boolean isValid(FlowExcelDataForm item, boolean initValue, boolean checkValue, String message) {
        if (!checkValue) {
            item.addMessage(message);
            return false;
        } else {
            return initValue;
        }
    }

}