package com.engine.htsc.workflow.cmd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.management.openmbean.OpenMBeanConstructorInfo;
import java.util.*;
import java.util.stream.Collectors;

public class DesignedProcessorCmd extends AbstractCommonCommand<Map<String, Object>> {

    private static final String OTHERS = "others";
    private static final String ALL = "all";

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

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        writeLog("DesignedProcessor input ele:{}", JSONObject.toJSONString(params));
        String userCodes = Util.null2String(params.get("userIds")); //发起人id
//      List<String> userCodeList = JSON.parseArray(userCodes,String.class);
        String lcId = Util.null2String(params.get("lcId"));//流程id
        String tjMap = Util.null2String(params.get("tjMap"));//条件map，格式为{zd=z, zd=z, zd=z}
        Map<String, String> inputMap = JSON.parseObject(tjMap, Map.class);
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(userCodes) || StringUtils.isEmpty(lcId) || StringUtils.isEmpty(tjMap)) {
            //如果入参有问题，则返回空
            writeLog("DesignedProcessor input error");
            result.put("zdclr", new ArrayList<>());
            return result;
        }
        writeLog("DesignedProcessor inputMap:{}", JSONObject.toJSONString(inputMap));
        try {
            RecordSet rs = new RecordSet();//从主表中选出涉及到该流程和发起人的配置
            rs.executeQuery("select tt.* from uf_zdclrpz tt , workflow_base e " +
                            "where e.unnumber = tt.lc " +
                            "and e.id=? " +
                            "and tt.dqpzld in ( " + userCodes + " )",
                    new Object[]{lcId});
            List<String> zdclrList = new ArrayList<>();
            while (rs.next()) {
                //从明细表中选出该流程对应的详细配置，并判断是否满足详细条件
                String id = Util.null2String(rs.getString("id"));

                writeLog("DesignedProcessor uf_zdclrpz id: " + id);

                RecordSet rt = new RecordSet();
                rt.executeQuery("select * from UF_ZDCLRPZ_DT1 " +
                        " where mainid = ?", id);
                Map<String, Map<String, String>> zdclrMap = new HashMap<>();
                while (rt.next()) {
                    String zd = Util.null2String(rt.getString("zd"));
                    String z = Util.null2String(rt.getString("z"));
                    String zdclr = Util.null2String(rt.getString("zdclr"));
                    writeLog("DesignedProcessor zd:{} and z:{} and zdclr:{}", zd, z, zdclr);
                    if (!zdclrMap.containsKey(zdclr)) {
                        Map<String, String> zdMap = new HashMap<>();
                        zdMap.put(zd, z);
                        zdclrMap.put(zdclr, zdMap);
                    } else {
                        zdclrMap.get(zdclr).put(zd, z);
                    }
                }
                writeLog("DesignedProcessor zdclrMap:{}", JSONObject.toJSONString(zdclrMap));
                String others = "";
                for (Map.Entry<String, Map<String, String>> entry : zdclrMap.entrySet()) {
                    Map<String, String> zdclrValueMap = entry.getValue();
                    writeLog("DesignedProcessor zdclrValueMap:{}", JSONObject.toJSONString(zdclrValueMap));
                    boolean isTrue = true;
                    for (Map.Entry<String, String> inputEntry : inputMap.entrySet()) {
                        String zd = inputEntry.getKey();
                        String z = inputEntry.getValue();
                        writeLog("DesignedProcessor for zd:{} and z:{}", zd, z);
                        if (!zdclrValueMap.containsKey(zd)) {
                            isTrue = false;
                            continue;
                        }
                        String zdclrValue = zdclrValueMap.get(zd);
                        writeLog("DesignedProcessor zdclrValue:{}", JSONObject.toJSONString(zdclrValue));
                        List<String> zList = Arrays.asList(zdclrValue.split(","));
                        writeLog("DesignedProcessor zList:{}", JSONObject.toJSONString(zList));
                        if (zList.contains(OTHERS)) {
                            others = entry.getKey();
                        }
                        if (!zList.contains(z) && !zList.contains(OTHERS) && !zList.contains(ALL)) {
                            writeLog("DesignedProcessor isfalse");
                            isTrue = false;
                        }
                    }
                    if (isTrue) {
                        writeLog("DesignedProcessor istrue");
                        zdclrList.add(entry.getKey());
                    }
                }
                writeLog("DesignedProcessor zdclrList:{}", JSONObject.toJSONString(zdclrList));
                if (zdclrList.size() > 1) {
                    zdclrList.remove(others);
                }

//                while (rt.next()){
//
//                    zdMap.put(zd,z);
//                    boolean isTrue = true;
//                    String value = inputMap.get(zd);
//                    if(StringUtils.isEmpty(value) || !value.equals(z)){
//                      isTrue = false;
//                    }
//                    if(isTrue){
//                      zdclrList.add(Util.null2String(rt.getString("zdclr")));
//                    }
//                }

//                writeLog("DesignedProcessor zdMap:{}",JSONObject.toJSONString(zdMap));

            }
            List<Map<String, String>> idatalist = new ArrayList<>();
            Map<String, String> idata = new HashMap<>();
            if (zdclrList.size() > 0) {
                String zdclrStr = String.join(",", zdclrList);
                writeLog("DesignedProcessor zdclr:{}", zdclrStr);
                RecordSet rr = new RecordSet();//从主表中选出涉及到该流程和发起人的配置
                rr.executeQuery("select * from HRMRESOURCE where id in (" + zdclrStr + ")");
                while (rr.next()) {
                    String id = Util.null2String(rr.getString("id"));
                    String xm = Util.null2String(rr.getString("lastname"));
                    idata.put("id", id);
                    idata.put("name", xm);
                    idatalist.add(idata);
                }
            }
            writeLog("DesignedProcessor idata:{}", JSONObject.toJSONString(idatalist));
            result.put("zdclr", idatalist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }
}
