WflPath4ExtAction.java

package com.mycim.webapp.actions.wflinfo;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
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.utils.WflLinkContextSetupAttributeUtil;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.valueobject.prp.WflLinkContextValue;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.ContextValueForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 替代路径定义
 *
 * @author sandy
 * @version 6.0.0
 * @date 2019/9/16
 **/
public class WflPath4ExtAction extends PrpSetupAction {

    private static final String DEFAULT_KEY = "DEFAULT";

    private static final String IS_ROUTE_N = "1";

    private static final String IS_ROUTE_Y = "0";

    private static final String INSTANCE_ID = "instance_id";

    private static final String CONNECT_LINE = "---";

    public ActionForward init(ActionMapping mapping, HttpServletRequest request) throws Exception {
        String[] types = new String[]{"LOTPLANTYPE4MUTILPATH", "lotType"};
        registerOptionType(LocalContext.getFacilityRrn(), Arrays.asList(types), request);
        return mapping.getInputForward();
    }

    /**
     * 查询路径信息
     *
     * @param theform
     * @param mapping
     * @param request
     * @return
     * @throws Exception
     */
    public Map<String, Object> qryWflPathInfo(ContextValueForm theform, ActionMapping mapping,
                                              HttpServletRequest request) throws Exception {

        Long facilityRrn = LocalContext.getFacilityRrn();
        validProducProcessInfo(theform, request, facilityRrn);

        String processId = StringUtils.trimToUpperCase(theform.getProcessId());
        String productId = StringUtils.trimToUpperCase(theform.getProductId());
        String processVersionId = StringUtils.trimToUpperCase(theform.getProcessVersionId());
        int processVersion = parseProcessVersion(processVersionId);

        ContextValue contextValue = new ContextValue(
                getInstanceRrn(ContextNames.WFL_LINK_CONTEXT, facilityRrn, ObjectList.CONTEXT_KEY));
        contextValue.setStatus("ACTIVE");

        if (StringUtils.isNotBlank(productId)) {
            contextValue.setContextKey1(productId);
        }
        contextValue.setContextKey2(processId);
        contextValue.setContextKey14(String.valueOf(processVersion));
        Page page = new Page(theform.getPage(), theform.getLimit());
        boolean hasActiveContext = validProducProcessInfo(theform, request, facilityRrn);
        page = ctxService.queryContextValuesForPage(contextValue, facilityRrn, page);
        if (page.getResults() != null && !page.getResults().isEmpty()) {
            List<ContextValue> contextValues = (ArrayList<ContextValue>) page.getResults();
            for (ContextValue ct : contextValues) {
                if (hasActiveContext) {
                    ct.setFlag("1");
                } else {
                    ct.setFlag("0");
                }
                ContextValue tempContext = ctxService.filterContextValueToRrn(ct, facilityRrn);
                // 对于byManual单独check
                if (WflLinkContextSetupAttributeUtil.isByManualRouteMultipath(tempContext) ||
                        WflLinkContextSetupAttributeUtil.isByManualOperationMultipath(tempContext)) {
                    WflLinkContextValue wflLinkContextValue = new WflLinkContextValue(tempContext);
                    if (!specService.checkMultiPathHasManualForContextValue(wflLinkContextValue)) {
                        ct.setResultValue2(FALSE);
                    }
                    ct.setResultValue1(StringUtils.EMPTY);
                }else if (prpService.invalidateContextValue(tempContext)) {//invalidate context value
                    ct.setResultValue2(FALSE);
                }
            }
        }
        ctxService.filterContextValuesToId((List) page.getResults());

        Map<String, Object> dataMap = new HashMap<String, Object>(10);
        dataMap.put("data", page.getResults());
        dataMap.put("totalCount", page.getTotalItems());

        return dataMap;
    }

    /**
     * 查询左边流程树
     *
     * @param theform
     * @param mapping
     * @param request
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> getWflTreeInfo(ContextValueForm theform, ActionMapping mapping,
                                                    HttpServletRequest request) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        /*String searchFlag = WebUtils.getParameter("searchFlag", request);
        if (StringUtils.isEmpty(searchFlag)) {
            List<Map<String, Object>> list = new ArrayList<>();
            return list;
        }*/
        String productId = StringUtils.trimToUpperCase(theform.getProductId());
        String processId = StringUtils.trimToUpperCase(theform.getProcessId());
        String processVersionId = StringUtils.trimToUpperCase(theform.getProcessVersionId());

        if (StringUtils.isNotEmpty(productId) && StringUtils.isNotEmpty(processId) &&
                StringUtils.isNotEmpty(processVersionId)) {
            boolean hasActiveContext = validProducProcessInfo(theform, request, facilityRrn);

            long workflowRrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
            int workflowVersion = parseProcessVersion(processVersionId);
            List wflTree = prpService.getWorkFlowTree(workflowRrn, workflowVersion, getMatchRules(theform));
            int contextStatus = 0;
            if (hasActiveContext) {
                contextStatus = 1;
            }
            if (null == wflTree) {
                wflTree = new ArrayList();
            }
            List<Map<String, Object>> wflRouteByProcess = prpService.getWflRouteByProcess(workflowRrn, workflowVersion);

            List<Map<String, Object>> list = this.pareWflTree(wflTree, contextStatus, wflRouteByProcess);

            return list;
        } else {
            List<Map<String, Object>> list = new ArrayList<>();
            return list;
        }

    }

    public Boolean validProducProcessInfo(ContextValueForm theform, HttpServletRequest request,
                                          Long facilityRrn) throws Exception {
        String processId = StringUtils.trimToUpperCase(theform.getProcessId());
        String productId = StringUtils.trimToUpperCase(theform.getProductId());
        String processVersionId = StringUtils.trimToUpperCase(theform.getProcessVersionId());

        Assert.isFalse(StringUtils.isEmpty(processId),
                       Errors.create().key(MessageIdList.PROCESS_EMPTY_ID).content("process id empty").build());

        long workflowRrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);

        Assert.isFalse(workflowRrn <= 0,
                       Errors.create().key(MessageIdList.PROCESS_MISSING_PROCESS).content("Process does not exist!")
                             .build());

        Assert.isFalse(StringUtils.isBlank(processVersionId), Errors.create().key(MessageIdList.PROCESS_EMPTY_VERSIONID)
                                                                    .content("Process version is required!").build());

        Long productRrn = null;
        if (StringUtils.isNotBlank(productId)) {
            productRrn = getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);

            Assert.isFalse(productRrn <= 0, Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING)
                                                  .content("Product Id does not exist !").build());

            boolean isProductProcessBined = isProductProcessBined(productRrn, workflowRrn);
            Assert.isTrue(isProductProcessBined, Errors.create().key(MessageIdList.PRODUCT_PRODUCT_PROCESS_NOT_BOUND)
                                                       .content("Product and process is not bound!").build());
        }
        int processVersion = parseProcessVersion(processVersionId);
        boolean hasActiveFlag = specService.hasActiveProcessSpec(workflowRrn, processVersion);

        return hasActiveFlag;
    }

    /**
     * 解析wfltree,封装成map
     *
     * @param wflTree
     * @return
     */
    public List<Map<String, Object>> pareWflTree(List wflTree, int contextStatus, List<Map<String, Object>> routeMap) {
        List<Map<String, Object>> nodes = new ArrayList<>();
        String seq;
        for (int i = 0; i < wflTree.size(); i++) {
            Map<String, Object> routeNode = null;
            String[] node = (String[]) wflTree.get(i);
            if (IS_ROUTE_Y.equals(node[7])) {
                routeNode = new HashMap<String, Object>();
                String stepRrn = node[6].substring(node[6].indexOf(".") + 1, node[6].length());
                stepRrn = stepRrn.substring(stepRrn.indexOf(".") + 1, stepRrn.length());
                List<Map<String, Object>> wflPaths = prpService.getWflPathsByCurNode(Long.parseLong(stepRrn));
                if (wflPaths.size() > 1) {
                    routeNode.put("text", "<font color='#ff0000'>" + node[2] + "</font>");
                    routeNode.put("multiLinks", true);
                    for (Map<String, Object> wflPath : wflPaths) {
                        String pathName = MapUtils.getString(wflPath, "pathName");
                        Long nextStepRrn = MapUtils.getLong(wflPath, "nextStepRrn");
                        if (!DEFAULT_KEY.equalsIgnoreCase(pathName)) {
                            buildRoute(wflTree, routeMap, nextStepRrn);
                        }
                    }
                } else {
                    routeNode.put("text", node[2]);
                    routeNode.put("multiLinks", false);
                }
                seq = node[2];
                routeNode.put("routeSeq", seq.substring(0, seq.indexOf("-")));
                routeNode.put("cls", "folder");
                routeNode.put("children", new ArrayList());
                routeNode.put("id", node[6]);
                routeNode.put("contextStatus", contextStatus);
                nodes.add(routeNode);
            } else if (IS_ROUTE_N.equals(node[7])) {
                Map<String, Object> operationNode = new HashMap<String, Object>();
                String stepRrn = node[6].substring(node[6].indexOf("|") + 1, node[6].length());
                stepRrn = stepRrn.substring(stepRrn.indexOf(".") + 1, stepRrn.length());
                stepRrn = stepRrn.substring(stepRrn.indexOf(".") + 1, stepRrn.length());

                // #39728 前台显示将null过滤
                String node5 = StringUtils.replace(node[5], "null", "");

                Map curRouteNode = nodes.get(nodes.size() - 1);
                List<Map<String, Object>> wflPaths = prpService.getWflPathsByCurNode(Long.parseLong(stepRrn));
                if (wflPaths.size() > 1) {
                    operationNode.put("multiLinks", true);
                    operationNode.put("text", "<font color='#ff0000'>" + node[2] + "---" + node5 + "</font>");
                    if (!MapUtils.getBoolean(curRouteNode, "multiLinks")) {
                        curRouteNode.put("text", "<font color='#ff0000'>" + MapUtils.getString(curRouteNode, "text") +
                                "</font>");
                    }
                } else {
                    operationNode.put("text", node[2] + "---" + node5);
                    operationNode.put("multiLinks", false);
                }
                seq = node[2];
                operationNode.put("operationSeq", seq.substring(0, seq.indexOf("-")));
                operationNode.put("leaf", new Boolean(true));
                operationNode.put("id", node[6]);
                operationNode.put("contextStatus", contextStatus);

                List<Map<String, Object>> curRouteChildNodes = (List<Map<String, Object>>) curRouteNode.get("children");
                curRouteChildNodes.add(operationNode);
            }
        }
        return nodes;
    }

    /**
     * 组装其他分支route数据,放入wflTree中
     * @param wflTree
     * @param routeMap
     * @param nextStepRrn
     */
    private void buildRoute(List wflTree, List<Map<String, Object>> routeMap, Long nextStepRrn) {
        for (Map<String, Object> map : routeMap) {
            Long routeRrn = MapUtils.getLong(map, "routeRrn");
            String routeSeq = MapUtils.getString(map, "routeSeq");
            String routeId = MapUtils.getString(map, "routeId");
            Integer routeVersion = MapUtils.getInteger(map, "routeVersion");
            Long wflStepRrn = MapUtils.getLong(map, "wflStepRrn");
            Long processRrn = MapUtils.getLong(map, "processRrn");
            Integer processVersion = MapUtils.getInteger(map, "processVersion");
            if (nextStepRrn.longValue() == wflStepRrn) {
                String[] routeNode = new String[8];
                routeNode[0] = IS_ROUTE_N;
                routeNode[1] = StringUtils.toString(wflStepRrn);
                routeNode[2] = routeSeq + CONNECT_LINE + routeId;
                routeNode[3] = INSTANCE_ID;
                routeNode[4] = StringUtils.toString(routeRrn);
                routeNode[5] = StringUtils.SPACE;
                routeNode[6] = processRrn + StringUtils.POINT_SIGN + processVersion + StringUtils.POINT_SIGN + wflStepRrn;
                routeNode[7] = IS_ROUTE_Y;
                wflTree.add(routeNode);
                List workFlowTree = prpService
                        .getWorkFlowTree(routeRrn, routeVersion, new HashMap<>());
                wflTree.addAll(workFlowTree);
                break;
            }
        }
    }

    /**
     * 获取workflow path 下拉框信息
     *
     * @param map
     * @return
     */
    public Map<String, Object> getWflPaths(Map<String, Object> map) throws Exception {

        String stepRrn = MapUtils.getString(map, "stepRrn");
        List<Map<String, Object>> wflPaths = prpService.getWflPathsByCurNode(NumberUtils.toLong(stepRrn, 0L));
        List<Map<String, Object>> wflPathInfos = new ArrayList<>();
        for (Iterator it = wflPaths.iterator(); it.hasNext(); ) {
            Map<String, Object> wflPath = (Map<String, Object>) it.next();
            Map<String, Object> wflPathInfo = new HashMap<String, Object>();
            wflPathInfo.put("pathId", wflPath.get("pathId"));
            wflPathInfo.put("pathName", wflPath.get("pathName"));
            wflPathInfos.add(wflPathInfo);
        }
        Map<String, Object> dataMap = new HashMap<String, Object>(10);
        dataMap.put("wflPaths", wflPathInfos);
        return dataMap;
    }

    /**
     * 添加路径信息
     *
     * @param theform
     * @return
     * @throws Exception
     */

    public Map<String, Object> saveWflPath(ContextValueForm theform) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        Long userRrn = LocalContext.getUserRrn();
        String user = LocalContext.getUserId();
        String wflPath = StringUtils.trimToUpperCase(theform.getResultValue1());
        if (StringUtils.isNotEmpty(wflPath)) {
            theform.setWflPath(wflPath);
            theform.setResultValue1(null);
        }
        ContextValue contextValue = validateAndBuildContextValue(theform, TransactionNames.CREATE_KEY);

        validateMultipathInfo(facilityRrn, contextValue);

        contextValue.setStatus("ACTIVE");
        ctxService.removeContextValue(contextValue, facilityRrn, userRrn);
        ctxService.createContextValueWithActivatedEcn(facilityRrn, userRrn, user, contextValue);
        Map<String, Object> map = new HashMap<>();
        map.put("success", new Boolean(true));
        return map;
    }

    /**
     * 删除路径信息
     *
     * @param theform
     * @return
     * @throws Exception
     */
    public Map<String, Object> deleteWflPath(ContextValueForm theform) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        Long userRrn = LocalContext.getUserRrn();
        String wflPath = StringUtils.trimToUpperCase(theform.getResultValue1());
        if (StringUtils.isNotEmpty(wflPath)) {
            theform.setWflPath(wflPath);
            theform.setResultValue1(null);
        }

        ContextValue contextValue = validateAndBuildContextValue(theform, TransactionNames.DELETE_KEY);
        contextValue.setStatus("ACTIVE");
        ctxService.removeContextValue(contextValue, facilityRrn, userRrn);
        Map<String, Object> map = new HashMap<>();
        map.put("success", new Boolean(true));
        return map;
    }


    public String getStepRrnForOpertion(Map map) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String processId = MapUtils.getString(map, "processId");
        String routeId = MapUtils.getString(map, "routeId");
        String operationId = MapUtils.getString(map, "operationId");
        ;
        String processVersionId = MapUtils.getString(map, "processVersionId");
        ;

        Long workflowRrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
        int workflowVersion = parseProcessVersion(processVersionId);
        Long routeRrn = getInstanceRrn(routeId, facilityRrn, ObjectList.WFL_KEY);
        // 获取当前流程下的route version
        Integer routeVersion = prpService.getWFLVersionByProcess(workflowRrn, workflowVersion, routeRrn);
        long steprrn = prpService.getStepRrnForStep(routeRrn, operationId, routeVersion);
        return steprrn + "";
    }

    public String getStepRrnForRoute(Map map) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();

        String processId = MapUtils.getString(map, "processId");
        String routeId = MapUtils.getString(map, "routeId");

        Long workflowRrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
        String processVersionId = MapUtils.getString(map, "processVersionId");
        ;
        int workflowVersion = parseProcessVersion(processVersionId);

        long steprrn = prpService.getStepRrnForStep(workflowRrn, routeId, workflowVersion);
        return steprrn + "";
    }


    private Map getMatchRules(ContextValueForm theform) throws Exception {
        Map matchingRules = new HashMap();
        Long facilityRrn = LocalContext.getFacilityRrn();

        String productId = StringUtils.trimToUpperCase(theform.getProductId());
        String processId = StringUtils.trimToUpperCase(theform.getProcessId());
        if (StringUtils.isNotBlank(productId)) {
            matchingRules
                    .put("productRrn", Long.toString(getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY)));
        }

        matchingRules.put("processRrn", Long.toString(getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY)));

        return matchingRules;
    }

    private boolean isProductProcessBined(Long productRrn, long processRrn) throws Exception {
        List<Long> processRrns = prpService.getProcessRrnsByProductRrn(productRrn);
        return processRrns.contains(processRrn);
    }

    private ContextValue validateAndBuildContextValue(ContextValueForm theform, String handle) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();

        ContextValue context = new ContextValue(
                getInstanceRrn(ContextNames.WFL_LINK_CONTEXT, facilityRrn, ObjectList.CONTEXT_KEY));
        String productId = StringUtils.trimToUpperCase(theform.getProductId());
        String processId = StringUtils.trimToUpperCase(theform.getProcessId());
        String processVersionId = StringUtils.trimToUpperCase(theform.getProcessVersionId());
        int processVersion = parseProcessVersion(processVersionId);

        String routeId = StringUtils.trimToUpperCase(theform.getRouteId());
        String operationId = StringUtils.trimToUpperCase(theform.getOperationId());
        String paramLowerLimit = StringUtils.trimToUpperCase(theform.getParamLowerLimit());
        String paramUpperLimit = StringUtils.trimToUpperCase(theform.getParamUpperLimit());
        String lotPlanType = StringUtils.trimToUpperCase(theform.getLotPlanType());
        String lotType = StringUtils.trimToUpperCase(theform.getLotType());
        String parameterId = StringUtils.trimToUpperCase(theform.getParameterId());
        Long stepRrn = theform.getStepRrn();

        String recipeId = StringUtils.trimToUpperCase(theform.getRecipeId());

        String wflPath = StringUtils.trimToUpperCase(theform.getWflPath());

        String switchType = StringUtils.trim(theform.getSwitchType());
        Long rrn;

        if (StringUtils.isNotBlank(productId)) {
            rrn = getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);

            Assert.isFalse(rrn <= 0, Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING)
                                           .content("product Id does not exist !").build());
            context.setContextKey1(productId);
        }

        if (StringUtils.isNotBlank(processId)) {

            rrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.PROCESS_EMPTY_ID).content("Process id empty").build());
            context.setContextKey2(processId);
        }
        if (StringUtils.isNotBlank(routeId)) {
            rrn = getInstanceRrn(routeId, facilityRrn, ObjectList.WFL_KEY);
            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.ROUTE_EMPTY_ID).content("route id empty").build());

            context.setContextKey3(routeId);
        }
        if (StringUtils.isNotBlank(operationId)) {

            rrn = getInstanceRrn(operationId, facilityRrn, ObjectList.OPERATION_KEY);
            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.OPERATION_INVALID_ID).content("Step Id does not exist!")
                                 .build());

            context.setContextKey4(operationId);
        }

        if (processVersion != 0) {
            context.setContextKey14(processVersion + "");
        }

        List<ContextValue> contextvalues = ctxService.getContextValues(context, facilityRrn);

        if (StringUtils.isNotBlank(recipeId)) {
            rrn = getInstanceRrn(recipeId, facilityRrn, ObjectList.RECIPE_KEY);
            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.RECIPE_NOT_EXISTS).content("Recipe Id does not exist!")
                                 .build());
        }

        context.setContextKey9(recipeId);

        Assert.isFalse(!((StringUtils.isEmpty(paramLowerLimit) && StringUtils.isEmpty(paramUpperLimit) &&
                StringUtils.isEmpty(parameterId)) ||
                (!StringUtils.isEmpty(paramLowerLimit) && !StringUtils.isEmpty(paramUpperLimit) &&
                        !StringUtils.isEmpty(parameterId))), Errors.create().key(MessageIdList.WLFPATH_PARAMETER_LIMITS)
                                                                   .content("Parameter and limits must both " +
                                                                                    "empty or filled!").build());

        if (WflLinkContextSetupAttributeUtil.BY_EDC.equals(switchType)) {

            rrn = getInstanceRrn(parameterId, facilityRrn, ObjectList.PARAMETER_KEY);
            Assert.isFalse(rrn <= 0,
                           Errors.create().key(MessageIdList.PARAMETER_EMPTY_ID).content("Parameter Id does not exist!")
                                 .build());

            if (!StringUtils.equals(handle, TransactionNames.DELETE_KEY)) {
                for (ContextValue ctx : contextvalues) {
                    Assert.isTrue(StringUtils.isBlank(ctx.getContextKey10()) ||
                                          StringUtils.equals(parameterId, ctx.getContextKey10()),
                                  Errors.create().content("Different Parameter Id already exist").build());
                }
            }

            context.setContextKey10(parameterId);

            Assert.isTrue(NumberUtils.isCreatable(paramLowerLimit),
                          Errors.create().key(MessageIdList.PARAMETER_LOWER_MUST_BE_NUMBER)
                                .content("Parameter lower must be number!").build());

            Assert.isTrue(NumberUtils.isCreatable(paramUpperLimit),
                          Errors.create().key(MessageIdList.PARAMETER_UPPER_MUST_BE_NUMBER)
                                .content("Parameter upper must be number!").build());

            Double lowerLimit = NumberUtils.toDouble(paramLowerLimit);
            Double upperLimit = NumberUtils.toDouble(paramUpperLimit);

            Assert.isFalse(lowerLimit > upperLimit, Errors.create().key(MessageIdList.PARAMETER_UPPER_LOWER_INVALIDATE)
                                                          .content("Parameter upper and lower limit set " +
                                                                           "unreasonable!").build());
            contextvalues = ctxService.getContextValues(context, facilityRrn);
            if (contextvalues != null && !contextvalues.isEmpty()) {
                Iterator<ContextValue> it = contextvalues.iterator();
                ContextValue temContext;

                Double temLowerLimit;
                Double temUpperLimit;
                while (it.hasNext()) {
                    temContext = it.next();
                    if (StringUtils.equalsIgnoreCase(temContext.getContextKey11(), paramLowerLimit) &&
                            StringUtils.equalsIgnoreCase(temContext.getContextKey12(), paramUpperLimit)) {
                        continue;
                    }
                    temLowerLimit = NumberUtils.toDouble(temContext.getContextKey11(), 0D);
                    temUpperLimit = NumberUtils.toDouble(temContext.getContextKey12(), 0D);

                    Assert.isTrue(lowerLimit >= temUpperLimit || upperLimit < temLowerLimit,
                                  Errors.create().key(MessageIdList.PARAMETER_UPPER_LOWER_INVALIDATE)
                                        .content("Parameter limits set unreasonable!").build());
                }
            }

            context.setContextKey11(paramLowerLimit);
            context.setContextKey12(paramUpperLimit);
        }

        if (StringUtils.isNotBlank(lotPlanType)) {
            context.setContextKey13(lotPlanType);
        }
        if (StringUtils.isNotBlank(lotType)) {
            context.setContextKey15(lotType);
        }

        Assert.isFalse(StringUtils.isEmpty(switchType), Errors.create().content("Please choose switchType!").build());

        context.setResultValue6(switchType);

        if (!WflLinkContextSetupAttributeUtil.BY_MANUAL.equals(switchType)) {
            Assert.isFalse(StringUtils.isEmpty(wflPath), Errors.create().key(MessageIdList.WFLPATH_CHOOSE_PATH)
                                                               .content("Please choose workflow path!").build());

            if (!StringUtils.equalsIgnoreCase(DEFAULT_KEY, wflPath) &&
                    !StringUtils.equals(handle, TransactionNames.DELETE_KEY)) {
                Assert.isFalse(prpService.getWorkflowLinkCountByLinkIdAndStepRrn(wflPath, stepRrn) <= 0,
                               Errors.create().content("Please choose the correct workflow path!").build());
            }
        }


        context.setResultValue1(wflPath);
        return context;
    }

    /**
     * by prod, by edc, by manual三种方式只能设定一种
     *
     * @return
     */
    private void validateMultipathInfo(Long facilityRrn, ContextValue ctx) {

        Boolean byOperationFlag = StringUtils.isNotBlank(ctx.getContextKey4());
        Boolean byProdFlag = byOperationFlag ? WflLinkContextSetupAttributeUtil.isByProdOperationMultipath(ctx) &&
                !WflLinkContextSetupAttributeUtil.isByManualOperationMultipath(ctx) :
                WflLinkContextSetupAttributeUtil.isByProdRouteMultipath(ctx) &&
                        !WflLinkContextSetupAttributeUtil.isByManualRouteMultipath(ctx);

        if (byProdFlag) {
            ContextValue tmp = new ContextValue();
            tmp.setContextRrn(ctx.getContextRrn());
            tmp.setContextKey1(ctx.getContextKey1());
            tmp.setContextKey2(ctx.getContextKey2());
            tmp.setContextKey14(ctx.getContextKey14());
            tmp.setContextKey3(ctx.getContextKey3());
            if (byOperationFlag) {
                tmp.setContextKey4(ctx.getContextKey4());
            }
            List<ContextValue> contextValues = ctxService.getContextValues(tmp, facilityRrn);
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil
                                           .isByEdcOperationMultipath(contextValue) : WflLinkContextSetupAttributeUtil
                                           .isByEdcRouteMultipath(contextValue),
                                   Errors.create().content("By EDC multipath already exists!").build());

                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil
                                           .isByManualOperationMultipath(contextValue) :
                                           WflLinkContextSetupAttributeUtil
                                           .isByManualRouteMultipath(contextValue),
                                   Errors.create().content("By Manual multipath already exists!").build());
                }
            }
        }
        Boolean byEdcFlag = byOperationFlag ? WflLinkContextSetupAttributeUtil
                .isByEdcOperationMultipath(ctx) : WflLinkContextSetupAttributeUtil.isByEdcRouteMultipath(ctx);

        if (byEdcFlag) {
            ContextValue tmp = new ContextValue();
            tmp.setContextRrn(ctx.getContextRrn());
            tmp.setContextKey1(ctx.getContextKey1());
            tmp.setContextKey2(ctx.getContextKey2());
            tmp.setContextKey14(ctx.getContextKey14());
            tmp.setContextKey3(ctx.getContextKey3());
            if (byOperationFlag) {
                tmp.setContextKey4(ctx.getContextKey4());
            }
            List<ContextValue> contextValues = ctxService.getContextValues(tmp, facilityRrn);
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil.isByProdOperationMultipath(contextValue) &&
                                           !WflLinkContextSetupAttributeUtil.isByManualOperationMultipath(contextValue) :
                                           WflLinkContextSetupAttributeUtil.isByProdRouteMultipath(contextValue) &&
                                                   !WflLinkContextSetupAttributeUtil.isByManualRouteMultipath(contextValue),
                                   Errors.create().content("By product multipath already exists!").build());

                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil
                                           .isByManualOperationMultipath(contextValue) :
                                           WflLinkContextSetupAttributeUtil
                                           .isByManualRouteMultipath(contextValue),
                                   Errors.create().content("By Manual multipath already exists!").build());

                }
            }
        }
        Boolean byManualFlag = byOperationFlag ? WflLinkContextSetupAttributeUtil
                .isByManualOperationMultipath(ctx) : WflLinkContextSetupAttributeUtil.isByManualRouteMultipath(ctx);
        if (byManualFlag) {
            ContextValue tmp = new ContextValue();
            tmp.setContextRrn(ctx.getContextRrn());
            tmp.setContextKey1(ctx.getContextKey1());
            tmp.setContextKey2(ctx.getContextKey2());
            tmp.setContextKey14(ctx.getContextKey14());
            tmp.setContextKey3(ctx.getContextKey3());
            if (byOperationFlag) {
                tmp.setContextKey4(ctx.getContextKey4());
            }
            List<ContextValue> contextValues = ctxService.getContextValues(tmp, facilityRrn);
            if (CollectionUtils.isNotEmpty(contextValues)) {
                for (ContextValue contextValue : contextValues) {
                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil.isByProdOperationMultipath(contextValue) &&
                                           !WflLinkContextSetupAttributeUtil.isByManualOperationMultipath(contextValue) :
                                           WflLinkContextSetupAttributeUtil.isByProdRouteMultipath(contextValue) &&
                                                   !WflLinkContextSetupAttributeUtil.isByManualRouteMultipath(contextValue),
                                   Errors.create().content("By product multipath already exists!").build());

                    Assert.isFalse(byOperationFlag ? WflLinkContextSetupAttributeUtil
                                           .isByEdcOperationMultipath(contextValue) : WflLinkContextSetupAttributeUtil
                                           .isByEdcRouteMultipath(contextValue),
                                   Errors.create().content("By EDC multipath already exists!").build());

                }
            }
        }

    }

    public List<Map> getProcessByProductId(Map<String, Object> requestParameter) {
        String productId = StringUtils.trimToUpperCase(MapUtils.getString(requestParameter, "productId"));
        long facilityRrn = LocalContext.getFacilityRrn();
        List<Map> maps = new ArrayList<>();
        if (StringUtils.isNotBlank(productId)) {
            long productRrn = this.getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);
            List<String> processIds = prpService.getProcessIdsByProductRrn(productRrn);
            maps = WebUtils.analyticalComboData(processIds);
        }
        return maps;
    }

    public List<Map> getProcessVersionByProcess(Map<String, Object> requestParameter) {
        String processId = StringUtils.trimToUpperCase(MapUtils.getString(requestParameter, "processId"));
        long facilityRrn = LocalContext.getFacilityRrn();
        List<Map> maps = new ArrayList<>();
        if (StringUtils.isNotBlank(processId)) {
            long processRrn = this.getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
            ProcessPlanning processPlanning = new ProcessPlanning();
            processPlanning.setInstanceRrn(processRrn);
            Collection processVersions = prpService.getProcessVersions(processPlanning);
            Assert.isFalse(CollectionUtils.isEmpty(processVersions),
                           Errors.create().key(MessageIdList.PROCESS_NO_VERSION)
                                 .content("There is no version under the " + "process!").build());
            List<String> processVersionIds = new ArrayList<String>();
            for (Iterator iterator = processVersions.iterator(); iterator.hasNext(); ) {
                ProcessVersion processVersion = (ProcessVersion) iterator.next();
                processVersionIds
                        .add(processVersion.getInstanceVersion() + "(" + processVersion.getVersionStatus() + ")");
            }
            maps = WebUtils.analyticalComboData(processVersionIds);
        }
        return maps;
    }

}