package org.security.common.util;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Created by yzy
 * @date 2022/3/21 11:50
 */
public class DeadlineUtil {
	public static String getDDLByFreq(String freq, Date endDate) {
		//当前日期
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DATE);
		int weekday = c.get(Calendar.DAY_OF_WEEK);
		//		按天，取当日日期
		if ("byDay".equals(freq)) {
			SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
			String date = ft.format(new Date());
			return date;
		}
		//		按周，获取当周最后一天
		else if ("byWeek".equals(freq)) {
			Calendar c1 = Calendar.getInstance();
			int count = 7 - weekday;
			c1.add(Calendar.DATE, count);
			String date = DateFormatUtils.format(c1.getTimeInMillis(), "yyyy-MM-dd");
			return date;
		}
		//		按月，获取当月最后一天
		else if ("byMonth".equals(freq)) {
			Calendar c2 = Calendar.getInstance();
			c2.set(Calendar.YEAR, year);
			c2.set(Calendar.MONTH, month + 1);
			int maxDay = c2.getActualMaximum(Calendar.DAY_OF_MONTH);
			c2.set(Calendar.DAY_OF_MONTH, maxDay);
			String date = DateFormatUtils.format(c2.getTimeInMillis(), "yyyy-MM-dd");
			return date;
		}
		//		按季度，获取该季度最后一天
		else if ("byQuarter".equals(freq)) {
			Calendar c3 = getQuarterEnd(c);
			String date = DateFormatUtils.format(c3.getTimeInMillis(), "yyyy-MM-dd");
			return date;
		}
		//		按半年，获取所处上/下半年的最后一天
		else if ("byHalfYear".equals(freq)) {
			Calendar c4 = getHalfYearEnd(c);
			String date = DateFormatUtils.format(c4.getTimeInMillis(), "yyyy-MM-dd");
			return date;
		}
		//		按年,取当前年度最后一天
		else if ("byYear".equals(freq)) {
			Calendar c5 = Calendar.getInstance();
			c5.set(Calendar.YEAR, year);
			c5.set(Calendar.MONTH, 12);
			c5.set(Calendar.DAY_OF_MONTH, 31);
			String date = DateFormatUtils.format(c5.getTimeInMillis(), "yyyy-MM-dd");
			return date;
		}
		//		单次提报，直接返回endDate
		return Func.isNotEmpty(endDate) ? DateUtil.formatDate(endDate) : null;
	}

	//传入日期，判断属于第几季度，并返回该季度最后一天
	public static Calendar getQuarterEnd(Calendar c) {
		Calendar c1 = Calendar.getInstance();
		c1.set(c.get(Calendar.YEAR), Calendar.JANUARY, 1);
		Calendar c3 = Calendar.getInstance();
		c3.set(c.get(Calendar.YEAR), Calendar.MARCH, 31);
		Calendar c4 = Calendar.getInstance();
		c4.set(c.get(Calendar.YEAR), Calendar.APRIL, 1);
		Calendar c6 = Calendar.getInstance();
		c6.set(c.get(Calendar.YEAR), Calendar.JUNE, 30);
		Calendar c7 = Calendar.getInstance();
		c7.set(c.get(Calendar.YEAR), Calendar.JULY, 1);
		Calendar c9 = Calendar.getInstance();
		c9.set(c.get(Calendar.YEAR), Calendar.SEPTEMBER, 30);
		Calendar c10 = Calendar.getInstance();
		c10.set(c.get(Calendar.YEAR), Calendar.OCTOBER, 1);
		Calendar c12 = Calendar.getInstance();
		c12.set(c.get(Calendar.YEAR), Calendar.DECEMBER, 31);
		if (c.compareTo(c1) >= 0 && c.compareTo(c3) <= 0) {//一季度
			return c3;
		} else if (c.compareTo(c4) >= 0 && c.compareTo(c6) <= 0) {//二季度
			return c6;
		} else if (c.compareTo(c7) >= 0 && c.compareTo(c9) <= 0) {//三季度
			return c9;
		} else {//四季度
			return c12;
		}
	}

	//传入日期，判断属于上半年或下半年，并返回该半年度最后一天
	public static Calendar getHalfYearEnd(Calendar c) {
		Calendar c1 = Calendar.getInstance();
		c1.set(c.get(Calendar.YEAR), Calendar.JANUARY, 1);
		Calendar c6 = Calendar.getInstance();
		c6.set(c.get(Calendar.YEAR), Calendar.JUNE, 30);
		Calendar c7 = Calendar.getInstance();
		c7.set(c.get(Calendar.YEAR), Calendar.JULY, 1);
		Calendar c12 = Calendar.getInstance();
		c12.set(c.get(Calendar.YEAR), Calendar.DECEMBER, 31);
		if (c.compareTo(c1) >= 0 && c.compareTo(c6) <= 0) {//上半年
			return c6;
		} else {//下半年
			return c12;
		}
	}
}
