package com.engine.workflow.cmd.newRule.ruleDesign;

import com.engine.core.interceptor.CommandContext;
import com.engine.workflow.biz.newRule.DroolsGenerator;
import com.engine.workflow.biz.newRule.RuleExportBiz;
import com.engine.workflow.biz.newRule.RuleImportBiz;
import com.engine.workflow.biz.newRule.RuleSetBiz;
import com.engine.workflow.util.CommandUtil;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保存节点字段校验信息
 */
public class DoSaveNodeRuleCmd extends CommandUtil<Map<String, Object>> {

    public DoSaveNodeRuleCmd(){}

    public DoSaveNodeRuleCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<>();
        int isCheckAll = Util.getIntValue(Util.null2String(params.get("isCheckAll")), 0);
        int relation = Util.getIntValue(Util.null2String(params.get("relation")), 1);
        int openNodeCheck = Util.getIntValue(Util.null2String(params.get("openNodeCheck")), 0);
        int openNodeCheckRemark = Util.getIntValue(Util.null2String(params.get("openNodeCheckRemark")), 1);
        int deleteOldRule = Util.getIntValue(Util.null2String(params.get("deleteOldRule")), 0);
        String mapSrc = Util.null2String(params.get("mapSrc"));
        String srcId = Util.null2String(params.get("srcId"));
        String syncNodes = Util.null2String(params.get("syncNodes"));

        boolean result = true;

        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rsInner = new RecordSet();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            rst.executeUpdate("update workflow_flownode set openNodeCheck = ?, openNodeCheckRemark = ? where nodeid = ?", openNodeCheck, openNodeCheckRemark, srcId);
            rst.executeUpdate("delete from NEWRULE_MAPINFO where mapsrc = ? and srcid = ?", mapSrc, srcId);
            rst.executeUpdate("insert into NEWRULE_MAPINFO (mapsrc, srcid, ischeckall, relation) values (?,?,?,?)", mapSrc, srcId, isCheckAll, relation);

            //存在需同步的节点
            if (!"".equals(syncNodes)) {
                String[] syncArr = syncNodes.split(",");
                rst.executeUpdate("update workflow_flownode set openNodeCheck = ?, openNodeCheckRemark = ? where nodeid in (" + syncNodes + ")", openNodeCheck, openNodeCheckRemark);
                for (String ayncNode : syncArr) {

                    //删除原节点的规则
                    if (deleteOldRule == 1) {
                        rst.executeUpdate("delete from newrule_valuemap where mapid in (select mapid from newrule_maplist where mapsrc = ? and srcid = ?)", mapSrc, ayncNode);
                        rst.executeUpdate("delete from newrule_maplist where mapsrc = ? and srcid = ?", mapSrc, ayncNode);
                    }

                    //同步映射基本信息
                    rst.executeUpdate("delete from NEWRULE_MAPINFO where mapsrc = ? and srcid = ?", mapSrc, ayncNode);
                    rst.executeUpdate("insert into NEWRULE_MAPINFO (mapsrc, srcid, ischeckall, relation) values (?,?,?,?)", mapSrc, ayncNode, isCheckAll, relation);

                    //同步规则映射列表
                    rs.executeQuery("select a.rulepassedtip,a.rulenopasstip,a.processtype,a.orderid,a.mapid,a.ruleId,a.mapsrc,a.workflowid,b.source,b.rulename,b.rulePassedTip,b.ruleNoPassTip from newrule_maplist a,newrule_base b where a.mapsrc = ? and a.srcid = ? and a.ruleid = b.ruleid", mapSrc, srcId);
                    while (rs.next()) {
                        String rulepassedTip = rs.getString("rulepassedtip");
                        String rulenopassTip = rs.getString("rulenopasstip");
                        String processType = rs.getString("processtype");
                        int orderid = Util.getIntValue(rs.getString("orderid"),0);
                        String oldMapId = rs.getString("mapid");
                        String newMapId = RuleSetBiz.generateNewId();
                        String ruleId = rs.getString("ruleId");
                        String mapsrc = rs.getString("mapsrc");
                        String workflowid = rs.getString("workflowid");
                        String source = rs.getString("source");
                        String rulename = rs.getString("rulename");
                        String rulePassedTip = rs.getString("rulePassedTip");
                        String ruleNoPassTip = rs.getString("ruleNoPassTip");

                        if("1".equals(source)){
                            String exportJson = new RuleExportBiz().doExportJsonStr(ruleId);
                            RuleImportBiz ruleImportBiz = new RuleImportBiz(user);
                            String newRuleid = ruleImportBiz.doImport(exportJson);
                            Map<String, String> idMap = ruleImportBiz.getIdMap();
                            String newMapid = RuleSetBiz.generateNewId();
                            rs1.executeQuery("select rulePassedTip,ruleNoPassTip from newrule_base where ruleid = ?",newRuleid);
                            if(rs1.next()){
                                rulePassedTip = rs1.getString("rulePassedTip");
                                ruleNoPassTip = rs1.getString("ruleNoPassTip");
                            }
                            rst.executeUpdate("insert into newrule_maplist (mapid, ruleid, mapsrc, srcid, workflowid, rulePassedTip, ruleNoPassTip, processType) values (?,?,?,?,?,?,?,?)", newMapid,newRuleid,mapsrc,ayncNode,workflowid,rulePassedTip,ruleNoPassTip,processType);
                            rst.executeUpdate("update newrule_base set rulename = ? where ruleid = ?",rulename,newRuleid);
                            rs1.executeQuery("select * from newrule_valuemap where ruleid = ? order by valueid",ruleId);
                            while(rs1.next()){
                                String valueid = rs1.getString("valueid");
                                String objtype = rs1.getString("objtype");
                                String objvalue = rs1.getString("objvalue");
                                String paramOrderId = rs1.getString("paramOrderId");
                                rst.executeUpdate("insert into newrule_valueMap (ruleid, mapid, valueid, paramOrderId, objtype, objvalue) values (?,?,?,?,?,?)",newRuleid,newMapid,idMap.get(valueid),"".equals(paramOrderId)?null:paramOrderId,objtype,objvalue);
                            }

                        }else{
                            rst.executeUpdate("insert into newrule_maplist (mapsrc, srcid, rulepassedtip, rulenopasstip, processtype, orderid, mapid, ruleid) values (?,?,?,?,?,?,?,?)",
                                    mapSrc, ayncNode, rulepassedTip, rulenopassTip, processType, orderid, newMapId, ruleId);
                            //同步值映射
                            rsInner.executeQuery("select * from newrule_valuemap where mapid = ?", oldMapId);
                            while (rsInner.next()) {
                                String valueId = rsInner.getString("valueid");
                                String paramOrderId = rsInner.getString("paramOrderId");
                                String objType = rsInner.getString("objType");
                                String objValue = rsInner.getString("objValue");
                                rst.executeUpdate("insert into newrule_valuemap (ruleid, valueid, paramOrderId, objtype, objvalue, mapid) values (?,?,?,?,?,?)", ruleId, valueId, "".equals(paramOrderId) ? null : paramOrderId, objType, objValue, newMapId);
                            }
                        }

                    }
                }
            }

            rst.commit();
            apidatas.put("nodeFieldCheck", openNodeCheck==1);
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
            rst.rollback();
        }

        apidatas.put("result", result);
        return apidatas;
    }
}
