package com.chinaweal.aiccs.common.util;

import com.chinaweal.aiccs.aiccs.business.rules.init.InitRules;
import com.chinaweal.aiccs.aiccs.business.rules.model.NodeModel;
import com.chinaweal.aiccs.aiccs.business.rules.model.RuleModel;
import com.chinaweal.aiccs.aiccs.business.rules.model.SequenceFlowModel;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021/1/29 17:13
 **/
public class WorkFlowUtil {
    private final static Map<String, String> nodeNameCacheMap = new ConcurrentHashMap<>();

    public static String getNodeName(String nodeid) {
        Map<String, RuleModel> ruleModelMap = InitRules.getRuleModels();
        String nodeName = nodeNameCacheMap.get(nodeid);
        if (nodeName != null) {
            return nodeName;
        } else {
            NodeModel nodeModel;
            for (Map.Entry<String, RuleModel> entry : ruleModelMap.entrySet()) {
                for (Map.Entry<String, NodeModel> _entry : entry.getValue().getNodeModelMap().entrySet()) {
                    nodeModel = _entry.getValue();
                    if (nodeModel.getId().equals(nodeid)) {
                        nodeNameCacheMap.put(nodeid, nodeModel.getName());
                        return nodeModel.getName();
                    }
                }
            }
            return null;
        }
    }

    public static void checkXMLRule(String definedname, String fromNodeID, String nextNodeID) {
        if (fromNodeID == null) {
            throw new BusinessException("开始节点为空，请检查。");
        }
        if (nextNodeID == null) {
            throw new BusinessException("下一节点为空，请检查。");
        }
        RuleModel ruleModel = InitRules.getRuleModels().get(definedname);
        if (ruleModel == null) {
            throw new BusinessException("找不到规则定义" + definedname + "，请检查。");
        }
        Map<String, NodeModel> nodeModelMap = ruleModel.getNodeModelMap();
        NodeModel fromNode = nodeModelMap.get(fromNodeID);
        if (fromNode == null) {
            throw new BusinessException("开始节点" + fromNodeID + "不存在，请检查。");
        }
        NodeModel toNode = nodeModelMap.get(nextNodeID);
        if (toNode == null) {
            throw new BusinessException("下一节点" + nextNodeID + "不存在，请检查。");
        }
        Map<String, SequenceFlowModel> sequenceFlowModelMap = ruleModel.getSequenceFlowModelMap();
        if (sequenceFlowModelMap.get(fromNodeID + "_" + nextNodeID) == null) {
            throw new BusinessException(fromNodeID + "_" + nextNodeID + "流程线不存在，请检查。");
        }
    }

    /**
     * 判断下一节点在流程中是前进、回退
     *
     * @param currentNode  当前流程节点
     * @param nextNode     下一流程节点
     * @return             '>0'是正向，'<0'则是回退，'=0'则同一流程节点
     */
    public static int checkNextOrder(String currentNode,String nextNode){
        String next = FlowUtil.getChangeLink(nextNode);
        if(next == null){
            throw new BusinessException("下一节点" + nextNode + "不存在，请检查。");
        }
        String cur = FlowUtil.getChangeLink(currentNode);
        if(cur == null) {
            throw new BusinessException("下一节点" + currentNode + "不存在，请检查。");
        }
        Integer nextInt = Integer.valueOf(next);
        Integer curInt = Integer.valueOf(cur);
        return nextInt-curInt;
    }
}
