package com.engine.htsc.subcompany.tools;

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 XgtyfkUfzgsServiceTools extends UfzgsServiceTools{
    @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);
        List<UfZgsspl> ufFzjgsplBm = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplfklx = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplysxe = 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")))&&zgsUser.getDepId().equals(Util.null2String(ufZgsspl.getBm()))){
                ufFzjgsplBm.add(ufZgsspl);
            }else if (ufZgsspl.getJg().equals(zgsUser.getZgsDept().getDepTypeCode())&&"".equals(Util.null2String(ufZgsspl.getBm()))){
                if(ufZgsspl.getTj().indexOf("fklx")!= -1){
                    ufFzjgsplfklx.add(ufZgsspl);//付款类型
                }else if(ufZgsspl.getTj().indexOf("flag")!= -1){
                    ufFzjgsplysxe.add(ufZgsspl);//是否超预算
                }
            }
        }
        if (ufFzjgsplBm.size()>0){
            UfZgssplListR.addAll(ufFzjgsplBm);
        }else if (ufFzjgsplfklx.size()>0){
            UfZgssplListR.addAll(ufFzjgsplfklx);
        }else if (ufFzjgsplysxe.size()>0){
            UfZgssplListR.addAll(ufFzjgsplysxe);
        }
        for(UfZgsspl ufZgsspl:UfZgssplListR){
            JAVATools.writelog("筛选审批链id为："+ufZgsspl.getId());
            ReportSequenceVO reportSequenceVO = getVo(ufZgsspl,zgsUser,map,unumbercode);
            resultList.add(reportSequenceVO);
        }
        return resultList;
    }
    //二级部门负责人
    @Override
    protected String getEjbmfzr(String value, HashMap<String, String> map, ZgsUser zgsUser) {
        String hrmids="";
        ZgsDept zgsDept = new ZgsDept(Util.null2String(map.get("fycdbm")));
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id
                if ("1".equals(hrmst)){
                    if ("2".equals(zgsDept.getDepLevel())){
                        hrmids+=hrmids==""?zgsDept.getDirectorId():","+zgsDept.getDirectorId();
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids ;
    }

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