This commit is contained in:
zpc 2025-09-18 03:01:40 +08:00
parent 6f75dfbabd
commit 09018795bf
4 changed files with 99 additions and 0 deletions

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<NuGetAuditMode>all</NuGetAuditMode>
</PropertyGroup>
<ItemGroup>

View File

@ -10,6 +10,7 @@
using System;
using Hangfire;
namespace CoreCms.Net.Task
@ -65,6 +66,9 @@ namespace CoreCms.Net.Task
//定时刷新获取微信AccessToken
RecurringJob.AddOrUpdate<RefreshWeChatAccessTokenJob>(s => s.Execute(), "0 0/4 * * * ? ", TimeZoneInfo.Local); // 每2分钟刷新获取微信AccessToken
// 预约状态处理(每分钟执行一次)
RecurringJob.AddOrUpdate<SQReservationJob>(s => s.Execute(), "0 0/1 * * * ? ", TimeZoneInfo.Local);
}
#endregion

View File

@ -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))
{

View File

@ -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
{
/// <summary>
/// 预约定时清理
/// </summary>
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);
}
}
}
}
}