irc-netcore-api/IRaCIS.Core.Application/MassTransit/Consumer/CommonEmailHelper.cs

103 lines
3.2 KiB
C#

using DocumentFormat.OpenXml;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IRaCIS.Core.Application.MassTransit.Consumer;
public static class CommonEmailHelper
{
public static async Task<EmailNoticeConfig> GetEmailSubejctAndHtmlInfoAndBuildAsync(IRepository<EmailNoticeConfig> _emailNoticeConfigrepository, EmailBusinessScenario scenario, MimeMessage messageToSend,
Func<(string topicStr, string htmlBodyStr), (string topicStr, string htmlBodyStr)> emailFunc)
{
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
var configInfo = await _emailNoticeConfigrepository.Where(t => t.BusinessScenarioEnum == scenario).FirstOrDefaultAsync();
if (configInfo == null)
{
throw new BusinessValidationFailedException("系统未找到当前场景邮件配置信息,请联系运维人员核查");
}
var (topicStr, htmlBodyStr) = isEn_US ? (configInfo.EmailTopic, configInfo.EmailHtmlContent) : (configInfo.EmailTopicCN, configInfo.EmailHtmlContentCN);
try
{
//每个场景修改主题 和body的逻辑不一样
(topicStr, htmlBodyStr) = emailFunc((topicStr, htmlBodyStr));
}
catch (Exception ex)
{
throw new BusinessValidationFailedException("邮件模板内容有误,填充内容出现问题,请联系运维人员核查");
}
messageToSend.Subject = topicStr;
var builder = new BodyBuilder();
builder.HtmlBody = htmlBodyStr;
messageToSend.Body = builder.ToMessageBody();
return configInfo;
}
public static string ReplaceCompanyName(SystemEmailSendConfig _systemEmailConfig, string needDealtxt)
{
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
var str = needDealtxt.Replace("{company}", isEn_US ? _systemEmailConfig.CompanyName : _systemEmailConfig.CompanyNameCN)
.Replace("{company abbreviation}", isEn_US ? _systemEmailConfig.CompanyShortName : _systemEmailConfig.CompanyShortNameCN);
return str;
}
/// <summary>
/// 翻译字典
/// </summary>
/// <param name="inDto"></param>
/// <returns></returns>
public static string TranslationDictionary(TranslationDictionaryDto inDto)
{
var dic = inDto.DictionaryRepository.Where(x => x.Parent.Code == inDto.DictionaryCode && x.Code == inDto.EnumValue).FirstOrDefault();
return dic == null ? string.Empty : (inDto.IsEn_US ? dic.Value : dic.ValueCN);
}
}
/// <summary>
/// 转换字典的Dto
/// </summary>
public class TranslationDictionaryDto
{
/// <summary>
/// 字典仓储
/// </summary>
public IRepository<Dictionary> DictionaryRepository { get; set; }
/// <summary>
/// 字典Code
/// </summary>
public string DictionaryCode { get; set; }
/// <summary>
/// 枚举值
/// </summary>
public string EnumValue { get; set; }
/// <summary>
/// 是否是英文
/// </summary>
public bool IsEn_US { get; set; }
}