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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.XgtyfkUfzgsServiceTools;
import weaver.conn.RecordSet;
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 XgtyfkSubCompanyLinkServiceImpl extends SubCompanyLinkServiceImpl {

    protected XgtyfkUfzgsServiceTools xgtyfkUfzgsServiceTools = new XgtyfkUfzgsServiceTools();

    @Override
    protected List<ReportSequenceVO> getReportVO(String unumbercode, List<Map<String,String>> reqMapList) {
        //香港通用付款逻辑
        //只有拟稿人所在部门是香港人力资源部的情况下才匹配该条（每条付款明细遍历，构造condition的时候，如果拟稿部门和付款类型满足条件，则优先匹配这条）
        //1.付款明细的预算科目编码为8601050401或8602050401 则认为包含律师费
        String code = "";
        for (Map<String,String> mapReq : reqMapList) {
            ZgsUser zgsUser = new ZgsUser(mapReq.get("ngr"));
            if("ZZ323340".equals(zgsUser.getZgsDept().getDepCode())){
                if("8601050401".equals(Util.null2String(mapReq.get("yskm")))||"8602050401".equals(Util.null2String(mapReq.get("yskm")))){
                    code = "1";
                }else{
                    code = "0";
                }
            }
        }
        //判断单项预算科目对应的限额是否超过
        for (Map<String,String> mapReq : reqMapList) {
            String flag = "0";
            Double dxfymx = Util.getDoubleValue(Util.null2String(mapReq.get("dxfymx")));
            Double yssped = Util.getDoubleValue(Util.null2String(mapReq.get("yssped")));
            if (dxfymx<=yssped||yssped<0){
                flag = "0";
            }else {
                flag = "1";
            }
            mapReq.put("flag",flag);
        }
        List<ReportSequenceVO> respListBefore = new ArrayList<>();
        for(Map<String,String> mingxiMap : reqMapList) {
            //根据不同业务获取Dao层入参
            //条件过滤并赋值
            mingxiMap.put("code",code);
            Double zfmxhj = Util.getDoubleValue(Util.null2String(mingxiMap.get("zfmxhj")));
            String biz = Util.null2String(mingxiMap.get("biz"));
            Map<String,Double> hlMap = ufZgssplDao.gethlMap();
            //汇率
            Double hl = hlMap.get(biz);
            if(null!=hl){
                zfmxhj = zfmxhj * hl;
            }
            mingxiMap.put("zfmxhj",zfmxhj.toString());
            Map<String,String> mapReqSql = this.getMapReqSql(unumbercode, mingxiMap);
            //查询初步符合条件的审批链
            List<UfZgsspl> ufFzjgsplList = ufZgssplDao.getUfzgsspl(mapReqSql);
            List<ReportSequenceVO> fzjgsplVoList = xgtyfkUfzgsServiceTools.convertModelToVO(ufFzjgsplList,mingxiMap.get("ngr"),(HashMap)mingxiMap,unumbercode);
            respListBefore.addAll(fzjgsplVoList);
        }
        return respListBefore;
    }

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

    public String saveSubCompanySpl(String requestid) {
        RecordSet rs = new RecordSet();
        String tableName = JAVATools.getMaintable(requestid);//主表名
        String tableNameDtl = tableName + "_dt8";//明细表
        JAVATools.writelog("requestid:" + requestid + " 香港通用付款，系统发起节点提交时，调用审批链接口，主表名:" + tableName);
        try {
            //查询主表数据
            String sql = " select ngr,id,fklx,biz,lcbh from " + tableName + " where requestid='" + requestid + "'";
            rs.executeQuery(sql);
            if(rs.next()){
                String ngr = Util.null2String(rs.getString("ngr"));//获取拟稿人
                String mainid = Util.null2String(rs.getString("id"));//获取主表id
                String fklx = Util.null2String(rs.getString("fklx"));//付款类型
                String biz = Util.null2String(rs.getString("biz"));//币种
                String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
                //查询明细表数据
                String sqldt = " select * from " + tableNameDtl + " where mainid ='" + mainid + "'";
                rs.executeQuery(sqldt);
                List<Map<String,String>> req = new ArrayList<>();
                Double zfmxhj = 0.0;//支付明细合计
                Map<String,Double> hlMap = ufZgssplDao.gethlMap();
                //汇率
                Double hl = hlMap.get(biz);
                while (rs.next()){
                    Map<String,String> map = new HashMap<>();
                    //拟稿人
                    map.put("ngr",ngr);
                    //单项费用金额
                    Double je = Double.valueOf(Util.null2String(rs.getString("je")));
                    //转换币种
                    if(null!=hl){
                        je = je * hl;
                    }
                    map.put("dxfymx",je.toString());
                    //计算支付明细合计
                    zfmxhj = zfmxhj + je;
                    //费用承担部门fycdbm
                    String fycdbm = Util.null2String(rs.getString("fysjcdbm"));
                    map.put("fycdbm",fycdbm);
                    //预算科目
                    String yskm = Util.null2String(rs.getString("yskm"));
                    map.put("yskm",yskm);
                    //付款类型
                    map.put("fklx",fklx);
                    //单项费用限额
                    String yssped = Util.null2String(rs.getString("yssped"));
                    map.put("yssped",yssped);
                    req.add(map);
                }
                for(Map<String,String> map:req){
                    map.put("zfmxhj",zfmxhj.toString());
                }
                HashMap<String,Object> reqMap = new HashMap();
                reqMap.put("unnumber", "xgtyfk");
                reqMap.put("unnumbercode","xgtyfk");
                reqMap.put("reqMapList", JSONArray.parseArray(JSON.toJSONString(req)));
                JSONObject res = getSubCompanySpl(reqMap);
                JAVATools.writelog("requestid:" + requestid + " 香港通用付款，系统发起节点提交时，调用审批链接口，获取审批链结果:" + res.toJSONString());
                Map<String, String> mapRes = JSONObject.toJavaObject(res, Map.class);
                String zgsejb = Util.null2String(mapRes.get("ejbmfzr"));
                String zgsyjb = Util.null2String(mapRes.get("yjbmfzr"));
                String hqbmflb = Util.null2String(mapRes.get("hqbmflb"));
                String hqbmfxglb = Util.null2String(mapRes.get("hqbmfxglb"));
                String zbzjglg = Util.null2String(mapRes.get("zbzjglg"));
                String zbzjyybfzr = Util.null2String(mapRes.get("zbzjyybfzr"));
                String sxyyg = Util.null2String(mapRes.get("sxyyg"));
                String sxcwg = Util.null2String(mapRes.get("sxzxg"));
                String jtldsp =  Util.null2String(mapRes.get("jtld"));
                String cwzxcn = Util.null2String(mapRes.get("cwzxcn"));
                String cwzxhj = Util.null2String(mapRes.get("kj"));
                String sqlUpdate = " update "+tableName+" set zgsejb = '" + zgsejb
                        + "' , zgsyjb = '" + zgsyjb
                        + "' , hqbmflb = '" + hqbmflb
                        + "' , hqbmfxglb = '" + hqbmfxglb
                        + "' , zbzjglg = '" + zbzjglg
                        + "' , zbzjyybfzr = '" + zbzjyybfzr
                        + "' , sxyyg = '" + sxyyg
                        + "' , sxcwg = '" + sxcwg
                        + "' , jtldsp = '" + jtldsp
                        + "' , cwzxhj = '" + cwzxhj
                        + "' , cwzxcn = '" + cwzxcn + "' where requestid = '" + requestid +"'";
                JAVATools.writelog("requestid:" + requestid + " 香港通用付款，系统发起节点提交时，调用审批链接口，审批链更新到主表:" + sqlUpdate);
                rs.execute(sqlUpdate);
                //更新workflow_requestbase的流程编号
                JAVATools.writelog("update workflow_requestbase set requestmark='" + lcbh + "' where requestid=" + requestid);
                rs.executeUpdate("update workflow_requestbase set requestmark='" + lcbh + "' where requestid=" + requestid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }
        return "SUCCESS";
    }
}
