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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
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 com.engine.htsc.general.service.GeneralPaymentLinkService;
import com.engine.htsc.general.tools.UfzbsplServiceTools;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

public class GeneralPaymentLinkServiceImpl extends Service implements GeneralPaymentLinkService {

    protected UfZbsplDao ufZbsplDao = new UfZbsplDao();
    protected UfzbsplServiceTools ufzbsplServiceTools = new UfzbsplServiceTools();

    public Map getGeneralSpl(Map<String, Object> map) {
        String unnumber = map.get("unnumber").toString();
        String unnumbercode = map.get("unnumbercode").toString();
        //获取所需要的传参
        List<Map<String,String>> req = (List<Map<String,String>>) JSONArray.parse(map.get("reqMapList").toString());
        //数据库匹配获取对应需要的入参
        List<String> zdmcList = ufZbsplDao.getZbsplgx(unnumber);
        List<Map<String,String>> reqMapList = new ArrayList<>();
        Map<String,Map<String,String>> reqMapReq = new HashMap<>();
        Map<String,Double> fycdbmMap = new HashMap<>();
        Map<String,String> fycdbmYskmlxMap = new HashMap<>();
        //假设全部是虚拟部门
        String fycdbmlx = "0";
        for (Map<String,String> mapReq : req){
            String fycdbm = Util.null2String(mapReq.get("fycdbm"));
            ZbDept zbDept = new ZbDept(fycdbm);
            if("0".equals(zbDept.getCanceled())){
                fycdbmlx = "1";
            }
            Double dxfymx = Util.getDoubleValue(Util.null2String(mapReq.get("dxfymx")));
            if (dxfymx<0){
                dxfymx = 0.0;
            }
            if(null==fycdbmMap.get(fycdbm)){
                fycdbmMap.put(fycdbm,dxfymx);
            }else {
                fycdbmMap.put(fycdbm,dxfymx+fycdbmMap.get(fycdbm));
            }
            //判断是否含装修费
            String yskm = Util.null2String(mapReq.get("yskm"));
            //预算科目类型 0：不包含装修及VI改造，1：只有装修及VI改造，2，包含装修及VI改造且有其他装修费
            if(null==fycdbmYskmlxMap.get(fycdbm)){
                if("8701020101".equals(yskm)){
                    fycdbmYskmlxMap.put(fycdbm,"1");
                }else {
                    fycdbmYskmlxMap.put(fycdbm,"0");
                }
            }else {
                if ("8701020101".equals(yskm)&&"0".equals(fycdbmYskmlxMap.get(fycdbm))){
                    fycdbmYskmlxMap.put(fycdbm,"2");
                }else if (!"8701020101".equals(yskm)&&"1".equals(fycdbmYskmlxMap.get(fycdbm))){
                    fycdbmYskmlxMap.put(fycdbm,"2");
                }
            }
            HashMap<String,String> mapR = new HashMap<>();
            for (String str : zdmcList){
                mapR.put(str, Util.null2String(mapReq.get(str)));
            }

            reqMapReq.put(fycdbm,mapR);
        }
        //判断是否是多费用承担部门 1是0否
        for(String key:reqMapReq.keySet()){
            if(!"".equals(key)){
                if (reqMapReq.size()>1){
                    reqMapReq.get(key).put("dbmFlag","1");
                }else {
                    reqMapReq.get(key).put("dbmFlag","0");
                }
                reqMapReq.get(key).put("fycdbmlx",fycdbmlx);
                reqMapReq.get(key).put("dgfycdbmhj",fycdbmMap.get(key).toString());
                reqMapReq.get(key).put("yskmlx",fycdbmYskmlxMap.get(key));
                reqMapList.add(reqMapReq.get(key));
            }
        }
        //获取未去重的审批链
        List<UfZbsplVo> respListBefore = this.getReportVO(unnumbercode, reqMapList,new ArrayList<>(reqMapReq.keySet()));
        //系统层面去重
        Map mapRes = this.removeDuplicateCommon(respListBefore,unnumbercode);
        JSONObject res = (JSONObject) JSONObject.toJSON(mapRes);
        JAVATools.writelog("所有数据相加并去重:"+ res.toJSONString());
        JAVATools.writelog("使用审批链id:"+ Util.null2String(mapRes.get("reportSequenceId")));
        JAVATools.writelog("二级部门负责人:"+ Util.null2String(mapRes.get("ejbmfzr")));
        JAVATools.writelog("部门预算管理员:"+ Util.null2String(mapRes.get("bmysgly")));
        JAVATools.writelog("一级部门负责人:"+ Util.null2String(mapRes.get("yjbmfzr")));
        JAVATools.writelog("信息技术部预算管理员:"+ Util.null2String(mapRes.get("xjysgly")));
        JAVATools.writelog("费用承担部门二级部门负责人:"+ Util.null2String(mapRes.get("fycdbmerbmfzr")));
        JAVATools.writelog("费用承担部门预算管理员:"+ Util.null2String(mapRes.get("fycdbmysgly")));
        JAVATools.writelog("费用承担部门一级部门负责人:"+ Util.null2String(mapRes.get("fycdbmyjbmfzr")));
        JAVATools.writelog("会签部门负责人:"+ Util.null2String(mapRes.get("hqbmfzr")));
        JAVATools.writelog("会签部门负责人xj:"+ Util.null2String(mapRes.get("hqbmfzrxj")));
        JAVATools.writelog("会签部门负责人zt:"+ Util.null2String(mapRes.get("hqbmfzrzt")));
        JAVATools.writelog("纸质发票提交:"+ Util.null2String(mapRes.get("zzfptj")));
        JAVATools.writelog("费用会计:"+ Util.null2String(mapRes.get("fykj")));
        JAVATools.writelog("财务负责人:"+ Util.null2String(mapRes.get("cwfzr")));
        JAVATools.writelog("党委副书记:"+ Util.null2String(mapRes.get("dwfsj")));
        JAVATools.writelog("党委书记:"+ Util.null2String(mapRes.get("dwsj")));
        JAVATools.writelog("首席财务官:"+ Util.null2String(mapRes.get("sxcwg")));
        JAVATools.writelog("分管领导审批:"+ Util.null2String(mapRes.get("sp")));
        JAVATools.writelog("总裁审批:"+ Util.null2String(mapRes.get("ldsp")));
        JAVATools.writelog("出纳:"+ Util.null2String(mapRes.get("cn")));
        JAVATools.writelog("出纳人员工号:"+ Util.null2String(mapRes.get("cnrygh")));
        return mapRes;
    }

    protected List<UfZbsplVo> getReportVO(String unnumbercode, List<Map<String, String>> reqMapList ,List fycdbmList) {
        List<UfZbsplVo> respListBefore = new ArrayList<>();
        //合并不同入参
        for(Map<String,String> mingxiMap : reqMapList) {
            //根据不同业务获取Dao层入参
            Map<String,String> mapReqSql = this.getMapReqSql(unnumbercode, mingxiMap);
            //查询初步符合条件的审批链
            List<UfZbspl> ufZbsplList = ufZbsplDao.getUfZbspl(mapReqSql);
            //条件过滤并赋值
            List<UfZbsplVo> ufZbsplVoList = ufzbsplServiceTools.convertModelToVO(ufZbsplList,mingxiMap.get("ngr"),(HashMap)mingxiMap,unnumbercode,fycdbmList);
            respListBefore.addAll(ufZbsplVoList);
        }
        return respListBefore;
    }

    /**
     * 根据map生成查询审批链的条件map
     * [子类按需重写]
     * @param mapR
     * @return
     */
    protected Map<String,String> getMapReqSql(String unumbercode,Map<String,String> mapR) {
        HashMap<String,String> mapReqSql = new HashMap<>();
        ZbUser zbUser = new ZbUser(mapR.get("ngr"));
        String cxsprjs = zbUser.getJobGradeLevel();
        mapReqSql.put("cxspr","ngr");//查询审批人
        mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
        mapReqSql.put("sslc",unumbercode);//查询所属流程
        mapReqSql.put("jg",zbUser.getDepLevel());//查询所属流程
        mapReqSql.put("userCode","WK_" + zbUser.getWorkCode());//直接使用人员Code进行匹配
        return mapReqSql;
    }

    protected Map removeDuplicateCommon(List<UfZbsplVo> respListBefore, String unnumbercode) {
        Map<String, String> mapRes = new HashMap<>();
        TreeMap<String,ArrayList<String>> lsjdListTree = ufZbsplDao.getSpljd(unnumbercode,"code");
        String reportSequenceId = "";
        String ejbmfzr = "";
        String bmysgly = "";
        String yjbmfzr = "";
        String xjysgly = "";
        String fycdbmerbmfzr = "";
        String fycdbmysgly = "";
        String fycdbmyjbmfzr = "";
        String hqbmfzr = "";
        String zzfptj = "";
        String fykj = "";
        String cwfzr = "";
        String dwfsj = "";
        String dwsj = "";
        String sxcwg = "";
        String sp = "";
        String ldsp = "";
        String cn = "";
        String cnrygh = "";
        //所有的人员id
        String userIds = "";
        for(UfZbsplVo vo:respListBefore){
            ejbmfzr = addIds(ejbmfzr,Util.null2String(vo.getEjbmfzr()));
            userIds = addIds(userIds,ejbmfzr);
            bmysgly = addIds(bmysgly,Util.null2String(vo.getBmysgly()));
            userIds = addIds(userIds,bmysgly);
            yjbmfzr = addIds(yjbmfzr,Util.null2String(vo.getYjbmfzr()));
            userIds = addIds(userIds,yjbmfzr);
            xjysgly = addIds(xjysgly,Util.null2String(vo.getXjysgly()));
            userIds = addIds(userIds,xjysgly);
            fycdbmerbmfzr = addIds(fycdbmerbmfzr,Util.null2String(vo.getFycdbmerbmfzr()));
            userIds = addIds(userIds,fycdbmerbmfzr);
            fycdbmysgly = addIds(fycdbmysgly,Util.null2String(vo.getFycdbmysgly()));
            userIds = addIds(userIds,fycdbmysgly);
            fycdbmyjbmfzr = addIds(fycdbmyjbmfzr,Util.null2String(vo.getFycdbmyjbmfzr()));
            userIds = addIds(userIds,fycdbmyjbmfzr);
            hqbmfzr = addIds(hqbmfzr,Util.null2String(vo.getHqbmfzr()));
            userIds = addIds(userIds,hqbmfzr);
            zzfptj = addIds(zzfptj,Util.null2String(vo.getZzfptj()));
            userIds = addIds(userIds,zzfptj);
            fykj = addIds(fykj,Util.null2String(vo.getFykj()));
            userIds = addIds(userIds,fykj);
            cwfzr = addIds(cwfzr,Util.null2String(vo.getCwfzr()));
            userIds = addIds(userIds,cwfzr);
            dwfsj = addIds(dwfsj,Util.null2String(vo.getDwfsj()));
            userIds = addIds(userIds,dwfsj);
            dwsj = addIds(dwsj,Util.null2String(vo.getDwsj()));
            userIds = addIds(userIds,dwsj);
            sxcwg = addIds(sxcwg,Util.null2String(vo.getSxcwg()));
            userIds = addIds(userIds,sxcwg);
            sp = addIds(sp,Util.null2String(vo.getSp()));
            userIds = addIds(userIds,sp);
            ldsp = addIds(ldsp,Util.null2String(vo.getLdsp()));
            userIds = addIds(userIds,ldsp);
            cn = addIds(cn,Util.null2String(vo.getCn()));
            userIds = addIds(userIds,cn);
            cnrygh = addIds(cnrygh,Util.null2String(vo.getCnrygh()));
            reportSequenceId = addIds(reportSequenceId,Util.null2String(vo.getReportSequenceId()));
        }
        mapRes.put("reportSequenceId",reportSequenceId);
        mapRes.put("ejbmfzr",ejbmfzr);
        mapRes.put("bmysgly",bmysgly);
        mapRes.put("yjbmfzr",yjbmfzr);
        mapRes.put("xjysgly",xjysgly);
        mapRes.put("fycdbmerbmfzr",fycdbmerbmfzr);
        mapRes.put("fycdbmysgly",fycdbmysgly);
        mapRes.put("fycdbmyjbmfzr",fycdbmyjbmfzr);
        mapRes.put("hqbmfzr",hqbmfzr);
        mapRes.put("zzfptj",zzfptj);
        mapRes.put("fykj",fykj);
        mapRes.put("cwfzr",cwfzr);
        //当总部通用付款流程同时需要党委书记张伟和党委副书记尹立鸿（018500）审批时，仅由党委书记张伟（015150）审批即可；
        if (!"".equals(dwsj)&&!"".equals(dwfsj)){
            String dwsjgh = JAVATools.gethrmidtoworkcode(dwsj);
            String dwfsjgh = JAVATools.gethrmidtoworkcode(dwfsj);
            if (dwsjgh.contains("015150")&&dwfsjgh.contains("018500")){
                dwfsj = "";
            }
        }
        mapRes.put("dwfsj",dwfsj);
        mapRes.put("dwsj",dwsj);
        mapRes.put("sxcwg",sxcwg);
        mapRes.put("sp",sp);
        mapRes.put("ldsp",ldsp);
        mapRes.put("cn",cn);
        mapRes.put("cnrygh",cnrygh);
        JAVATools.writelog("所有数据相加,去重前节点数据");
        JAVATools.writelog("使用审批链id:"+ Util.null2String(mapRes.get("reportSequenceId")));
        JAVATools.writelog("二级部门负责人:"+ Util.null2String(mapRes.get("ejbmfzr")));
        JAVATools.writelog("部门预算管理员:"+ Util.null2String(mapRes.get("bmysgly")));
        JAVATools.writelog("一级部门负责人:"+ Util.null2String(mapRes.get("yjbmfzr")));
        JAVATools.writelog("信息技术部预算管理员:"+ Util.null2String(mapRes.get("xjysgly")));
        JAVATools.writelog("费用承担部门二级部门负责人:"+ Util.null2String(mapRes.get("fycdbmerbmfzr")));
        JAVATools.writelog("费用承担部门预算管理员:"+ Util.null2String(mapRes.get("fycdbmysgly")));
        JAVATools.writelog("费用承担部门一级部门负责人:"+ Util.null2String(mapRes.get("fycdbmyjbmfzr")));
        JAVATools.writelog("会签部门负责人:"+ Util.null2String(mapRes.get("hqbmfzr")));
        JAVATools.writelog("纸质发票提交:"+ Util.null2String(mapRes.get("zzfptj")));
        JAVATools.writelog("费用会计:"+ Util.null2String(mapRes.get("fykj")));
        JAVATools.writelog("财务负责人:"+ Util.null2String(mapRes.get("cwfzr")));
        JAVATools.writelog("党委副书记:"+ Util.null2String(mapRes.get("dwfsj")));
        JAVATools.writelog("党委书记:"+ Util.null2String(mapRes.get("dwsj")));
        JAVATools.writelog("首席财务官:"+ Util.null2String(mapRes.get("sxcwg")));
        JAVATools.writelog("分管领导审批:"+ Util.null2String(mapRes.get("sp")));
        JAVATools.writelog("总裁审批:"+ Util.null2String(mapRes.get("ldsp")));
        JAVATools.writelog("出纳:"+ Util.null2String(mapRes.get("cn")));
        JAVATools.writelog("出纳人员工号:"+ Util.null2String(mapRes.get("cnrygh")));
        String userIdString = String.join(",", userIds);
        Map<String,String> idAndCodes = ufZbsplDao.getUserMap(userIdString).get(0);
        //遍历节点，获取需要去重的节点jdAndCodeMap,获取每次去重的list
        for (String lcjdsx:lsjdListTree.keySet()){
            List<String> lsjdList = lsjdListTree.get(lcjdsx);
            Set<String> codeUsed = new HashSet<>();
            for (String jd:lsjdList){
                String userId = mapRes.get(jd);
                String userIdNew = "";
                if(!"".equals(userId)){
                    List<String> userIdList = Arrays.asList(userId.split(","));
                    for (String id:userIdList){
                        //判断节点人员工号是否出现过
                        if(!codeUsed.contains(idAndCodes.get(id))){
                            userIdNew+=userIdNew==""?id:","+id;
                            codeUsed.add(idAndCodes.get(id));
                        }
                    }
                    mapRes.put(jd,userIdNew);
                }
            }
        }
//        会签部门逻辑调整
//        1、hqbmfzr入参不变
//        2、响应参数需要处理
//        1）如果hqbmfzr中存在'20010,10971' 返回hqbmfzrxj;
//        2）如果hqbmfzr中存在'23058,26377' 返回hqbmfzrzt;
//        3) 如果hqbmfzr中不存在('20010,10971', '23058,26377')的值 返回hqbmfzr;
        hqbmfzr = mapRes.get("hqbmfzr");
        List<String> hqbmfzrList = Arrays.asList(hqbmfzr.split(","));
        List<String> newhqbmfzrList = new ArrayList<>();
        List<String> hqbmfzrxjList = new ArrayList<>();
        List<String> hqbmfzrztList = new ArrayList<>();
        for(String id:hqbmfzrList){
            if("001411".equals(idAndCodes.get(id))||"016205".equals(idAndCodes.get(id))){
                hqbmfzrxjList.add(id);
            }else if("001730".equals(idAndCodes.get(id))||"002447".equals(idAndCodes.get(id))){
                hqbmfzrztList.add(id);
            }else {
                newhqbmfzrList.add(id);
            }
        }
        mapRes.put("hqbmfzr",String.join(",",newhqbmfzrList));
        mapRes.put("hqbmfzrxj",String.join(",",hqbmfzrxjList));
        mapRes.put("hqbmfzrzt",String.join(",",hqbmfzrztList));
        return mapRes;
    }

    public String addIds(String ids,String value){
        if(value==null||"".equals(value)){
            List idArray = new ArrayList(Arrays.asList(ids.split(",")));
            HashSet idSet = new HashSet(idArray);
            ids = String.join(",", idSet);
            return ids;
        }
        if(ids==null||"".equals(ids)){
            List valueArray = new ArrayList(Arrays.asList(value.split(",")));
            HashSet valueSet = new HashSet(valueArray);
            value = String.join(",", valueSet);
            return value;
        }
        List idArray = new ArrayList(Arrays.asList(ids.split(",")));
        List valueArray = new ArrayList(Arrays.asList(value.split(",")));
        idArray.addAll(valueArray);
        HashSet idSet = new HashSet(idArray);
        String str = String.join(",", idSet);
        return str;
    }
}
