|
|
|
@ -1,9 +1,21 @@
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.PrintModule;
|
|
|
|
|
using DS.Module.SqlSugar;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using DS.WMS.Core.Info.Entity;
|
|
|
|
|
using DS.WMS.Core.Op.Dtos;
|
|
|
|
|
using DS.WMS.Core.Op.Dtos.TaskInteraction;
|
|
|
|
|
using DS.WMS.Core.Op.Interface;
|
|
|
|
|
using DS.WMS.Core.Op.Interface.TaskInteraction;
|
|
|
|
|
using DS.WMS.Core.Sys.Entity;
|
|
|
|
|
using MailKit.Net.Smtp;
|
|
|
|
|
using Masuit.Tools;
|
|
|
|
|
using Masuit.Tools.Systems;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MimeKit;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using RazorEngineCore;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
{
|
|
|
|
@ -12,6 +24,13 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MailActionExecutor : IActionExecutor
|
|
|
|
|
{
|
|
|
|
|
static readonly ApiFox api;
|
|
|
|
|
|
|
|
|
|
static MailActionExecutor()
|
|
|
|
|
{
|
|
|
|
|
api = new ApiFox();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送邮件
|
|
|
|
|
/// </summary>
|
|
|
|
@ -19,8 +38,14 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task ExecuteAsync(ActionExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
var db = context.ServiceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
var saasService = context.ServiceProvider.GetRequiredService<ISaasDbService>();
|
|
|
|
|
var user = context.ServiceProvider.GetRequiredService<IUser>();
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
|
|
|
|
|
var service = context.ServiceProvider.GetRequiredService<ITaskMailService>();
|
|
|
|
|
var logService = context.ServiceProvider.GetRequiredService<ITaskLogService>();
|
|
|
|
|
var seService = context.ServiceProvider.GetRequiredService<ISeaExportService>();
|
|
|
|
|
|
|
|
|
|
var mailName = context.AdditionalData["MailName"] as string;
|
|
|
|
|
if (mailName.IsNullOrEmpty())
|
|
|
|
@ -35,9 +60,141 @@ namespace DS.WMS.Core.Op.Method.TaskInteraction
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = seService.GetSeaExportInfo(context.TaskInfo.BusinessId.ToString());
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await logService.WriteLogAsync(context.TaskInfo, $"未能获取Id={context.TaskInfo.BusinessId}的{context.TaskInfo.BusinessType.GetDescription()}数据");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var templateModel = new MailTemplateModel<SeaExportRes> { Primary = result.Data };
|
|
|
|
|
IRazorEngine razorEngine = new RazorEngine();
|
|
|
|
|
IRazorEngineCompiledTemplate titleTemplate = razorEngine.Compile(mailConfig.Title);
|
|
|
|
|
IRazorEngineCompiledTemplate contentTemplate = razorEngine.Compile(mailConfig.Content);
|
|
|
|
|
var titleTemplate = razorEngine.Compile<RazorEngineTemplateBase<MailTemplateModel<SeaExportRes>>>(mailConfig.Title);
|
|
|
|
|
string title = titleTemplate.Run(x =>
|
|
|
|
|
{
|
|
|
|
|
x.Model = templateModel;
|
|
|
|
|
});
|
|
|
|
|
var contentTemplate = razorEngine.Compile<RazorEngineTemplateBase<MailTemplateModel<SeaExportRes>>>(mailConfig.Content);
|
|
|
|
|
string content = contentTemplate.Run(x =>
|
|
|
|
|
{
|
|
|
|
|
x.Model = templateModel;
|
|
|
|
|
});
|
|
|
|
|
var textPart = new TextPart("plain") { Text = content };
|
|
|
|
|
var message = new MimeMessage
|
|
|
|
|
{
|
|
|
|
|
Subject = title,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//设置发件人
|
|
|
|
|
List<long> senderIds = new List<long>();
|
|
|
|
|
if (mailConfig.Sender.IsSale)
|
|
|
|
|
{
|
|
|
|
|
senderIds.Add(templateModel.Primary.SaleId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Sender.IsOperator)
|
|
|
|
|
{
|
|
|
|
|
senderIds.Add(templateModel.Primary.OperatorId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Sender.IsCustomerService)
|
|
|
|
|
{
|
|
|
|
|
senderIds.Add(templateModel.Primary.CustomerService);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Sender.IsVouchingClerk)
|
|
|
|
|
{
|
|
|
|
|
senderIds.Add(templateModel.Primary.Doc);
|
|
|
|
|
}
|
|
|
|
|
var senderList = await db.Queryable<SysUser>().Where(x => senderIds.Contains(x.Id) && x.Email != null && x.Email != string.Empty)
|
|
|
|
|
.Select(x => new { x.UserName, x.UserEnName, x.Email }).ToListAsync();
|
|
|
|
|
foreach (var sender in senderList)
|
|
|
|
|
message.From.Add(new MailboxAddress(sender.UserName, sender.Email));
|
|
|
|
|
|
|
|
|
|
//设置收件人
|
|
|
|
|
List<long> receiverIds = new List<long>();
|
|
|
|
|
if (mailConfig.Receiver.IsCarrier)
|
|
|
|
|
{
|
|
|
|
|
receiverIds.Add(templateModel.Primary.CarrierId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Receiver.IsBooking)
|
|
|
|
|
{
|
|
|
|
|
//receiverIds.Add(templateModel.Primary.CarrierId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Receiver.IsYard)
|
|
|
|
|
{
|
|
|
|
|
receiverIds.Add(templateModel.Primary.YardId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Receiver.IsTruck)
|
|
|
|
|
{
|
|
|
|
|
receiverIds.Add(templateModel.Primary.TruckerId);
|
|
|
|
|
}
|
|
|
|
|
if (mailConfig.Receiver.IsController)
|
|
|
|
|
{
|
|
|
|
|
receiverIds.Add(templateModel.Primary.CustomerId);
|
|
|
|
|
}
|
|
|
|
|
var receiverList = await tenantDb.Queryable<InfoClient>().Where(x => receiverIds.Contains(x.Id) && x.Email != null && x.Email != string.Empty)
|
|
|
|
|
.Select(x => new { x.ShortName, x.EnShortName, x.Email }).ToListAsync();
|
|
|
|
|
foreach (var item in receiverList)
|
|
|
|
|
message.To.Add(new MailboxAddress(item.ShortName, item.Email));
|
|
|
|
|
|
|
|
|
|
//需要上传附件
|
|
|
|
|
if (mailConfig.Attachments?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var multipart = new Multipart("mixed");
|
|
|
|
|
multipart.Add(textPart);
|
|
|
|
|
|
|
|
|
|
if (api.DefaultHeaders.Contains("Authorization"))
|
|
|
|
|
api.DefaultHeaders.Remove("Authorization");
|
|
|
|
|
|
|
|
|
|
api.DefaultHeaders.Add("Authorization", "Bearer " + user.GetToken());
|
|
|
|
|
|
|
|
|
|
long tenantId = long.Parse(user.TenantId);
|
|
|
|
|
foreach (var item in mailConfig.Attachments)
|
|
|
|
|
{
|
|
|
|
|
var req = new OpenPrintReq
|
|
|
|
|
{
|
|
|
|
|
ParamJsonStr = JsonConvert.SerializeObject(new { Id = context.TaskInfo.BusinessId }),
|
|
|
|
|
PrintType = "1",
|
|
|
|
|
TemplateId = item.TemplateId,
|
|
|
|
|
TenantId = tenantId
|
|
|
|
|
};
|
|
|
|
|
var reqResult = await api.PostAsync<PrintResult>(item.RequestURL, req);
|
|
|
|
|
if (reqResult.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//var attachment = new MimePart("image", "gif")
|
|
|
|
|
//{
|
|
|
|
|
// Content = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
|
|
|
|
|
// ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
|
|
|
|
// ContentTransferEncoding = ContentEncoding.Base64,
|
|
|
|
|
// FileName = Path.GetFileName(path)
|
|
|
|
|
//};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message.Body = textPart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var client = new SmtpClient();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Connect(mailConfig.Server.Host, mailConfig.Server.Port, mailConfig.Server.UseSSL);
|
|
|
|
|
if (!mailConfig.Server.LoginName.IsNullOrEmpty() && !mailConfig.Server.Password.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
client.Authenticate(mailConfig.Server.LoginName, mailConfig.Server.Password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.Send(message);
|
|
|
|
|
client.Disconnect(true);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await ex.LogAsync(db);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
client?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|