All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 4m27s
Docker Build & Deploy / Deploy to Production (push) Successful in 7s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
131 lines
3.7 KiB
C#
131 lines
3.7 KiB
C#
using System.Text.Json.Serialization;
|
|
|
|
namespace Service;
|
|
|
|
/// <summary>
|
|
/// 节假日服务接口
|
|
/// </summary>
|
|
public interface IHolidayService
|
|
{
|
|
/// <summary>
|
|
/// 从API获取并缓存节假日数据
|
|
/// </summary>
|
|
Task<bool> FetchAndCacheHolidaysAsync(int year);
|
|
|
|
/// <summary>
|
|
/// 获取指定年份的节假日数据
|
|
/// </summary>
|
|
Task<List<Holiday>> GetHolidaysByYearAsync(int year);
|
|
|
|
/// <summary>
|
|
/// 获取指定日期范围的节假日数据
|
|
/// </summary>
|
|
Task<List<Holiday>> GetHolidaysByDateRangeAsync(string startDate, string endDate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 节假日服务实现
|
|
/// </summary>
|
|
public class HolidayService(
|
|
IHolidayRepository holidayRepository,
|
|
IHttpClientFactory httpClientFactory,
|
|
ILogger<HolidayService> logger) : IHolidayService
|
|
{
|
|
private const string HolidayApiUrl = "https://publicapi.xiaoai.me/holiday/year";
|
|
|
|
public async Task<bool> FetchAndCacheHolidaysAsync(int year)
|
|
{
|
|
try
|
|
{
|
|
logger.LogInformation("开始获取 {Year} 年节假日数据", year);
|
|
|
|
// 调用API获取数据
|
|
var httpClient = httpClientFactory.CreateClient();
|
|
var response = await httpClient.GetStringAsync($"{HolidayApiUrl}?date={year}");
|
|
|
|
// 解析JSON
|
|
var apiResponse = JsonSerializer.Deserialize<HolidayApiResponse>(response);
|
|
if (apiResponse?.Code != 0 || apiResponse.Data == null)
|
|
{
|
|
logger.LogError("获取节假日数据失败: {Message}", apiResponse?.Msg);
|
|
return false;
|
|
}
|
|
|
|
// 删除旧数据
|
|
await holidayRepository.DeleteByYearAsync(year);
|
|
|
|
// 转换并保存新数据
|
|
var holidays = apiResponse.Data.Select(item => new Holiday
|
|
{
|
|
Year = year,
|
|
DayType = item.DayType,
|
|
HolidayName = item.Holiday,
|
|
Rest = item.Rest,
|
|
Date = item.Date,
|
|
Week = item.Week,
|
|
WeekDescCn = item.WeekDescCn
|
|
}).ToList();
|
|
|
|
await holidayRepository.AddRangeAsync(holidays);
|
|
|
|
logger.LogInformation("成功缓存 {Count} 条节假日数据", holidays.Count);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 静默失败,仅记录日志
|
|
logger.LogError(ex, "获取并缓存节假日数据失败");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<List<Holiday>> GetHolidaysByYearAsync(int year)
|
|
{
|
|
return await holidayRepository.GetByYearAsync(year);
|
|
}
|
|
|
|
public async Task<List<Holiday>> GetHolidaysByDateRangeAsync(string startDate, string endDate)
|
|
{
|
|
return await holidayRepository.GetByDateRangeAsync(startDate, endDate);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 节假日API响应模型
|
|
/// </summary>
|
|
public class HolidayApiResponse
|
|
{
|
|
[JsonPropertyName("code")]
|
|
public int Code { get; set; }
|
|
|
|
[JsonPropertyName("msg")]
|
|
public string Msg { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("data")]
|
|
public List<HolidayApiData>? Data { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 节假日API数据模型
|
|
/// </summary>
|
|
public class HolidayApiData
|
|
{
|
|
[JsonPropertyName("daytype")]
|
|
public int DayType { get; set; }
|
|
|
|
[JsonPropertyName("holiday")]
|
|
public string Holiday { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("rest")]
|
|
public int Rest { get; set; }
|
|
|
|
[JsonPropertyName("date")]
|
|
public string Date { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("week")]
|
|
public int Week { get; set; }
|
|
|
|
[JsonPropertyName("week_desc_cn")]
|
|
public string WeekDescCn { get; set; } = string.Empty;
|
|
}
|