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

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 com.engine.htsc.subcompany.tools.LhUfzgsServiceTools;
import weaver.general.Util;

import java.util.*;

public class LhSubCompanyLinkServiceImpl extends SubCompanyLinkServiceImpl {

    private final UfZgssplDao ufZgssplDao = new UfZgssplDao();

    protected LhUfzgsServiceTools lhUfzgsServiceTools = new LhUfzgsServiceTools();

    /**
     * 查询具体审批链
     * [子类建议重写]
     * @param unumbercode
     * @param reqMapList
     * @return
     */

    @Override
    protected  List<ReportSequenceVO> getReportVO(String unumbercode, List<Map<String,String>> reqMapList) {
        //联合特殊逻辑
        String code = "0";
        Double zdfS = 0.0;
        boolean flag_moblie = true;//手机费
        boolean flag_zxwy = false;//执行委员会
        boolean flag_dsh = false;//董事会
        for (Map<String,String> mapReq : reqMapList) {
            ZgsDept zgsDept = new ZgsDept(mapReq.get("fycdbm"));
            //董事会
            if ("101".equals(zgsDept.getDepCode())){
                flag_dsh = true;
            }
            //执行委员会
            if ("103".equals(zgsDept.getDepCode())){
                flag_zxwy = true;
            }
            //判断只报销手机话费
            if (!"8601010603".equals(mapReq.get("yskm"))){
                flag_moblie = false;
            }
            //判断只报销手机话费
            if (null!=mapReq.get("zdfbs")&&"0".equals(mapReq.get("zdfbs"))){
                if (null!=mapReq.get("zdf")){
                    zdfS+= Util.getDoubleValue(mapReq.get("zdf"));
                }
            }
        }
        if (flag_moblie){
            code = "LHMobileFee";
        }else if (flag_dsh&&flag_zxwy){
            code = "-1";
        }else if (flag_dsh&&!flag_zxwy){
            code = "-3";
        }else if (!flag_dsh&&flag_zxwy){
            code = "-2";
        }
        List<ReportSequenceVO> respListBefore = new ArrayList<>();
        for(Map<String,String> mingxiMap : reqMapList) {
            //根据不同业务获取Dao层入参
            //条件过滤并赋值
            mingxiMap.put("code",code);
            Map<String,String> mapReqSql = this.getMapReqSql(unumbercode, mingxiMap);
            //查询初步符合条件的审批链
            List<UfZgsspl> ufFzjgsplList = ufZgssplDao.getUfzgsspl(mapReqSql);
            mingxiMap.put("zdf",zdfS.toString());
            List<ReportSequenceVO> fzjgsplVoList = lhUfzgsServiceTools.convertModelToVO(ufFzjgsplList,mingxiMap.get("bxry"),(HashMap)mingxiMap,unumbercode);
            respListBefore.addAll(fzjgsplVoList);
        }
        return respListBefore;
    }

    @Override
    protected Map<String,String> getMapReqSql(String unumbercode,Map<String,String> mapR) {
        HashMap<String,String> mapReqSql = new HashMap<>();
        ZgsUser zgsUser = new ZgsUser(mapR.get("bxry"));
        String cxsprjs = ufzgsplModelTools.getUfzgsJobGrade(mapR.get("bxry"));
        mapReqSql.put("cxspr","bxry");//查询审批人
        mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
        mapReqSql.put("sslc",unumbercode);//查询所属流程
        mapReqSql.put("userCode","WK_"+zgsUser.getWorkCode());//人员工号
        if (!"0".equals(mapR.get("code"))) {
            mapReqSql.put("tj","code="+mapR.get("code"));
        }
        return mapReqSql;
    }

    /**
     * 去重逻辑联合
     * @param rawReportVOList
     * @return
     */
    @Override
    protected Map<String, String> removeDuplicateCommon(List<ReportSequenceVO> rawReportVOList) {
        Map<String, String> mapRes = new HashMap<>();
        String ngr = "";//拟稿人
        String reportSequenceId = "";//匹配到的数据库审批链id
        String fyqr = "";//费用确认
        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 = "";//财务中心出纳
        //单一节点去重
        for (ReportSequenceVO reportSequenceVO:rawReportVOList){
            reportSequenceId = addIds(reportSequenceId, Util.null2String(reportSequenceVO.getReportSequenceId()));
            ngr= addIds(ngr, Util.null2String(reportSequenceVO.getNgr()));
            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()));
        }
        mapRes.put("reportSequenceId",reportSequenceId);
        String all = ngr;
        // 费用确认和部门领导的去重逻辑
        zgsdsz = distinctByCode(zgsdsz,all);
        // 费用承担部门的优先级低于二级部门和一级部门负责人
        fycdbmfzr = distinctByCode(fycdbmfzr,ejbmfzr);
        fycdbmfzr = distinctByCode(fycdbmfzr,yjbmfzr);
        fycdbmfzr = distinctByCode(fycdbmfzr,zc);
        fycdbmfzr = distinctByCode(fycdbmfzr,zgsdsz);
        fycdbmfzr = distinctByCode(fycdbmfzr,ngr);
        fycdbmfzr = distinctByCode(fycdbmfzr,fyqr);

        all = addIds(all, zgsdsz);
        zc = distinctByCode(zc,all);
        all = addIds(all, zc);
        fgld = distinctByCode(fgld,all);
        all = addIds(all, fgld);
        xmssbmfzr = distinctByCode(xmssbmfzr,all);
        all = addIds(all, xmssbmfzr);
        yjbmfzr = distinctByCode(yjbmfzr,all);
        all = addIds(all, yjbmfzr);
        ejbmfzr = distinctByCode(ejbmfzr,all);
        all = addIds(all, ejbmfzr);
        thqyfzr = distinctByCode(thqyfzr,all);
        all = addIds(all, thqyfzr);
        xmfzr = distinctByCode(xmfzr,all);
        all = addIds(all, xmfzr);
        fyqr = distinctByCode(fyqr,all);
        all = addIds(all, fyqr);
        mapRes.put("fyqr",fyqr);
        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);
        mapRes.put("sxyyg",sxyyg);
        mapRes.put("sxzxg",sxzxg);
        mapRes.put("cwzxcn",cwzxcn);
        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;
    }
}
