package com.engine.htsc.generaltrip.tools;

import com.engine.htsc.generaltrip.dao.UfCcsqsplDao;
import com.engine.htsc.generaltrip.model.CcsqDept;
import com.engine.htsc.generaltrip.model.CcsqUser;
import com.engine.htsc.generaltrip.model.UfCcsqspl;
import com.engine.htsc.generaltrip.model.UfCcsqsplVo;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class UfCcsqServiceTools {

    protected UfCcsqsplDao ufCcsqsplDao = new UfCcsqsplDao();

    protected CcsqModelTools ccsqModelTools  = new CcsqModelTools();

    public List<UfCcsqsplVo> convertModelToVO(List<UfCcsqspl> UfCcsqsplList, String ngr, HashMap<String, String> mingxiMap, String unnumbercode) {
        List<UfCcsqspl> ufCcsqsplListJg = new ArrayList<>();//机构
        List<UfCcsqspl> ufCcsqsplListBm = new ArrayList<>();//部门
        List<UfCcsqspl> ufCcsqsplListRy = new ArrayList<>();//人员
        List<UfCcsqspl> ufCcsqsplListResult = new ArrayList<>();//筛选结果
        List<UfCcsqsplVo> resultList = new ArrayList<>();//返回值Vo
        CcsqUser ccsqUser = new CcsqUser(ngr);
        //过滤审批链

        for(UfCcsqspl ufCcsqspl:UfCcsqsplList){
            //判断条件
            if(null!=ufCcsqspl.getTj()&&!"".equals(ufCcsqspl.getTj())){
                if(!ccsqModelTools.getConitionParams(ufCcsqspl.getTj(),mingxiMap)){
                    continue;
                }
            }
            List<String> list = Arrays.asList(ufCcsqspl.getBm().split(","));
            //判断是否符合 人员>部门>机构
            if (ufCcsqspl.getCxsprjs().equals("WK_"+ccsqUser.getWorkCode())){
                ufCcsqsplListRy.add(ufCcsqspl);//单个人员
            }else if (list.contains(ccsqUser.getDepId())){
                ufCcsqsplListBm.add(ufCcsqspl);//符合部门
            }else if (ufCcsqspl.getJg().equals(ccsqUser.getCcsqDept().getJglx())&&"".equals(ufCcsqspl.getBm())){
                ufCcsqsplListJg.add(ufCcsqspl);//部门为空且符合机构
            }
        }
        if (ufCcsqsplListRy.size()>0){
            ufCcsqsplListResult.addAll(ufCcsqsplListRy);
        }else if (ufCcsqsplListBm.size()>0){
            ufCcsqsplListResult.addAll(ufCcsqsplListBm);
        }else if (ufCcsqsplListJg.size()>0){
            ufCcsqsplListResult.addAll(ufCcsqsplListJg);
        }
        for (UfCcsqspl ufCcsqspl:ufCcsqsplListResult){
            JAVATools.writelog("筛选后获取匹配的审批链id为："+ufCcsqspl.getId());
            UfCcsqsplVo ufCcsqsplVo = getVo(ufCcsqspl,ccsqUser,unnumbercode,mingxiMap);
            resultList.add(ufCcsqsplVo);
        }
        return resultList;
    }

    protected UfCcsqsplVo getVo(UfCcsqspl ufCcsqspl, CcsqUser ccsqUser, String unnumbercode, HashMap<String, String> mingxiMap) {
        UfCcsqsplVo ufCcsqsplVo = new UfCcsqsplVo();
        String bmfzr = mingxiMap.get("bmfzr");
        ufCcsqsplVo.setCxspr(ccsqUser);
        ufCcsqsplVo.setNgr(ccsqUser);
        ufCcsqsplVo.setReportSequenceId(ufCcsqspl.getId().toString());
        //二级部门负责人
        ufCcsqsplVo.setEjbmfzr(getEjbmfzr(Util.null2String(ufCcsqspl.getEjbmfzr()),ccsqUser,bmfzr));
        //一级部门负责人
        ufCcsqsplVo.setYjbmfzr(getYjbmfzr(Util.null2String(ufCcsqspl.getYjbmfzr()),ccsqUser,bmfzr));
        //分管领导审批
        ufCcsqsplVo.setFgld(getFgld(Util.null2String(ufCcsqspl.getFgld()),ccsqUser));
        //总经理审批
        ufCcsqsplVo.setZjlsp(getNormal(Util.null2String(ufCcsqspl.getZjlsp())));
        //董事长审批
        ufCcsqsplVo.setDszsp(getNormal(Util.null2String(ufCcsqspl.getDszsp())));
        //领导审批
        ufCcsqsplVo.setLdsp(getNormal(Util.null2String(ufCcsqspl.getLdsp())));
        return ufCcsqsplVo;
    }

    protected String getFgld(String value, CcsqUser ccsqUser) {
        String hrmids = "";
        CcsqDept ccsqDept = ccsqUser.getCcsqDept();
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    hrmids+=hrmids==""?ccsqDept.getLeaderId():","+ccsqDept.getLeaderId();
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }

    //一级部门负责人
    protected String getYjbmfzr(String value, CcsqUser ccsqUser,String bmfzr) {
        String hrmids = "";
        CcsqDept ccsqDept = ccsqUser.getCcsqDept();
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    if("2".equals(ccsqDept.getDepLevel())&&!"".equals(Util.null2String(ccsqDept.getDirectorId()))){
                        if ("shzx".equals(ccsqDept.getJglx())){
                            ccsqDept = new CcsqDept(ccsqDept.getParentDepId());
                            hrmids+=hrmids==""?ccsqDept.getDirectorFlId():","+ccsqDept.getDirectorFlId();
                        }else {
                            ccsqDept = new CcsqDept(ccsqDept.getParentDepId());
                            hrmids+=hrmids==""?ccsqDept.getDirectorId():","+ccsqDept.getDirectorId();
                        }
                    }else if("1".equals(ccsqDept.getDepLevel())){
                        hrmids+=hrmids==""?bmfzr:","+bmfzr;
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }

    //二级部门负责人
    protected String getEjbmfzr(String value, CcsqUser ccsqUser,String bmfzr) {
        String hrmids = "";
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    hrmids+=hrmids==""?bmfzr:","+bmfzr;
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }

    //数据库直接配置人员类型
    protected String getNormal(String value) {
        String hrmids="";
        if("".equals(value)){
            return hrmids;
        }
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            hrmids+=hrmids==""?hrmid:","+hrmid;
        }
        return hrmids;
    }

    //根据type来获取人员
    public String getHrmType(String value){
        if("".equals(value)){
            return "";
        }
        if(value.indexOf("WK_")>=0){
            String workcodeS=value.split("WK_")[1];
            String[] codeArray = workcodeS.split("_");
            String workcode = codeArray[0];
            if(codeArray.length>1){
                String deptcode = codeArray[1];
                return ufCcsqsplDao.getUserIdByCode(workcode,deptcode);
            }else{
                return JAVATools.GetWorkcodetoid(workcode);
            }
        }
        return "";
    }

}
