using DS.Module.Core; 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 { /// /// 邮件模板配置服务 /// public class TaskMailService : ServiceBase, ITaskMailService { readonly Lazy actionService; readonly MailService mailService; /// /// 初始化 /// /// public TaskMailService(IServiceProvider provider) : base(provider) { actionService = new Lazy(provider.GetRequiredService()); mailService = new(provider); } /// /// 渲染模板 /// /// 单据类型 /// 业务ID /// 业务类型 /// public async Task> RenderTemplateAsync(DocumentType docType, long bsId, BusinessType businessType) { var taskMail = await TenantDb.Queryable() .Where(x => x.DocumentType == docType).OrderByDescending(x => x.CreateTime).FirstAsync(); if (taskMail == null) return DataResult.FailedWithDesc(MultiLanguageConst.EmptyData); if (string.IsNullOrEmpty(taskMail.Content)) return DataResult.Success(taskMail.Content); var model = await mailService.GetTemplateModelAsync(actionService.Value, bsId, BusinessType.OceanShippingExport); if (model.Primary == null) return DataResult.FailedWithDesc(MultiLanguageConst.EmptyData); string content = await MailService.RenderTemplateAsync(taskMail.Content, model); return DataResult.Success(content); } /// /// 获取邮件内容 /// /// 单据类型 /// 邮件模板名称 /// 委托编号 /// public async Task>> GetMailContentAsync(string customerNO, DocumentType docType, string? templateName = null) { var taskMail = await TenantDb.Queryable() .Where(x => x.DocumentType == docType || x.Name == templateName).OrderByDescending(x => x.CreateTime).FirstAsync(); if (taskMail == null) return DataResult>.FailedWithDesc(MultiLanguageConst.EmptyData); var bsId = await TenantDb.Queryable().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>.Success(new Tuple(title, content)); } /// /// 获取分页列表 /// /// /// public async Task>> GetListAsync(PageRequest request) { var whereList = request.GetConditionalModels(Db); return await TenantDb.Queryable() .Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC) .Where(whereList).ToQueryPageAsync(request.PageCondition); } /// /// 详情 /// /// /// public async Task> GetAsync(long id) { var entity = await TenantDb.Queryable() .Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC).Includes(x => x.Attachments) .Where(x => x.Id == id).FirstAsync(); return DataResult.Success(entity); } /// /// 根据配置名获取 /// /// /// public async Task GetAsync(string name) { return await TenantDb.Queryable() .Includes(x => x.Receiver).Includes(x => x.Sender).Includes(x => x.CC).Includes(x => x.Attachments) .Where(x => x.Name.Contains(name)).FirstAsync(); } /// /// 编辑 /// /// /// public async Task 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(); } else { await TenantDb.UpdateNav(taskMail) .Include(x => x.Receiver).Include(x => x.Sender).Include(x => x.CC) .ExecuteCommandAsync(); } 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().Where(x => x.TaskMailId == taskMail.Id).ExecuteCommandAsync(); await TenantDb.Insertable(taskMail.Attachments).ExecuteCommandAsync(); //await TenantDb.Storageable(taskMail.Attachments).DefaultAddElseUpdate().ExecuteCommandAsync(); } 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)); } } /// /// 删除 /// /// /// public async Task DeleteAsync(IdModel model) { await TenantDb.Ado.BeginTranAsync(); try { await TenantDb.DeleteNav(x => model.Ids.Contains(x.Id)) .Include(x => x.Receiver).Include(x => x.Sender).Include(x => x.CC).Include(x => x.Attachments) .ExecuteCommandAsync(); 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)); } } /// /// 删除附件 /// /// /// public async Task DeleteAttachmentsAsync(List attachments) { await TenantDb.Deleteable(attachments).ExecuteCommandAsync(); return DataResult.Success; } } }