| | |
| | | import com.xiaopeng.common.core.utils.HttpUtils; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.Duration; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | @Value("${holiday.api.url}") |
| | | private String holidayApiUrl; |
| | | |
| | | public boolean isHoliday() { |
| | | LocalDate now = LocalDate.now(); |
| | | |
| | | public boolean isHoliday(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); |
| | | String result = HttpUtil.get(holidayApiUrl + now); |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | | Object data = jsonObject.get("data"); |
| | | JSONArray parse = JSONArray.parse(data.toString()); |
| | |
| | | if (cacheObject!=null) { |
| | | return Integer.valueOf(cacheObject.toString()); |
| | | } |
| | | String result = HttpUtils.sendGet("https://publicapi.xiaoai.me/holiday/day?date=" +now); |
| | | String result = HttpUtils.sendGet(holidayApiUrl + now); |
| | | |
| | | // String result = HttpUtils.sendGet("https://publicapi.xiaoai.me/holiday/day?date=2026-01-04"); |
| | | JSONObject jsonObject = JSONObject.parseObject(result); |
| | |
| | | for (int i = 0; i < parse.size(); i++) { |
| | | JSONObject jsonObject1 = parse.getJSONObject(i); |
| | | String daytype = jsonObject1.get("daytype").toString(); |
| | | // 计算今天结束还差多少秒 |
| | | long secondsToExactEndOfDay = getSecondsToExactEndOfDay(); |
| | | if (daytype.equals("0")) { |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "0", 1l, TimeUnit.DAYS); |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "0", secondsToExactEndOfDay, TimeUnit.SECONDS); |
| | | return 0; |
| | | } else if (daytype.equals("1")) { |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "1", 1l, TimeUnit.DAYS); |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "1", secondsToExactEndOfDay, TimeUnit.SECONDS); |
| | | return 1; |
| | | } else if (daytype.equals("2")) { |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "0", 1l, TimeUnit.DAYS); |
| | | redisService.setCacheObject(CacheConstants.IS_DAY + now, "2", secondsToExactEndOfDay, TimeUnit.SECONDS); |
| | | return 0; |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | public static long getSecondsToExactEndOfDay() { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | |
| | | // 精确到23:59:59(不包含毫秒/纳秒) |
| | | LocalDateTime endOfDay = LocalDateTime.of( |
| | | LocalDate.now(), |
| | | LocalTime.of(23, 59, 59) |
| | | ); |
| | | |
| | | return Duration.between(now, endOfDay).getSeconds(); |
| | | } |
| | | } |