package com.sqx.modules.utils;

import cn.hutool.core.date.DateUtil;
import com.sqx.modules.artificer.entity.Period;

import java.util.Calendar;
import java.util.Date;

public class PeriodUtil {
    //获取传入日期的周期的开始时间以及结束时间
    public static Period obtainCycle(Date date) {
        Period period = new Period();
        final Calendar c = Calendar.getInstance();
        c.setTime(date);
        //如果日期为1号到10号 返回当月1日与10日
        if(c.get(Calendar.DAY_OF_MONTH) >=1 && c.get(Calendar.DAY_OF_MONTH)<=15){
            c.set(Calendar.DATE,1);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            period.setStartFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
            c.set(Calendar.DATE,15);
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            c.set(Calendar.MILLISECOND, 999);
            period.setEndFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
        } else if (c.get(Calendar.DAY_OF_MONTH)>=16 && c.get(Calendar.DAY_OF_MONTH)<=31) {
            //如果日期为11号到20号 返回当月11日与20日
            c.set(Calendar.DATE,16);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            period.setStartFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
//            c.set(Calendar.DATE,20);
//            c.set(Calendar.HOUR_OF_DAY, 23);
//            c.set(Calendar.MINUTE, 59);
//            c.set(Calendar.SECOND, 59);
//            c.set(Calendar.MILLISECOND, 999);
//            period.setEndFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
//        }else if (c.get(Calendar.DAY_OF_MONTH)>=21 && c.get(Calendar.DAY_OF_MONTH)<=31) {
//            //如果日期为21号到31号 返回当月21日与当月最后一天
//            c.set(Calendar.DATE,21);
//            c.set(Calendar.HOUR_OF_DAY, 0);
//            c.set(Calendar.MINUTE, 0);
//            c.set(Calendar.SECOND, 0);
//            c.set(Calendar.MILLISECOND, 0);
//            period.setStartFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
            //获取月底的最后一天
            c.set(Calendar.DATE,DateUtil.endOfMonth(c).get(Calendar.DATE));
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            c.set(Calendar.MILLISECOND, 999);
            period.setEndFundData(DateUtil.parse(DateUtil.format(c.getTime(),"yyyy-MM-dd HH:mm:ss")));
        }
        return period;
    }
    //传入时间计算上一期时间
    public static Period previousIssue(Date date) {
        //取当前时间的上一个周期
        Date previousDate =  DateUtil.offsetDay(date,-10);
        //获取上一个周期的开始以及结束时间
        Period period = obtainCycle(previousDate);
        return period;
    }
    //传入时间计算周期
    public static Period calculationPeriod(Period period) {
        Period selectPeriod;
        switch (period.getFundType()){
            case 1:
                //本期 根据传入的时间获取周期开始时间以及结束时间
                selectPeriod = obtainCycle(period.getFundData());
                period.setStartFundData(selectPeriod.getStartFundData());
                period.setEndFundData(selectPeriod.getEndFundData());
                break;
            case 2:
                //上期
                selectPeriod = obtainCycle(period.getFundData());
                selectPeriod = previousIssue(selectPeriod.getStartFundData());
                period.setStartFundData(selectPeriod.getStartFundData());
                period.setEndFundData(selectPeriod.getEndFundData());
                break;
            case 3:
                //本月
                Date beginOfMonth = DateUtil.parse(DateUtil.format(DateUtil.beginOfMonth(period.getFundData()), "yyyy/MM/dd"));
                Date endOfMonth = DateUtil.parse(DateUtil.format(DateUtil.endOfMonth(period.getFundData()), "yyyy/MM/dd"));
                final Calendar beginOfMonthc = Calendar.getInstance();
                beginOfMonthc.setTime(beginOfMonth);
                beginOfMonthc.set(Calendar.HOUR_OF_DAY, 0);
                beginOfMonthc.set(Calendar.MINUTE, 0);
                beginOfMonthc.set(Calendar.SECOND, 0);
                beginOfMonthc.set(Calendar.MILLISECOND, 0);
                final Calendar endOfMonthc = Calendar.getInstance();
                endOfMonthc.setTime(endOfMonth);
                endOfMonthc.set(Calendar.HOUR_OF_DAY, 23);
                endOfMonthc.set(Calendar.MINUTE, 59);
                endOfMonthc.set(Calendar.SECOND, 59);
                endOfMonthc.set(Calendar.MILLISECOND, 999);
                period.setStartFundData(DateUtil.parse(DateUtil.format(beginOfMonthc.getTime(),"yyyy-MM-dd HH:mm:ss")));
                period.setEndFundData(DateUtil.parse(DateUtil.format(endOfMonthc.getTime(),"yyyy-MM-dd HH:mm:ss")));
                break;
            case 4:
                //上月
                Date beginOfMonth2 = DateUtil.parse(DateUtil.format(DateUtil.beginOfMonth(DateUtil.offsetMonth(period.getFundData(),-1)), "yyyy/MM/dd"));
                Date endOfMonth2 = DateUtil.parse(DateUtil.format(DateUtil.endOfMonth(DateUtil.offsetMonth(period.getFundData(),-1)), "yyyy/MM/dd"));
                final Calendar beginOfMonthc2 = Calendar.getInstance();
                beginOfMonthc2.setTime(beginOfMonth2);
                beginOfMonthc2.set(Calendar.HOUR_OF_DAY, 0);
                beginOfMonthc2.set(Calendar.MINUTE, 0);
                beginOfMonthc2.set(Calendar.SECOND, 0);
                beginOfMonthc2.set(Calendar.MILLISECOND, 0);
                final Calendar endOfMonthc2 = Calendar.getInstance();
                endOfMonthc2.setTime(endOfMonth2);
                endOfMonthc2.set(Calendar.HOUR_OF_DAY, 23);
                endOfMonthc2.set(Calendar.MINUTE, 59);
                endOfMonthc2.set(Calendar.SECOND, 59);
                endOfMonthc2.set(Calendar.MILLISECOND, 999);
                period.setStartFundData(DateUtil.parse(DateUtil.format(beginOfMonthc2.getTime(),"yyyy-MM-dd HH:mm:ss")));
                period.setEndFundData(DateUtil.parse(DateUtil.format(endOfMonthc2.getTime(),"yyyy-MM-dd HH:mm:ss")));
                break;
        }
        return period;
    }
}
