DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Email/MailBodyFactory.cs
2022-05-04 15:02:12 +05:00

123 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace AsbCloudInfrastructure
{
class MailBodyFactory
{
protected readonly string platformName;
private readonly string platformUrl;
protected readonly string companyName;
protected readonly string supportMail;
public MailBodyFactory(IConfiguration configuration)
{
platformName = configuration.GetValue("email:platformName", "Цифровое бурение");
platformUrl = configuration.GetValue("email:platformUrl", "");
companyName = configuration.GetValue("email:companyName", "ООО \"Цифровое бурение\"");
supportMail = configuration.GetValue("email:supportMail", "support@digitaldrilling.ru");
}
public static string MakeSubject(WellDto well, string action)
{
var subj = $"{well.Deposit}, {well.Cluster}, {well.Caption}. Программа бурения. {action}";
return subj;
}
public string MakeMailBodyForNewPublisher(WellDto well, string userName, string documentCategory)
{
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} началось создание программы бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
$"<br><br>От вас ожидается загрузка на портал документа «{documentCategory}» в формате excel (*.xlsx)." +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForApproverNewFile(WellDto well, string userName, int idFile, string fileName)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портал {platformName} загружен документ {fileDownloadHref}" +
$" для согласования при создании программы бурения скважины {drillingProgramHref}, куст ({well.Cluster})" +
$", месторождение ({well.Deposit}).<br>" +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForPublisherOnReject(WellDto well, string userName, int idFile, string fileName, FileMarkDto fileMark)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} отклонен загруженный вами документ {fileDownloadHref} " +
$" по программе бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
$" Комментарий согласующего ({fileMark.User.Name} {fileMark.User.Surname}):<br>{fileMark.Comment}" +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string userName, int idFile, string fileName)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} полностью согласован документ {fileDownloadHref} " +
$" по программе бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
MakeSignatue() +
$"</body></html>";
return body;
}
private string MakeFileDownloadHref(int idWell, int idFile, string fileName)
{
var fileDownloadUrl = $"{platformUrl}/api/well/{idWell}/files/{idFile}";
var fileDownloadHref = MakeHref(fileDownloadUrl, fileName);
return fileDownloadHref;
}
private string MakeDrillingProgramHref(WellDto well)
{
var drillingProgramUrl = $"{platformUrl}/api/well/{well.Id}/drillingProgram";
var drillingProgramHref = MakeHref(drillingProgramUrl, well.Caption);
return drillingProgramHref;
}
private static string MakeHref(string url, string text)
=> $"<a href=\"{url}\">{text}</a>";
protected string MakeSignatue()
{
var logo = GetImageBase64("logo_32.png");
var sign = $"<br>---<br><img src=\"{logo}\"/>" +
$"{companyName}<br>Это письмо сформировано автоматически.<br>Для получения помощи по работе на портале " +
$"{platformName} обращайтесь по адресу {supportMail}";
return sign;
}
public static string GetImageBase64(string resourceFileName)
{
if (string.IsNullOrEmpty(resourceFileName))
return null;
var format = Path.GetExtension(resourceFileName).Trim('.');
var logoFilePath = Path.Combine("Res" + resourceFileName);
var imageBytes = File.ReadAllBytes(logoFilePath);
return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes);
}
}
}