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
{
///
/// 马士基API定时自动订舱
///
public class MSKAPIBookingWorker : ISpareTimeWorker
{
///
/// 发送马士基API自动订舱
///
///
///
[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>();
var _repRunJob = App.GetService>();
var _log = App.GetService>();
var bookingMSKAPIService = App.GetService();
var _repDjyApiAuth = App.GetService>();
var _cache = App.GetService();
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 {
{ "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}");
}
}
}
}
///
/// 生成当天的发送结果统计给操作
///
///
///
[SpareTime(60000, "MSKAPIResultReportToOp", Description = "生成当天的发送结果统计给操作", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void MSKAPIResultReportToOp(SpareTimer timer, long count)
{
Log.Information($"UserSyncWorker {DateTime.Now}");
/*
1、提取当天的MSK API订舱记录生成列表。
2、生成邮件正文发送给制单人邮箱
*/
var _repBookingDelivery = App.GetService>();
var _repRunJob = App.GetService>();
var _log = App.GetService>();
var bookingMSKAPIService = App.GetService();
_log.LogInformation($"开始生成并邮件发送报告");
var rlt = bookingMSKAPIService.GenerateTimerReportEmail().GetAwaiter().GetResult();
_log.LogInformation($"结束生成并邮件发送报告,结果={rlt}");
}
}
}