package com.engine.htsc.ReimbursementFlow.tool;

import com.engine.htsc.ReimbursementFlow.Dao.UfZbbxandclsplDaoImpl;
import com.engine.htsc.ReimbursementFlow.model.NewDepart;
import com.engine.htsc.ReimbursementFlow.model.NewUser;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

public  class ElementInstance {

    private final UfZbbxandclsplDaoImpl ufZbbxandclsplDao=new UfZbbxandclsplDaoImpl();
    private final  PropBean propBean=new PropBean();
    //获取送总裁审批的人
    public String getzcsp(String value){
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        //目前总裁只有一种，就是用编号
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            if(!"".equals(hrmid)){
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }
    //获取送分管领导的人
    public String getfgldry (String value, NewUser newUser){
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        //目前总裁只有一种，就是用编号
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            if(!"".equals(hrmid)){
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }else{
                String deptfg=newUser.getNewDepart().getLeaderId();
                //如果不是WK这种形式的，那么久获取当前审批的人分管领导
                hrmids+=hrmids==""?deptfg:","+deptfg;
            }

        }
        return hrmids;
    }
    //获取费用确认节点
    public String getfyqr(String value, NewUser newUser,NewUser ngrUser){
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        for (String hrmst:value.split(",") ) {
            //如果是1的情况下，那么则选择费用确认（仅自己）
            String hrmid=getHrmType(hrmst);
            if("".equals(hrmid)){
                if("1".equals(value)){
                    //12.2劳务派遣人员不进行费用确认
                    if(!ngrUser.getWorkCode().equals(newUser.getWorkCode())&&!newUser.getWorkCode().startsWith("p")) {
                        hrmids = newUser.getUserId();
                    }
                }
            }else{
                hrmids=hrmid;
            }
        }
        return hrmids ;
    }
    //预算管理员
    public String getysgly(String value, String deptid){
        if(!"1".equals(value)){
            return  "";
        }
        String hrmids="";
        //如果是信息技术部才走，后续增加提建模来匹配获取数据
        String xxjsbdept=new PropBean().getPropName("DT_xxjsb");
        if("".equals(deptid)){
            return  hrmids;
        }
        if(xxjsbdept.equals(deptid)) {
            //  String workecode = "002629";
            hrmids=propBean.getPropName("travelexpense_ysgly");

            // hrmids = JAVATools.GetWorkcodetoid(workecode);
        }
        return hrmids;
    }

    public String getsfsld(String value,Map<String,Object> detailmap){
        String flag="0";
        if("".equals(value)){
            return "";
        }
        if("1".equals(value)){
            return "1";
        }
        //
        if (Logictools.getconitionparams(value,detailmap)) {
            flag="1";
        }
        return flag;


    }

    //费用会计节点
    public String getfykj(String fysjcdbm,String unnumber,String value){
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            if(!"".equals(hrmid)){
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }else{
                if(!"".equals(hrmst)){
                 hrmids=ufZbbxandclsplDao.GetFykjtoHrmid(fysjcdbm,unnumber);
                }

            }
        }

        return hrmids;
    }

    //财务总监节点
    public String getcwzj(String value){
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            if(!"".equals(hrmid)){
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }else{
                String cfovalue=new PropBean().getPropName("travelexpense_CFO");
                hrmids+=hrmids==""?cfovalue:","+cfovalue;

            }
        }

        return hrmids;
    }

    public String getsumfykj(String value){
        String hrmids="";
        String str=  JAVATools.disinctrlist(value.split(","));
        if("".equals(str)||str.split(",").length>1){
            //待确认

            hrmids=propBean.getPropName("travelexpense_accountant");
            // hrmids
        }else{
            hrmids=value;
        }
        return hrmids;
    }

    //二级部门负责人
    //二级部门负责人
    public String gettwoDeptAdmin(String value,String deptid,String userids,String bmldfileid,NewUser ngrUser){
        String hrmids="";

        if("ST_3".equals(ngrUser.getDeptLevelcode()) && "PT_1,PT_2,PT_3,PT_4".equals(ngrUser.getRoleCode())){
            return bmldfileid;
        }

        if("".equals(value)||"".equals(deptid)||"".equals(userids)){
            return hrmids;
        }

        for (String hrmst:value.split(",") ) {
            //如果是1的情况下，那么则选择费用确认（仅自己）
            String hrmid=getHrmType(hrmst);
            if("".equals(hrmid)){
                if("2".equals(new NewDepart(deptid).getDepLevel())){
                    //获取部门负责人
                    hrmids+=hrmids==""?userids:","+userids;
                }
            }else{
                hrmids=hrmid;
            }
        }
        return hrmids ;
    }

    //二级部门负责人
    public String gettwoDeptAdmin(String value,String deptid,String userids){

        String hrmids="";
        if("".equals(value)||"".equals(deptid)||"".equals(userids)){
            return hrmids;
        }
        for (String hrmst:value.split(",") ) {
            //如果是1的情况下，那么则选择费用确认（仅自己）
            String hrmid=getHrmType(hrmst);
            if("".equals(hrmid)){
                if("2".equals(new NewDepart(deptid).getDepLevel())){
                    //获取部门负责人
                    hrmids+=hrmids==""?userids:","+userids;
                    //   hrmids=userids;
                }
            }else{
                hrmids=hrmid;
            }
        }
        return hrmids ;
    }




    //一级部门负责人 ，查询审批人等于报销人 (old)
    public String getONeDeptAdmin(String value,String deptid,String userids){
        String hrmids="";
        if("".equals(value)||"".equals(deptid)||"".equals(userids)){
            return hrmids;
        }

        for (String hrmst:value.split(",") ) {
            //如果是1的情况下，那么则选择费用确认（仅自己）
            String hrmid=getHrmType(hrmst);
            if("".equals(hrmid)){
                if("1".equals(value)){
                    //获取部门负责人
                    if("1".equals(new NewDepart(deptid).getDepLevel())){
                        //   hrmids=userids;
                        hrmids+=hrmids==""?userids:","+userids;
                    }else{
                        String parentDirectorId=new NewDepart(deptid).getParentDirectorId();
                        hrmids+=hrmids==""?parentDirectorId:","+parentDirectorId;
                    }
                }
            }else{
                hrmids=hrmid;
            }
        }
        return hrmids ;
    }


    //一级部门负责人 ，查询审批人等于报销人 (old)
    public String getONeDeptAdmin(String value,String deptid,String userids,String bmldfileid,String bxruserid){
        String hrmids="";


        if("1".equals(new NewDepart(new NewUser(bxruserid).getDepId()).getDepLevel())){
            if(!"".equals(bmldfileid)){
                hrmids=bmldfileid;
            }
        }

        if("".equals(value)||"".equals(deptid)||("".equals(userids)&&"".equals(hrmids))){
            return hrmids;
        }
        String notuserids="22797";
        String yyglc="ZZ001001";
        if(new NewDepart(deptid).getDepCode().equals(yyglc)) {
            if (!"".equals(userids) || userids.indexOf(notuserids) > 0) {
                List<String> list = Arrays.asList(userids.split(","));
                userids=JAVATools.ListeuqestString(list,notuserids);
            }
        }

        for (String hrmst:value.split(",") ) {
            //如果是1的情况下，那么则选择费用确认（仅自己）
            String hrmid=getHrmType(hrmst);
            if("".equals(hrmid)){
                if("1".equals(value)){
                    //获取部门负责人
                    if("1".equals(new NewDepart(deptid).getDepLevel())){
                        if(!"".equals(userids)){
                            hrmids+=hrmids==""?userids:","+userids;
                        }

                    }else{
                        String parentdirectorid= new NewDepart(deptid).getParentDirectorId();
                        hrmids+=hrmids==""?parentdirectorid:","+parentdirectorid;

                    }

                }
            }else{
                hrmids=hrmid;
            }
        }
        return hrmids ;
    }

    public String getNewDeptAdmin(String value,String deptid,String userids,String bmldfileid,String bxruserid,NewUser ngruser){
        String hrmids="";
        NewUser bxrUser=new NewUser(bxruserid);

        if("ST_3".equals(ngruser.getDeptLevelcode()) &&  "PT_1,PT_2,PT_3,PT_4".equals(ngruser.getRoleCode())){
          return   new NewDepart(deptid).getParentDirectorId();
        }

        if("1".equals(new NewDepart(bxrUser.getDepId()).getDepLevel())){
            if(!"".equals(bmldfileid)){
                hrmids=bmldfileid;
            }
        }
        if("".equals(value)||"".equals(deptid)||("".equals(userids)&&"".equals(hrmids))){
            return "";
        }

        //部门大佬的权限目前只用于pt维度。不需要统一配置
        String Dept_onepices="PT_1,PT_2,PT_3,PT_4";
        String bxrrolecode= bxrUser.getRoleCode().toString();
        if(Dept_onepices.indexOf(bxrrolecode)>=0){
            // 则获取表单字段
            return hrmids;
        }else{
            //如果不是部门大佬，则清空下方带入的值
            hrmids="";
            for (String hrmst:value.split(",") ) {
                //如果是1的情况下，那么则选择费用确认（仅自己）
                String hrmid=getHrmType(hrmst);
                if("".equals(hrmid)){
                    if("1".equals(value)){
                        //获取部门负责人
                        if("1".equals(new NewDepart(deptid).getDepLevel())){
                            if(!"".equals(userids)){
                                hrmids+=hrmids==""?userids:","+userids;
                            }
                        }else{
                            String parentdirectorid= new NewDepart(deptid).getParentDirectorId();
                            hrmids+=hrmids==""?parentdirectorid:","+parentdirectorid;
                        }

                    }
                }else{
                    hrmids=hrmid;
                }
            }
        }





        return hrmids ;
    }



    //根据type来获取人员
    public String getHrmType(String value){
        if("".equals(value)){
            return "";
        }
        if(value.indexOf("WK_")>=0){
            String workcode=value.split("WK_")[1];
            return JAVATools.GetWorkcodetoid(workcode);
        }
        return "";
    }


    public static Map<String,String> getfgldreloads(String fgldid){
        Map<String,String> map=new HashMap<>();
        PropBean propbeanc=new PropBean();
        String dwfsj="";
        String dwsj="";
        String fgld="";
        //String cfo="";
        if(!"".equals(fgldid)){
            for (String value:fgldid.split(",")){
                NewUser newUser=new NewUser(value);
                if(newUser!=null||!"".equals(newUser.getRoleCode())){
                    //党委委员/副书记
                    if("PT_2".equals(newUser.getRoleCode())){
                        dwfsj= JAVATools.addreloadString(dwfsj,newUser.getUserId());
                    }else
                        //党委书记
                        if("PT_3".equals(newUser.getRoleCode())){
                            dwsj= JAVATools.addreloadString(dwsj,newUser.getUserId());
                        }else {
                            fgld= JAVATools.addreloadString(fgld,value);
                        }

                }
            }
        }
        map.put("dwfsj",dwfsj);
        map.put("dwsj",dwsj);
        //  map.put("cfo",cfo);
        map.put("fgld",fgld);
        return  map;
    }
}
