package com.engine.htsc.subcompany.tools;

import com.engine.htsc.subcompany.dao.UfZgssplDao;
import com.engine.htsc.subcompany.model.ReportSequenceVO;
import com.engine.htsc.subcompany.model.UfZgsspl;
import com.engine.htsc.subcompany.model.ZgsDept;
import com.engine.htsc.subcompany.model.ZgsUser;
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 LhUfzgsServiceTools extends UfzgsServiceTools{

    private final UfZgssplDao ufZgssplDao = new UfZgssplDao();

    @Override
    public List<ReportSequenceVO> convertModelToVO(List<UfZgsspl> ufZgssplList, String userId, HashMap<String, String> map, String unumbercode) {
        List<ReportSequenceVO> resultList = new ArrayList<>();
        //获取人员及部门信息
        ZgsUser zgsUser = new ZgsUser(userId);
        //是否为紫金的费用承担部门
        String fycdbm = Util.null2String(map.get("fycdbm"));
        List<UfZgsspl> ufFzjgsplPhone = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplCode = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplRy = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplFycd = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplBm = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplBms = new ArrayList<>();
        List<UfZgsspl> ufFzjgspljg = new ArrayList<>();
        List<UfZgsspl> ufFzjgspljgS = new ArrayList<>();
        List<UfZgsspl> UfZgssplListR = new ArrayList<>();
        //过滤审批链
        for(UfZgsspl ufZgsspl:ufZgssplList){
            //判断条件
            if(null!=ufZgsspl.getTj()&&!"".equals(ufZgsspl.getTj())){
                if(!getConitionParams(ufZgsspl.getTj(),map)){
                    continue;
                }
            }
            //判断是否符合部门 人员>费用承担部门>部门>机构
            List<String> list = Arrays.asList(ufZgsspl.getBm().split(","));
            //判断为手机费
            if (!"".equals(Util.null2String(map.get("code")))&&"LHMobileFee".equals(Util.null2String(map.get("code")))&&"code=LHMobileFee".equals(ufZgsspl.getTj())){
                ufFzjgsplPhone.add(ufZgsspl);
            }else if (ufZgsspl.getCxsprjs().equals("WK_"+zgsUser.getWorkCode())&&ufZgsspl.getBm().equals(zgsUser.getDepId())){
                ufFzjgsplRy.add(ufZgsspl);//单人，联合部分为董事会或执行委员会成员,且要是董事会或执行委员会的账号
            }else if (!"0".equals(Util.null2String(map.get("code")))&&!"".equals(Util.null2String(map.get("code")))&&list.contains(zgsUser.getDepId())&&ufZgsspl.getTj().contains("code")){
                ufFzjgsplCode.add(ufZgsspl);//费用承担部门执行委员会，董事会，部门为综合管理部
            }else if(list.contains(fycdbm)&&list.size()==1){
                ufFzjgsplFycd.add(ufZgsspl);//单个费用承担
            }else if (list.contains(zgsUser.getDepId())&&list.size()==1){
                ufFzjgsplBm.add(ufZgsspl);//单个部门
            }else if (list.contains(zgsUser.getZgsDept().getDepCode())||list.contains(zgsUser.getDepId())){
                ufFzjgsplBms.add(ufZgsspl);//多部门
            }else if (ufZgsspl.getBm().equals(zgsUser.getZgsDept().getDepTypeCode())){
                ufFzjgspljg.add(ufZgsspl);//机构中的几个部门
            }else if (ufZgsspl.getJg().equals(zgsUser.getZgsDept().getDepTypeCode())&&"".equals(Util.null2String(ufZgsspl.getBm()))){
                ufFzjgspljgS.add(ufZgsspl);//单个机构所有部门
            }
        }
        if (ufFzjgsplPhone.size()>0){
            UfZgssplListR.addAll(ufFzjgsplPhone);
        }else if (ufFzjgsplRy.size()>0){
            UfZgssplListR.addAll(ufFzjgsplRy);
        }else if (ufFzjgsplCode.size()>0){
            UfZgssplListR.addAll(ufFzjgsplCode);
        }else if (ufFzjgsplFycd.size()>0){
            UfZgssplListR.addAll(ufFzjgsplFycd);
        }else if (ufFzjgsplBm.size()>0){
            UfZgssplListR.addAll(ufFzjgsplBm);
        }else if (ufFzjgsplBms.size()>0){
            UfZgssplListR.addAll(ufFzjgsplBms);
        }else if (ufFzjgspljg.size()>0){
            UfZgssplListR.addAll(ufFzjgspljg);
        }else {
            UfZgssplListR.addAll(ufFzjgspljgS);
        }
        for(UfZgsspl ufZgsspl:UfZgssplListR){
            JAVATools.writelog("筛选审批链id为："+ufZgsspl.getId());
            ReportSequenceVO reportSequenceVO = getVo(ufZgsspl,zgsUser,map,unumbercode);
            //有投行区域负责人的，审批链中的一级部门负责人和分管领导会置为空
            if ("2".equals(zgsUser.getZgsDept().getDepLevel())&&!"".equals(reportSequenceVO.getThqyfzr())){
                reportSequenceVO.setFgld("");
                reportSequenceVO.setYjbmfzr("");
            }
            resultList.add(reportSequenceVO);
        }
        return resultList;
    }


    //二级部门负责人
    protected String getEjbmfzr(String value, HashMap<String, String> map, ZgsUser zgsUser) {
        String hrmids="";
        ZgsDept zgsDept = zgsUser.getZgsDept();
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    while (!"2".equals(zgsDept.getDepLevel())&&!"".equals(zgsDept.getDepLevel())){
                        zgsDept = new ZgsDept(zgsDept.getParentDepId());
                    }
                    String[] directorIds = zgsDept.getDirectorId().split(",");
                    if(null!=directorIds&&directorIds.length>0){
                        hrmids+=hrmids==""?directorIds[0]:","+directorIds[0];
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids ;
    }

    //一级部门负责人
    protected String getYjbmfzr(String value, HashMap<String, String> map, ZgsUser zgsUser) {
        String hrmids="";
        ZgsDept zgsDept = zgsUser.getZgsDept();
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    while (!"1".equals(zgsDept.getDepLevel())&&!"".equals(zgsDept.getDepLevel())){
                        zgsDept = new ZgsDept(zgsDept.getParentDepId());
                    }
                    String[] directorIds = zgsDept.getDirectorId().split(",");
                    if(null!=directorIds&&directorIds.length>0){
                        hrmids+=hrmids==""?directorIds[0]:","+directorIds[0];
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids ;
    }

    //费用承担部门负责人
    protected String getFycdbmfzr(String value, HashMap<String, String> map, ZgsUser zgsUser) {
        String fycdbm = map.get("fycdbm");
        String hrmids = "";
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    if (!fycdbm.equals(zgsUser.getDepId())){
                        ZgsDept bxbmvo = new ZgsDept(fycdbm);
                        if (null!=bxbmvo.getDirectorId()&&!"".equals(bxbmvo.getDirectorId())){
                            hrmids+=hrmids==""?bxbmvo.getDirectorId():","+bxbmvo.getDirectorId();
                        }
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids ;
    }

    //投行区域负责人（联合投行等二级部特有）
    protected String getThqyfzr(String value, HashMap<String, String> map, ZgsUser zgsUser) {
        String hrmids="";
        String thqyfzr = ufZgssplDao.getThfzr(zgsUser.getDepId());
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    String[] directorIds = thqyfzr.split(",");
                    if(null!=directorIds&&directorIds.length>0){
                        hrmids+=hrmids==""?directorIds[0]:","+directorIds[0];
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }


}
