116 lines
5.6 KiB
C#
116 lines
5.6 KiB
C#
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, remarks = $"获取支付单失败" }, 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, remarks = $"创建退款单失败" }, 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, remarks = $"查询退款单并执行原路退款失败" }, 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, remarks = $"发起退款失败!{refundInfo.refundId},{doRefundRes.msg}" }, 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, remarks = $"出现异常,退款失败!{ex.Message}" }, it => it.id == participant.id);
|
||
}
|
||
}
|
||
Console.WriteLine($"[SQReservationRefundJob] 执行结束 @ {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
|
||
}
|
||
}
|