33using ClassIsland . Core . Attributes ;
44using Microsoft . Extensions . Logging ;
55using System ;
6+ using System . Collections . Concurrent ;
67using System . Threading . Tasks ;
78using SystemTools . Settings ;
89
@@ -18,6 +19,8 @@ public class LoadTemporaryClassPlanAction(
1819 private readonly IProfileService _profileService = profileService ;
1920 private readonly IExactTimeService _exactTimeService = exactTimeService ;
2021
22+ private static readonly ConcurrentDictionary < Guid , TempClassPlanSnapshot > PreviousSnapshots = new ( ) ;
23+
2124 protected override async Task OnInvoke ( )
2225 {
2326 if ( ! Guid . TryParse ( Settings . ClassPlanId , out var classPlanId ) )
@@ -32,11 +35,38 @@ protected override async Task OnInvoke()
3235 return ;
3336 }
3437
38+ if ( IsRevertable )
39+ {
40+ PreviousSnapshots [ ActionSet . Guid ] = new TempClassPlanSnapshot (
41+ _profileService . Profile . TempClassPlanId ,
42+ _profileService . Profile . TempClassPlanSetupTime ) ;
43+ }
44+
3545 _profileService . Profile . TempClassPlanId = classPlanId ;
3646 _profileService . Profile . TempClassPlanSetupTime = _exactTimeService . GetCurrentLocalDateTime ( ) ;
3747 _profileService . SaveProfile ( ) ;
3848 _logger . LogInformation ( "已加载临时课表:{ClassPlanName} ({ClassPlanId})" , classPlan . Name , classPlanId ) ;
3949
4050 await base . OnInvoke ( ) ;
4151 }
52+
53+ protected override async Task OnRevert ( )
54+ {
55+ await base . OnRevert ( ) ;
56+
57+ if ( PreviousSnapshots . TryRemove ( ActionSet . Guid , out var snapshot ) )
58+ {
59+ _profileService . Profile . TempClassPlanId = snapshot . TempClassPlanId ;
60+ _profileService . Profile . TempClassPlanSetupTime = snapshot . TempClassPlanSetupTime ;
61+ _profileService . SaveProfile ( ) ;
62+ _logger . LogInformation ( "已恢复临时课表为触发前状态。ActionSet={ActionSetGuid}" , ActionSet . Guid ) ;
63+ return ;
64+ }
65+
66+ _profileService . Profile . TempClassPlanId = null ;
67+ _profileService . SaveProfile ( ) ;
68+ _logger . LogInformation ( "未找到触发前状态,已清除临时课表。ActionSet={ActionSetGuid}" , ActionSet . Guid ) ;
69+ }
70+
71+ private readonly record struct TempClassPlanSnapshot ( Guid ? TempClassPlanId , DateTime TempClassPlanSetupTime ) ;
4272}
0 commit comments