From 09018795bfaf30bb3c0a99deb2c56abf5d3ddf9a Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 18 Sep 2025 03:01:40 +0800 Subject: [PATCH] 321 --- CoreCms.Net.Task/CoreCms.Net.Task.csproj | 1 + CoreCms.Net.Task/HangfireDispose.cs | 4 + .../RefreshWeChatAccessTokenJob.cs | 5 ++ CoreCms.Net.Task/SQReservationJob.cs | 89 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 CoreCms.Net.Task/SQReservationJob.cs diff --git a/CoreCms.Net.Task/CoreCms.Net.Task.csproj b/CoreCms.Net.Task/CoreCms.Net.Task.csproj index 9716875..b3342ee 100644 --- a/CoreCms.Net.Task/CoreCms.Net.Task.csproj +++ b/CoreCms.Net.Task/CoreCms.Net.Task.csproj @@ -2,6 +2,7 @@ net8.0 + all diff --git a/CoreCms.Net.Task/HangfireDispose.cs b/CoreCms.Net.Task/HangfireDispose.cs index 3b63879..a523a27 100644 --- a/CoreCms.Net.Task/HangfireDispose.cs +++ b/CoreCms.Net.Task/HangfireDispose.cs @@ -10,6 +10,7 @@ using System; + using Hangfire; namespace CoreCms.Net.Task @@ -65,6 +66,9 @@ namespace CoreCms.Net.Task //定时刷新获取微信AccessToken RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/4 * * * ? ", TimeZoneInfo.Local); // 每2分钟刷新获取微信AccessToken + // 预约状态处理(每分钟执行一次) + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/1 * * * ? ", TimeZoneInfo.Local); + } #endregion diff --git a/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs b/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs index 70f4848..4bda2db 100644 --- a/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs +++ b/CoreCms.Net.Task/RefreshWeChatAccessTokenJob.cs @@ -10,14 +10,18 @@ using System; + using CoreCms.Net.Caching.AutoMate.RedisCache; using CoreCms.Net.Configuration; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.WeChat.Service.HttpClients; using CoreCms.Net.WeChat.Service.Options; + using Microsoft.Extensions.Options; + using Newtonsoft.Json; + using SKIT.FlurlHttpClient; using SKIT.FlurlHttpClient.Wechat; using SKIT.FlurlHttpClient.Wechat.Api; @@ -53,6 +57,7 @@ namespace CoreCms.Net.Task { try { + Console.WriteLine("刷新微信token"); //微信公众号刷新 if (!string.IsNullOrEmpty(_weChatOptions.WeiXinAppId) && !string.IsNullOrEmpty(_weChatOptions.WeiXinAppSecret)) { diff --git a/CoreCms.Net.Task/SQReservationJob.cs b/CoreCms.Net.Task/SQReservationJob.cs new file mode 100644 index 0000000..092cdfd --- /dev/null +++ b/CoreCms.Net.Task/SQReservationJob.cs @@ -0,0 +1,89 @@ +using CoreCms.Net.IServices; + +using SqlSugar; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CoreCms.Net.Task +{ + /// + /// 预约定时清理 + /// + public class SQReservationJob + { + private readonly ISQReservationsServices _reservationsServices; + private readonly ISQReservationParticipantsServices _participantsServices; + public SQReservationJob(ISQReservationsServices reservationsServices, + ISQReservationParticipantsServices participantsServices) + { + _reservationsServices = reservationsServices; + _participantsServices = participantsServices; + } + + public async System.Threading.Tasks.Task Execute() + { + + Console.WriteLine("SQReservationJob"); + var now = DateTime.Now; + Console.WriteLine("将已到结束时间的预约置为已结束(3)"); + // 1) 将已到结束时间的预约置为已结束(3) + var endedList = await _reservationsServices.QueryListByClauseAsync( + r => r.status == 2 && r.end_time <= now, + r => r.id, OrderByType.Asc); + if (endedList != null && endedList.Count > 0) + { + foreach (var item in endedList) + { + item.status = 3; + item.updated_at = now; + } + await _reservationsServices.UpdateAsync(endedList); + } + Console.WriteLine("将开始时间已到且未结束的预约置为进行中"); + // 2) 将开始时间已到且未结束的预约置为进行中(2) + var startedList = await _reservationsServices.QueryListByClauseAsync( + r => r.status == 1 && r.start_time <= now && r.end_time > now, + r => r.id, OrderByType.Asc); + if (startedList != null && startedList.Count > 0) + { + foreach (var item in startedList) + { + item.status = 2; + item.updated_at = now; + } + await _reservationsServices.UpdateAsync(startedList); + } + + // 3) 开始前30分钟内,人数已凑齐则置为锁定(1) + Console.WriteLine("开始前30分钟内,人数已凑齐则置为锁定(1)"); + var threshold = now.AddMinutes(30); + var toLockList = await _reservationsServices.QueryListByClauseAsync( + r => r.status == 0 && r.start_time > now && r.start_time <= threshold, + r => r.id, OrderByType.Asc); + if (toLockList != null && toLockList.Count > 0) + { + foreach (var item in toLockList) + { + // 当前有效参与人数(未退出) + var count = await _participantsServices.GetCountAsync(p => p.reservation_id == item.id && p.status == 0); + if (count >= item.player_count) + { + item.status = 1; + item.updated_at = now; + } + } + // 仅更新被修改的项 + var changed = toLockList.Where(x => x.status == 1).ToList(); + if (changed.Count > 0) + { + await _reservationsServices.UpdateAsync(changed); + } + } + + } + } +}