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.ZgsUser;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

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

public class ZjClUfzgsServiceTools extends UfzgsServiceTools {

    private 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);
        List<UfZgsspl> ufFzjgsplTj = new ArrayList<>();
        List<UfZgsspl> ufFzjgsplRy = new ArrayList<>();
        List<UfZgsspl> UfZgssplListR = new ArrayList<>();
        //过滤审批链
        for(UfZgsspl ufZgsspl:ufZgssplList){
            //判断条件
            if(null!=ufZgsspl.getTj()&&!"".equals(ufZgsspl.getTj())){
                if(!getConitionParams(ufZgsspl.getTj(),map)){
                    continue;
                }
            }
            //判断是否符合部门 人员>费用承担部门>部门>机构
            if (ufZgsspl.getCxsprjs().equals("WK_"+zgsUser.getWorkCode())){
                ufFzjgsplRy.add(ufZgsspl);//单人，联合部分为董事会或执行委员会成员,且要是董事会或执行委员会的账号
            }else {
                ufFzjgsplTj.add(ufZgsspl);
            }
        }
        if (ufFzjgsplRy.size()>0){
            UfZgssplListR.addAll(ufFzjgsplRy);
        }else if (ufFzjgsplTj.size()>0){
            UfZgssplListR.addAll(ufFzjgsplTj);
        }
        for(UfZgsspl ufZgsspl:UfZgssplListR){
            JAVATools.writelog("筛选审批链id为："+ufZgsspl.getId());
            ReportSequenceVO reportSequenceVO = getVo(ufZgsspl,zgsUser,map,unumbercode);
            resultList.add(reportSequenceVO);
        }
        return resultList;
    }

    @Override
    protected ReportSequenceVO getVo(UfZgsspl ufZgsspl, ZgsUser zgsUser, HashMap<String, String> map, String unumbercode) {
        String fycdbm = map.get("fycdbm");
        //获取紫金的节点操作人
        Map<String,String> mapBmjd = new HashMap<>();
        //紫金差旅报销类型为 0
        List<Map<String,String>>  mapList =  ufZgssplDao.getZjbmgjdcz(fycdbm,"2");
        for (Map<String,String> mapBm:mapList){
            if ("2".equals(mapBm.get("lclx"))){
                mapBmjd = mapBm;
            }
        }
        ReportSequenceVO reportSequenceVO = new ReportSequenceVO();
        reportSequenceVO.setNgr(Util.null2String(map.get("ngr")));
        reportSequenceVO.setZgsUser(zgsUser);
        reportSequenceVO.setZgsDept(zgsUser.getZgsDept());
        reportSequenceVO.setReportSequenceId(ufZgsspl.getId().toString());
        String ysgly = Util.null2String(mapBmjd.get("ysgly"));
        String cwzxhj = Util.null2String(mapBmjd.get("cwzxhj"));
        String cwzxjl = Util.null2String(mapBmjd.get("cwzxjl"));
        String zgszjl = Util.null2String(mapBmjd.get("zgszjl"));
        Map<String,String> mapBm = ufZgssplDao.getZjfzrMap(fycdbm);
        String bmfzr = Util.null2String(mapBm.get("bmfzrid"));
        String cn = Util.null2String(mapBm.get("cn"));
        //费用承担部门负责人
        reportSequenceVO.setFycdbmfzr(getZjNormal(Util.null2String(ufZgsspl.getFycdbmfzr()),bmfzr));
        //预算管理员
        reportSequenceVO.setYsgly(getZjNormal(Util.null2String(ufZgsspl.getYsgly()),ysgly));
        //财务中心会计
        reportSequenceVO.setKj(getZjNormal(Util.null2String(ufZgsspl.getKj()),cwzxhj));
        //财务中心经理
        reportSequenceVO.setCwjl(getZjNormal(Util.null2String(ufZgsspl.getCwjl()),cwzxjl));
        //子公司总经理
        reportSequenceVO.setZgszjl(getZjNormal(Util.null2String(ufZgsspl.getZgszjl()),zgszjl));
        //紫金总经理
        reportSequenceVO.setZjzjl(getNormal(Util.null2String(ufZgsspl.getZjzjl())));
        //子公司董事长
        reportSequenceVO.setZgsdsz(getNormal(Util.null2String(ufZgsspl.getZgsdsz())));
        //首席财务官
        reportSequenceVO.setSxcwg(getNormal(Util.null2String(ufZgsspl.getSxcwg())));
        //出纳
        reportSequenceVO.setCn(cn);
        return reportSequenceVO;
    }


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

    //数据库直接配置人员类型
    private 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;
    }
}
