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

import com.engine.core.impl.Service;
import com.engine.htsc.trip.service.GetStandardService;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class GetStandardServiceImpl extends Service implements GetStandardService {

    @Override
    public List<Map<String,String>> getReimbursementStandard(List<Map<String,String>> reqList) { List<Map<String,String>> resultList = new ArrayList<>();
        RecordSet rs = new RecordSet();
        try {
            PropBean propBean = new PropBean();
            String departmentStr =  propBean.getPropName("travelexpense_dept");
            String[] departmentList = departmentStr.split(",");
            int mapSize = 0;
//            String sqlDepartmentSum = "";
            for(Map<String,String> reqMap : reqList){
                String city = reqMap.get("cityId");
                String travelCode = reqMap.get("travelCode");
                String startTime = reqMap.get("startTime");
                String mapSql = "";
                String endTime = reqMap.get("endTime");
                mapSql = "t1.id = "+reqMap.get("userId")+" ";
                String sqlDepartment = "select t1.id as userId,"+"'"+city+"'"+ " as cityReq,"+"t1.lastname,t2.id as departmentId,t2.departmentcode,t2.departmentname,t3.Field9 \n" +
                        " from hrmresource t1 left join hrmdepartment t2 on t1.DEPARTMENTID = t2.ID left join cus_fielddata t3 on t1.id = t3.id \n" +
                        " where t3.SCOPEID = '1' and " + mapSql + " ";
                JAVATools.SetCmdwritelog("查询人员职级" + "语句:" + sqlDepartment);
                rs.execute(sqlDepartment);
                //获取人员的业务部门id，总部党委，董事会，经营管理层三个部门时报时销，不在后端判断
                if (rs.next()){
                    HashMap<String,String> resultMap = new HashMap<>();
                    //部门id
                    String departmentId = Util.null2String(rs.getString("departmentId"));
                    //部门是否是业务部门 0是 1否
                    String bmsx = "0";
                    if (Arrays.asList(departmentList).contains(departmentId)){
                        bmsx = "1";
                    }
                    //职级
                    String level = Util.null2String(rs.getString("Field9"));
                    //判断是否是否为业务部门
                    String userId = Util.null2String(rs.getString("userId"));
                    //获取城市等级
//                    HashMap<String,String> cityMap = getCityUtil(city);
                    String[] areas = city.split(",");
                    String citySql = "select t.uuid,t.code,t.EXPENSE_LIMIT_TYPE from travel_area t,travel_area t2 where t.parent_code = t2.code and t2.code = '"
                            +areas[0]+ "' and t.code = '"+areas[1]+"'";
                    RecordSetDataSource rsdM = new RecordSetDataSource("Travel");
                    rsdM.execute(citySql);
                    if (rsdM.next()) {
                        String cityId = Util.null2String(rsdM.getString("uuid"));
                        String cityCode = Util.null2String(rsdM.getString("code"));
                        String cityLevel = Util.null2String(rsdM.getString("EXPENSE_LIMIT_TYPE"));
                        resultMap = getStandard(userId, cityId, level, cityLevel, bmsx, city,travelCode,startTime,endTime);
                        resultList.add(resultMap);
                    }
                }
            }
            if (null!=resultList){
                resultList = getRealStandard(resultList);
                resultList = getTimeStandard(resultList);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

    private List<Map<String, String>> getTimeStandard(List<Map<String, String>> resultList) {
        List<Map<String, String>> result = new ArrayList<>();
        //计算出每个入参所对应的差标
        for(Map<String, String> map: resultList){
            if (!"".equals(Util.null2String(map.get("standard")))&&!"".equals(Util.null2String(map.get("time")))){
                double time = Double.valueOf(map.get("time"));
                double standard = Double.valueOf(map.get("standard"));
                double value = time * standard;
                map.put("standard",String.valueOf(value));
            }
        }
        //计算出travelCode，userId对应的拆标
        Map<String,Map<String,String>> paramMap = new HashMap<>();
        for(Map<String, String> map: resultList){
            String codeAndId = map.get("travelCode")+"====="+map.get("userId");
            if (null==paramMap.get(codeAndId)){
                paramMap.put(codeAndId,new HashMap<String, String>());
                Map<String,String> param = new HashMap<>();
                param.put("travelCode",map.get("travelCode"));
                param.put("userId",map.get("userId"));
                param.put("standard",map.get("standard"));
                param.put("bmsx",map.get("bmsx"));
                param.put("fj",map.get("fj"));
                param.put("lc",map.get("lc"));
                param.put("jttl",map.get("jttl"));
                param.put("level",map.get("level"));
                param.put("cityLevel",map.get("cityLevel"));
                param.put("cityId",map.get("cityId"));
                param.put("standardLevel",map.get("standardLevel"));
                param.put("cityName",map.get("cityName"));
                paramMap.put(codeAndId,param);
            }else {
                double standardSum = Double.valueOf(paramMap.get(codeAndId).get("standard"));
                double standard = Double.valueOf(map.get("standard"));
                paramMap.get(codeAndId).put("standard",String.valueOf(standardSum+standard));
            }
        }
        for (String key : paramMap.keySet()){
            result.add(paramMap.get(key));
        }
        return result;
    }



    //获取城市信息和城市等级
    public HashMap<String,String> getCityUtil(String cityReq){
        HashMap<String,String> cityMap = new HashMap<>();
        String[] cityArray = cityReq.split("-");
        if (cityArray.length<2){
            cityArray = cityReq.split(" ");
        }
        String area = cityReq.substring(0,2);
        String city = cityArray[1].replace("市","");
        cityMap.put("area",area);
        cityMap.put("city",city);
        return cityMap;
    }

    //处理结果
    private List<Map<String, String>> getRealStandard(List<Map<String, String>> resultList) {

        String sql = "select BXRZJ, CBDJ, BMSX, JLCSCB, YLCSCB, BLCSCB, FJ, LC, JTTL from uf_zbcbbz where CBDJ in ('BE4','BE3','BE2','BE1','FE3','FE2','FE1')";
        RecordSet rs = new RecordSet();
        //按照travelCode进行分类，判断是否是同行的人
        Map<String, ArrayList<Map<String,String>>> mapCount = new HashMap<>();
        for(Map<String, String> map:resultList){
            if (null!=mapCount.get(map.get("travelCode"))&&mapCount.get(map.get("travelCode")).size()>0){
                mapCount.get(map.get("travelCode")).add(map);
            }else {
                mapCount.put(map.get("travelCode"),new ArrayList<>());
                mapCount.get(map.get("travelCode")).add(map);
            }
        }

        //获取可能因MD随行修改的四种情况和未包含的特殊情况
        Map<String,Map<String, String>> mapAll = new HashMap();
        try {
            JAVATools.SetCmdwritelog("查询剩余部分差标" + "语句:" + sql);
            rs.execute(sql);
            while (rs.next()){
                HashMap<String,String> resultMap = new HashMap<>();
                resultMap.put("level",Util.null2String(rs.getString("bxrzj")));
                resultMap.put("bmsx",Util.null2String(rs.getString("bmsx")));
                resultMap.put("standardLevel",Util.null2String(rs.getString("CBDJ")));
                resultMap.put("standardj",Util.null2String(rs.getString("JLCSCB")));
                resultMap.put("standardy",Util.null2String(rs.getString("YLCSCB")));
                resultMap.put("standardb",Util.null2String(rs.getString("BLCSCB")));
                resultMap.put("fj",Util.null2String(rs.getString("FJ")));
                resultMap.put("lc",Util.null2String(rs.getString("LC")));
                resultMap.put("jttl",Util.null2String(rs.getString("JTTL")));
                mapAll.put(resultMap.get("standardLevel"),resultMap);
            }
            if (null!=mapAll&&mapAll.size()>0){
                //更据出差单号进行分组遍历，判断是否需要升级 ，对每组进行分析
                for(String code:mapCount.keySet()){
                    //判断同行需要升级情况获取最高等级
                    String highestLevel = getHighestLevel(mapCount.get(code));

                    //存在随行领导,最高FE1
                    if ("FE1".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("FE2".equals(map.get("standardLevel"))||"BE1".equals(map.get("standardLevel"))||"BE2".equals(map.get("standardLevel"))||"FE3".equals(map.get("standardLevel"))||"BE3".equals(map.get("standardLevel"))){
                                //FE2、BE1、BE2、BE3、FE3全部升级为FE1
                                map.put("standardLevel","FE1");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE1").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE1").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("FE1").get("standardb"));
                                }
                                map.put("fj",mapAll.get("FE1").get("fj"));
                                map.put("lc",mapAll.get("FE1").get("lc"));
                                map.put("jttl",mapAll.get("FE1").get("jttl"));
                            }else if("BE4".equals(map.get("standardLevel"))){
                                //BE4升级为FE2
                                map.put("standardLevel","FE2");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE2").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE2").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("FE2").get("standardb"));
                                }
                                map.put("fj",mapAll.get("FE2").get("fj"));
                                map.put("lc",mapAll.get("FE2").get("lc"));
                                map.put("jttl",mapAll.get("FE2").get("jttl"));
                            }
                        }
                    }
                    //存在随行领导,最高FE2
                    if ("FE2".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("BE2".equals(map.get("standardLevel"))||"BE3".equals(map.get("standardLevel"))||"BE4".equals(map.get("standardLevel"))||"FE3".equals(map.get("standardLevel"))){
                                //BE2、BE3、BE4、FE3升级为FE2
                                map.put("standardLevel","FE2");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE2").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE2").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("FE2").get("standardb"));
                                }
                                map.put("fj",mapAll.get("FE2").get("fj"));
                                map.put("lc",mapAll.get("FE2").get("lc"));
                                map.put("jttl",mapAll.get("FE2").get("jttl"));
                            }else if("BE1".equals(map.get("standardLevel"))){
                                //BE1升级为FE1
                                map.put("standardLevel","FE1");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE1").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE1").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("FE1").get("standardb"));
                                }
                                map.put("fj",mapAll.get("FE1").get("fj"));
                                map.put("lc",mapAll.get("FE1").get("lc"));
                                map.put("jttl",mapAll.get("FE1").get("jttl"));
                            }
                        }
                    }
                    //存在随行领导,最高BE1
                    if ("BE1".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("BE2".equals(map.get("standardLevel"))||"BE3".equals(map.get("standardLevel"))||"FE3".equals(map.get("standardLevel"))){
                                //BE2、BE3、FE3升级为BE1
                                map.put("standardLevel","BE1");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE1").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE1").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("BE1").get("standardb"));
                                }
                                map.put("fj",mapAll.get("BE1").get("fj"));
                                map.put("lc",mapAll.get("BE1").get("lc"));
                                map.put("jttl",mapAll.get("BE1").get("jttl"));
                            }else if("BE4".equals(map.get("standardLevel"))){
                                //BE4升级为BE2
                                map.put("standardLevel","BE2");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE2").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE2").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("BE2").get("standardb"));
                                }
                                map.put("fj",mapAll.get("BE2").get("fj"));
                                map.put("lc",mapAll.get("BE2").get("lc"));
                                map.put("jttl",mapAll.get("BE2").get("jttl"));
                            }
                        }
                    }
                    //存在随行领导,最高BE2
                    if ("BE2".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("FE3".equals(map.get("standardLevel"))||"BE3".equals(map.get("standardLevel"))||"BE4".equals(map.get("standardLevel"))){
                                //FE3、BE3、BE4升级为BE2
                                map.put("standardLevel","BE2");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE2").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE2").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("BE2").get("standardb"));
                                }
                                map.put("fj",mapAll.get("BE2").get("fj"));
                                map.put("lc",mapAll.get("BE2").get("lc"));
                                map.put("jttl",mapAll.get("BE2").get("jttl"));
                            }
                        }
                    }
                    //存在随行领导,最高FE3
                    if ("FE3".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("BE3".equals(map.get("standardLevel"))||"BE4".equals(map.get("standardLevel"))){
                                //BE3、BE4升级为FE3
                                map.put("standardLevel","FE3");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE3").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("FE3").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("FE3").get("standardb"));
                                }
                                map.put("fj",mapAll.get("FE3").get("fj"));
                                map.put("lc",mapAll.get("FE3").get("lc"));
                                map.put("jttl",mapAll.get("FE3").get("jttl"));
                            }
                        }
                    }
                    //存在随行领导,最高BE3
                    if ("BE3".equals(highestLevel)) {
                        for (Map<String, String> map : mapCount.get(code)) {
                            String cityLevel = map.get("cityLevel");
                            if ("BE4".equals(map.get("standardLevel"))){
                                //BE4升级为BE3
                                map.put("standardLevel","BE3");
                                //A级城市
                                if (null!=cityLevel&&"A".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE3").get("standardj"));
                                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                                    map.put("standard",mapAll.get("BE3").get("standardy"));
                                }else {
                                    map.put("standard",mapAll.get("BE3").get("standardb"));
                                }
                                map.put("fj",mapAll.get("BE3").get("fj"));
                                map.put("lc",mapAll.get("BE3").get("lc"));
                                map.put("jttl",mapAll.get("BE3").get("jttl"));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

    private String getHighestLevel(ArrayList<Map<String, String>> maps) {
        String highestLevel = "";
        for (Map<String, String> map:maps){
            if ("FE1".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "FE1";
            }
        }
        for (Map<String, String> map:maps){
            if ("FE2".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "FE2";
            }
        }
        for (Map<String, String> map:maps){
            if ("BE1".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "BE1";
            }
        }
        for (Map<String, String> map:maps){
            if ("BE2".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "BE2";
            }
        }
        for (Map<String, String> map:maps){
            if ("FE3".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "FE3";
            }
        }
        for (Map<String, String> map:maps){
            if ("BE3".equals(Util.null2String(map.get("standardLevel")))&&"".equals(highestLevel)){
                highestLevel = "BE3";
            }
        }
        return highestLevel;
    }

    //初步获取差标
    public HashMap<String,String> getStandard(String userId,String cityId,String level,String cityLevel,String bmsx,String cityReq,String travelCode,String startTime,String endTime){
        HashMap<String,String> resultMap = new HashMap<>();
        String sqlDV = "select BXRZJ, CBDJ, BMSX, JLCSCB, YLCSCB, BLCSCB, FJ, LC, JTTL from uf_zbcbbz where CBDJ in ('BE4','FE3')";
        JAVATools.SetCmdwritelog("查询差标BE4,FE3"+ " uf_zbcbbz " + "语句:" + sqlDV);
        RecordSet rs = new RecordSet();
        rs.execute(sqlDV);
        Map<String,Map<String, String>> mapAll = new HashMap();
        while (rs.next()){
            HashMap<String,String> map = new HashMap<>();
            map.put("level",Util.null2String(rs.getString("bxrzj")));
            map.put("bmsx",Util.null2String(rs.getString("bmsx")));
            map.put("standardLevel",Util.null2String(rs.getString("CBDJ")));
            map.put("standardj",Util.null2String(rs.getString("JLCSCB")));
            map.put("standardy",Util.null2String(rs.getString("YLCSCB")));
            map.put("standardb",Util.null2String(rs.getString("BLCSCB")));
            map.put("fj",Util.null2String(rs.getString("FJ")));
            map.put("lc",Util.null2String(rs.getString("LC")));
            map.put("jttl",Util.null2String(rs.getString("JTTL")));
            mapAll.put(map.get("standardLevel"),map);
        }
        String sqlCb = "select BXRZJ, CBDJ, BMSX, JLCSCB, YLCSCB, BLCSCB, FJ, LC, JTTL" +
                " from uf_zbcbbz where bmsx = " + bmsx +  " and  ','|| bxrzj||',' like '%,"+level+",%' ";
        try {
            JAVATools.SetCmdwritelog("查询差标"+ " uf_zbcbbz " + "语句:" + sqlCb);
            rs.execute(sqlCb);
            resultMap.put("userId",userId);
            resultMap.put("cityId",cityId);
            resultMap.put("level",level);
            resultMap.put("bmsx",bmsx);
            resultMap.put("cityLevel",cityLevel);
            resultMap.put("cityName",cityReq);
            resultMap.put("travelCode",travelCode);
            resultMap.put("startTime",startTime);
            resultMap.put("endTime",endTime);
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate dateS = LocalDate.parse(startTime, fmt);
            LocalDate dateE = LocalDate.parse(endTime, fmt);
            long daysDiff = ChronoUnit.DAYS.between(dateS, dateE);
            resultMap.put("time", String.valueOf(daysDiff));
            if (rs.next()) {
                resultMap.put("standardLevel",Util.null2String(rs.getString("CBDJ")));
                //A级城市
                if (null!=cityLevel&&"A".equals(cityLevel)){
                    resultMap.put("standard",Util.null2String(rs.getString("JLCSCB")));
                }else if(null!=cityLevel&&"B".equals(cityLevel)){
                    resultMap.put("standard",Util.null2String(rs.getString("YLCSCB")));
                }else {
                    resultMap.put("standard",Util.null2String(rs.getString("BLCSCB")));
                }
                resultMap.put("fj",Util.null2String(rs.getString("FJ")));
                resultMap.put("lc",Util.null2String(rs.getString("LC")));
                resultMap.put("jttl",Util.null2String(rs.getString("JTTL")));
            }else {
                //业务D以下
                if ("0".equals(bmsx)){
                    resultMap.put("standardLevel","FE3");
                    //A级城市
                    if (null!=cityLevel&&"A".equals(cityLevel)){
                        resultMap.put("standard",mapAll.get("FE3").get("standardj"));
                    }else if(null!=cityLevel&&"B".equals(cityLevel)){
                        resultMap.put("standard",mapAll.get("FE3").get("standardy"));
                    }else {
                        resultMap.put("standard",mapAll.get("FE3").get("standardb"));
                    }
                    resultMap.put("fj",mapAll.get("FE3").get("fj"));
                    resultMap.put("lc",mapAll.get("FE3").get("lc"));
                    resultMap.put("jttl",mapAll.get("FE3").get("jttl"));

                }else {
                    //非业务VP以下
                    resultMap.put("standardLevel","BE4");
                    //A级城市
                    //A级城市
                    if (null!=cityLevel&&"A".equals(cityLevel)){
                        resultMap.put("standard",mapAll.get("BE4").get("standardj"));
                    }else if(null!=cityLevel&&"B".equals(cityLevel)){
                        resultMap.put("standard",mapAll.get("BE4").get("standardy"));
                    }else {
                        resultMap.put("standard",mapAll.get("BE4").get("standardb"));
                    }
                    resultMap.put("fj",mapAll.get("BE4").get("fj"));
                    resultMap.put("lc",mapAll.get("BE4").get("lc"));
                    resultMap.put("jttl",mapAll.get("BE4").get("jttl"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

}
