Coreshop/CoreCms.Net.Task/SQReservationJob.cs
2025-09-20 15:11:31 +08:00

176 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using DotLiquid.Tags;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types;
namespace CoreCms.Net.Task
{
/// <summary>
/// 预约定时清理
/// </summary>
public class SQReservationJob
{
private readonly ISQReservationsServices _reservationsServices;
private readonly ISQReservationParticipantsServices _participantsServices;
private readonly IRedisOperationRepository _redisOperationRepository;
public SQReservationJob(ISQReservationsServices reservationsServices,
ISQReservationParticipantsServices participantsServices,
IRedisOperationRepository redisOperationRepository)
{
_reservationsServices = reservationsServices;
_participantsServices = participantsServices;
_redisOperationRepository = redisOperationRepository;
}
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);
}
// 2.5) 到达开始时间但人数未满的预约置为取消4
Console.WriteLine("到达开始时间但人数未满的预约置为取消4");
var toCancelList = await _reservationsServices.QueryListByClauseAsync(
r => r.status == 0 && r.start_time <= now && r.end_time > now,
r => r.id, OrderByType.Asc);
if (toCancelList != null && toCancelList.Count > 0)
{
foreach (var item in toCancelList)
{
var count = await _participantsServices.GetCountAsync(p => p.reservation_id == item.id && p.status == 0);
if (count < item.player_count)
{
item.status = 4;
item.updated_at = now;
}
}
var toCancelChanged = toCancelList.Where(x => x.status == 4).ToList();
if (toCancelChanged.Count > 0)
{
await _reservationsServices.UpdateAsync(toCancelChanged);
foreach (var item in toCancelChanged)
{
var userList = await _participantsServices.QueryListByClauseAsync(p => p.reservation_id == item.id && p.status == 0);
foreach (var user in userList)
{
JObject parameters = new JObject();
parameters.Add("title", item.title);
parameters.Add("context", $"组局失败!");
parameters.Add("createTime", item.start_time);
parameters.Add("tips", $"组局人数未满,自动解散!");
var @params = new JObject();
@params.Add("parameters", parameters);
var data = new
{
user.user_id,
code = "reservation_change",
parameters = @params
};
await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.SendWxTemplateMessage, JsonConvert.SerializeObject(data));
}
}
}
}
// 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);
foreach (var item in changed)
{
var userList = await _participantsServices.QueryListByClauseAsync(p => p.reservation_id == item.id && p.status == 0);
foreach (var user in userList)
{
JObject parameters = new JObject();
parameters.Add("title", item.title);
parameters.Add("roomName", item.room_name);
parameters.Add("createTime", item.start_time);
parameters.Add("tips", $"组局成功!");
var @params = new JObject();
@params.Add("parameters", parameters);
var data = new
{
user.user_id,
code = "reservation_success",
parameters = @params
};
await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.SendWxTemplateMessage, JsonConvert.SerializeObject(data));
}
}
}
}
}
}
}