package com.weaver.seconddev.njwebservice.util;

import com.weaver.common.hrm.util.Util;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName WfUtil
 * Author
 * Date 2025/1/2 8:56
 * Description WfUtil
 * Version 1.0
 */
public class WfUtil {
    private final Logger log = LoggerFactory.getLogger(WfUtil.class);

    /**
     *
     * @param jtzhTenantKey 集团租户TenantKey
     * @param workcode 流程创建人工号
     * @param wfid 流程id
     * @return
     */
    public Map<String,String> getZhWfMap(String jtzhTenantKey,String workcode,String wfid){
        log.error("getZhWfMap start : " + wfid );
        String currentWfid = wfid;//触发过来的wfid
        String validWfif = "";//有效wfid
        if(currentWfid.length()<=8){
            currentWfid = getStrNum(currentWfid,8);
        }
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.ebuilder.getType();
        rs.setTenantKey(jtzhTenantKey);
        //从集团租户 根据传入的wfid获取有效的wfid
        validWfif = getValidWfid(jtzhTenantKey,currentWfid,"E10JTZH",jtzhTenantKey);
        log.error("getZhWfMap validWfif : " + validWfif );
        if("".equals(validWfif)){//如果为空就是其他租户的新流程
            validWfif = currentWfid;
        }
        //根据传入的wfid和获取到的当前有效的wfid 从配置表获取对应租户和对应wfid
        String sszh = "";//所属租户
        String jtwfid = "";//集团wfid
        String gdwfid = "";//光电wfid
        String hhwfid = "";//华海wfid
        String workflowid = "";
        String sql = "select sszh,jtwfid,gdwfid,hhwfid from e10_common.dbo.uf_zhwfid_mt where (cfwfid like '%"+currentWfid+"' or cfwfid like '%"+validWfif+"') and delete_type ='0'";
        rs.executeSql(sql,poolname);
        if(rs.next()){
            sszh = Util.null2String(rs.getString("sszh"));
            jtwfid = Util.null2String(rs.getString("jtwfid"));
            gdwfid = Util.null2String(rs.getString("gdwfid"));
            hhwfid = Util.null2String(rs.getString("hhwfid"));
        }

       log.error("getZhWfMap sszh : " + sszh );
       String  tenantKey = "";
        if("3".equals(sszh)){//全租户 判断创建人是哪个租户的 取对应租户的 wfid;
            String zhbs = getZhbs(workcode,jtzhTenantKey);
            sszh = getZhbsInfo(zhbs,jtzhTenantKey);
        }

        Map<String,String > zhmap = getZhTenantKey(sszh,jtzhTenantKey);
        //log.error(JSON.toJSONString(zhmap));
        tenantKey = zhmap.get("zzkey");
        String zhsjy = zhmap.get("zhsjy");;

       if("0".equals(sszh)){//集团
           workflowid =jtwfid;
       }else if("1".equals(sszh)){//光电
           workflowid =gdwfid;
       }else if("2".equals(sszh)){//华海租户
           workflowid = hhwfid;
       }
        log.error("getZhWfMap workflowid : " + workflowid );
       if("".equals(workflowid)){
           workflowid = validWfif;
           tenantKey = jtzhTenantKey;
           zhsjy = "E10JTZH";
       }
        validWfif = getValidWfid(tenantKey,workflowid,zhsjy,jtzhTenantKey);

        Map<String,String> map = new HashMap<>();
        map.put("workflowid",validWfif);
        map.put("tenantKey",tenantKey);
        map.put("zhsjy",zhsjy);
        log.error("map : " + map );
        return map;
    }

    public Map<String,String> getZhWfMapNoUser(String jtzhTenantKey,String wfid){
        log.error("getZhWfMapNoUser start : " + wfid );
        String currentWfid = wfid;//触发过来的wfid
        String validWfif = "";//有效wfid
        if(currentWfid.length()<=8){
            currentWfid = getStrNum(currentWfid,8);
        }
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.ebuilder.getType();
        rs.setTenantKey(jtzhTenantKey);
        //从集团租户 根据传入的wfid获取有效的wfid
        validWfif = getValidWfid(jtzhTenantKey,currentWfid,"E10JTZH",jtzhTenantKey);
        log.error("getZhWfMapNoUser validWfif : " + validWfif );
        if("".equals(validWfif)){//如果为空就是其他租户的新流程
            validWfif = currentWfid;
        }
        //根据传入的wfid和获取到的当前有效的wfid 从配置表获取对应租户和对应wfid
        String sszh = "";//所属租户
        String jtwfid = "";//集团wfid
        String gdwfid = "";//光电wfid
        String hhwfid = "";//华海wfid
        String workflowid = "";
        String sql = "select sszh,jtwfid,gdwfid,hhwfid from e10_common.dbo.uf_zhwfid_mt where (cfwfid like '%"+currentWfid+"' or cfwfid like '%"+validWfif+"') and delete_type ='0'";
        rs.executeSql(sql,poolname);
        if(rs.next()){
            sszh = Util.null2String(rs.getString("sszh"));
            jtwfid = Util.null2String(rs.getString("jtwfid"));
            gdwfid = Util.null2String(rs.getString("gdwfid"));
            hhwfid = Util.null2String(rs.getString("hhwfid"));
        }

        log.error("getZhWfMapNoUser sszh : " + sszh );
        String  tenantKey = "";
//        if("3".equals(sszh)){//全租户 判断创建人是哪个租户的 取对应租户的 wfid;
//            String zhbs = getZhbs(workcode,jtzhTenantKey);
//            sszh = getZhbsInfo(zhbs,jtzhTenantKey);
//        }
        Map<String,String > zhmap = getZhTenantKey(sszh,jtzhTenantKey);
        //log.error(JSON.toJSONString(zhmap));
        tenantKey = zhmap.get("zzkey");
        String zhsjy = zhmap.get("zhsjy");

        if("0".equals(sszh)){//集团
            workflowid =jtwfid;
        }else if("1".equals(sszh)){//光电
            workflowid =gdwfid;
        }else if("2".equals(sszh)){//华海租户
            workflowid = hhwfid;
        }
        log.error("getZhWfMapNoUser workflowid : " + workflowid );
        if("".equals(workflowid)){
            workflowid = validWfif;
            tenantKey = jtzhTenantKey;
        }
        validWfif = getValidWfid(tenantKey,workflowid,zhsjy,jtzhTenantKey);


        Map<String,String> map = new HashMap<>();
        map.put("workflowid",validWfif);
        map.put("tenantKey",tenantKey);
        map.put("zhsjy",zhsjy);
        log.error("map : " + map );

        return map;
    }

    public String getZhbs(String workcode,String jtzhTenantKey){
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.hrm.getType();
        rs.setTenantKey(jtzhTenantKey);
        String field3 = "";
        String sql = "select isnull(field3,'') as field3 from eteams.dbo.employee a,eteams.dbo.hrmemployeedefined b where a.formdata =b.FORM_DATA_ID  and a.job_num = '"+workcode+"' AND a.delete_type = 0 AND a.tenant_key = '"+jtzhTenantKey+"' AND b.delete_type = 0 AND b.tenant_key = '"+jtzhTenantKey+"'";
        rs.executeSql(sql,poolname);
        if(rs.next()){
            field3 = Util.null2String(rs.getString("field3"));
        }
        if("".equals(field3)||"mgl".equals(field3)){
            field3="htjt";
        }
        return field3;
    }

    public String getZhbsInfo(String zhbs,String jtzhTenantKey){
        String sszh = "";
        Map<String,String> map = new HashMap<>();
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.ebuilder.getType();
        rs.setTenantKey(jtzhTenantKey);
        String sql = "select zzkey,sszh from e10_common.dbo.uf_tenant_mt where zhbs='"+zhbs+"' and delete_type ='0'";
        rs.executeSql(sql,poolname);
        if(rs.next()){
            sszh = Util.null2String(rs.getString("sszh"));
        }
        return sszh;
    }


    public Map<String,String> getZhTenantKey(String sszh,String jtzhTenantKey){
        String zzkey = "";
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.ebuilder.getType();
        rs.setTenantKey(jtzhTenantKey);
        String zhsjy = "";
        String sql = "select zzkey,zhsjy from e10_common.dbo.uf_tenant_mt where sszh='"+sszh+"' and delete_type ='0'";
        rs.executeSql(sql,poolname);
        if(rs.next()){
            zzkey = Util.null2String(rs.getString("zzkey"));
            zhsjy = Util.null2String(rs.getString("zhsjy"));
        }
        Map<String,String> map = new HashMap<>();
        map.put("zzkey",zzkey);
        map.put("zhsjy",zhsjy);
        return map;
    }



    public String getValidWfid(String tenantKey,String wfid,String zhsjy,String jtzhTenantKey) {
        String workflowid = "";
        try{

            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            rs.setTenantKey(jtzhTenantKey);
            String sql = "SELECT CASE  WHEN status = 1 THEN id ELSE activewfid END AS workflowid FROM e10_core_business.dbo.wfp_base WHERE id like '%" + wfid + "' AND  delete_type = 0 AND  tenant_key = '" + tenantKey + "'";
            log.error("oaflowid: "+sql+" zhsjy: "+zhsjy + "   tenantKey: " +tenantKey );
            boolean b1 = rs.executeQuery(sql, zhsjy);
            log.error("b1 : "+b1 + "result: "+rs.getExecuteResult().toString());

            if (rs.next()) {
                workflowid = Util.null2String(rs.getString("workflowid"));
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("getValidWfid -err " + e);
        }

        return workflowid;
    }


    public  String getStrNum(String num,int len){
        String buff = num;
        int max = len - buff.length();
        for(int index = 0; index < max;index++){
            buff = "0" + buff;
        }
        return buff;
    }
}
