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.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

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

/**
 * 循环按期别分批发放
 */
@Slf4j
public class BySchedulePaymentWay implements HolidayGenerationWay{
    @Override
    public boolean support(int personClassfiy) {
        if (personClassfiy == 1){
            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"));
        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"));
        //提前执行发放操作的天数
        Integer dqtqffts = Integer.valueOf(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();
        }
        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);


        //
        if (dataList.size()>0) {
            String ffsj = Util.null2String(dataList.get(0).get("ffsj"));
            String nowEndYearTime = ffsj.split("-")[0]+"-12-31";
            String forWardTime = DateUtil.beforeDay(nowEndYearTime,dqtqffts) +" "+ffdtffsj;

            int ffsjMonths = DateUtil.getTime(ffsj).getMonthValue();
            int releaseDateMonths = DateUtil.getTime(releaseDate).getMonthValue();
            if (DateUtil.getTime(ffsj).compareTo(DateUtil.getTime(forWardTime)) >=0){
                ffsjMonths=1;
            }
            nowEndYearTime = releaseDate.split("-")[0]+"-12-31";
            forWardTime = DateUtil.beforeDay(nowEndYearTime,dqtqffts) +" "+ffdtffsj;
            if (DateUtil.getTime(releaseDate).compareTo(DateUtil.getTime(forWardTime)) >=0){
                releaseDateMonths=1;
            }
            if (ffsjMonths == releaseDateMonths && !"1".equals(cover)){
                return insertHoliDayParam;
            }
        }
        String sxrq = insertHoliDayParam.get("sxrq").toString();
        //实际休息时长
        double restTime = 0;
        //工龄小于1年的新员工,且额度有效期单位为自然年
        if (allMonths < 12 && edyxq.equals(StartingUnitEnum.NATURAL_YEAR.getKey())) {

            if (QuotaChangeMethodEnum.CONVERT.getKey().equals(edbgsedclfs) || CheckBoxEnum.CHECKED.getKey().equals(schdedablzs)) {

                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,releaseDate,userId);
            } else {
                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.NO_CONVERT,releaseDate,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])) {
                //起算年和发生日期在同一年，生成假期为起算年的当年
                restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,releaseDate,userId);
            } else {
                if (QuotaChangeMethodEnum.CONVERT.getKey().equals(edbgsedclfs)) {
                    restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.CONVERT,releaseDate,userId);
                } else if (QuotaChangeMethodEnum.NO_CONVERT.getKey().equals(edbgsedclfs)) {
                    restTime = getRestTime(insertHoliDayParam,vocationList,QuotaChangeMethodEnum.NO_CONVERT,releaseDate,userId);
                }
            }
        }else if (edyxq.equals(StartingUnitEnum.STARTING_YEAR.getKey())){
            int betweenYear = DateUtil.getBetWeenYears(qsrq,releaseDate.split(" ")[0]);
            Map<String,Object> vocationMap = VocationCommonUtil.getVocationMap(vocationList,betweenYear,yjzd,userId);
            int occurMonths = DateUtil.getTime(releaseDate).getMonthValue();
            int StartMonths = DateUtil.getTime(qsrq).getMonthValue();

            int months = occurMonths-StartMonths;
            if (months < 0){
                months = 12-StartMonths+occurMonths;
            }
            String stage = String.valueOf(months);
            if (months < 10){
                 stage = "0"+stage;
            }
            if (vocationMap.size() >0){
                restTime = Double.valueOf(vocationMap.get("fdsc"+stage).toString());
            }else {
                restTime=0;
            }
        }
        insertHoliDayParam.put("ktsc",restTime);

        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 releaseDate,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 dqtqffts = Integer.valueOf(Util.null2String(vocationList.get(0).get("dqtqffts")));
        //执行发放的时间点
        String ffdtffsj = Util.null2String(vocationList.get(0).get("ffdtffsj"));

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

        String nowEndYearTime = releaseDate.split("-")[0]+"-12-31";
        String forWardTime = DateUtil.beforeDay(nowEndYearTime,dqtqffts) +" "+ffdtffsj;

        if (DateUtil.getTime(releaseDate).compareTo(DateUtil.getTime(forWardTime)) >=0){
            String nextYear = DateUtil.nextYear(releaseDate,1,DateUtil.yyyy);
            releaseDate = nextYear +"-01-01";
        }
        int qsrqMonths = DateUtil.getTime(qsrq).getMonthValue();
        int releaseDateMonths = DateUtil.getTime(releaseDate).getMonthValue();
        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];
            divideTime = DateUtil.nextMonth(divideTime,1,DateUtil.yyyyMM)+"-01";
            int divideTimeMonths = DateUtil.getTime(divideTime).getMonthValue();

            if (beginEdktsc == endEdktsc){
                //当前后额定可休时长一样时，代表没有残年情况

                if (DateUtil.getTime(qsrq).compareTo(DateUtil.getTime(sxrq)) >0){
                    //生成起算日期当年的假期余额，通常用于没有工作经验的新员工
                    int months = releaseDateMonths-qsrqMonths+1;
                    restTime = getRestTime(months,endYearmap);
                }else {
                    restTime = getRestTime(releaseDateMonths,endYearmap);
                }
            }else {
                //考虑残年
                if (DateUtil.getTime(qsrq).compareTo(DateUtil.getTime(sxrq)) >0){
                    //生成起算日期当年的假期余额，通常用于有工作经验的新员工
                    if (DateUtil.getTime(divideTime).compareTo(DateUtil.getTime(qsrq)) >=0){

                        if (DateUtil.getTime(releaseDate).compareTo(DateUtil.getTime(divideTime)) <0){
                            int months = releaseDateMonths-qsrqMonths+1;
                            restTime = getRestTime(months,beginYearmap);
                        }else {
                            int months = releaseDateMonths-divideTimeMonths+1;
                            restTime = getRestTime(months,endYearmap);
                        }
                    }else {
                        int months = releaseDateMonths-qsrqMonths+1;
                        restTime = getRestTime(months,endYearmap);
                    }
                }else {
                    if (DateUtil.getTime(releaseDate).compareTo(DateUtil.getTime(divideTime)) <0){
                        int months = releaseDateMonths-qsrqMonths+1;
                        restTime = getRestTime(months,beginYearmap);
                    }else {
                        int months = releaseDateMonths-divideTimeMonths+1;
                        restTime = getRestTime(months,endYearmap);
                    }
                }
            }

        }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);
            restTime = getRestTime(releaseDateMonths,beginYearmap);
        }
        return restTime;
    }

    public Double getRestTime(int i,Map<String,Object> endYearmap){
        String str = String.valueOf(i);
        if (i < 10){
            str = "0"+str;
        }

        String fdsc = Util.null2String(endYearmap.get("fdsc"+str));
        if ("".equals(fdsc)){
            fdsc="0";
        }
        return Double.valueOf(fdsc);
    }
}
