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.

205 lines
8.2 KiB
C#

using DS.Module.Core;
4 months ago
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Op.Entity.TaskInteraction;
using DS.WMS.Core.Op.Interface.TaskInteraction;
using Microsoft.Extensions.DependencyInjection;
namespace DS.WMS.Core.Op.Method.TaskInteraction
{
/// <summary>
/// 邮件模板配置服务
/// </summary>
public class TaskMailService : ServiceBase, ITaskMailService
{
readonly Lazy<IActionManagerService> actionService;
readonly MailService mailService;
/// <summary>
/// 初始化
/// </summary>
/// <param name="provider"></param>
public TaskMailService(IServiceProvider provider) : base(provider)
{
actionService = new Lazy<IActionManagerService>(provider.GetRequiredService<IActionManagerService>());
mailService = new(provider);
}
/// <summary>
/// 渲染模板
/// </summary>
/// <param name="docType">单据类型</param>
/// <param name="bsId">业务ID</param>
/// <param name="businessType">业务类型</param>
/// <returns></returns>
public async Task<DataResult<string>> RenderTemplateAsync(DocumentType docType, long bsId, BusinessType businessType)
{
var taskMail = await TenantDb.Queryable<BusinessTaskMail>()
.Where(x => x.DocumentType == docType).OrderByDescending(x => x.CreateTime).FirstAsync();
if (taskMail == null)
return DataResult<string>.FailedWithDesc(MultiLanguageConst.EmptyData);
if (string.IsNullOrEmpty(taskMail.Content))
return DataResult<string>.Success(taskMail.Content);
var model = await mailService.GetTemplateModelAsync(actionService.Value, bsId, BusinessType.OceanShippingExport);
if (model.Primary == null)
return DataResult<string>.FailedWithDesc(MultiLanguageConst.EmptyData);
string content = await MailService.RenderTemplateAsync(taskMail.Content, model);
return DataResult<string>.Success(content);
}
/// <summary>
/// 获取邮件内容
/// </summary>
/// <param name="docType">单据类型</param>
/// <param name="templateName">邮件模板名称</param>
/// <param name="customerNO">委托编号</param>
/// <returns></returns>
public async Task<DataResult<Tuple<string, string>>> GetMailContentAsync(string customerNO, DocumentType docType, string? templateName = null)
{
var taskMail = await TenantDb.Queryable<BusinessTaskMail>()
.Where(x => x.DocumentType == docType || x.Name == templateName).OrderByDescending(x => x.CreateTime).FirstAsync();
if (taskMail == null)
return DataResult<Tuple<string, string>>.FailedWithDesc(MultiLanguageConst.EmptyData);
var bsId = await TenantDb.Queryable<SeaExport>().Where(x => x.CustomerNo.Contains(customerNO)).Select(x => x.Id).FirstAsync();
var model = await mailService.GetTemplateModelAsync(actionService.Value, bsId, BusinessType.OceanShippingExport);
string title = await MailService.RenderTemplateAsync(taskMail.Title, model);
string content = await MailService.RenderTemplateAsync(taskMail.Content, model);
return DataResult<Tuple<string, string>>.Success(new Tuple<string, string>(title, content));
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<BusinessTaskMail>>> GetListAsync(PageRequest request)
{
var whereList = request.GetConditionalModels(Db);
return await TenantDb.Queryable<BusinessTaskMail>()
.Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC)
.Where(whereList).ToQueryPageAsync(request.PageCondition);
}
4 months ago
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<DataResult<BusinessTaskMail>> GetAsync(long id)
{
var entity = await TenantDb.Queryable<BusinessTaskMail>()
.Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC).Includes(x => x.Attachments)
4 months ago
.Where(x => x.Id == id).FirstAsync();
return DataResult<BusinessTaskMail>.Success(entity);
}
/// <summary>
/// 根据配置名获取
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public async Task<BusinessTaskMail> GetAsync(string name)
{
return await TenantDb.Queryable<BusinessTaskMail>()
.Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC).Includes(x => x.Attachments)
.Where(x => x.Name.Contains(name)).FirstAsync();
}
4 months ago
/// <summary>
/// 编辑
/// </summary>
/// <param name="taskMail"></param>
/// <returns></returns>
public async Task<DataResult> EditAsync(BusinessTaskMail taskMail)
{
await TenantDb.Ado.BeginTranAsync();
try
{
if (taskMail.Id == 0)
{
taskMail.Receiver ??= new();
taskMail.Sender ??= new();
taskMail.CC ??= new();
taskMail = await TenantDb.InsertNav(taskMail)
.Include(x => x.Receiver).Include(x => x.Sender).Include(x => x.CC)
.ExecuteReturnEntityAsync();
4 months ago
}
else
{
await TenantDb.UpdateNav(taskMail)
.Include(x => x.Receiver).Include(x => x.Sender).Include(x => x.CC)
.ExecuteCommandAsync();
4 months ago
}
4 months ago
if (taskMail.Attachments?.Count > 0)
{
var list = taskMail.Attachments.FindAll(x => x.TaskMailId == 0);
foreach (var item in list)
item.TaskMailId = taskMail.Id;
await TenantDb.Deleteable<BusinessTaskAttachment>().Where(x => x.TaskMailId == taskMail.Id).ExecuteCommandAsync();
await TenantDb.Insertable(taskMail.Attachments).ExecuteCommandAsync();
//await TenantDb.Storageable(taskMail.Attachments).DefaultAddElseUpdate().ExecuteCommandAsync();
4 months ago
}
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public async Task<DataResult> DeleteAsync(IdModel model)
{
await TenantDb.Ado.BeginTranAsync();
try
{
await TenantDb.DeleteNav<BusinessTaskMail>(x => model.Ids.Contains(x.Id))
.Include(x => x.Receiver).Include(x => x.Sender).Include(x => x.CC).Include(x => x.Attachments)
.ExecuteCommandAsync();
4 months ago
await TenantDb.Ado.CommitTranAsync();
return DataResult.Success;
}
catch (Exception ex)
{
await TenantDb.Ado.RollbackTranAsync();
await ex.LogAsync(Db);
return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed));
}
}
/// <summary>
/// 删除附件
/// </summary>
/// <param name="attachments"></param>
/// <returns></returns>
public async Task<DataResult> DeleteAttachmentsAsync(List<BusinessTaskAttachment> attachments)
{
await TenantDb.Deleteable(attachments).ExecuteCommandAsync();
return DataResult.Success;
}
}
}