package com.engine.attendance.vacation.job.holidaygeneration.tactics;


import com.engine.attendance.enums.CheckBoxEnum;
import com.engine.attendance.enums.QuotaChangeMethodEnum;
import com.engine.attendance.enums.StartingUnitEnum;
import com.engine.attendance.vacation.util.VocationCommonUtil;
import com.engine.common.util.DateUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 循环单次发放全额
 */
@Slf4j
public class SinglePaymentInFullWay implements HolidayGenerationWay {
    @Override
    public boolean support(int personClassfiy) {
        if (personClassfiy == 0) {
            return true;
        }
        return false;
    }

    @Override
    public Map<String,Object> compute(Map<String, Object> data) {
        String userId = Util.null2String(data.get("userId"));
        String releaseDate = Util.null2String(data.get("releaseDate"));
        String cover = Util.null2String(data.get("cover"));
        String isOneTime = Util.null2String(data.get("isOneTime"));
        if ("".equals(releaseDate)) {
            releaseDate = DateUtil.getCurrentTime();
        }
        List<Map<String, Object>> vocationList = (List<Map<String, Object>>) data.get("vocationList");
        //起算日期
        String qsrq = Util.null2String(vocationList.get(0).get("qsrqValue"));
        //额度有效期间
        String edyxqsz = Util.null2String(vocationList.get(0).get("edyxqsz"));
        //额度有效期间单位
        String edyxq = Util.null2String(vocationList.get(0).get("edyxq"));
        //累计承认工龄月份数栏位值
        String ljcrglyfslwz = Util.null2String(vocationList.get(0).get("ljcrglyfslwzValue")).equals("")?"0":Util.null2String(vocationList.get(0).get("ljcrglyfslwzValue"));
        //依据字段
        String yjzd = Util.null2String(vocationList.get(0).get("yjzdValue"));
        //调整起算日期策略
        String dzqsrq = Util.null2String(vocationList.get(0).get("dzqsrq"));
        //分割日期
        String fgrq = Util.null2String(vocationList.get(0).get("fgrq"));
        //提前执行发放操作的天数
        String dqtqffts = Util.null2String(vocationList.get(0).get("dqtqffts"));
        //执行发放的时间点
        String ffdtffsj = Util.null2String(vocationList.get(0).get("ffdtffsj"));
        //入职当期额度按比例折算
        String rzdqedablzs = Util.null2String(vocationList.get(0).get("rzdqedablzs"));
        //首次获得额度按比例折算
        String schdedablzs = Util.null2String(vocationList.get(0).get("schdedablzs"));
        //额度变更时额度处理方式
        String edbgsedclfs = Util.null2String(vocationList.get(0).get("edbgsedclfs"));
        //额度单位
        String eddw = Util.null2String(vocationList.get(0).get("edbgsedclfs"));
        //尾数处理
        String wscl = Util.null2String(vocationList.get(0).get("wscl"));
        //首次获得额度控制生效日期
        String schdedkzsxrq = Util.null2String(vocationList.get(0).get("schdedkzsxrq"));

        if ("".equals(qsrq)){
            return Maps.newHashMap();
        }
        if (!"1".equals(isOneTime)){
            qsrq = VocationCommonUtil.getAfterAdjustTime(qsrq, dzqsrq, fgrq);
            for (Map<String, Object> vocationMap : vocationList) {
                vocationMap.put("qsrqValue", qsrq);
            }
        }
        if (DateUtil.getTime(releaseDate).compareTo(DateUtil.getTime(qsrq)) < 0){
            //releaseDate=qsrq;
            return Maps.newHashMap();
        }

        //相差天数
        int betweenDays = DateUtil.getBetWeenDays(qsrq, releaseDate.split(" ")[0]);
        //相差月份
        int betweenMonth = DateUtil.getBetWeenMonths(qsrq, releaseDate.split(" ")[0]);
        //总工龄天数
        int allWorkDays = Double.valueOf(ljcrglyfslwz).intValue() * 30 + betweenDays;
        //总月数
        int allMonths = betweenMonth + Double.valueOf(ljcrglyfslwz).intValue();


        Map<String, Object> insertHoliDayParam = VocationCommonUtil.getInsertHoliDayParam(vocationList.get(0), userId, qsrq, releaseDate);

        List<Map<String,Object>> dataList = VocationCommonUtil.ifexist("uf_jcl_kq_jqye", insertHoliDayParam);
       // List<Map<String,Object>> dataList = new ArrayList<>();

        //已存在数据且不覆盖
        if (dataList.size()>0 && !"1".equals(cover)) {
            return insertHoliDayParam;
        }

        String sxrq = insertHoliDayParam.get("sxrq").toString();
        //实际休息时长
        double restTime = 0;
        //工龄小于1年的新员工,且额度有效期单位为自然年
        if (allMonths < 12 && edyxq.equals(StartingUnitEnum.NATURAL_YEAR.getKey())) {
//            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, 0, yjzd);
//
//            //额定休息时长
//            double edktsc = Double.valueOf(vocationMap.get("edktsc") == null?  "0" :Util.null2String(vocationMap.get("edktsc")));
            if (QuotaChangeMethodEnum.CONVERT.getKey().equals(edbgsedclfs) || CheckBoxEnum.CHECKED.getKey().equals(schdedablzs)) {
//                if (!sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年和发生日期在同一年，生成假期在下一年
//                    int days = DateUtil.getBetWeenDays(qsrq, qsrq.split("-")[0] + "-12-31") + 1;
//                    int yearDays = DateUtil.getDays(qsrq, Calendar.DAY_OF_YEAR);
//                    restTime = VocationCommonUtil.computeRestDays(days, yearDays, edktsc, wscl);
//                } else if (sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年和发生日期在同一年，生成假期为起算年的当年
//                    int days = Double.valueOf(ljcrglyfslwz).intValue() * 30;
//                    int yearDays = DateUtil.getDays(DateUtil.beforeYear(releaseDate,1,DateUtil.yyyyMMdd), Calendar.DAY_OF_YEAR);
//                    restTime = VocationCommonUtil.computeRestDays(days, yearDays, edktsc, wscl);
//                } else if (sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && !releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年在发生日期的上一年，起算年的后一年
//                    int days = DateUtil.getBetWeenDays(qsrq, qsrq.split("-")[0] + "-12-31") + 1;
//                    int yearDays = DateUtil.getDays(qsrq, Calendar.DAY_OF_YEAR);
//                    restTime = VocationCommonUtil.computeRestDays(days, yearDays, edktsc, wscl);
//                } else if (!sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && !releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年在发生日期的上一年，生成假期为起算年的后第二年
//                    restTime = edktsc;
//                }
                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,userId);

            } else {
//                restTime = edktsc;
                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.NO_CONVERT,userId);
            }


            if (CheckBoxEnum.CHECKED.getKey().equals(schdedkzsxrq)) {
                int seniority = "".equals(ljcrglyfslwz)?0:Double.valueOf(ljcrglyfslwz).intValue();
                String divideTime = DateUtil.beforeMonth(qsrq,seniority);
                divideTime = sxrq.split("-")[0]+"-"+divideTime.split("-")[1]+"-"+divideTime.split("-")[2];
                insertHoliDayParam.put("sxrq", divideTime);

                dataList = VocationCommonUtil.ifexist("uf_jcl_kq_jqye", insertHoliDayParam);
                //已存在数据且不覆盖
                if (dataList.size()>0 && !"1".equals(cover)) {
                    return insertHoliDayParam;
                }
            }
            insertHoliDayParam.put("ktsc", restTime);
        } else if (allMonths >= 12 && edyxq.equals(StartingUnitEnum.NATURAL_YEAR.getKey())) {
            //入职不满一年，且入职当期额度按比例折算
            if (CheckBoxEnum.CHECKED.getKey().equals(rzdqedablzs) && betweenMonth < 12
                    && sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
                //起算年和发生日期在同一年，生成假期为起算年的当年
//                Map<String, Object>  vocationMap = VocationCommonUtil.getVocationMap(vocationList, Double.valueOf(ljcrglyfslwz).intValue() / 12, yjzd);
//                double edktsc = Double.valueOf(vocationMap.get("edktsc").toString());
//                int days = DateUtil.getBetWeenDays(qsrq, releaseDate.split(" ")[0]) + 1;
//                int yearDays = DateUtil.getDays(qsrq, Calendar.DAY_OF_YEAR);
//                restTime = VocationCommonUtil.computeRestDays(days, yearDays, edktsc, wscl);
                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,userId);
            } else {
//
//                if (!sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年和发生日期在同一年，生成假期在下一年
//                    //betweenMonth = DateUtil.getBetWeenMonths(qsrq,qsrq.split("-")[0] + "-12-31");
//                    betweenMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
//
//                } else if (sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年和发生日期在同一年，生成假期为发生日期的当年
//                    betweenMonth=0;
//
//                } else if (sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && !releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年在发生日期的之前，生成假期为发生日期的当年
//                    //betweenMonth = DateUtil.getBetWeenMonths(qsrq,DateUtil.beforeYear(releaseDate,1,DateUtil.yyyy) + "-12-31");
//                    betweenMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
//
//                } else if (!sxrq.split("-")[0].equals(releaseDate.split("-")[0]) && !releaseDate.split("-")[0].equals(qsrq.split("-")[0])) {
//                    //起算年在发生日期的之前，生成假期为发生日期的下一年
//                    //betweenMonth = DateUtil.getBetWeenMonths(qsrq,releaseDate.split("-")[0] + "-12-31");
//                    betweenMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
//
//                }
//                allMonths = betweenMonth + Double.valueOf(ljcrglyfslwz).intValue();
//                int intervalYear = allMonths/12;
//                Map<String, Object>  vocationMap = VocationCommonUtil.getVocationMap(vocationList, intervalYear / 12, yjzd);
//                double edktsc = Double.valueOf(vocationMap.get("edktsc").toString());

                if (QuotaChangeMethodEnum.CONVERT.getKey().equals(edbgsedclfs)) {
//                    Map<String, Object> beforeVocationMap = VocationCommonUtil.getVocationMap(vocationList, intervalYear - 1, yjzd);
//                    if (!beforeVocationMap.get("detailid").equals(vocationMap.get("detailid"))) {
//                        //考虑残年情况
//                        double beforeEdktsc = Double.valueOf(beforeVocationMap.get("edktsc").toString());
//                        int needBackMonths = (intervalYear - 1) * 12 - Double.valueOf(ljcrglyfslwz).intValue();
//
//                        String middleTime = releaseDate.split("-")[0] + "-" + DateUtil.nextMonth(qsrq, needBackMonths, DateUtil.yyyyMMdd).split("-")[1] + "-" + qsrq.split("-")[2];
//                        int beforeDays = DateUtil.getBetWeenDays(releaseDate.split("-")[0] + "-01-01", middleTime);
//                        int afterDays = DateUtil.getBetWeenDays(middleTime, releaseDate.split("-")[0] + "-12-31");
//
//                        int yearDays = DateUtil.getDays(releaseDate, Calendar.DAY_OF_YEAR);
//                        restTime = VocationCommonUtil.computeRestDays(beforeDays, yearDays, beforeEdktsc, wscl) + VocationCommonUtil.computeRestDays(afterDays, yearDays, edktsc, wscl);
//                    } else {
//                        restTime = edktsc;
//                    }
                    restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,userId);
                } else if (QuotaChangeMethodEnum.NO_CONVERT.getKey().equals(edbgsedclfs)) {
//                    restTime = edktsc;
                    restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.NO_CONVERT,userId);
                }
            }
        } else if (edyxq.equals(StartingUnitEnum.STARTING_YEAR.getKey())) {
            int betweenYear = DateUtil.getBetWeenYears(qsrq,releaseDate.split(" ")[0]);
            //起算年
            allMonths = Double.valueOf(ljcrglyfslwz).intValue();
            int intervalYear = allMonths / 12+betweenYear;
            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, intervalYear, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;
        } else if (edyxq.equals(StartingUnitEnum.NATURAL_MONTH.getKey())){
            //自然月
            betweenMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
            if (DateUtil.getTime(sxrq).compareTo(DateUtil.getTime(qsrq)) <=0){
                betweenMonth = 0;
            }
            allMonths = betweenMonth+Double.valueOf(ljcrglyfslwz).intValue();

            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, allMonths, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;

        } else if (edyxq.equals(StartingUnitEnum.STARTING_MONTH.getKey())){
            //起算月
            betweenMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
            allMonths = betweenMonth+Double.valueOf(ljcrglyfslwz).intValue();

            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, allMonths, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;
        } else if (edyxq.equals(StartingUnitEnum.NATURAL_WEEK.getKey())){
            //起自然周
            betweenDays = DateUtil.getBetWeenDays(qsrq,sxrq)+1;
            if (DateUtil.getTime(sxrq).compareTo(DateUtil.getTime(qsrq)) <=0){
                betweenDays = 0;
            }
            allWorkDays = Double.valueOf(ljcrglyfslwz).intValue()*30+betweenDays;
            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, allWorkDays/7, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;

        } else if (edyxq.equals(StartingUnitEnum.STARTING_WEEK.getKey())){
            //起算周
            betweenDays = DateUtil.getBetWeenDays(qsrq,sxrq)+1;
            if (betweenDays <=7){
                betweenDays=0;
            }
            allWorkDays = Double.valueOf(ljcrglyfslwz).intValue()*30+betweenDays;
            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, allWorkDays/7, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;

        } else if (edyxq.equals(StartingUnitEnum.NATURAL_DAY.getKey())){
            //自然天
            betweenDays = DateUtil.getBetWeenDays(qsrq,sxrq)+1;
            allWorkDays = Double.valueOf(ljcrglyfslwz).intValue()*30+betweenDays;
            Map<String, Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList, allWorkDays, yjzd,userId);
            restTime = vocationMap.size()>0?Double.valueOf(vocationMap.get("edktsc").toString()):0;
        }
        insertHoliDayParam.put("ktsc", restTime);


        if (!"1".equals(isOneTime)){
            VocationCommonUtil.updateHolidayBalance("uf_jcl_kq_jqye", insertHoliDayParam,dataList);
        }
        return insertHoliDayParam;
    }


    public double getRestTime(Map<String, Object> insertHoliDayParam,List<Map<String, Object>> vocationList,QuotaChangeMethodEnum changeMethodEnum,String userId){
        String sxrq = insertHoliDayParam.get("sxrq").toString();
        String qsrq = Util.null2String(vocationList.get(0).get("qsrqValue"));
        String nextSxrq = DateUtil.nextYear(sxrq,1,DateUtil.yyyyMMdd);
        //累计承认工龄月份数栏位值
        String ljcrglyfslwz = Util.null2String(vocationList.get(0).get("ljcrglyfslwzValue"));
        //依据字段
        String yjzd = Util.null2String(vocationList.get(0).get("yjzdValue"));
        //尾数处理
        String wscl = Util.null2String(vocationList.get(0).get("wscl"));

        int seniority = "".equals(ljcrglyfslwz)?0:Double.valueOf(ljcrglyfslwz).intValue();

        double restTime=0.0;
        //折算
        if (changeMethodEnum == QuotaChangeMethodEnum.CONVERT){
            int beginYearMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
            if (beginYearMonth < 0){
                beginYearMonth = 0;
            }
            beginYearMonth =  beginYearMonth+seniority;
            int endYearMonth = DateUtil.getBetWeenMonths(qsrq,nextSxrq);
            endYearMonth = endYearMonth+seniority;
            int beginYear = beginYearMonth/12;
            int endYear = endYearMonth/12;
            int endYearRemainder = endYearMonth%12;
            Map<String,Object> beginYearmap = VocationCommonUtil.getVocationMap(vocationList, beginYear, yjzd,userId);
            Map<String,Object> endYearmap = VocationCommonUtil.getVocationMap(vocationList, endYear, yjzd,userId);
            double beginEdktsc = Double.valueOf(beginYearmap.get("edktsc") == null?  "0" :Util.null2String(beginYearmap.get("edktsc")));
            double endEdktsc = Double.valueOf(endYearmap.get("edktsc") == null?  "0" :Util.null2String(endYearmap.get("edktsc")));

            //残年分割日期
            String divideTime = DateUtil.beforeMonth(qsrq,seniority);
            divideTime = sxrq.split("-")[0]+"-"+divideTime.split("-")[1]+"-"+divideTime.split("-")[2];
            int yearDays = DateUtil.getDays(sxrq, Calendar.DAY_OF_YEAR);

            if (beginEdktsc == endEdktsc){
                //当前后额定可休时长一样时，代表没有残年情况
                if (DateUtil.getTime(qsrq).compareTo(DateUtil.getTime(sxrq)) >0){
                    int days = DateUtil.getBetWeenDays(qsrq,nextSxrq);
                    restTime = VocationCommonUtil.computeRestDays(days, yearDays, endEdktsc, wscl);
                }else {
                    restTime = beginEdktsc;
                }
            }else {
                //考虑残年
                if (DateUtil.getTime(qsrq).compareTo(DateUtil.getTime(sxrq)) >0){

                    if (DateUtil.getTime(divideTime).compareTo(DateUtil.getTime(qsrq)) >=0){
                        int beforeDays = DateUtil.getBetWeenDays(qsrq,divideTime);
                        int afterDays = DateUtil.getBetWeenDays(divideTime,nextSxrq)+1;

                        double beforeRestTime = VocationCommonUtil.computeRestDays(beforeDays, yearDays, beginEdktsc, wscl);
                        double afterRestTime = VocationCommonUtil.computeRestDays(afterDays, yearDays, endEdktsc, wscl);
                        restTime = beforeRestTime+afterRestTime;
                    }else {
                        int afterDays = DateUtil.getBetWeenDays(qsrq,nextSxrq);
                        restTime = VocationCommonUtil.computeRestDays(afterDays, yearDays, endEdktsc, wscl);
                    }

                }else {
                    int beforeDays = DateUtil.getBetWeenDays(sxrq,divideTime);
                    int afterDays = DateUtil.getBetWeenDays(divideTime,nextSxrq)+1;
                    double beforeRestTime = VocationCommonUtil.computeRestDays(beforeDays, yearDays, beginEdktsc, wscl);
                    double afterRestTime = VocationCommonUtil.computeRestDays(afterDays, yearDays, endEdktsc, wscl);
                    restTime = beforeRestTime+afterRestTime;
                }
            }

        }else {
            //不折算
            int beginYearMonth = DateUtil.getBetWeenMonths(qsrq,sxrq);
            if (beginYearMonth < 0){
                beginYearMonth = 0;
            }
            beginYearMonth =  beginYearMonth+seniority;
            int beginYear = beginYearMonth/12;
            Map<String,Object> beginYearmap = VocationCommonUtil.getVocationMap(vocationList, beginYear, yjzd,userId);
            double beginEdktsc = Double.valueOf(beginYearmap.get("edktsc") == null?  "0" :Util.null2String(beginYearmap.get("edktsc")));
            restTime=beginEdktsc;
        }
        return restTime;
    }

}
