package com.engine.htsc.general.tools;

import com.engine.htsc.general.dao.UfZbsplDao;
import com.engine.htsc.general.model.UfZbspl;
import com.engine.htsc.general.model.UfZbsplVo;
import com.engine.htsc.general.model.ZbDept;
import com.engine.htsc.general.model.ZbUser;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

public class ItUfzbsplServiceTools extends UfzbsplServiceTools{
    @Override
    public List<UfZbsplVo> convertModelToVO(List<UfZbspl> ufZbsplList, String ngr, HashMap<String, String> mingxiMap, String unnumbercode, List fycdbmList) {
        List<UfZbspl> ufZbsplListJg = new ArrayList<>();//机构
        List<UfZbspl> ufZbsplListNgBm = new ArrayList<>();//拟稿人部门
        List<UfZbspl> ufZbsplListRy = new ArrayList<>();//人员
        List<UfZbspl> ufZbsplListRes = new ArrayList<>();//筛选结果
        List<UfZbsplVo> resultList = new ArrayList<>();
        ZbUser zbUser = new ZbUser(ngr);
        //过滤审批链
        for(UfZbspl ufZbspl:ufZbsplList){
            //判断条件
            if(null!=ufZbspl.getTj()&&!"".equals(ufZbspl.getTj())){
                if(!getConitionParams(ufZbspl.getTj(),mingxiMap)){
                    continue;
                }
            }
            List<String> list = Arrays.asList(ufZbspl.getBm().split(","));
            //判断是否符合 人员>费用承担部门>拟稿人部门>机构
            if (ufZbspl.getCxsprjs().equals("WK_"+zbUser.getWorkCode())){
                ufZbsplListRy.add(ufZbspl);//单个人员
            }else if (list.contains(zbUser.getDepId())){
                ufZbsplListNgBm.add(ufZbspl);//拟稿人部门
            }else if (ufZbspl.getJg().equals(zbUser.getZbDept().getDepLevel())&&"".equals(ufZbspl.getBm())){
                ufZbsplListJg.add(ufZbspl);//部门为空且符合机构
            }
        }
        if (ufZbsplListRy.size()>0){
            ufZbsplListRes.addAll(ufZbsplListRy);
        }else if (ufZbsplListNgBm.size()>0){
            ufZbsplListRes.addAll(ufZbsplListNgBm);
        }else if (ufZbsplListJg.size()>0){
            ufZbsplListRes.addAll(ufZbsplListJg);
        }
        for (UfZbspl ufZbspl:ufZbsplListRes){
            JAVATools.writelog("筛选审批链id为："+ufZbspl.getId());
            UfZbsplVo ufZbsplVo = getVo(ufZbspl,zbUser,unnumbercode,mingxiMap,fycdbmList);
            resultList.add(ufZbsplVo);
        }
        return resultList;
    }

    @Override
    protected UfZbsplVo getVo(UfZbspl ufZbspl, ZbUser zbUser, String unnumbercode, HashMap<String, String> mingxiMap, List fycdbmList) {
        UfZbsplVo ufZbsplVo = new UfZbsplVo();
        //费用承担部门
        ZbDept zbDept = new ZbDept(Util.null2String(mingxiMap.get("fycdbm")));
        //拟稿人选择的部门负责人
        String bmfzr = mingxiMap.get("bmfzr");
        //拟稿页面选择的费用承担部门部门负责人
        String fycdbmfzr = Util.null2String(mingxiMap.get("fycdbmfzr"));
        ufZbsplVo.setNgr(zbUser);
        ufZbsplVo.setReportSequenceId(ufZbspl.getId().toString());
        //二级部门负责人
        ufZbsplVo.setEjbmfzr(getEjbmfzr(Util.null2String(ufZbspl.getEjbmfzr()),zbUser,bmfzr));
        //拟稿人部门预算管理员
        ufZbsplVo.setBmysgly(getBmysgly(Util.null2String(ufZbspl.getBmysgly()),zbUser));
        //一级部门负责人
        ufZbsplVo.setYjbmfzr(getYjbmfzr(Util.null2String(ufZbspl.getYjbmfzr()),zbUser,bmfzr));
        //费用承担部门二级部门负责人
        ufZbsplVo.setFycdbmerbmfzr(getFycdbmerbmfzr(Util.null2String(ufZbspl.getFycdbmerbmfzr()),zbDept,zbUser,fycdbmfzr));
        //费用承担部门一级部门负责人
        ufZbsplVo.setFycdbmyjbmfzr(getFycdbmyjbmfzr(Util.null2String(ufZbspl.getFycdbmyjbmfzr()),zbDept,zbUser,fycdbmfzr));
        //费用承担部门预算管理员
        ufZbsplVo.setFycdbmysgly(getFycdbmysgly(Util.null2String(ufZbspl.getFycdbmysgly()),zbDept,zbUser));
        //会签部门负责人
        ufZbsplVo.setHqbmfzr(Util.null2String(mingxiMap.get("hqbmfzr")));
        //费用会计
        ufZbsplVo.setFykj(getFykj(Util.null2String(ufZbspl.getFykj()),zbUser,zbDept,mingxiMap,fycdbmList));
        //纸质发票提交,扫码收单权限开关smsdqxkg、是否全为电子发票（扫码）sfqwdzfpsm两个值，都等于0时，取值纸质发票节点值。
        if ("0".equals(mingxiMap.get("smsdqxkg"))&&"0".equals(mingxiMap.get("sfqwdzfpsm"))){
            ufZbsplVo.setZzfptj(ufZbsplVo.getFykj());
        }
        //财务负责人
        ufZbsplVo.setCwfzr(getNormal(Util.null2String(ufZbspl.getCwfzr())));
        //党委副书记
        ufZbsplVo.setDwfsj(getNormal(Util.null2String(ufZbspl.getDwfsj())));
        //党委书记
        ufZbsplVo.setDwsj(getNormal(Util.null2String(ufZbspl.getDwsj())));
        //首席财务官
        ufZbsplVo.setSxcwg(getNormal(Util.null2String(ufZbspl.getSxcwg())));
        //分管领导审批
        ufZbsplVo.setSp(getSp(Util.null2String(ufZbspl.getSp()),zbUser,mingxiMap));
        //总裁审批
        ufZbsplVo.setLdsp(getLdsp(Util.null2String(ufZbspl.getLdsp()),zbUser,ufZbsplVo.getSp()));
        //出纳
        ufZbsplVo.setCn(getNormal(Util.null2String(ufZbspl.getCn())));
        //出纳人员工号
        ufZbsplVo.setCnrygh(getCnrygh(Util.null2String(ufZbspl.getCn())));
        //信息技术部预算管理员审批
        ufZbsplVo.setXjysgly(getNormal(Util.null2String(ufZbspl.getXjysgly())));
        return ufZbsplVo;
    }

    @Override
    protected String getFycdbmyjbmfzr(String value, ZbDept zbDept, ZbUser zbUser,String fycdbmfzr) {
        String hrmids = "";
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id,总部IT付款排除信息技术部
                if(!zbUser.getDepId().equals(zbDept.getDepId())&&!"ZZ001014".equals(zbDept.getDepCode())){
                    if ("1".equals(hrmst)) {
                        if("ZZ001999".equals(zbDept.getDepCode())||!"1".equals(zbDept.getCanceled())) {
                            if ("1".equals(zbDept.getDepLevel())){
                                hrmids += hrmids == "" ? fycdbmfzr : "," + fycdbmfzr;
                            }else {
                                while (!"1".equals(zbDept.getDepLevel()) && !"".equals(zbDept.getDepLevel())) {
                                    zbDept = new ZbDept(zbDept.getParentDepId());
                                }
                                if (!zbDept.getDepId().equals(zbUser.getZbDept().getDepId())) {
                                    hrmids += hrmids == "" ? zbDept.getDirectorId() : "," + zbDept.getDirectorId();
                                }
                            }
                        }
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }

    @Override
    protected String getFycdbmysgly(String value, ZbDept zbDept, ZbUser zbUser) {
        String hrmids = "";
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id,总部IT付款排除信息技术部
                if(!zbUser.getDepId().equals(zbDept.getDepId())&&!"ZZ001014".equals(zbDept.getDepCode())){
                    if ("1".equals(hrmst)) {
                        if("ZZ001999".equals(zbDept.getDepCode())||!"1".equals(zbDept.getCanceled())) {
                            if (!zbUser.getZbDept().getDepId().equals(zbDept.getDepId())) {
                                String bmysgly = ufZbsplDao.getBmysgly(zbDept.getDepId());
                                hrmids += hrmids == "" ? bmysgly : "," + bmysgly;
                            }
                        }
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }

    //费用承担部门二级部门负责人
    @Override
    protected String getFycdbmerbmfzr(String value, ZbDept zbDept, ZbUser zbUser,String fycdbmfzr) {
        String hrmids = "";
        for (String hrmst:value.split(",") ) {
            String hrmid=getHrmType(hrmst);
            //不是指定WK_则判断是否为1
            if("".equals(hrmid)){
                //如果是1的情况下，便从获取对应负责人id,总部IT付款排除信息技术部
                if ("1".equals(hrmst)){
                    if(!zbUser.getDepId().equals(zbDept.getDepId())&&!"ZZ001014".equals(zbDept.getDepCode())){
                        //虚拟部门不过费用承担部门节点
                        if("ZZ001999".equals(zbDept.getDepCode())||!"1".equals(zbDept.getCanceled())){
                            if ("2".equals(zbDept.getDepLevel())){
                                hrmids += hrmids == "" ? fycdbmfzr : "," + fycdbmfzr;
                            }
                        }
                    }
                }
            }else{
                hrmids+=hrmids==""?hrmid:","+hrmid;
            }
        }
        return hrmids;
    }
}
