package com.xhkjedu.gateway.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

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

/**
 * @ClassName ApiUtil
 * Description api工具类
 * Author WN
 * Date 2022/2/21 11:08
 **/
@Slf4j
public class ApiUtil {

    /*
    * @Description 比较当前时间和时间段
    * @Author WN
    * @Date 2023/6/15 11:24:59
    */
    public static boolean compareCurrDateTime(String starttimeStr,String stoptimeStr) throws Exception{
        boolean rtn = false;
        if(isEmpty(starttimeStr) || isEmpty(stoptimeStr)){
            rtn = false;
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

            Date starttime = sdf.parse(starttimeStr); //开始时间
            Date stoptime = sdf.parse(stoptimeStr); //结束时间

            Date nowtime = new Date();
            String times = sdf.format(nowtime);
            Date currtime = sdf.parse(times);//当前时间

            //当前时间在开始时间之前或者等于开始时间  并且 当前时间在结束时间之后或者等于结束时间
            if((starttime.before(currtime) || starttime.equals(currtime)) && (stoptime.after(currtime) || stoptime.equals(currtime))){
                rtn = true;
            }
        }

        return rtn;
    }
    /**
     * 字符串是否为空
     *
     * @param: @param obj
     * @param: @return
     * @author WN
     */
    public static boolean isNotEmpty(Object obj) {
        if (null == obj || "".equals(obj.toString().trim()) || "null".equals(obj.toString().trim())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 字符串是否为空
     *
     * @param: @param obj
     * @param: @return
     * @author zj
     */
    public static boolean isEmpty(Object obj) {
        if (null == obj || "".equals(obj.toString().trim()) || "null".equals(obj.toString().trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @Description 是否是白名单接口
     * @Date 2023/11/16 9:02
     * @Author YWX
     * @Param [requestURI, whitelistAPI]
     * @Return boolean
     **/
    public static boolean whiteAPI(String requestURI, String whitelistAPI) {
        boolean rtn = false;
        if (isEmpty(whitelistAPI)) return rtn;

        String[] apis = whitelistAPI.split(",");
        for (String api : apis) {
            if (requestURI.contains(api)) {
                rtn = true;
                break;
            }
        }
        return rtn;
    }

    /**
     * @Description 判断list是否不为空
     * @Date 2023/11/16 9:01
     * @Author YWX
     * @Param [list]
     * @Return boolean
     **/
    public static boolean isListNotEmpty(List list) {
        if (CollectionUtils.isEmpty(list)){
            return false;
        } else {
            return true;
        }
    }

    /**
     * @Description list转字符
     * @Date 2023/11/16 9:01
     * @Author YWX
     * @Param [list]
     * @Return java.lang.String
     **/
    public static String listToStr(List<String> list) {
        String str = "";
        if (isListNotEmpty(list)) {
            for (String s : list) {
                str += s;
            }
        }
        return str;
    }
}
