From dffaae6ffa09e7a43f6dc96de55e299bc17f3ddf Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Tue, 20 Aug 2024 10:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A9=AC=E5=A3=AB=E5=9F=BA?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E8=AE=A2=E8=88=B1=E6=8A=A5=E5=91=8A=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entity/BookingDeliveryRecordJobRun.cs | 54 +++++++++ .../Job/MSKAPIBookingWorker.cs | 109 ++++++++++++++++++ .../BookingOrder/BookingMSKAPIService.cs | 37 +++--- .../MSKAPITimerReportTemplate.html | 54 +++++++++ 4 files changed, 240 insertions(+), 14 deletions(-) create mode 100644 Myshipping.Application/Entity/BookingDeliveryRecordJobRun.cs create mode 100644 Myshipping.Application/Job/MSKAPIBookingWorker.cs create mode 100644 Myshipping.Web.Entry/wwwroot/EmailTemplate/MSKAPITimerReportTemplate.html diff --git a/Myshipping.Application/Entity/BookingDeliveryRecordJobRun.cs b/Myshipping.Application/Entity/BookingDeliveryRecordJobRun.cs new file mode 100644 index 00000000..8d76b1f3 --- /dev/null +++ b/Myshipping.Application/Entity/BookingDeliveryRecordJobRun.cs @@ -0,0 +1,54 @@ +using Myshipping.Core.Entity; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Myshipping.Application.Entity +{ + /// + /// 订舱递送记录表定时订舱运行表 + /// + [SugarTable("booking_delivery_record_job_run")] + [Description("订舱递送记录表定时订舱运行表")] + public class BookingDeliveryRecordJobRun : DBEntityTenant + { + /// + /// 主记录ID + /// + public long RECORD_ID { get; set; } + + /// + /// 是否定时任务 1-是 0-否 + /// + public Nullable JOB_TIME { get; set; } + + /// + /// 发送时间 + /// + public Nullable SEND_TIME { get; set; } + + /// + /// 定时方案ID + /// + public string TIMER_PLAN_ID { get; set; } + + /// + /// 定时方案名称 + /// + public string TIMER_PLAN_NAME { get; set; } + + /// + /// 是否已推送报告 1-已推送 0-未推送 + /// + public bool IS_SEND_REPORT { get; set; } + + /// + /// 推送报告时间 + /// + public Nullable REPORT_TIME { get; set; } + } +} diff --git a/Myshipping.Application/Job/MSKAPIBookingWorker.cs b/Myshipping.Application/Job/MSKAPIBookingWorker.cs new file mode 100644 index 00000000..f4c6b6d6 --- /dev/null +++ b/Myshipping.Application/Job/MSKAPIBookingWorker.cs @@ -0,0 +1,109 @@ +using Furion; +using Furion.DataEncryption; +using Furion.Logging; +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 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 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).ToList(); + + if (doList.Count > 0) + { + foreach (var item in doList) + { + try + { + //先写入运行表,再执行订舱 + 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); + + var sendRlt = bookingMSKAPIService.SendMSKBookingById(item.Id).GetAwaiter().GetResult(); + + Thread.Sleep(200); + } + catch (Exception ex) + { + + } + } + } + + } + + /// + /// 生成当天的发送结果统计给操作 + /// + /// + /// + [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>(); + + } + } +} diff --git a/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs b/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs index a9048dcc..cd500ec1 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingMSKAPIService.cs @@ -67,7 +67,7 @@ namespace Myshipping.Application.Service.BookingOrder IDjyWebsiteAccountConfigService webAccountConfig, SqlSugarRepository bookingDeliveryRecordRep, ISysDataUserMenu sysDataUserMenuService, SqlSugarRepository bookingDeliveryRecordCtnRep, SqlSugarRepository bookingDeliveryRecordShipScheduleRep, - SqlSugarRepository djyUserMailAccount, SqlSugarRepository bookingDeliveryRecordJobRunScheduleRep) + SqlSugarRepository djyUserMailAccount, SqlSugarRepository bookingDeliveryRecordJobRunScheduleRep, SqlSugarRepository sysUserRepository) { _logger = logger; _cache = cache; @@ -78,6 +78,7 @@ namespace Myshipping.Application.Service.BookingOrder _bookingDeliveryRecordShipScheduleRep = bookingDeliveryRecordShipScheduleRep; _djyUserMailAccount = djyUserMailAccount; _bookingDeliveryRecordJobRunScheduleRep = bookingDeliveryRecordJobRunScheduleRep; + _sysUserRepository = sysUserRepository; } #region 检索海运船期详情 @@ -2344,8 +2345,7 @@ namespace Myshipping.Application.Service.BookingOrder DateTime startDate = new DateTime(nowDate.Year, nowDate.Month, nowDate.Day); var runList = _bookingDeliveryRecordJobRunScheduleRep.AsQueryable().Filter(null, true) - .Where(a => a.CreatedTime >= startDate && a.IsDeleted == false && a.IS_SEND_REPORT == false).ToList(); - + .Where(a => a.SEND_TIME >= startDate && a.IsDeleted == false && a.IS_SEND_REPORT == false).ToList(); if (runList.Count > 0) { @@ -2358,11 +2358,11 @@ namespace Myshipping.Application.Service.BookingOrder { var nextJobTime = DateTime.MinValue; - var nextOrder = _bookingDeliveryRecordRep.AsQueryable().Filter(null, true) - .Where(a => a.IsDeleted == false && a.JOB_TIME != null && a.JOB_TIME.Value >= nowDate && a.CreatedUserId == b.Key).OrderBy(a=>a.JOB_TIME.Value).First(); + var waitList = _bookingDeliveryRecordRep.AsQueryable().Filter(null, true) + .Where(a => a.IsDeleted == false && a.JOB_TIME != null && a.JOB_TIME.Value >= nowDate && a.CreatedUserId == b.Key && a.STATUS == "TEMP").OrderBy(a=>a.JOB_TIME.Value).ToList(); - if (nextOrder != null) - nextJobTime = nextOrder.JOB_TIME.Value; + if (waitList.Count > 0) + nextJobTime = waitList.FirstOrDefault().JOB_TIME.Value; var opUser = _sysUserRepository.AsQueryable().Filter(null, true).First(a => a.Id == b.Key); @@ -2390,15 +2390,21 @@ namespace Myshipping.Application.Service.BookingOrder string baseHtml = File.ReadAllText(templatePath); - HtmlDocument html = new HtmlDocument(); - html.LoadHtml(baseHtml); - baseHtml = baseHtml.Replace("#ReportTime#", nowDate.ToString("yyyy-MM-dd HH:mm:ss")); baseHtml = baseHtml.Replace("#total#", currList.Count.ToString()); baseHtml = baseHtml.Replace("#succ#", currList.Count(x=>x.STATUS == "SUCC").ToString()); baseHtml = baseHtml.Replace("#failure#", currList.Count(x => x.STATUS == "FAILURE").ToString()); + if(waitList.Count > 0) + { + baseHtml = baseHtml.Replace("#wait#", currList.Count().ToString()); + } + else + { + baseHtml = baseHtml.Replace("#wait#", "0"); + } + if (nextJobTime != DateTime.MinValue) { baseHtml = baseHtml.Replace("#NextTime#", nextJobTime.ToString("yyyy-MM-dd HH:mm")); @@ -2408,6 +2414,8 @@ namespace Myshipping.Application.Service.BookingOrder baseHtml = baseHtml.Replace("#NextTime#", ""); } + HtmlDocument html = new HtmlDocument(); + html.LoadHtml(baseHtml); var tableNode = html.DocumentNode.SelectSingleNode(".//table[@id='show-table']"); @@ -2417,10 +2425,10 @@ namespace Myshipping.Application.Service.BookingOrder for (int i = 0; i < currList.Count; i++) { - tableBuilder.Append($"{(currList[i].SEND_TIME.HasValue ? currList[i].SEND_TIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "")}"); - tableBuilder.Append($"{currList[i].STATUS_NAME}{currList[i].BOOKING_REFERENCE}{currList[i].PLACERECEIPT}{currList[i].PLACEDELIVERY}"); + tableBuilder.Append($"{i+1}{(currList[i].SEND_TIME.HasValue ? currList[i].SEND_TIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : "")}"); + tableBuilder.Append($"{currList[i].STATUS_NAME}{currList[i].BOOKING_REFERENCE}{currList[i].PLACERECEIPT_CITY}{currList[i].PLACEDELIVERY_CITY}"); tableBuilder.Append($"{currList[i].CTN_STAT}{(currList[i].ETD.HasValue ? currList[i].ETD.Value.ToString("yyyy-MM-dd HH:mm") : "")}"); - tableBuilder.Append($"{(currList[i].TOTAL_CARGO_WEIGHT.HasValue? currList[i].TOTAL_CARGO_WEIGHT.Value.ToString():"")}{currList[i].PRICE_OWNER_REFERENCE}"); + tableBuilder.Append($"{(currList[i].TOTAL_CARGO_WEIGHT.HasValue? currList[i].TOTAL_CARGO_WEIGHT.Value.ToString():"")}{currList[i].PRICE_REFERENCE}"); tableBuilder.Append($"{(currList[i].IS_RECV_BC ? "1" : "")}{(currList[i].IS_RECV_BK_CANCEL ? "1" : "")}{(currList[i].JOB_TIME.HasValue ? currList[i].JOB_TIME.Value.ToString("yyyy-MM-dd HH:mm") : "")}"); } @@ -2428,11 +2436,12 @@ namespace Myshipping.Application.Service.BookingOrder tableNode.ChildNodes.Add(HtmlNode.CreateNode(tableBuilder.ToString())); } + emailHtml = html.DocumentNode.OuterHtml; EmailApiUserDefinedDto emailApiUserDefinedDto = new EmailApiUserDefinedDto { SendTo = opUser.Email, - //CCTo = opEmail, + CCTo = string.Join(";", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList().ToArray()), Title = $"马士基API定时订舱统计报告 {nowDate.ToString("yyyy-MM-dd HH:mm:ss")}", Body = emailHtml, Account = publicMailAccount.MailAccount?.Trim(), diff --git a/Myshipping.Web.Entry/wwwroot/EmailTemplate/MSKAPITimerReportTemplate.html b/Myshipping.Web.Entry/wwwroot/EmailTemplate/MSKAPITimerReportTemplate.html new file mode 100644 index 00000000..4f14e0b8 --- /dev/null +++ b/Myshipping.Web.Entry/wwwroot/EmailTemplate/MSKAPITimerReportTemplate.html @@ -0,0 +1,54 @@ + + + + + + + + 邮件模板 + + + + + + + + + +
+ + + + + + + + + + +
+

以下是马士基API定时订舱报告 时间:#ReportTime#

+
+

定时任务共计:#total# 条;成功 #succ# 条;失败 #failure# 条;还有待订舱数量 #wait# 条;下一个订舱日期:#NextTime#

+
+ + + + + + + + + + + + + + + + +
序号订舱日期状态订舱号起运港目的港箱型箱量ETD总重(KGS)合约号BCCancellation定时时间
+
+
+ +