package com.engine.htsc.generaltrip.tools;

import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

public class CcsqModelTools {
    public String getZbJobGrade(String userId) {
        String sql = "select a.deptTypeCode,a.deptdepth,a.deptdirectors,h.workcode,h.SECLEVEL " +
                "from hrmdepartmentdefined a,hrmresource h where a.deptid = h.departmentid " +
                " and h.id = " + userId + "";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        Integer deptdepth = null;
        Integer secLevel = null;
        String workcode = "";
        String deptTypeCode = "";
        String result = "";
        if (rs.next()) {
            deptdepth = Util.getIntValue(Util.null2String(rs.getString("deptdepth")));
            secLevel = Util.getIntValue(Util.null2String(rs.getString("SECLEVEL")));
            workcode = Util.null2String(rs.getString("workcode"));
            deptTypeCode = Util.null2String(rs.getString("deptTypeCode"));
            if ("JGLX01".contains(deptTypeCode)){
                if (secLevel>=0&&secLevel<50) {
                    if (2==deptdepth){
                        result = "PT_LV2";//二级部门普通员工
                    }else {
                        result = "PT";//一级部门普通员工
                    }
                } else if (secLevel == 70) {
                    if (2==deptdepth){
                        result = "PT_LV2";//二级部门普通员工
                    }else {
                        result = "PT";//一级部门普通员工
                    }
                } else if (secLevel == 50) {
                    if (2==deptdepth){
                        result = "FZR_LV2";//二级部门负责人
                    }else {
                        result = "FZR";//一级部门负责人
                    }
                }
                if ("3".equals(getSflwpq(workcode))){
                    result = "LWPQ";//劳务派遣员工
                }
            }else {
                if (secLevel>=0&&secLevel<50) {
                    result = "PT";//部门普通员工
                }else if (secLevel == 70) {
                    result = "PT";//部门普通员工
                }else if (secLevel == 50) {
                    result = "FZR";//一级部门负责人
                }
            }
        }
        String sqlFgld = "select id,deptleaders from hrmdepartmentdefined where  ','||deptleaders||',' like '%,"+userId+",%' and deptTypeCode in ('JGLX14','JGLX15')";
        RecordSet rsFgld = new RecordSet();
        rsFgld.execute(sqlFgld);
        if (rsFgld.next()){
            result = "FGLD";//资管的分管领导
        }
        return result;
    }

    //数组前后去重,返回param中base不包含的数据
    public String distinctString(String param,String base){
        String result = "";
        if (null!=base&&!"".equals(base)){
            List<String> baseArray = Arrays.asList(base.split(","));
            if (null!=param&&!"".equals(param)){
                List<String> paramArray = Arrays.asList(param.split(","));
                for (String str:paramArray){
                    if (!baseArray.contains(str)){
                        result+=result==""?str:","+str;
                    }
                }
            }
        }else {
            result = param;
        }
        return result;
    }

    //判断是否为劳务派遣,1为普通数字开头，2为sx，SX实习生，3为其他劳务派遣
    public String getSflwpq(String code){
        String result = "";
        Pattern pNum = Pattern.compile("[0-9]*");
        Matcher isNum = pNum.matcher(code.charAt(0)+"");
        if (isNum.matches()){
            result = "1";
        }else if (code.startsWith("sx")||code.startsWith("SX")){
            result = "2";
        }else {
            result = "3";
        }
        return result;
    }

    //判断条件逻辑与和逻辑或 ：|| ，&&
    public static boolean getConitionParams(String tj, Map<String,String> detailmap){
        boolean flag=false;
        if("".equals(tj)){
            return true;
        }
        String value="";
        List<String> list=new ArrayList();

        String[] split=tj.split("\\|\\|");
        for (String tjsplitljy:split) {
            //开始做逻辑与操作
            String[] splitljy = tjsplitljy.split("\\&\\&");
            list=new ArrayList();
            for (String tjsplitvalue : splitljy) {
                if(tjsplitvalue.split("#").length==3){
                    list.add(tjsplitvalue.replace("#",""));
                }else {
                    list.add(tjsplitvalue);
                }
            }
            for (String tjvalue : list) {
                flag = IsConform(tjvalue, detailmap);
                if (flag == false) {
                    break;
                }
            }
            if(flag==true){
                break;
            }
        }
        return flag;
    }
    //判断是否符合要求
    public static boolean IsConform(String tj,Map<String,String> map){
        boolean flag=true;
        String[] params = getParamsSplit(tj);
        if(params==null){
            return false;
        }
        String key = params[0];
        String value = params[1];
        String conition = params[2];
        if (">=".equals(conition)){
            Double valuedouble= weaver.general.Util.getDoubleValue(weaver.general.Util.null2String(map.get(key)));
            if(!getDoubleIsNull(valuedouble)){
                flag=false;
            }
            if( valuedouble< weaver.general.Util.getDoubleValue(value)){
                flag=false;
            }
        }else if ("<=".equals(conition)){
            Double valuedouble= weaver.general.Util.getDoubleValue(weaver.general.Util.null2String(map.get(key)));
            if(!getDoubleIsNull(valuedouble)){
                flag=false;
            }
            if(valuedouble > weaver.general.Util.getDoubleValue(value)){
                flag=false;
            }
        }else if (">".equals(conition)){
            Double valuedouble= weaver.general.Util.getDoubleValue(weaver.general.Util.null2String(map.get(key)));
            if(!getDoubleIsNull(valuedouble)){
                flag=false;
            }
            if(valuedouble <= weaver.general.Util.getDoubleValue(value)){
                flag=false;
            }
        }else if ("<".equals(conition)){
            Double valuedouble= weaver.general.Util.getDoubleValue(weaver.general.Util.null2String(map.get(key)));
            if(!getDoubleIsNull(valuedouble)){
                flag=false;
            }
            if(valuedouble >= weaver.general.Util.getDoubleValue(value)){
                flag=false;
            }
        }else if ("!=".equals(conition)){
            if(weaver.general.Util.null2String(map.get(key)).equals(value)){
                flag=false;
            }
        }else if ("=".equals(conition)){
            if(!weaver.general.Util.null2String(map.get(key)).equals(value) ){
                flag=false;
            }
        }
        return  flag;
    }

    public static String[] getParamsSplit(String value){
        String[] values = new String[3];
        if(value.split(">=").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split(">="),values);
            values[2]=">=";
            return values;
        }
        if(value.split("<=").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split("<="),values);
            values[2]="<=";
            return values;
        }
        if(value.split(">").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split(">"),values);
            values[2]=">";
            return values;
        }
        if(value.split("<").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split("<"),values);
            values[2]="<";
            return values;
        }

        if(value.split("!=").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split("!="),values);
            values[2]="!=";
            return values;
        }

        if(value.split("=").length==2){
            values= JAVATools.ArrayCodyAtoB(value.split("="),values);
            values[2]="=";
            return values;
        }
        return null;
    }

    public static boolean getDoubleIsNull(double i){
        boolean flag=true;
        if(i<0){
            flag=false;
        }
        return flag;
    }

    public String getJglx(String depTypeCode) {
        String jglx = "";
        if ("JGLX01".contains(depTypeCode)){
            jglx = "zb";//总部
        }else if("JGLX03".contains(depTypeCode)){
            jglx = "fgs";//分公司
        }else if("JGLX04".contains(depTypeCode)){
            jglx = "yyb";//营业部
        }else if("JGLX09".contains(depTypeCode)){
            jglx = "ywb";//业务部
        }else if("JGLX02".contains(depTypeCode)){
            jglx = "shzx";//私行中心
        }else if("JGLX14,JGLX15".contains(depTypeCode)){
            jglx = "zg";//资管
        }else if("JGLX20".contains(depTypeCode)){
            jglx = "zj";//紫金
        }else if("JGLX22".contains(depTypeCode)){
            jglx = "ct";//创投
        }
        return jglx;
    }
}
