package com.ecep.contract.service; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import com.ecep.contract.WebSocketClientService; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; /** * 节假日服务类 * * @author System */ @Lazy @Service @RequiredArgsConstructor @Slf4j public class HolidayService { @Autowired private WebSocketClientService webSocketClientService; @Autowired protected ObjectMapper objectMapper; /** * 调整日期到工作日 * * @param date 要调整的日期 * @return 调整的日期 */ public LocalDate adjustToWorkDay(LocalDate date) { if (date == null) { return null; } try { JsonNode json = webSocketClientService.invoke("holidayService", "adjustToWorkDay", date).get(); if (json != null && !json.isEmpty()) { return objectMapper.convertValue(json, LocalDate.class); } } catch (Exception e) { log.error("调整日期到工作日失败: {}", e.getMessage(), e); } // 如果调用失败,使用客户端本地逻辑作为后备 return adjustToWorkDayLocally(date); } /** * 客户端本地日期调整逻辑,作为服务器调用失败的后备方案 * * @param date 要调整的日期 * @return 调整的日期 */ private LocalDate adjustToWorkDayLocally(LocalDate date) { while (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) { date = date.plusDays(1); } return date; } }