90 lines
3.4 KiB
C#
90 lines
3.4 KiB
C#
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);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|