package weaver.backup.impl;

import com.engine.workflow.biz.newRule.RuleSetBiz;
import weaver.backup.beans.FieldBean;
import weaver.backup.beans.TableBean;
import weaver.backup.itf.AbstractTableAction;
import weaver.backup.services.ImportService;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NewruleValuemapTableAction extends AbstractTableAction {
    //插入数据前执行动作
    public void doBeforeInsert(TableBean table, ImportService importService) throws Exception {
        ArrayList<TableBean> tables = importService.getTables();
        RecordSet st = new RecordSet();
        st.executeQuery("select 1 from newrule_valuemap where ruleid = ?",table.getField("ruleid").getValue());
        if(!st.next()){
            ArrayList<TableBean> newRuleBaseTable = new ArrayList<>();
            ArrayList<TableBean> newRuleMaplistTable = new ArrayList<>();
            for (TableBean tabletmp:tables){
                if("newrule_base".equals(tabletmp.getTableName())){
                    newRuleBaseTable.add(tabletmp);
                }else if("newrule_maplist".equals(tabletmp.getTableName())){
                    newRuleMaplistTable.add(tabletmp);
                }
            }
            Map<String,String> newRuleMap = importService.getFieldIds("newrule_base","ruleid");
            Map<String,String> newRuleValueMap = importService.getFieldIds("newrule_valuebase","valueid");
            Map<String,List<String>> newRuleTipsMap = new HashMap();
            if(newRuleBaseTable!=null){
                for(TableBean newRulebase : newRuleBaseTable){
                    if(newRulebase == null || newRulebase.getField("ruleid")==null){
                        continue;
                    }
                    String ruleid = newRulebase.getField("ruleid").getValue();
                    String rulepassedtip = newRulebase.getField("rulepassedtip").getValue();
                    String rulenopasstip = newRulebase.getField("rulenopasstip").getValue();
                    String drl = newRulebase.getField("drl").getValue();
                    FieldBean uuidBean = newRulebase.getField(newRulebase.getUuidFieldName());
                    String uuid = uuidBean == null ? "" : uuidBean.getValue();
                    String source = newRulebase.getField("source").getValue();

                    List<String> ruleTips = new ArrayList();
                    ruleTips.add(Util.null2String(rulepassedtip));
                    ruleTips.add(Util.null2String(rulenopasstip));
                    newRuleTipsMap.put(ruleid,ruleTips);


                    if(!"".equals(drl)){
                        String regex = "rules_\\w+";
                        Pattern r = Pattern.compile(regex);
                        Matcher m = r.matcher(drl);
                        while (m.find()) {
                            String value = Util.null2String(m.group());
                            value = value.replace("rules_","");
                            if(!"".equals(Util.null2String(newRuleMap.get(value)))){
                                drl = drl.replace(value,newRuleMap.get(value));
                                break;
                            }
                        }
                    }

                    if(!"".equals(drl)){
                        String regex = "\\$obj.getValue\\(\"[\\s\\S]+?\"\\)";
                        Pattern r = Pattern.compile(regex);
                        Matcher m = r.matcher(drl);
                        while (m.find()) {
                            String value = Util.null2String(m.group());
                            value = value.replace("$obj.getValue(\"","").replace("\")","");
                            if(!"".equals(Util.null2String(newRuleValueMap.get(value)))){
                                drl = drl.replace(value,newRuleValueMap.get(value));
                            }
                        }
                    }

                    String regex = "\\$_[\\s\\S]+?_\\$";
                    Pattern r = Pattern.compile(regex);
                    for(int i=0;i<ruleTips.size();i++) {
                        String ruleTip = ruleTips.get(i);
                        Matcher m = r.matcher(ruleTip);
                        while (m.find()) {
                            String value = Util.null2String(m.group());
                            String fieldId = value.replace("_$", "");
                            if (value.startsWith("$_variableName")) {
                                fieldId = fieldId.replace("$_variableName", "");
                                if(!"".equals(Util.null2String(newRuleValueMap.get(fieldId)))){
                                    ruleTip = ruleTip.replace("$_variableName" + fieldId + "_$", "$_variableName" + newRuleValueMap.get(fieldId) + "_$");
                                }
                            } else if (value.startsWith("$_variable")) {
                                fieldId = fieldId.replace("$_variable", "");
                                if(!"".equals(Util.null2String(newRuleValueMap.get(fieldId)))){
                                    ruleTip = ruleTip.replace("$_variable" + fieldId + "_$", "$_variable" + newRuleValueMap.get(fieldId) + "_$");
                                }
                            }
                        }
                        ruleTips.set(i, ruleTip);
                    }

                    st.executeUpdate("update newrule_base set drl = ?,rulepassedtip = ?,rulenopasstip = ? where ruleid = ? and uuid = ?",drl,ruleTips.get(0),ruleTips.get(1),ruleid,uuid);
                }
            }

            if(newRuleMaplistTable!=null){
                for(TableBean newRulemaplist : newRuleMaplistTable) {
                    if(newRulemaplist == null || newRulemaplist.getField("ruleid")==null){
                        continue;
                    }
                    String ruleid = newRulemaplist.getField("ruleid").getValue();
                    String mapid = newRulemaplist.getField("mapid").getValue();

                    FieldBean uuidBean = newRulemaplist.getField(newRulemaplist.getUuidFieldName());
                    String uuid = uuidBean == null ? "" : uuidBean.getValue();
                    List<String> ruleTips = newRuleTipsMap.get(ruleid);

                    if(ruleTips==null){
                        continue;
                    }

                    st.executeUpdate("update newrule_maplist set rulepassedtip = ?,rulenopasstip = ? where mapid = ? and uuid = ?",ruleTips.get(0),ruleTips.get(1),mapid,uuid);
                }
            }
        }

        Map<String,String> fieldMap = importService.getFieldIds("workflow_billfield","id");
        String objvalue = Util.null2String(table.getField("objvalue").getValue());
        if(!"".equals(objvalue) && objvalue.indexOf("$")>-1){
            String regex = "\\$field[\\s\\S]+?\\$";
            Pattern r = Pattern.compile(regex);
            Matcher m = r.matcher(objvalue);
            while (m.find()) {
                String value1 = Util.null2String(m.group());
                String value2 = value1.substring(6,value1.length()-1);
                String newvlaue = fieldMap.get(value2);
                if(newvlaue!=null){
                    objvalue = objvalue.replace(value1,"$field"+newvlaue+"$");
                }
            }
            table.getField("objvalue").setValue(objvalue);
        }
    };
    //更新数据前执行动作
    public void doBeforeUpdate(TableBean table, ImportService importService, String newuuid) throws Exception {};
    //导入后执行动作
    public void doAfter(TableBean table, ImportService importService) throws Exception{};
    //所有都导入完成后执行动作
    public void doAfterAll(ImportService importService) throws Exception{};
}
