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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.generaltrip.dao.UfCcsqsplDao;
import com.engine.htsc.generaltrip.model.CcsqUser;
import com.engine.htsc.generaltrip.model.UfCcsqspl;
import com.engine.htsc.generaltrip.model.UfCcsqsplVo;
import com.engine.htsc.generaltrip.service.GeneralTripLinkService;
import com.engine.htsc.generaltrip.tools.CcsqModelTools;
import com.engine.htsc.generaltrip.tools.UfCcsqServiceTools;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;

import java.util.*;

public class GeneralTripLinkServiceImpl extends Service implements GeneralTripLinkService {

    protected UfCcsqsplDao ufCcsqsplDao = new UfCcsqsplDao();
    protected UfCcsqServiceTools ufCcsqServiceTools = new UfCcsqServiceTools();
    protected CcsqModelTools ccsqModelTools  = new CcsqModelTools();

    @Override
    public JSONObject getGeneralTripSplDetail(HashMap<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 = ufCcsqsplDao.getCcsqsplGx(unnumber);
        //实际所需要的入参
        List<Map<String,String>> reqMapList = new ArrayList<>();
        for (Map<String,String> mapReq : req) {
            HashMap<String,String> mapR = new HashMap<>();
            for (String str : zdmcList){
                mapR.put(str, Util.null2String(mapReq.get(str)));
            }
            reqMapList.add(mapR);
        }
        //获取未去重的审批链
        List<UfCcsqsplVo> respListBefore = this.getReportVO(unnumbercode, reqMapList);
        //节点相加与去重
        Map mapRes = this.removeDuplicateCommon(respListBefore,unnumbercode);
        JSONObject res = (JSONObject) JSONObject.toJSON(mapRes);
        JAVATools.writelog("所有数据相加,去重前节点数据");
        JAVATools.writelog("使用审批链id:"+ Util.null2String(mapRes.get("reportSequenceId")));
        JAVATools.writelog("二级部门负责人:"+ Util.null2String(mapRes.get("ejbmfzr")));
        JAVATools.writelog("一级部门负责人:"+ Util.null2String(mapRes.get("yjbmfzr")));
        JAVATools.writelog("分管领导:"+ Util.null2String(mapRes.get("fgld")));
        JAVATools.writelog("总经理审批:"+ Util.null2String(mapRes.get("zjlsp")));
        JAVATools.writelog("董事长审批:"+ Util.null2String(mapRes.get("dszsp")));
        JAVATools.writelog("领导审批:"+ Util.null2String(mapRes.get("ldsp")));
        return res;

    }

    protected List<UfCcsqsplVo> getReportVO(String unnumbercode, List<Map<String, String>> reqMapList) {
        List<UfCcsqsplVo> respListBefore = new ArrayList<>();
        //合并不同入参
        for(Map<String,String> mingxiMap : reqMapList) {
            //根据不同业务获取Dao层入参
            Map<String,String> mapReqSql = this.getMapReqSql(unnumbercode, mingxiMap);
            //查询初步符合条件的审批链
            List<UfCcsqspl> ufZbsplList = ufCcsqsplDao.getCcsqspl(mapReqSql);
            //条件过滤并赋值
            List<UfCcsqsplVo> ufZbsplVoList = ufCcsqServiceTools.convertModelToVO(ufZbsplList,mingxiMap.get("bxry"),(HashMap)mingxiMap,unnumbercode);
            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<>();
        CcsqUser ccsqUser = new CcsqUser(mapR.get("bxry"));
        String cxsprjs = ccsqUser.getJobGradeLevel();
        String jg = ccsqUser.getCcsqDept().getJglx();
        mapReqSql.put("cxspr","bxry");//查询审批人
        mapReqSql.put("cxsprjs",cxsprjs);//查询审批人角色
        mapReqSql.put("sslc",unumbercode);//查询所属流程
        mapReqSql.put("jg",jg);//查询所属流程
        mapReqSql.put("userCode","WK_" + ccsqUser.getWorkCode());//直接使用人员Code进行匹配
        return mapReqSql;
    }

    protected Map removeDuplicateCommon(List<UfCcsqsplVo> respListBefore, String unnumbercode) {
        Map<String, String> mapRes = new HashMap<>();
        String reportSequenceId = "";//匹配到的数据库审批链id
        String ejbmfzr = "";//二级部门负责人 0或空或自定义编码 数据库用来判断是否跳过
        String yjbmfzr = "";//一级部门负责人
        String fgld = "";//分管领导审批
        String zjlsp = "";//总经理审批
        String dszsp = "";//董事长审批
        String ldsp = "";//领导审批
        for(UfCcsqsplVo vo:respListBefore) {
            reportSequenceId = addIds(reportSequenceId,Util.null2String(vo.getReportSequenceId()));
            ejbmfzr = addIds(ejbmfzr,Util.null2String(vo.getEjbmfzr()));
            yjbmfzr = addIds(yjbmfzr,Util.null2String(vo.getYjbmfzr()));
            fgld = addIds(fgld,Util.null2String(vo.getFgld()));
            zjlsp = addIds(zjlsp,Util.null2String(vo.getZjlsp()));
            dszsp = addIds(dszsp,Util.null2String(vo.getDszsp()));
            ldsp = addIds(ldsp,Util.null2String(vo.getLdsp()));
        }
        mapRes.put("reportSequenceId",reportSequenceId);
        mapRes.put("ejbmfzr",ejbmfzr);
        mapRes.put("yjbmfzr",yjbmfzr);
        mapRes.put("fgld",fgld);
        mapRes.put("zjlsp",zjlsp);
        mapRes.put("dszsp",dszsp);
        mapRes.put("ldsp",ldsp);
        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;
    }

}
