Skip to content

Commit aaddd54

Browse files
committed
添加从服务器获取节假日列表功能
1 parent b78ac57 commit aaddd54

File tree

3 files changed

+115
-15
lines changed

3 files changed

+115
-15
lines changed

src/WeatherCalendar/Models/Holiday.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace WeatherCalendar.Models;
45

@@ -18,14 +19,67 @@ public class Holiday
1819
public string Name { get; set; }
1920

2021
/// <summary>
21-
/// 休息日期
22+
/// 休息日期
2223
/// </summary>
2324
public DateTime[] RestDates { get; set; }
2425

2526
/// <summary>
26-
/// 工作日期
27+
/// 工作日期
2728
/// </summary>
2829
public DateTime[] WorkDates { get; set; }
2930

30-
public override string ToString() => Name;
31+
public override string ToString()
32+
{
33+
return $"{Year} - {Name}";
34+
}
35+
36+
public static bool Equals(Holiday one, Holiday two)
37+
{
38+
if (one == null || two == null)
39+
return false;
40+
41+
if (one.Year != two.Year)
42+
return false;
43+
44+
if (one.Name != two.Name)
45+
return false;
46+
47+
if (one.RestDates?.Length != two.RestDates?.Length)
48+
return false;
49+
50+
if (one.WorkDates?.Length != two.WorkDates?.Length)
51+
return false;
52+
53+
return true;
54+
}
55+
56+
public static Holiday Combine(Holiday one, Holiday two)
57+
{
58+
var holiday = new Holiday
59+
{
60+
Year = one.Year,
61+
Name = one.Name
62+
};
63+
64+
var rest = new List<DateTime>();
65+
if (one.RestDates != null)
66+
rest.AddRange(one.RestDates);
67+
if (two.RestDates != null)
68+
foreach (var date in two.RestDates)
69+
if (!rest.Contains(date))
70+
rest.Add(date);
71+
72+
var work = new List<DateTime>();
73+
if (one.WorkDates != null)
74+
work.AddRange(one.WorkDates);
75+
if (two.WorkDates != null)
76+
foreach (var date in two.WorkDates)
77+
if (!work.Contains(date))
78+
work.Add(date);
79+
80+
holiday.RestDates = rest.ToArray();
81+
holiday.WorkDates = work.ToArray();
82+
83+
return holiday;
84+
}
3185
}

src/WeatherCalendar/Services/HolidayFileService.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using RestSharp;
5+
using RestSharp.Serializers.NewtonsoftJson;
36
using WeatherCalendar.Models;
47
using WeatherCalendar.Utils;
58

@@ -13,17 +16,60 @@ public sealed class HolidayFileService : IHolidayService
1316

1417
public Holiday[] Load(string file)
1518
{
19+
List<Holiday> fileHolidays;
1620
try
1721
{
18-
Holidays = JsonHelper.LoadFromFileToList<Holiday>(file, "yyyy-MM-dd").ToArray();
22+
fileHolidays = JsonHelper.LoadFromFileToList<Holiday>(file, "yyyy-MM-dd");
1923
}
2024
catch
2125
{
22-
Holidays = Array.Empty<Holiday>();
26+
fileHolidays = [];
27+
}
28+
29+
List<Holiday> apiHolidays;
30+
try
31+
{
32+
var client = new RestClient("http://yjammak.site",
33+
configureSerialization: cfg => cfg.UseNewtonsoftJson());
34+
var request = new RestRequest("api/weatherCalender/holidays");
35+
apiHolidays = client.Get<List<Holiday>>(request);
36+
}
37+
catch
38+
{
39+
apiHolidays = [];
2340
}
2441

2542
File = file;
2643

44+
var fileMaxYear = fileHolidays.Count > 0 ? fileHolidays.Max(f => f.Year) : -1;
45+
var apiMaxYear = apiHolidays.Count > 0 ? apiHolidays.Max(a => a.Year) : -1;
46+
if (apiMaxYear > fileMaxYear)
47+
{
48+
foreach (var fileHoliday in fileHolidays)
49+
{
50+
var holiday = apiHolidays.FirstOrDefault(h => h.Year == fileHoliday.Year && h.Name == fileHoliday.Name);
51+
if (holiday == null)
52+
{
53+
apiHolidays.Add(fileHoliday);
54+
}
55+
else
56+
{
57+
if (!Holiday.Equals(holiday, fileHoliday))
58+
{
59+
apiHolidays.Remove(holiday);
60+
apiHolidays.Add(Holiday.Combine(holiday, fileHoliday));
61+
}
62+
}
63+
}
64+
65+
Holidays = apiHolidays.ToArray();
66+
Save();
67+
}
68+
else
69+
{
70+
Holidays = fileHolidays.ToArray();
71+
}
72+
2773
return Holidays;
2874
}
2975

@@ -68,18 +114,18 @@ public void Add(int year, string name, DateTime date, bool isRestDay)
68114

69115
if (isRestDay)
70116
{
71-
holiday.WorkDates = Array.Empty<DateTime>();
72-
holiday.RestDates = new[] { date.Date };
117+
holiday.WorkDates = [];
118+
holiday.RestDates = [date.Date];
73119
}
74120
else
75121
{
76-
holiday.WorkDates = new[] { date.Date };
77-
holiday.RestDates = Array.Empty<DateTime>();
122+
holiday.WorkDates = [date.Date];
123+
holiday.RestDates = [];
78124
}
79125

80126
Holidays =
81127
Holidays == null
82-
? new[] { holiday }
128+
? [holiday]
83129
: Holidays
84130
.Append(holiday)
85131
.OrderBy(h => h.Year)

src/WeatherCalendar/WeatherCalendar.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<TargetFramework>net9.0-windows</TargetFramework>
66
<UseWPF>true</UseWPF>
77
<ApplicationIcon>Icon.ico</ApplicationIcon>
8-
<Copyright>Copyright © 2021-2024 YJammak All rights reserved.</Copyright>
9-
<AssemblyVersion>0.4.5.0</AssemblyVersion>
10-
<FileVersion>0.4.5.1</FileVersion>
11-
<Version>0.4.5.1</Version>
8+
<Copyright>Copyright © 2021-2025 YJammak All rights reserved.</Copyright>
9+
<AssemblyVersion>0.4.5.2</AssemblyVersion>
10+
<FileVersion>0.4.5.2</FileVersion>
11+
<Version>0.4.5.2</Version>
1212
<Authors>YJammak</Authors>
1313
<Product>天气日历</Product>
1414
<PackageProjectUrl>https://github.com/YJammak/Weather</PackageProjectUrl>
@@ -32,7 +32,7 @@
3232
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3333
</PackageReference>
3434
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.2.0" />
35-
<PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
35+
<PackageReference Include="MaterialDesignThemes" Version="5.2.0" />
3636
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3737
<PackageReference Include="NLog" Version="5.3.4" />
3838
<PackageReference Include="NPinyin.Core" Version="3.0.0" />

0 commit comments

Comments
 (0)