package com.xiaopeng.order.utils.holiday; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.xiaopeng.common.core.constant.CacheConstants; import com.xiaopeng.common.core.utils.HttpUtils; import com.xiaopeng.common.redis.service.RedisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.util.concurrent.TimeUnit; @Component public class HolidayUtil { @Autowired private RedisService redisService; public boolean isHoliday() { LocalDate now = LocalDate.now(); // 判断缓存里面是否有节假日算法 if (redisService.getCacheObject(CacheConstants.IS_HOLIDAY+now) != null && ("1").equals(redisService.getCacheObject(CacheConstants.IS_HOLIDAY+now).toString())) { return true; } String result = HttpUtil.get("https://publicapi.xiaoai.me/holiday/day?date=" +now); JSONObject jsonObject = JSONObject.parseObject(result); Object data = jsonObject.get("data"); JSONArray parse = JSONArray.parse(data.toString()); for (int i = 0; i < parse.size(); i++) { JSONObject jsonObject1 = parse.getJSONObject(i); if (jsonObject1.get("daytype").toString().equals("1")) { redisService.setCacheObject(CacheConstants.IS_HOLIDAY+now, "1", 1l, TimeUnit.DAYS); return true; } } redisService.setCacheObject(CacheConstants.IS_HOLIDAY+now, "0", 1l, TimeUnit.DAYS); return false; } /** * 2非工作日 0工作日 1节假日 3调休日 * @return */ public Integer isHolidayToday() { LocalDate now = LocalDate.now(); Object cacheObject = redisService.getCacheObject(CacheConstants.IS_DAY + now); // 判断缓存里面是否有节假日算法 if (cacheObject!=null) { return Integer.valueOf(cacheObject.toString()); } String result = HttpUtils.sendGet("https://publicapi.xiaoai.me/holiday/day?date=" +now); // String result = HttpUtils.sendGet("https://publicapi.xiaoai.me/holiday/day?date=2026-01-04"); JSONObject jsonObject = JSONObject.parseObject(result); Object data = jsonObject.get("data"); JSONArray parse = JSONArray.parse(data.toString()); for (int i = 0; i < parse.size(); i++) { JSONObject jsonObject1 = parse.getJSONObject(i); String daytype = jsonObject1.get("daytype").toString(); if (daytype.equals("0")) { redisService.setCacheObject(CacheConstants.IS_DAY + now, "0", 1l, TimeUnit.DAYS); return 0; } else if (daytype.equals("1")) { redisService.setCacheObject(CacheConstants.IS_DAY + now, "1", 1l, TimeUnit.DAYS); return 1; } else if (daytype.equals("2")) { redisService.setCacheObject(CacheConstants.IS_DAY + now, "0", 1l, TimeUnit.DAYS); return 0; } } return 0; } }