package com.engine.htsc.branch.approvalChain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.branch.approvalChain.dao.FzjgTravelLinkDao;
import com.engine.htsc.branch.approvalChain.dao.UfFzjgfkgwppDao;
import com.engine.htsc.branch.approvalChain.model.FzjgsplVo;
import com.engine.htsc.branch.approvalChain.model.UfFzjgspl;
import com.engine.htsc.branch.approvalChain.service.FzjgTravelLinkService;
import com.engine.htsc.branch.approvalChain.tools.FzjgModelTools;
import com.engine.htsc.branch.approvalChain.tools.ServiceTools;
import com.engine.integration.util.StringUtils;
import com.sun.source.util.JavacTask;
import weaver.conn.RecordSet;
import weaver.general.StringUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

public class FzjgTravelLinkServiceImpl extends Service implements FzjgTravelLinkService {

    private FzjgTravelLinkDao fzjgTravelLinkDao = new FzjgTravelLinkDao();

    private final UfFzjgfkgwppDao ufFzjgfkgwppDao = new UfFzjgfkgwppDao();

    @Override
    public JSONObject getFzjgDetail(Map<String,Object> map) {
        //根据数据库中获取的字段名称从入参中获取相应的值 List<Map<String, String>> req, String unnumber, String unnumbercode
        JSONObject jsonObject = new JSONObject();
        ServiceTools serviceTools = new ServiceTools();
        FzjgModelTools fzjgModelTools = new FzjgModelTools();
        String unnumber = map.get("unnumber").toString();//fzjgclbx是否是分支机构报销，从关系表中获取字段，获取入参中对应字段的值
        String unnumbercode = map.get("unnumbercode").toString();
        List<String> zdmcList = fzjgTravelLinkDao.getUfFzjgsplGx(unnumber);
        List<Map<String,String>> reqMapList = (List<Map<String,String>>) JSONArray.parse(map.get("reqMapList").toString());
        ArrayList<Map<String, String>> respList = new ArrayList<>();
        List<FzjgsplVo> respListBefore = new ArrayList<>();
        for (Map<String,String> mapReq : reqMapList){
            HashMap<String,String> mapR = new HashMap<>();
            for (String str : zdmcList){
                mapR.put(str, Util.null2String(mapReq.get(str)));
            }
            //拟稿人
            if (null!=mapR.get("ngr")&&!"".equals(mapR.get("ngr"))){
                HashMap<String,String> mapReqSql = new HashMap<>();
                String cxsprjs = fzjgModelTools.getFyftJobCrade(mapR.get("ngr"));
                mapReqSql.put("cxspr","ngr");//查询审批人
                mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
                mapReqSql.put("sslc",unnumbercode);//查询所属流程
                //首先判断是否是营业部普通员工，是则判断是否送领导按钮是否隐藏如果隐藏则不传sfsld
                if("ST_5".equals(cxsprjs)){
                    if (null!=mapR.get("sfsldxsyc")&&"1".equals(mapR.get("sfsldxsyc"))){
                        if (null!=mapR.get("sfsld")&&!"".equals(mapR.get("sfsld"))){
                            mapReqSql.put("sfsld",mapR.get("sfsld"));//判断是否送领导，拟稿节点为空，财务节点后会传值
                        }
                    }
                }
                //查询初步符合条件的审批链
                List<UfFzjgspl> ufFzjgsplList = fzjgTravelLinkDao.getUfjgspl(mapReqSql);
                //条件过滤并赋值
                List<FzjgsplVo> fzjgsplVoList = serviceTools.convertModelToVO(ufFzjgsplList,mapR.get("ngr"),mapR);
                respListBefore.addAll(fzjgsplVoList);
            }
            //报销人员
            if (null!=mapR.get("bxry")&&!"".equals(mapR.get("bxry"))){
                HashMap<String,String> mapReqSql = new HashMap<>();
                String cxsprjs = "";
                if ("fzjgclbx".equals(unnumber)){
                    mapR.put("unnumber",unnumber);
                    cxsprjs = fzjgModelTools.getFyftClbxJobCrade(Util.null2String(mapR.get("bxry")),Util.null2String(mapR.get("szbm")));
                }else {
                    cxsprjs = fzjgModelTools.getFyftJobCrade(mapR.get("bxry"));
                }
                mapReqSql.put("cxspr","bxry");//查询审批人
                mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
                mapReqSql.put("sslc",unnumbercode);//查询所属流程
                //首先判断是否是营业部普通员工，是则判断是否送领导按钮是否隐藏如果隐藏则不传sfsld
                if("ST_5".equals(cxsprjs)) {
                    if (null != mapR.get("sfsldxsyc") && "1".equals(mapR.get("sfsldxsyc"))) {
                        if (null != mapR.get("sfsld") && !"".equals(mapR.get("sfsld"))) {
                            mapReqSql.put("sfsld", mapR.get("sfsld"));//判断是否送领导，拟稿节点为空，财务节点后会传值
                        }
                    }
                }
                //查询初步符合条件的审批链
                List<UfFzjgspl> ufFzjgsplList = fzjgTravelLinkDao.getUfjgspl(mapReqSql);
                JAVATools.writelog("查询获取初步符合条件的审批链:"+ JSONObject.toJSONString(ufFzjgsplList));
                //条件过滤并赋值
                List<FzjgsplVo> fzjgsplVoList = serviceTools.convertModelToVO(ufFzjgsplList,mapR.get("bxry"),mapR);
                respListBefore.addAll(fzjgsplVoList);
            }
        }
        //所有数据相加并去重
        Map mapRes = distinctListFzjgsplVo(respListBefore);
        jsonObject = (JSONObject) JSONObject.toJSON(mapRes);
        JAVATools.writelog("所有数据相加并去重:"+ jsonObject.toJSONString());
        JAVATools.writelog("审批链Id:"+ mapRes.get("id"));
        JAVATools.writelog("费用确认:"+ mapRes.get("fyqr"));
        JAVATools.writelog("分公司二级部门负责人:"+ mapRes.get("ejbmfzr"));
        JAVATools.writelog("营业部部门负责人:"+ mapRes.get("yybbmfzr"));
        JAVATools.writelog("财务中心会计:"+ mapRes.get("cwzxhj"));
        JAVATools.writelog("财务中心经理:"+ mapRes.get("cwzxjl"));
        JAVATools.writelog("北京西北营业部负责人:"+ mapRes.get("bjxbyybfzr"));
        JAVATools.writelog("分公司部门负责人:"+ mapRes.get("fgsbmfzr"));
        JAVATools.writelog("上级部门负责人:"+ mapRes.get("sjbmfzr"));
        JAVATools.writelog("总部财务审核:"+ mapRes.get("zbcw"));
        JAVATools.writelog("首席财务官:"+ mapRes.get("sxcwg"));
        JAVATools.writelog("出纳:"+ mapRes.get("cn"));
        JAVATools.writelog("出纳人员工号:"+ mapRes.get("cnrygh"));
        return jsonObject;
    }

    @Override
    public String getCwzxhj(String bm) {
        //获取财务中心数据
        Map<String,String> cwzxMap = ufFzjgfkgwppDao.getBybm(bm);
        String cwzxhj = Util.null2String(cwzxMap.get("kj"));
        return cwzxhj;
    }

    @Override
    public void getSaveBranchSpl(List<String> requestids) {
        RecordSet rs=new RecordSet();
        for (String requestid:requestids){
            List<Map<String,String>> req = new ArrayList<>();
            Map<String,Object> reqMap = new HashMap();
            reqMap.put("unnumber", "fzjgtyfk");
            reqMap.put("unnumbercode","fzjgtyfk");
            String maintable = JAVATools.getMaintable(requestid);
            String sql = "select ngr,ejbmfzrggfys,hjxxjezfmx from "+maintable+" where requestid = '"+requestid+"'";
            rs.execute(sql);
            if (rs.next()){
                Map<String,String> map = new HashMap<>();
                map.put("bxrbmfzr",Util.null2String(rs.getString("ejbmfzrggfys")));
                map.put("ngr",Util.null2String(rs.getString("ngr")));
                map.put("sfsld","0");
                map.put("sfsldxsyc","0");
                map.put("zfmxhj",Util.null2String(rs.getString("hjxxjezfmx")));
                req.add(map);
            }
            reqMap.put("reqMapList",JSONArray.parseArray(JSON.toJSONString(req)));
            JSONObject jsonObject = getFzjgDetail(reqMap);
            HashMap<String,String> resultMap = JSON.parseObject(jsonObject.toString(),HashMap.class);
            resultMap.put("requestid",requestid);
            fzjgTravelLinkDao.saveMainSpl(resultMap,maintable);
        }
    }

    //通过用户编码来去重
    public Map<String, String> distinctListFzjgsplVo(List<FzjgsplVo> respListBefore) {
        HashSet<String> userIdList = new HashSet<>();
        for (FzjgsplVo fzjgsplVo:respListBefore){
            userIdList.add(Util.null2String(fzjgsplVo.getFyqr()));
            userIdList.add(Util.null2String(fzjgsplVo.getEjbmfzr()));
            userIdList.add(Util.null2String(fzjgsplVo.getYybbmfzr()));
            userIdList.add(Util.null2String(fzjgsplVo.getCwzxhj()));
            userIdList.add(Util.null2String(fzjgsplVo.getCwzxjl()));
            userIdList.add(Util.null2String(fzjgsplVo.getBjxbyybfzr()));
            userIdList.add(Util.null2String(fzjgsplVo.getFgsbmfzr()));
            userIdList.add(Util.null2String(fzjgsplVo.getSjbmfzr()));
            userIdList.add(Util.null2String(fzjgsplVo.getZbcw()));
            userIdList.add(Util.null2String(fzjgsplVo.getSxcwg()));
            userIdList.add(Util.null2String(fzjgsplVo.getCn()));
        }
        userIdList.remove("");
        String UserIds = StringUtils.join(userIdList,",");
        String id = "";
        String fyqr = "";
        String ejbmfzr = "";
        String yybbmfzr = "";
//        String hqbmfzr = "";
        String cwzxhj = "";
        String cwzxjl = "";
        String bjxbyybfzr = "";
        String fgsbmfzr = "";
        String sjbmfzr = "";
        String zbcw = "";
        String sxcwg = "";
        String cn = "";
        //单一节点去重
        for (FzjgsplVo fzjgsplVo:respListBefore){
            id=JAVATools.addidinreloadString(id,Util.null2String(fzjgsplVo.getUfFzjgspl().getId()));
            fyqr=JAVATools.addidinreloadString(fyqr,Util.null2String(fzjgsplVo.getFyqr()));
            ejbmfzr=JAVATools.addidinreloadString(ejbmfzr,Util.null2String(fzjgsplVo.getEjbmfzr()));
            yybbmfzr=JAVATools.addidinreloadString(yybbmfzr,Util.null2String(fzjgsplVo.getYybbmfzr()));
//            hqbmfzr=JAVATools.addidinreloadString(hqbmfzr,Util.null2String(fzjgsplVo.getHqbmfzr()));
            cwzxhj=JAVATools.addidinreloadString(cwzxhj,Util.null2String(fzjgsplVo.getCwzxhj()));
            cwzxjl=JAVATools.addidinreloadString(cwzxjl,Util.null2String(fzjgsplVo.getCwzxjl()));
            bjxbyybfzr=JAVATools.addidinreloadString(bjxbyybfzr,Util.null2String(fzjgsplVo.getBjxbyybfzr()));
            fgsbmfzr=JAVATools.addidinreloadString(fgsbmfzr,Util.null2String(fzjgsplVo.getFgsbmfzr()));
            sjbmfzr=JAVATools.addidinreloadString(sjbmfzr,Util.null2String(fzjgsplVo.getSjbmfzr()));
            zbcw=JAVATools.addidinreloadString(zbcw,Util.null2String(fzjgsplVo.getZbcw()));
            sxcwg=JAVATools.addidinreloadString(sxcwg,Util.null2String(fzjgsplVo.getSxcwg()));
            cn=JAVATools.addidinreloadString(cn,Util.null2String(fzjgsplVo.getCn()));

        }

        //跨节点去重，分公司二级部门负责人和分公司部门负责人去重
        Map<String,String> mapRes = new HashMap();
//        费用确认和营业部负责人以及分公司负责人都要做
        ejbmfzr = distinctEjbmfzr(ejbmfzr,fgsbmfzr);
        fyqr = distinctByCode(fyqr,ejbmfzr);
        fyqr = distinctByCode(fyqr,yybbmfzr);
        fyqr = distinctByCode(fyqr,fgsbmfzr);
        fyqr = distinctByCode(fyqr,bjxbyybfzr);
        fyqr = distinctByCode(fyqr,sjbmfzr);
        mapRes.put("id",id);
        mapRes.put("fyqr",fyqr);
        mapRes.put("ejbmfzr",ejbmfzr);
        mapRes.put("yybbmfzr",yybbmfzr);
//        mapRes.put("hqbmfzr",hqbmfzr);
        mapRes.put("cwzxhj",cwzxhj);
        mapRes.put("cwzxjl",cwzxjl);
        mapRes.put("bjxbyybfzr",bjxbyybfzr);
        mapRes.put("fgsbmfzr",fgsbmfzr);
        mapRes.put("sjbmfzr",sjbmfzr);
        mapRes.put("zbcw",zbcw);
        mapRes.put("sxcwg",sxcwg);
        mapRes.put("cn",cn);
        //获取出纳人员工号
        String cnrygh = JAVATools.gethrmidtoworkcode(cn);
        mapRes.put("cnrygh",cnrygh);
        return mapRes;
    }

    private String distinctFyqr(String fyqr, String ejbmfzr, String yybbmfzr, String fgsbmfzr) {
        fyqr = distinctString(fyqr,ejbmfzr);
        fyqr = distinctString(fyqr,yybbmfzr);
        fyqr = distinctString(fyqr,fgsbmfzr);
        return fyqr;
    }

    //分公司二级部门负责人和分公司部门负责人去重
    private String distinctEjbmfzr(String ejbmfzr, String fgsbmfzr) {
        String result = "";
        List<Map<String,String>> mapListE = ufFzjgfkgwppDao.getUserMap(ejbmfzr);
        Map<String,String> ejbmfzrMap = mapListE.get(1);//分公司二级部门负责人的 code和id
        List<Map<String,String>> mapListF = ufFzjgfkgwppDao.getUserMap(fgsbmfzr);
        Map<String, String> fgsbmfzrMap = mapListF.get(1);//分公司部门负责人的 code和id
        for(String code:ejbmfzrMap.keySet()){
            if(null==fgsbmfzrMap.get(code)||"".equals(fgsbmfzrMap.get(code))){
                result+=result==""?Util.null2String(ejbmfzrMap.get(code)):","+Util.null2String(ejbmfzrMap.get(code));
            }
        }
        return result;
    }

    //跨界点使用code去重，去除param中包含base的部分
    private String distinctByCode(String param, String base) {
        String result = "";
        if (!"".equals(param)){
            List<Map<String,String>> baseMapList = ufFzjgfkgwppDao.getUserMap(base);
            Map<String,String> baseMap = baseMapList.get(1);//parm
            List<Map<String,String>> paramMapList = ufFzjgfkgwppDao.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;
    }

    //数组前后去重,返回param中base不包含的数据
    public String distinctString(String param,String base){
        String result = "";
        if (null!=base&&!"".equals(base)){
            List<String> baseArray = Arrays.asList(base.split(","));
            if (null!=param&&!"".equals(param)){
                List<String> paramArray = Arrays.asList(param.split(","));
                for (String str:paramArray){
                    if (!baseArray.contains(str)){
                        result+=result==""?str:","+str;
                    }
                }
            }
        }else {
            result = param;
        }
        return result;
    }

    //获取多个key对应的value
    public String getValues(String params,Map<String,String> map){
        String result = "";
        String[] array = params.split(",");
        Set<String> codeList = new HashSet<>(Arrays.asList(array));
        for (String id : codeList){
            if(!"".equals(Util.null2String(map.get(id)))){
                result+=result==""?Util.null2String(map.get(id)):","+Util.null2String(map.get(id));
            }
        }
        return result;
    }

}
