You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
8.5 KiB
C#

using EntrustSettle.Common;
using EntrustSettle.IServices;
using EntrustSettle.Model;
using EntrustSettle.Model.Dtos;
using EntrustSettle.Model.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Quartz;
//using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
/// <summary>
/// 这里要注意下,命名空间和程序集是一样的,不然反射不到(任务类要去JobSetup添加注入)
/// </summary>
namespace EntrustSettle.Tasks
{
[DisallowConcurrentExecution]
public class JobHydStatusQuartz : JobBase, IJob
{
private readonly ILogger<JobHydStatusQuartz> _logger;
private readonly IHYDService hydService;
private readonly IOrderService orderService;
private readonly IOrderHistoryService orderHistoryService;
private readonly IQueueService queueService;
private readonly IOrderFeeService orderFeeService;
public JobHydStatusQuartz(ILogger<JobHydStatusQuartz> logger,
ITasksQzServices tasksQzServices,
ITasksLogServices tasksLogServices,
IHYDService hydService,
IOrderService orderService,
IOrderHistoryService orderHistoryService,
IQueueService queueService,
IOrderFeeService orderFeeService)
: base(tasksQzServices, tasksLogServices)
{
_tasksQzServices = tasksQzServices;
_logger = logger;
this.hydService = hydService;
this.orderService = orderService;
this.orderHistoryService = orderHistoryService;
this.queueService = queueService;
this.orderFeeService = orderFeeService;
}
public async Task Execute(IJobExecutionContext context)
{
// 可以直接获取 JobDetail 的值
var jobKey = context.JobDetail.Key;
var jobId = jobKey.Name;
var executeLog = await ExecuteJob(context, async () => await Run(context, jobId.ObjToInt()));
}
public async Task Run(IJobExecutionContext context, int jobid)
{
if (jobid > 0)
{
Console.WriteLine($"{nameof(JobHydStatusQuartz)} 执行 {DateTime.Now.ToShortTimeString()}");
// 查询所有已接收但是还未完成的订单,进行轮询查询订单状态
var pendingOrderList = await orderService.Query(x => x.IsSend == true
&& x.Status != (int)OrderStatusEnum.
&& x.CreateTime <= DateTime.Now.AddMonths(6));
if (pendingOrderList.Count == 0)
{
return;
}
foreach (Order orderItem in pendingOrderList)
{
try
{
List<HydQueryResultDto> result = await hydService.Query(orderItem.Mblno);
foreach (HydQueryResultDto item in result)
{
var orderList = await orderService.Query(x => x.Bsno == item.id);
if (orderList == null || orderList.Count == 0)
{
_logger.LogInformation($"提单号{orderItem.Mblno}接收到回推后未查询到本地订单");
continue;
}
if (orderList.Count > 1)
{
_logger.LogInformation($"提单号{orderItem.Mblno}接收到回推后根据bsno查询到多个本地订单");
continue;
}
var order = orderList[0];
if (item.status != null && order.Status != item.status)
8 months ago
{
order.Status = item.status;
var updateSuccess = await orderService.Update(order, x => new { x.Status });
8 months ago
_logger.LogInformation($"提单号{orderItem.Mblno}状态更新{(updateSuccess ? "" : "")}status:{item.status}");
8 months ago
// 记录订单状态变更历史
await orderHistoryService.Add(new OrderHistory()
{
Pid = order.Id,
Status = item.status ?? 0,
StatusTime = DateTime.Now,
CreateBy = "系统",
Remark = "(后台任务自动设置)"
8 months ago
});
// 将更新后的状态及费用推送到消息队列
if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool())
{
7 months ago
_ = Task.Run(() =>
{
string msg = $"Id[{order.Id}],提单号:[{order.Mblno}],自动更新状态后推送队列";
try
{
StatusPushDto pushDto = new()
{
8 months ago
OrderId = order.Id,
Mblno = order.Mblno,
MessageType = 1,
MessageDesc = "状态更新推送",
Remark = "",
Status = item.status ?? 0,
StatusDesc = item.status switch
{
0 => "已下单",
1 => "已接单",
2 => "待缴费",
3 => "已缴费",
4 => "已完结",
_ => "未知状态",
}
};
7 months ago
var feeList = orderFeeService.AsQueryable().Where(x => x.OrderId == order.Id).ToList();
8 months ago
if (feeList.Count > 0)
{
pushDto.FeeList = feeList.Select(x => new StatusPushDto.FeeDto()
{
FeeId = x.Id,
FeeName = x.Name,
FeeAmount = x.Amount
}).ToList();
}
7 months ago
var json = JsonConvert.SerializeObject(pushDto, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
8 months ago
queueService.Push(msg, order.CompanyId, json);
}
catch (Exception ex)
{
_logger.LogError(ex, $"{msg},失败");
}
});
}
8 months ago
}
else
{
8 months ago
_logger.LogInformation($"提单号{orderItem.Mblno}状态不变");
}
}
await Task.Delay(500);
}
catch (Exception ex)
{
_logger.LogError(ex, $"提单号{orderItem.Mblno}查询状态失败时发生未知异常,继续处理下一票");
continue;
}
}
}
}
}
}