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.
BookingHeChuan/Myshipping.Application/Job/MSKAPIBookingWorker.cs

152 lines
6.5 KiB
C#

using Furion;
using Furion.DataEncryption;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Furion.TaskScheduler;
using Microsoft.Extensions.Logging;
using Myshipping.Application;
using Myshipping.Application.Entity;
using Myshipping.Application.Job;
using Myshipping.Core.Const;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Myshipping.Core.Job
{
/// <summary>
/// 马士基API定时自动订舱
/// </summary>
public class MSKAPIBookingWorker : ISpareTimeWorker
{
/// <summary>
/// 发送马士基API自动订舱
/// </summary>
/// <param name="timer"></param>
/// <param name="count"></param>
[SpareTime(60000, "MSKAPISendBooking", Description = "发送马士基API自动订舱", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void MSKAPISendBooking(SpareTimer timer, long count)
{
DateTime nowDate = DateTime.Now;
Log.Information($"UserSyncWorker {nowDate}");
/*
1
2
3
*/
//提取符合条件的订舱记录
var _repBookingDelivery = App.GetService<SqlSugarRepository<BookingDeliveryRecord>>();
var _repRunJob = App.GetService<SqlSugarRepository<BookingDeliveryRecordJobRun>>();
var _log = App.GetService<ILogger<MSKAPIBookingWorker>>();
var bookingMSKAPIService = App.GetService<IBookingMSKAPIService>();
var _repDjyApiAuth = App.GetService<SqlSugarRepository<DjyApiAuth>>();
var _cache = App.GetService<ISysCacheService>();
var sendUrl = _cache.GetAllDictData().Result.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "msk_api_timer_send_url").Value;
//获取定时订舱的任务
var doList = _repBookingDelivery.AsQueryable().Filter(null, true).Where(a => a.IsDeleted == false && a.STATUS == "TEMP"
&& (a.REQUEST_ACKNOWLEDGEMENT_ID == null || a.REQUEST_ACKNOWLEDGEMENT_ID == "") && (a.BOOKING_REFERENCE == null || a.BOOKING_REFERENCE == "")
&& a.IS_JOB == true && a.JOB_TIME.HasValue && a.JOB_TIME.Value <= nowDate && a.PRODUCT_CODE == "MaerskContract").ToList();
if (doList.Count > 0)
{
_log.LogInformation($"获取定时任务条数:{doList.Count} ids={string.Join(",", doList.Select(b => b.Id).ToArray())}");
foreach (var item in doList)
{
try
{
var auth = _repDjyApiAuth.AsQueryable().Filter(null, true).First(a => a.ApiCode.Equals("BCTaskManage") && a.IsDisable == false && a.TenantId == item.TenantId);
//先写入运行表,再执行订舱
var runInfo = new BookingDeliveryRecordJobRun
{
RECORD_ID = item.Id,
JOB_TIME = item.JOB_TIME,
TIMER_PLAN_ID = item.TIMER_PLAN_ID,
TIMER_PLAN_NAME = item.TIMER_PLAN_NAME,
CreatedTime = DateTime.Now,
CreatedUserId = item.CreatedUserId,
CreatedUserName = item.CreatedUserName,
};
_repRunJob.Insert(runInfo);
_log.LogInformation($"id={item.Id} 写入定时运行表");
_log.LogInformation($"id={item.Id} 开始发送订舱");
//这里采用API调用
var sendRlt = sendUrl.SetHeaders(new Dictionary<string, string> {
{ "USER_KEY", auth.ApiKey},
{ "USER_SECRET", auth.ApiSecret}
}).SetQueries(new
{
id = item.Id
}).GetAsStringAsync().GetAwaiter().GetResult();
var record = _repBookingDelivery.AsQueryable().Filter(null, true).First(a => a.Id == item.Id);
var logInfo = _repRunJob.AsQueryable().Filter(null, true).First(a => a.Id == runInfo.Id);
if(logInfo != null)
{
logInfo.SEND_TIME = record.SEND_TIME;
_repRunJob.AsUpdateable(logInfo).UpdateColumns(x=>new { x.SEND_TIME}).ExecuteCommand();
}
_log.LogInformation($"id={item.Id} 结束发送订舱,结果={sendRlt}");
Thread.Sleep(200);
}
catch (Exception ex)
{
_log.LogInformation($"id={item.Id} 定时订舱发生异常,原因:{ex.Message}");
}
}
}
}
/// <summary>
/// 生成当天的发送结果统计给操作
/// </summary>
/// <param name="timer"></param>
/// <param name="count"></param>
[SpareTime(60000, "MSKAPIResultReportToOp", Description = "生成当天的发送结果统计给操作", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void MSKAPIResultReportToOp(SpareTimer timer, long count)
{
Log.Information($"UserSyncWorker {DateTime.Now}");
/*
1MSK API
2
*/
var _repBookingDelivery = App.GetService<SqlSugarRepository<BookingDeliveryRecord>>();
var _repRunJob = App.GetService<SqlSugarRepository<BookingDeliveryRecordJobRun>>();
var _log = App.GetService<ILogger<MSKAPIBookingWorker>>();
var bookingMSKAPIService = App.GetService<IBookingMSKAPIService>();
_log.LogInformation($"开始生成并邮件发送报告");
var rlt = bookingMSKAPIService.GenerateTimerReportEmail().GetAwaiter().GetResult();
_log.LogInformation($"结束生成并邮件发送报告,结果={rlt}");
}
}
}