This commit is contained in:
zpc 2025-09-26 20:39:27 +08:00
parent 57300de4e1
commit fcb057ee5f
5 changed files with 288 additions and 137 deletions

View File

@ -128,9 +128,9 @@ namespace CoreCms.Net.Model.Entities
public System.String important_data { get; set; } public System.String important_data { get; set; }
/// <summary> /// <summary>
/// 是否需要退款。0 无需退款未交押金1 待支付 2 已支付 9退款失败 /// 是否需要退款。0 无需退款未交押金1 待支付 2 已支付 3 发起退款4退款中5退款完成。9退款失败
/// </summary> /// </summary>
[Display(Name = "是否需要退款。0 无需退款未交押金1 待支付 2 已支付 9退款失败")] [Display(Name = "是否需要退款。0 无需退款未交押金1 待支付 2 已支付 3 发起退款4退款中5退款完成。 9退款失败")]
public System.Int32 is_refund { get; set; } public System.Int32 is_refund { get; set; }
/// <summary> /// <summary>

View File

@ -68,6 +68,8 @@ namespace CoreCms.Net.Task
// 预约状态处理(每分钟执行一次) // 预约状态处理(每分钟执行一次)
RecurringJob.AddOrUpdate<SQReservationJob>(s => s.Execute(), "0 0/1 * * * ? ", TimeZoneInfo.Local); RecurringJob.AddOrUpdate<SQReservationJob>(s => s.Execute(), "0 0/1 * * * ? ", TimeZoneInfo.Local);
// 预约退款处理
//RecurringJob.AddOrUpdate<SQReservationRefundJob>(s => s.Execute(), "0 0/1 * * * ? ", TimeZoneInfo.Local);
} }

View File

@ -1,6 +1,7 @@
using CoreCms.Net.Caching.AutoMate.RedisCache; using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration; using CoreCms.Net.Configuration;
using CoreCms.Net.IServices; using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using DotLiquid.Tags; using DotLiquid.Tags;
@ -17,8 +18,8 @@ using System.Threading.Tasks;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types; using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinUserInfoBatchGetRequest.Types;
namespace CoreCms.Net.Task namespace CoreCms.Net.Task;
{
/// <summary> /// <summary>
/// 预约定时清理 /// 预约定时清理
/// </summary> /// </summary>
@ -114,6 +115,16 @@ namespace CoreCms.Net.Task
Console.WriteLine($"[SQReservationJob] 步骤2.5预约ID={item.id} 需通知用户数量 = {userList.Count}"); Console.WriteLine($"[SQReservationJob] 步骤2.5预约ID={item.id} 需通知用户数量 = {userList.Count}");
await _reservationsServices.NotifyReservationFailedAsync(item, userList); await _reservationsServices.NotifyReservationFailedAsync(item, userList);
Console.WriteLine($"[SQReservationJob] 步骤2.5预约ID={item.id} 组局失败通知入队完成"); Console.WriteLine($"[SQReservationJob] 步骤2.5预约ID={item.id} 组局失败通知入队完成");
// 解散(取消)的预约,标记需要退款押金的参与者为“待退款(3)”
// 条件:未退出(status=0) 且 押金已支付(is_refund=2) 且 有有效支付单号
await _participantsServices.UpdateAsync(
it => new SQReservationParticipants
{
is_refund = 3
},
it => it.reservation_id == item.id && it.status == 0 && it.is_refund == 2 && it.paymentId != null && it.paymentId != "");
Console.WriteLine($"[SQReservationJob] 步骤2.5预约ID={item.id} 已标记需要退款的参与者为待退款(3)");
} }
} }
} }
@ -162,4 +173,3 @@ namespace CoreCms.Net.Task
Console.WriteLine($"[SQReservationJob] 执行结束 @ {DateTime.Now:yyyy-MM-dd HH:mm:ss}"); Console.WriteLine($"[SQReservationJob] 执行结束 @ {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
} }
} }
}

View File

@ -0,0 +1,113 @@
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreCms.Net.Task;
public class SQReservationRefundJob
{
private readonly ISQReservationsServices _reservationsServices;
private readonly ISQReservationParticipantsServices _participantsServices;
private readonly IRedisOperationRepository _redisOperationRepository;
private readonly ICoreCmsBillPaymentsServices _billPaymentsServices;
private readonly ICoreCmsBillRefundServices _billRefundServices;
public SQReservationRefundJob(ISQReservationsServices reservationsServices,
ISQReservationParticipantsServices participantsServices,
IRedisOperationRepository redisOperationRepository,
ICoreCmsBillPaymentsServices billPaymentsServices,
ICoreCmsBillRefundServices billRefundServices)
{
_reservationsServices = reservationsServices;
_participantsServices = participantsServices;
_redisOperationRepository = redisOperationRepository;
_billPaymentsServices = billPaymentsServices;
_billRefundServices = billRefundServices;
}
public async System.Threading.Tasks.Task Execute()
{
Console.WriteLine($"[SQReservationRefundJob] 执行开始 @ {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
// 扫描需要退款的参与者is_refund=3发起退款、未退出(status=0)、有有效支付单号
var needRefundList = await _participantsServices.QueryListByClauseAsync(
p => p.is_refund == 3 && p.paymentId != null && p.paymentId != "",
p => p.id, OrderByType.Asc);
if (needRefundList == null || needRefundList.Count == 0)
{
Console.WriteLine("[SQReservationRefundJob] 无需处理的记录");
return;
}
foreach (var participant in needRefundList)
{
try
{
// 查询预约获取押金
var reservation = await _reservationsServices.QueryByClauseAsync(r => r.id == participant.reservation_id);
var deposit = reservation?.deposit_fee ?? 0m;
if (deposit <= 0)
{
// 无需退款,置为已完成
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 4 }, it => it.id == participant.id);
continue;
}
// 获取支付单
var payInfoRes = await _billPaymentsServices.GetInfo(participant.paymentId, participant.user_id);
if (payInfoRes.status == false || payInfoRes.data is not CoreCmsBillPayments payInfo)
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 9 }, it => it.id == participant.id);
continue;
}
// 退款金额:押金与实付金额取最小
var refundMoney = deposit > payInfo.money ? payInfo.money : deposit;
if (refundMoney <= 0)
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 4 }, it => it.id == participant.id);
continue;
}
// 创建退款单
var addRefundRes = await _billRefundServices.ToAdd(participant.user_id, payInfo.sourceId, payInfo.type, refundMoney, "");
if (addRefundRes.status == false)
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 9 }, it => it.id == participant.id);
continue;
}
// 查询退款单并执行原路退款
var refundInfo = await _billRefundServices.QueryByClauseAsync(p =>
p.userId == participant.user_id && p.sourceId == payInfo.sourceId && p.type == payInfo.type && p.money == refundMoney && p.status == 1);
if (refundInfo == null)
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 9 }, it => it.id == participant.id);
continue;
}
var doRefundRes = await _billRefundServices.ToRefund(refundInfo.refundId, 2, "");
if (doRefundRes.status)
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 4 }, it => it.id == participant.id);
}
else
{
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 9 }, it => it.id == participant.id);
}
}
catch (Exception ex)
{
Console.WriteLine($"[SQReservationRefundJob] 处理失败 participantId={participant.id}, ex={ex.Message}");
await _participantsServices.UpdateAsync(it => new SQReservationParticipants { is_refund = 9 }, it => it.id == participant.id);
}
}
Console.WriteLine($"[SQReservationRefundJob] 执行结束 @ {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
}
}

View File

@ -1036,7 +1036,19 @@ public class SQController : ControllerBase
}; };
} }
// 7.2发起者取消整个预约,所有参与人都标记为退出
// 7.2 有押金时,已支付押金(is_refund=2)的参与者为发起退款(3)
if (reservation.deposit_fee.HasValue && reservation.deposit_fee.Value > 0)
{
await _SQReservationParticipantsServices.UpdateAsync(
it => new SQReservationParticipants
{
is_refund = 3
},
it => it.reservation_id == dto.reservation_id && it.status == 0 && it.is_refund == 2 && it.paymentId != null && it.paymentId != "");
}
// 7.3发起者取消整个预约,所有参与人都标记为退出
await _SQReservationParticipantsServices.UpdateAsync( await _SQReservationParticipantsServices.UpdateAsync(
it => new SQReservationParticipants it => new SQReservationParticipants
{ {
@ -1061,6 +1073,16 @@ public class SQController : ControllerBase
quit_time = DateTime.Now quit_time = DateTime.Now
}, },
it => it.reservation_id == dto.reservation_id && it.user_id == userId); it => it.reservation_id == dto.reservation_id && it.user_id == userId);
// 有押金且已支付押金时,标记该参与者为发起退款(3)
if (reservation.deposit_fee.HasValue && reservation.deposit_fee.Value > 0)
{
await _SQReservationParticipantsServices.UpdateAsync(
it => new SQReservationParticipants
{
is_refund = 3
},
it => it.reservation_id == dto.reservation_id && it.user_id == userId && it.is_refund == 2 && it.paymentId != null && it.paymentId != "");
}
} }
return new WebApiDto() return new WebApiDto()
@ -1208,9 +1230,13 @@ public class SQController : ControllerBase
if (reservation.deposit_fee > 0) if (reservation.deposit_fee > 0)
{ {
//有押金,押金原路退回 // 有押金,标记到场人员为“发起退款(3)”,由定时任务统一处理实际退款
await _SQReservationParticipantsServices.UpdateAsync(
it => new SQReservationParticipants
{
is_refund = 3
},
it => it.reservation_id == dto.reservation_id && it.status == 0 && it.is_arrive == 1 && it.is_refund == 2 && it.paymentId != null && it.paymentId != "");
} }
_dbBase.Ado.CommitTran(); _dbBase.Ado.CommitTran();