-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoalHierarchyEngine.cs
More file actions
171 lines (150 loc) · 6.41 KB
/
GoalHierarchyEngine.cs
File metadata and controls
171 lines (150 loc) · 6.41 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace TimeTask
{
public class GoalHierarchyItem
{
public string GoalId { get; set; }
public string GoalDescription { get; set; }
public string TimeHorizon { get; set; }
public List<string> YearlyThemes { get; set; } = new List<string>();
public List<string> QuarterlyMilestones { get; set; } = new List<string>();
public List<string> WeeklyCommitments { get; set; } = new List<string>();
}
public class GoalHierarchySnapshot
{
public DateTime GeneratedAt { get; set; } = DateTime.Now;
public List<GoalHierarchyItem> Goals { get; set; } = new List<GoalHierarchyItem>();
}
public class GoalHierarchyEngine
{
private readonly string _filePath;
private readonly object _sync = new object();
public GoalHierarchyEngine(string dataPath)
{
string strategyPath = Path.Combine(dataPath, "strategy");
Directory.CreateDirectory(strategyPath);
_filePath = Path.Combine(strategyPath, "goal_hierarchy.json");
}
public GoalHierarchySnapshot BuildAndPersist(List<LongTermGoal> activeGoals, List<ItemGrid> allTasks, DateTime now)
{
lock (_sync)
{
var snapshot = Build(activeGoals, allTasks, now);
try
{
var options = new JsonSerializerOptions { WriteIndented = true };
File.WriteAllText(_filePath, JsonSerializer.Serialize(snapshot, options));
}
catch (Exception ex)
{
Console.WriteLine($"GoalHierarchyEngine persist failed: {ex.Message}");
}
return snapshot;
}
}
private static GoalHierarchySnapshot Build(List<LongTermGoal> activeGoals, List<ItemGrid> allTasks, DateTime now)
{
var goals = activeGoals ?? new List<LongTermGoal>();
var tasks = allTasks ?? new List<ItemGrid>();
var result = new GoalHierarchySnapshot { GeneratedAt = now };
foreach (var goal in goals.Where(g => g != null && g.IsActive))
{
var related = tasks
.Where(t => t != null && t.IsActive && string.Equals(t.LongTermGoalId, goal.Id, StringComparison.OrdinalIgnoreCase))
.OrderBy(t => t.OriginalScheduledDay > 0 ? t.OriginalScheduledDay : int.MaxValue)
.ThenBy(t => t.CreatedDate)
.ToList();
int durationDays = ParseDurationDays(goal.TotalDuration);
string horizon = durationDays <= 120 ? "1年内目标" : durationDays <= 365 ? "1-3年目标" : "3年以上目标";
var yearlyThemes = BuildYearlyThemes(goal, related);
var milestones = BuildQuarterlyMilestones(related);
var weekly = related.Take(5).Select(t => t.Task).Where(t => !string.IsNullOrWhiteSpace(t)).Distinct().ToList();
result.Goals.Add(new GoalHierarchyItem
{
GoalId = goal.Id,
GoalDescription = goal.Description,
TimeHorizon = horizon,
YearlyThemes = yearlyThemes,
QuarterlyMilestones = milestones,
WeeklyCommitments = weekly
});
}
return result;
}
private static List<string> BuildYearlyThemes(LongTermGoal goal, List<ItemGrid> related)
{
var themes = new List<string>();
if (goal != null && goal.IsLearningPlan)
{
themes.Add("能力建设与系统学习");
}
int highImportant = related.Count(t => string.Equals(t.Importance, "High", StringComparison.OrdinalIgnoreCase));
if (highImportant >= Math.Max(1, related.Count / 2))
{
themes.Add("高价值任务优先推进");
}
var keywordThemes = related
.SelectMany(t => (t.Task ?? string.Empty).Split(new[] { ' ', ',', ',', '。', ';', ';', '-', '_', '/', '\\' }, StringSplitOptions.RemoveEmptyEntries))
.Where(w => w.Length >= 2)
.GroupBy(w => w, StringComparer.OrdinalIgnoreCase)
.OrderByDescending(g => g.Count())
.Take(2)
.Select(g => $"围绕「{g.Key}」持续深化")
.ToList();
themes.AddRange(keywordThemes);
if (!themes.Any())
{
themes.Add("聚焦核心目标,持续推进关键里程碑");
}
return themes.Distinct().Take(4).ToList();
}
private static List<string> BuildQuarterlyMilestones(List<ItemGrid> related)
{
var result = new List<string>();
if (related == null || !related.Any())
{
return result;
}
var grouped = related
.Select(t =>
{
int day = t.OriginalScheduledDay > 0 ? t.OriginalScheduledDay : 90;
int q = Math.Min(4, Math.Max(1, ((day - 1) / 30) + 1));
return new { Quarter = q, Task = t.Task };
})
.GroupBy(x => x.Quarter)
.OrderBy(g => g.Key);
foreach (var g in grouped)
{
string picks = string.Join("、", g.Select(x => x.Task).Where(t => !string.IsNullOrWhiteSpace(t)).Take(2));
if (string.IsNullOrWhiteSpace(picks))
{
continue;
}
result.Add($"Q{g.Key}: 完成 {picks}");
}
return result.Take(8).ToList();
}
private static int ParseDurationDays(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return 90;
}
var match = Regex.Match(text, @"\d+");
if (!match.Success || !int.TryParse(match.Value, out int value))
{
return 90;
}
if (text.Contains("年")) return value * 365;
if (text.Contains("月")) return value * 30;
if (text.Contains("周")) return value * 7;
return value;
}
}
}