using DS.Module.Core; using DS.WMS.Core.HangfireJob.Dtos; using DS.WMS.Core.Op.Entity; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using MiniExcelLibs; using Quartz; using SqlSugar; namespace DS.WMS.Core.QuarztJobs { /// /// WSL报表服务 /// public class WSLReportJob : IJob { const string SOURCE_CODE = "FOB-WSL"; static readonly ApiFox api; ISqlSugarClient? db; Microsoft.Extensions.Configuration.IConfiguration config; IWebHostEnvironment hostEnvironment; static WSLReportJob() { api = new ApiFox(); } /// /// 初始化 /// /// public WSLReportJob(IServiceProvider serviceProvider) { db = serviceProvider.GetRequiredService(); config = serviceProvider.GetRequiredService(); hostEnvironment = serviceProvider.GetRequiredService(); } public async Task Execute(IJobExecutionContext context) { string path = Path.Combine(hostEnvironment.WebRootPath, "templates", "WSL.xlsx"); FileInfo templateFile = new FileInfo(path); if (!templateFile.Exists) throw new ApplicationException("未能在下列路径找到模板文件:" + path); db.QueryFilter.Clear(); var dbLinks = await db.Queryable().ToListAsync(); SqlSugarClient? tenantDb = null; var today = DateTime.Now; var today0 = new DateTime(today.Year, today.Month, today.Day); var yesterday0 = today0.AddDays(-1).Date; var yesterday = new DateTime(yesterday0.Year, yesterday0.Month, yesterday0.Day, 23, 59, 59); var lastMonth = today.AddMonths(-1); var nextMonth = today.AddMonths(1); var startDate = new DateTime(lastMonth.Year, lastMonth.Month, lastMonth.Day); var endDate = new DateTime(nextMonth.Year, nextMonth.Month, nextMonth.Day, 23, 59, 59); try { foreach (var dbLink in dbLinks) { if (config["TaskMail:DefaultSetting:Tenant"] != dbLink.TenantId.ToString()) continue; tenantDb = new SqlSugarClient(new ConnectionConfig { ConfigId = dbLink.Id, ConnectionString = dbLink.Connection, DbType = dbLink.DbType, IsAutoCloseConnection = true }); WSLModel model = new() { Date = today.ToString("yyyy.MM.dd"), Month = today.ToString("yyyy.MM") }; var list = await tenantDb.Queryable().Where(x => x.SourceCode == SOURCE_CODE && SqlFunc.Between(x.ETD, startDate, endDate)) .Select(x => new { x.Id, x.CustomerId, x.CustomerName, x.ETD, x.TEU }).ToListAsync(); if (list.Count == 0) return; var ids = list.Select(x => x.Id.ToString()); var ctnList = await tenantDb.Queryable().Where(x => ids.Contains(x.BSNO) && !SqlFunc.IsNullOrEmpty(x.CntrNo) && !SqlFunc.IsNullOrEmpty(x.SealNo)) .Select(x => new { x.BSNO, x.TEU }).ToListAsync(); model.List = list.GroupBy(x => new { x.CustomerId, x.CustomerName }).Select(x => new WSLItem { CustomerId = x.Key.CustomerId, CustomerName = x.Key.CustomerName, Date = model.Date, YesterdayTeu = x.Where(x => x.ETD >= yesterday0 && x.ETD <= yesterday).Sum(x => x.TEU), TodayTeu = x.Where(x => x.ETD >= today0 && x.ETD <= today).Sum(x => x.TEU), NextMonthTEU = x.Where(x => x.ETD.Value.Year == nextMonth.Year && x.ETD.Value.Month == nextMonth.Month).Sum(x => x.TEU), LastMonthTEU = x.Where(x => x.ETD.Value.Year == lastMonth.Year && x.ETD.Value.Month == lastMonth.Month).Sum(x => x.TEU), }).ToList(); foreach (var item in model.List) { var ids2 = list.Where(x => x.CustomerId == item.CustomerId).Select(x => x.Id.ToString()); item.TodayTeuCTNPickup = ctnList.Where(x => ids2.Contains(x.BSNO)).Sum(x => x.TEU); } MemoryStream ms = new MemoryStream(); await MiniExcel.SaveAsByTemplateAsync(ms, path, model); string base64Str = Convert.ToBase64String(ms.ToArray()); ms.Dispose(); var attaches = new List { new() { AttachName = "WSL Volume Daily Increase Report.xlsx", AttachContent = base64Str} }; dynamic[] mailParams = [new { SendTo = config["TaskMail:DefaultSetting:Receivers"], Title = "WSL Volume Daily Increase Report", Body = @"

Dear WSL Team,

Pls kindly check the daily report for your member's nomination booking: 

 

Thanks & Best regards

Candy SHAO 邵芳

General Manager| SUNNINESS LOGISTICS CO.,LTD.

TEL: +(86) (0532-80688387)1 MB./We chat: +(86)(18866622731) | QQ:2853083553|Email:candy@sunniness. net |WCA ID: 60272

", Account = config["TaskMail:DefaultSetting:Account"], Password = config["TaskMail:DefaultSetting:Password"], Server = config["TaskMail:DefaultSetting:Host"], Port = config["TaskMail:DefaultSetting:Port"], UseSSL = config["TaskMail:DefaultSetting:UseSSL"], Attaches = attaches }]; var mailResult = await api.SendRequestAsync(HttpMethod.Post, config["TaskMail:MailApiUrl"], mailParams); if (!mailResult.IsSuccessStatusCode) throw new ApplicationException("发送邮件失败"); } } finally { tenantDb?.Dispose(); } } class DefaultSetting { public long Tenant { get; set; } public string? Account { get; set; } public string? Password { get; set; } public string? Host { get; set; } public int? Port { get; set; } public bool UseSSL { get; set; } public string? Receivers { get; set; } } class Attachment { public string? AttachName { get; set; } public string? AttachContent { get; set; } } } }