package com.engine.htsc.subcompany.service.impl;

import com.engine.htsc.subcompany.model.ReportSequenceVO;
import com.engine.htsc.subcompany.model.UfZgsspl;
import com.engine.htsc.subcompany.model.ZgsUser;
import com.engine.htsc.subcompany.tools.ZjClUfzgsServiceTools;
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 ZjclSubCompanyLinkServiceImpl extends SubCompanyLinkServiceImpl {

    private ZjClUfzgsServiceTools zjClUfzgsServiceToolsj = new ZjClUfzgsServiceTools();

    @Override
    protected  List<ReportSequenceVO> getReportVO(String unumbercode, List<Map<String,String>> reqMapList) {
        List<ReportSequenceVO> respListBefore = new ArrayList<>();
        for(Map<String,String> mingxiMap : reqMapList) {
            Map<String,String> mapReqSql = getMapReqSql(unumbercode, mingxiMap);
            //查询初步符合条件的审批链
            List<UfZgsspl> ufFzjgsplList = ufZgssplDao.getUfzgsspl(mapReqSql);
            //条件过滤并赋值
            List<ReportSequenceVO> fzjgsplVoList = zjClUfzgsServiceToolsj.convertModelToVO(ufFzjgsplList,mingxiMap.get("bxry"),(HashMap)mingxiMap,unumbercode);
            respListBefore.addAll(fzjgsplVoList);
        }
        return respListBefore;
    }

    @Override
    protected Map<String,String> getMapReqSql(String unumbercode,Map<String,String> map) {
        HashMap<String,String> mapReqSql = new HashMap<>();
        String cxsprjs = ufzgsplModelTools.getUfzgsJobGrade(map.get("bxry"));
        ZgsUser zgsUser = new ZgsUser(map.get("bxry"));
        mapReqSql.put("cxspr","bxry");//查询审批人
        mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
        mapReqSql.put("sslc",unumbercode);//查询所属流程
        mapReqSql.put("userCode","WK_" + zgsUser.getWorkCode());//查询单个人
        return mapReqSql;
    }

    /**
     * 相加并去重
     * @param rawReportVOList
     * @return
     */
    protected Map<String, String> removeDuplicateCommon(List<ReportSequenceVO> rawReportVOList) {
        Map<String, String> mapRes = new HashMap<>();
        String reportSequenceId = "";//匹配到的数据库审批链id
        String fyqr = "";//费用确认
        String hqbmfzr = "";//会签部门负责人
        String xmfzr = "";//项目负责人（联合投行项目特有）
        String zjsjsh = "";//直接上级审核（香港特有）
        String ejbmfzr = "";//二级部门负责人 0或空或自定义编码 数据库用来判断是否跳过
        String thqyfzr = "";//投行区域负责人（联合投行等二级部特有）
        String yjbmfzr = "";//一级部门负责人
        String xmssbmfzr = "";//项目所属部门负责人
        String fyg = "";//费用岗审核
        String kj = "";//财务中心会计
        String cwjl = "";//财务中心经理
        String gsywfzr = "";//固收业务负责人（资管特有）
        String fycdbmfzr = "";//费用承担部门负责人（紫金虚拟基金特有）
        String fgld = "";//分管领导
        String cwfgld = "";//财务分管领导（联合付款特有）
        String zgszjl = "";//子公司总经理
        String zc = "";//总裁（联合特有）
        String zgsdsz = "";//子公司董事长
        String kjcl = "";//会计处理（联合特有）
        String cn = "";//出纳（暂定为空）
        String sxyyg = "";//首席运营官
        String sxzxg = "";//首席执行官
        String cwzxcn = "";//财务中心出纳
        String hqbmflb = "";//会签部门法律部
        String hqbmfxglb = "";//会签部门风险管理部
        String zbzjglg = "";//总部资金管理岗
        String zbzjyybfzr = "";//总部资金运营部负责人
        String jtld = "";//集团领导
        String sxcwg = "";//首席财务官
        String ysgly = "";//预算管理员
        String sxfxg = "";//首席风险官
        String ghzx = "";//工会主席
        String cwglbfzr = "";//财务管理部负责人
        String zjzjl = "";//紫金总经理
        //单一节点去重
        for (ReportSequenceVO reportSequenceVO:rawReportVOList){
            reportSequenceId = addIds(reportSequenceId, Util.null2String(reportSequenceVO.getReportSequenceId()));
            fyqr= addIds(fyqr, Util.null2String(reportSequenceVO.getFyqr()));
            xmfzr= addIds(xmfzr, Util.null2String(reportSequenceVO.getXmfzr()));
            zjsjsh= addIds(zjsjsh, Util.null2String(reportSequenceVO.getZjsjsh()));
            ejbmfzr= addIds(ejbmfzr, Util.null2String(reportSequenceVO.getEjbmfzr()));
            thqyfzr= addIds(thqyfzr, Util.null2String(reportSequenceVO.getThqyfzr()));
            yjbmfzr= addIds(yjbmfzr, Util.null2String(reportSequenceVO.getYjbmfzr()));
            xmssbmfzr= addIds(xmssbmfzr, Util.null2String(reportSequenceVO.getXmssbmfzr()));
            fyg = addIds(fyg, Util.null2String(reportSequenceVO.getFyg()));
            kj= addIds(kj, Util.null2String(reportSequenceVO.getKj()));
            cwjl= addIds(cwjl, Util.null2String(reportSequenceVO.getCwjl()));
            gsywfzr= addIds(gsywfzr, Util.null2String(reportSequenceVO.getGsywfzr()));
            fycdbmfzr= addIds(fycdbmfzr, Util.null2String(reportSequenceVO.getFycdbmfzr()));
            fgld= addIds(fgld, Util.null2String(reportSequenceVO.getFgld()));
            cwfgld= addIds(cwfgld, Util.null2String(reportSequenceVO.getCwfgld()));
            zgszjl= addIds(zgszjl, Util.null2String(reportSequenceVO.getZgszjl()));
            zc= addIds(zc, Util.null2String(reportSequenceVO.getZc()));
            zgsdsz= addIds(zgsdsz, Util.null2String(reportSequenceVO.getZgsdsz()));
            kjcl= addIds(kjcl, Util.null2String(reportSequenceVO.getKjcl()));
            cn= addIds(cn, Util.null2String(reportSequenceVO.getCn()));
            sxyyg = addIds(sxyyg, Util.null2String(reportSequenceVO.getSxyyg()));
            sxzxg = addIds(sxzxg, Util.null2String(reportSequenceVO.getSxzxg()));
            cwzxcn = addIds(cwzxcn, Util.null2String(reportSequenceVO.getCwzxcn()));
            hqbmflb = addIds(hqbmflb, Util.null2String(reportSequenceVO.getHqbmflb()));
            hqbmfxglb = addIds(hqbmfxglb, Util.null2String(reportSequenceVO.getHqbmfxglb()));
            zbzjglg = addIds(zbzjglg, Util.null2String(reportSequenceVO.getZbzjglg()));
            zbzjyybfzr = addIds(zbzjyybfzr, Util.null2String(reportSequenceVO.getZbzjyybfzr()));
            jtld = addIds(jtld, Util.null2String(reportSequenceVO.getJtld()));
            sxcwg = addIds(sxcwg, Util.null2String(reportSequenceVO.getSxcwg()));
            hqbmfzr = addIds(hqbmfzr, Util.null2String(reportSequenceVO.getHqbmfzr()));
            ysgly = addIds(ysgly, Util.null2String(reportSequenceVO.getYsgly()));
            sxfxg = addIds(sxfxg, Util.null2String(reportSequenceVO.getSxfxg()));
            ghzx = addIds(ghzx, Util.null2String(reportSequenceVO.getGhzx()));
            zjzjl = addIds(zjzjl, Util.null2String(reportSequenceVO.getZjzjl()));
            cwglbfzr = addIds(cwglbfzr, Util.null2String(reportSequenceVO.getCwglbfzr()));
        }
        mapRes.put("reportSequenceId",reportSequenceId);
        // 费用确认和部门领导的去重逻辑
        fyqr = distinctByCode(fyqr,ejbmfzr);
        fyqr = distinctByCode(fyqr,yjbmfzr);
        fyqr = distinctByCode(fyqr,zgszjl);
        //二级部门负责人和已经部门负责人相同时保留一级部门负责人
        ejbmfzr = distinctByCode(ejbmfzr,yjbmfzr);
        //子公司董事长和总经理去重留董事长
        zgszjl = distinctByCode(zgszjl,zgsdsz);
        zgszjl = distinctByCode(zgszjl,zjzjl);
        //对"紫金公司总经理""紫金公司董事长"节点去重
        zjzjl = distinctByCode(zjzjl,zgsdsz);
        //对"紫金公司总经理""紫金公司董事长"节点去重
        fycdbmfzr = distinctByCode(fycdbmfzr,zgsdsz);
        fycdbmfzr = distinctByCode(fycdbmfzr,zjzjl);
        fycdbmfzr = distinctByCode(fycdbmfzr,zgszjl);
        mapRes.put("fyqr",fyqr);
        mapRes.put("hqbmfzr",hqbmfzr);
        mapRes.put("xmfzr",xmfzr);
        mapRes.put("ejbmfzr",ejbmfzr);
        mapRes.put("zjsjsh",zjsjsh);
        mapRes.put("thqyfzr",thqyfzr);
        mapRes.put("yjbmfzr",yjbmfzr);
        mapRes.put("xmssbmfzr",xmssbmfzr);
        mapRes.put("fyg",fyg);
        mapRes.put("kj",kj);
        mapRes.put("cwjl",cwjl);
        mapRes.put("gsywfzr",gsywfzr);
        mapRes.put("fycdbmfzr",fycdbmfzr);
        mapRes.put("fgld",fgld);
        mapRes.put("cwfgld",cwfgld);
        mapRes.put("zgszjl",zgszjl);
        mapRes.put("zc",zc);
        mapRes.put("zgsdsz",zgsdsz);
        mapRes.put("kjcl",kjcl);
        mapRes.put("cn",cn);
        //获取出纳人员工号
        String cnrygh = JAVATools.gethrmidtoworkcode(cn);
        mapRes.put("cnrygh",cnrygh);
        mapRes.put("sxyyg",sxyyg);
        mapRes.put("sxzxg",sxzxg);
        mapRes.put("cwzxcn",cwzxcn);
        mapRes.put("hqbmflb",hqbmflb);
        mapRes.put("hqbmfxglb",hqbmfxglb);
        mapRes.put("zbzjglg",zbzjglg);
        mapRes.put("zbzjyybfzr",zbzjyybfzr);
        mapRes.put("jtld",jtld);
        mapRes.put("sxcwg",sxcwg);
        mapRes.put("ysgly",ysgly);
        mapRes.put("sxfxg",sxfxg);
        mapRes.put("ghzx",ghzx);
        mapRes.put("cwglbfzr",cwglbfzr);
        mapRes.put("zjzjl",zjzjl);
        return mapRes;
    }

    //跨界点使用code去重，去除param中包含base的部分
    private String distinctByCode(String param, String base) {
        String result = "";
        if (!"".equals(param)){
            List<Map<String,String>> baseMapList = ufZgssplDao.getUserMap(base);
            Map<String,String> baseMap = baseMapList.get(1);//parm
            List<Map<String,String>> paramMapList = ufZgssplDao.getUserMap(param);
            Map<String, String> paramMap = paramMapList.get(1);//base
            //遍历paramMap，判断baseMap是否存在paramMap中的key值，若不存在则放入结果返回
            for(String code:paramMap.keySet()){
                if(null==baseMap.get(code)||"".equals(baseMap.get(code))){
                    result+=result==""? Util.null2String(paramMap.get(code)):","+ Util.null2String(paramMap.get(code));
                }
            }
        }
        return result;
    }
}
