package com.mortals.xhx.common.utils;

import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @author liaoxin
 * @Date: 2021-08-2021/8/2-10:01
 * @Description:
 */
@Slf4j
public class DateByAffairUtils {
    public static final String P_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 指定日期获取工作随机时间
     *
     * @param begin
     * @param end
     * @return
     */
    public static Date getDayWorkingTimeByRandom(Date date, Integer begin, Integer end) {
        if (begin == null) {
            begin = 9;
        }
        if (end == null) {
            end = 17;
        }
        if (date == null) {
            date = new Date();
        }
        Calendar calStart = Calendar.getInstance();
        calStart.clear();
        calStart.setTime(date);
        calStart.set(Calendar.HOUR_OF_DAY, begin);
        calStart.set(Calendar.SECOND, 0);
        calStart.set(Calendar.MINUTE, 0);
        calStart.set(Calendar.MILLISECOND, 0);

        Calendar calEnd = Calendar.getInstance();
        calEnd.clear();
        calEnd.setTime(date);
        calEnd.set(Calendar.HOUR_OF_DAY, end);
        calEnd.set(Calendar.SECOND, 0);
        calEnd.set(Calendar.MINUTE, 0);
        calEnd.set(Calendar.MILLISECOND, 0);
        long dateTime = randomTime(calStart.getTimeInMillis(), calEnd.getTimeInMillis());
        DateFormat df = new SimpleDateFormat(P_yyyy_MM_dd_HH_mm_ss);//定义格式

        return StrToDateTime(df.format(dateTime), P_yyyy_MM_dd_HH_mm_ss);

    }


    private static long randomTime(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn < begin || rtn > end) {
            return randomTime(begin, end);
        }
        return rtn;
    }

    /**
     * 字符串转换为日期（包含小时分)
     *
     * @param str     日期字符串
     * @param pattern 时间模式 为null时默认为“yyy-MM-dd HH:mm:ss”
     * @return Date 返回 pattern形式的日期
     */
    public final static Date StrToDateTime(String str, String pattern) {
        Date returnDate = null;
        if (pattern == null) {
            pattern = P_yyyy_MM_dd_HH_mm_ss;
        }
        if (str == null || str.trim().length() == 0) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            returnDate = sdf.parse(str);
        } catch (Exception e) {
            log.debug("时间格式化异常-->" + str + "-->" + pattern + "-->" + e.getMessage());
            return returnDate;
        }
        return returnDate;
    }

    /**
     * 在指定时间的基础随机增加时间
     *
     * @param date
     * @param maxhousr 随机最大时间
     * @return
     */
    public static Date getTimeByAddRandom(Date date, Integer maxhousr) {
        if (null == maxhousr) {
            maxhousr = 4;
        }
        long time = date.getTime();
        time += Math.random() * maxhousr * 60 * 60 * 1000;// 要加上的小时转换成毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    /**
     * 在指定时间的基础随机增加时间(可自定义分钟)
     *
     * @param date
     * @param maxMinuteStart 随机最大时间
     * @param maxMinuteEnd 随机最大时间
     * @return 3~20分钟内的随机数据
     */
    public static Date getTimeByAddRandomDouble(Date date, Integer maxMinuteStart, Integer maxMinuteEnd) {
        if (null == maxMinuteStart) {
            maxMinuteStart = 3;
        }
        if ( null == maxMinuteEnd){
            maxMinuteEnd  = 20;
        }
        int i = new Random().nextInt(maxMinuteEnd-maxMinuteStart) + maxMinuteStart;
        long time = date.getTime();
        time +=  i * 60 * 60 * 10 ;// 要加上的小时转换成毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }

    /**
     * 在当天基础上随机前几天至今天
     * @param day
     * @return
     */
    public static String randomDate(int day) {
        Calendar cal = Calendar.getInstance();
        try {
            int num = (int) (Math.random() * day - 1);
            cal.add(Calendar.DATE, -num);
            return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 在当天基础上随机前后几天
     * @param day
     * @return
     */
    public static String randomBetweenDate(int day) {
        Calendar cal = Calendar.getInstance();
        try {
            int num = (int) (Math.random() * day - 1);
            if(getRandomBoolean()){
                cal.add(Calendar.DATE, -num);
            }else{
                cal.add(Calendar.DATE, num);
            }

            return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }
    private static boolean getRandomBoolean() {
        Random random = new Random();
        return random.nextBoolean();
    }

    /**
     * 指定日期获取工作随机时间
     *
     * @param begin
     * @param end
     * @return
     */
    public static Date getDayRadomWorkingTime(Date date, Integer begin, Integer end) {
        if (begin == null) {
            begin = 9;
        }
        if (end == null) {
            end = 17;
        }
        if (date == null) {
            date = new Date();
        }
        Calendar calStart = Calendar.getInstance();
        calStart.clear();
        calStart.setTime(date);
        calStart.set(Calendar.HOUR_OF_DAY, begin);
        calStart.set(Calendar.SECOND, 0);
        calStart.set(Calendar.MINUTE, 0);
        calStart.set(Calendar.MILLISECOND, 0);

        Calendar calEnd = Calendar.getInstance();
        calEnd.clear();
        calEnd.setTime(date);
        calEnd.set(Calendar.HOUR_OF_DAY, end);
        calEnd.set(Calendar.SECOND, 0);
        calEnd.set(Calendar.MINUTE, 0);
        calEnd.set(Calendar.MILLISECOND, 0);
        long dateTime = randomTime(calStart.getTimeInMillis(), calEnd.getTimeInMillis());
        DateFormat df = new SimpleDateFormat(P_yyyy_MM_dd_HH_mm_ss);//定义格式
//
//
//        String dateString = df.format(dateTime);//进行转换

        return StrToDateTime(df.format(dateTime), P_yyyy_MM_dd_HH_mm_ss);

    }

    /**
     * 获取当日工作随机时间（早上9点，下午5点）
     *
     * @param begin 开始 9
     * @param end   结束 17
     * @return
     */
    public static Date getTodayRadomWorkingTime(Integer begin, Integer end) {
        if (begin == null) {
            begin = 9;
        }
        if (end == null) {
            end = 17;
        }
        Calendar calStart = Calendar.getInstance();
        calStart.set(Calendar.HOUR_OF_DAY, begin);
        calStart.set(Calendar.SECOND, 0);
        calStart.set(Calendar.MINUTE, 0);
        calStart.set(Calendar.MILLISECOND, 0);

        Calendar calEnd = Calendar.getInstance();
        calEnd.set(Calendar.HOUR_OF_DAY, end);
        calEnd.set(Calendar.SECOND, 0);
        calEnd.set(Calendar.MINUTE, 0);
        calEnd.set(Calendar.MILLISECOND, 0);
        long dateTime = randomTime(calStart.getTimeInMillis(), calEnd.getTimeInMillis());
        return new Date(dateTime);

    }

    /**
     * 获取今天的最后时间
     *
     * @return
     */
    public static Date getTodayLastTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
    /**
     * 获取上月第一天的开始时间（yyyy-MM-dd HH:mm:ss）
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getLastMonthFirstDate(Date date, String pattern) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return getDateTime(cal.getTime(), pattern);
    }

    /**
     * 获取上月最后一天的结束时间
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getLastMonthLastDate(Date date, String pattern) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return getDateTime(cal.getTime(), pattern);
    }

    /**
     * 将日期时间转换成 pattern 格式的时间
     *
     * @param date    要转换的日期时间
     * @param pattern 时间模式 为null时默认为“yyy-MM-dd HH:mm:ss”
     * @return String 转换后的日期时间
     */
    public final static String getDateTime(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null || pattern.trim().length() == 0)
            pattern = P_yyyy_MM_dd_HH_mm_ss;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 获取本月第一天
     *
     * @param date
     * @return
     */
    public static Date getMonthFirstDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }
}
