package com.sundot.airport.exam.enums;

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

/**
 * <b>功能名：</b> DateRangeEnum <br>
 * <b>说明：</b> 时间范围枚举 <br>
 * <b>著作权：</b> Copyright (C) 2021 HUIFANEDU  CORPORATION<br>
 * <b>修改履历：</b><br>
 *
 * @author 2021-06-21 Mokou
 */
public enum DateRangeEnum {
    /**
     * 7天
     */
    WEEK("7天"),
    /**
     * 本月
     */
    MONTH("本月");

    /**
     * 枚举中文名
     */
    private String cnName;

    DateRangeEnum(String cnName) {
        this.cnName = cnName;
    }

    /**
     * <b>方法名: </b> getStartEnd <br>
     * <b>说明: </b> 通过当前枚举 获取日期范围Date数组 Date为当天00:00:00.000 <br>
     *
     * @param range DateRangeEnum 范围枚举
     * @return Date[] {开始时间Date, 结束时间Date}
     * <b>修改履历: </b>
     * @author 2021/6/24 Mokou
     */
    public static Date[] getStartEnd(DateRangeEnum range) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        switch (range) {
            case WEEK: {
                int past = 6;
                Date end = calendar.getTime();
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
                Date start = calendar.getTime();
                return new Date[]{start, end};
            }
            case MONTH: {
                calendar.set(Calendar.DATE, 1);
                Date start = calendar.getTime();
                calendar.roll(Calendar.DATE, -1);
                Date end = calendar.getTime();
                return new Date[]{start, end};
            }
            default: {
                Date now = new Date();
                return new Date[]{now, now};
            }
        }
    }

    public String getCnName() {
        return cnName;
    }
}
