package com.sqx.modules.common.utils;

import cn.hutool.core.date.*;
import com.google.gson.Gson;
import com.sqx.modules.common.enums.CommonEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @ClassName CommonConfigUtil
 * @Description TODO
 * @Author bai
 * @Date 2024/6/6 22:32
 * @Version 1.0
 **/
public class CommonConfigUtil {

	/**
	 *	判定空
	 * @param value 判断的值
	 * @return 是否是空
	 */
	public static boolean defValueIsEmpty(String value) {
		return !StringUtils.isNotBlank(value);
	}


	/**
	 *	转为Integer类型
	 * @param value 转换的值
	 * @return 转换后的值
	 */
	public static Integer getIntValue(String value) {
		if(defValueIsEmpty(value)) return null;
		return Integer.valueOf(value);
	}

	/**
	 *	转为Long类型
	 * @param value 转换的值
	 * @return 转换后的值
	 */
	public static Long getLongValue(String value) {
		if(defValueIsEmpty(value)) return null;
		return Long.valueOf(value);
	}

	/**
	 *	转为Double类型
	 * @param value 转换的值
	 * @return 转换后的值
	 */
	public static Double getDoubleValue(String value) {
		if(defValueIsEmpty(value)) return null;
		return Double.valueOf(value);
	}

	/**
	 *	转为boolean类型
	 * @param value 转换的值
	 * @return 转换后的值
	 */
	public static boolean getBooleanValue(String value) {
		if(defValueIsEmpty(value)) return false;
		return Boolean.parseBoolean(value);
	}



	/**
	 * 获取数组，默认按,分隔
	 * @param value 转换的值
	 * @return 数组
	 */
	public static String[] getArrayValue(String value) {
		return getArrayValue(value, ",");
	}

	/**
	 * 获取数组，按指定分隔符分隔
	 * @param value 转换的值
	 * @param splitStr 自定义分隔符
	 * @return 数组
	 */
	public static String[] getArrayValue(String value, String splitStr) {
		if(defValueIsEmpty(value)) return new String[0];
		return StringUtils.split(value, splitStr);
	}


	public static CommonEnum getCommonEnumByKey(Integer key) {
		if(key == null) return CommonEnum.NONE;
		CommonEnum enumData = null;
		for (CommonEnum enumd : CommonEnum.values()) {
			if(enumd != CommonEnum.NONE){
				if(enumd.getKey().equals(key)){
					enumData = enumd;
				}
			}
		}
		if(enumData != null){
			return enumData;
		} else {
			return CommonEnum.NONE;
		}
	}

	public static String getCommonEnumByKeyToJson(Integer key) {
		return objectToJson(getCommonEnumByKey(key));
	}

	public static String objectToJson(Object o) {
		return new Gson().toJson(o);
	}
	public static String commonEnumToJson() {
		return objectToJson(CommonEnum.values());
	}

	//将时间或日期按格式转成正常的时间对象
	public static DateTime changeMiniDateTimeToDate(String miniTime, String format) {
		return new DateTime(miniTime, format);
	}

	//校准日期(日期)
	public static DateTime changeMiniDateCheck(Date businessTime, String miniDate) {
		int year = DateUtil.year(businessTime);
		return changeMiniDateTimeToDate(year + miniDate, "yyyyMMdd");
	}

	//校准日期（时间）
	public static DateTime changeMiniTimeCheck(Date businessTime, String miniTime) {
		String now = DateUtil.format(businessTime, DatePattern.NORM_DATE_PATTERN);
		return changeMiniDateTimeToDate(now + miniTime, DatePattern.NORM_DATE_PATTERN + "HHmmss");
	}

	public static boolean calcIsDateSlotCentre(Date businessTime, String startDateStr, String endDateStr) {
		return calcIsDateSlotCentre(businessTime, startDateStr, endDateStr, true, true);
	}

	/**
	 * 计算时间是否在时间段中间
	 * @param businessTime 需要计算的时间
	 * @param isCriticalValueStart
	 * @param isCriticalValueEnd
	 * @return
	 */
	public static boolean calcIsDateSlotCentre(Date businessTime, String startDateStr, String endDateStr, boolean isCriticalValueStart, boolean isCriticalValueEnd) {
		DateTime startDate = changeMiniDateCheck(businessTime, startDateStr);
		DateTime endDate = changeMiniDateCheck(businessTime, endDateStr);

		boolean isStartTrue = false;
		boolean isEndTrue = false;

		long s_b = DateUtil.between(startDate, businessTime, DateUnit.DAY, false);
		long b_e = DateUtil.between(businessTime, endDate, DateUnit.DAY, false);

		if(isCriticalValueStart){
			//是临界
			if(s_b >= 0 ) isStartTrue = true;
		} else {
			if(s_b > 0 ) isStartTrue = true;
		}

		if(isCriticalValueEnd){
			//是临界
			if(b_e >= 0 ) isEndTrue = true;
		} else {
			if(b_e > 0 ) isEndTrue = true;
		}
		return isStartTrue && isEndTrue;
	}

	public static boolean calcIsTimeSlotCentre(Date businessTime, String startTimeStr, String endTimeStr) {
		return calcIsTimeSlotCentre(businessTime, startTimeStr, endTimeStr, true, true);
	}

	public static boolean calcIsTimeSlotCentre(Date businessTime, String startTimeStr, String endTimeStr, boolean isCriticalValueStart, boolean isCriticalValueEnd) {
		DateTime startTime = changeMiniTimeCheck(businessTime, startTimeStr);
		DateTime endTime = changeMiniTimeCheck(businessTime, endTimeStr);


		boolean isStartTrue = false;
		boolean isEndTrue = false;

		long s_b = DateUtil.between(startTime, businessTime, DateUnit.SECOND, false);
		long b_e = DateUtil.between(businessTime, endTime, DateUnit.SECOND, false);

		if(isCriticalValueStart){
			//是临界
			if(s_b >= 0 ) isStartTrue = true;
		} else {
			if(s_b > 0 ) isStartTrue = true;
		}

		if(isCriticalValueEnd){
			//是临界
			if(b_e >= 0 ) isEndTrue = true;
		} else {
			if(b_e > 0 ) isEndTrue = true;
		}
		return isStartTrue && isEndTrue;
	}

}
