Repository navigation
Expand file tree
/
Copy pathDataService.cs
More file actions
310 lines (285 loc) · 11.9 KB
/
Copy pathDataService.cs
File metadata and controls
310 lines (285 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace GamePrince
{
/// <summary>
/// 数据服务异常
/// </summary>
public class DataServiceException : Exception
{
public string FilePath { get; set; } = "";
public DataServiceException(string message) : base(message) { }
public DataServiceException(string message, Exception innerException) : base(message, innerException) { }
public DataServiceException(string message, string filePath, Exception innerException) : base(message, innerException)
{
FilePath = filePath;
}
}
public class TaskItem
{
public string Title { get; set; } = "";
public string Description { get; set; } = "";
public string Category { get; set; } = "";
public string Status { get; set; } = "Task Pool"; // Task Pool, In Progress, Completed
public string DateCreated { get; set; } = System.DateTime.Now.ToString("yyyy-MM-dd");
public string? DateCompleted { get; set; }
public int Urgency { get; set; } = 3; // 1-5
public int Importance { get; set; } = 3; // 1-5
public List<string> Tags { get; set; } = new();
public string MilestoneId { get; set; } = "";
public string? DueDate { get; set; }
public double EstimatedHours { get; set; } = 0;
public double LoggedHours { get; set; } = 0;
public string? LastTimerStart { get; set; }
}
public class Milestone
{
public string Id { get; set; } = System.Guid.NewGuid().ToString();
public string Title { get; set; } = "";
public string Description { get; set; } = "";
public string Version { get; set; } = "";
public string StartDate { get; set; } = System.DateTime.Now.ToString("yyyy-MM-dd");
public string TargetDate { get; set; } = "";
public bool IsCompleted { get; set; } = false;
}
public class ReleaseInfo
{
public string Id { get; set; } = System.Guid.NewGuid().ToString();
public string Version { get; set; } = ""; // 语义化版本号
public string Title { get; set; } = "";
public string Description { get; set; } = "";
public string ReleaseDate { get; set; } = "";
public string Status { get; set; } = "Planning"; // Planning, InProgress, Released
public List<string> Checklist { get; set; } = new();
public List<bool> ChecklistCompleted { get; set; } = new();
}
public static class DataService
{
private static readonly string FilePath = "tasks.json";
private static readonly string MilestonesPath = "milestones.json";
private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
/// <summary>
/// 加载任务列表(带错误处理)
/// </summary>
public static List<TaskItem> LoadTasks()
{
try
{
if (!File.Exists(FilePath))
{
LoggerService.Info("DataService", "任务文件不存在,创建默认数据");
return CreateDefaultTasks();
}
string json = File.ReadAllText(FilePath);
if (string.IsNullOrWhiteSpace(json))
{
LoggerService.Warning("DataService", "任务文件为空,创建默认数据");
return CreateDefaultTasks();
}
var tasks = JsonSerializer.Deserialize<List<TaskItem>>(json, JsonOptions);
if (tasks == null)
{
LoggerService.Warning("DataService", "任务数据解析失败,创建默认数据");
return CreateDefaultTasks();
}
LoggerService.Info("DataService", $"成功加载 {tasks.Count} 个任务");
return tasks;
}
catch (JsonException ex)
{
LoggerService.Error("DataService", $"JSON解析错误: {ex.Message}", ex);
return CreateDefaultTasks();
}
catch (UnauthorizedAccessException ex)
{
LoggerService.Error("DataService", $"文件访问权限错误: {FilePath}", ex);
return CreateDefaultTasks();
}
catch (IOException ex)
{
LoggerService.Error("DataService", $"文件IO错误: {ex.Message}", ex);
return CreateDefaultTasks();
}
catch (Exception ex)
{
LoggerService.Fatal("DataService", $"加载任务时发生未知错误: {ex.Message}", ex);
return CreateDefaultTasks();
}
}
/// <summary>
/// 保存任务列表(带错误处理)
/// </summary>
public static void SaveTasks(List<TaskItem> tasks)
{
if (tasks == null)
{
LoggerService.Warning("DataService", "尝试保存空任务列表");
return;
}
try
{
string json = JsonSerializer.Serialize(tasks, JsonOptions);
File.WriteAllText(FilePath, json);
LoggerService.Debug("DataService", $"成功保存 {tasks.Count} 个任务");
}
catch (UnauthorizedAccessException ex)
{
LoggerService.Error("DataService", $"文件写入权限错误: {FilePath}", ex);
throw new DataServiceException("无法写入任务文件,请检查文件权限", FilePath, ex);
}
catch (IOException ex)
{
LoggerService.Error("DataService", $"文件写入IO错误: {ex.Message}", ex);
throw new DataServiceException("写入任务文件时发生IO错误", FilePath, ex);
}
catch (Exception ex)
{
LoggerService.Fatal("DataService", $"保存任务时发生未知错误: {ex.Message}", ex);
throw new DataServiceException("保存任务时发生未知错误", FilePath, ex);
}
}
/// <summary>
/// 加载里程碑列表(带错误处理)
/// </summary>
public static List<Milestone> LoadMilestones()
{
try
{
if (!File.Exists(MilestonesPath))
{
LoggerService.Info("DataService", "里程碑文件不存在,创建默认数据");
return CreateDefaultMilestones();
}
string json = File.ReadAllText(MilestonesPath);
if (string.IsNullOrWhiteSpace(json))
{
LoggerService.Warning("DataService", "里程碑文件为空,创建默认数据");
return CreateDefaultMilestones();
}
var milestones = JsonSerializer.Deserialize<List<Milestone>>(json, JsonOptions);
if (milestones == null)
{
LoggerService.Warning("DataService", "里程碑数据解析失败,创建默认数据");
return CreateDefaultMilestones();
}
LoggerService.Info("DataService", $"成功加载 {milestones.Count} 个里程碑");
return milestones;
}
catch (JsonException ex)
{
LoggerService.Error("DataService", $"里程碑JSON解析错误: {ex.Message}", ex);
return CreateDefaultMilestones();
}
catch (Exception ex)
{
LoggerService.Fatal("DataService", $"加载里程碑时发生未知错误: {ex.Message}", ex);
return CreateDefaultMilestones();
}
}
/// <summary>
/// 保存里程碑列表(带错误处理)
/// </summary>
public static void SaveMilestones(List<Milestone> milestones)
{
if (milestones == null)
{
LoggerService.Warning("DataService", "尝试保存空里程碑列表");
return;
}
try
{
string json = JsonSerializer.Serialize(milestones, JsonOptions);
File.WriteAllText(MilestonesPath, json);
LoggerService.Debug("DataService", $"成功保存 {milestones.Count} 个里程碑");
}
catch (Exception ex)
{
LoggerService.Error("DataService", $"保存里程碑失败: {ex.Message}", ex);
throw new DataServiceException("保存里程碑失败", MilestonesPath, ex);
}
}
private static readonly string ReleasesPath = "releases.json";
/// <summary>
/// 加载发布信息列表(带错误处理)
/// </summary>
public static List<ReleaseInfo> LoadReleases()
{
try
{
if (!File.Exists(ReleasesPath))
{
LoggerService.Info("DataService", "发布信息文件不存在");
return new List<ReleaseInfo>();
}
string json = File.ReadAllText(ReleasesPath);
if (string.IsNullOrWhiteSpace(json))
{
LoggerService.Warning("DataService", "发布信息文件为空");
return new List<ReleaseInfo>();
}
var releases = JsonSerializer.Deserialize<List<ReleaseInfo>>(json, JsonOptions);
LoggerService.Info("DataService", $"成功加载 {releases?.Count ?? 0} 个发布信息");
return releases ?? new List<ReleaseInfo>();
}
catch (JsonException ex)
{
LoggerService.Error("DataService", $"发布信息JSON解析错误: {ex.Message}", ex);
return new List<ReleaseInfo>();
}
catch (Exception ex)
{
LoggerService.Fatal("DataService", $"加载发布信息时发生未知错误: {ex.Message}", ex);
return new List<ReleaseInfo>();
}
}
/// <summary>
/// 保存发布信息列表(带错误处理)
/// </summary>
public static void SaveReleases(List<ReleaseInfo> releases)
{
if (releases == null)
{
LoggerService.Warning("DataService", "尝试保存空发布信息列表");
return;
}
try
{
string json = JsonSerializer.Serialize(releases, JsonOptions);
File.WriteAllText(ReleasesPath, json);
LoggerService.Debug("DataService", $"成功保存 {releases.Count} 个发布信息");
}
catch (Exception ex)
{
LoggerService.Error("DataService", $"保存发布信息失败: {ex.Message}", ex);
throw new DataServiceException("保存发布信息失败", ReleasesPath, ex);
}
}
/// <summary>
/// 创建默认任务数据
/// </summary>
private static List<TaskItem> CreateDefaultTasks()
{
return new List<TaskItem>
{
new TaskItem { Title = "设计主角 3D 模型", Category = "美术", Status = "Task Pool" },
new TaskItem { Title = "实现核心战斗逻辑", Category = "程序", Status = "In Progress" }
};
}
/// <summary>
/// 创建默认里程碑数据
/// </summary>
private static List<Milestone> CreateDefaultMilestones()
{
return new List<Milestone>
{
new Milestone { Title = "v1.0 核心原型", Version = "1.0.0", Description = "完成基础移动与战斗逻辑" }
};
}
}
}