forked from ddrilling/AsbCloudServer
123 lines
5.3 KiB
C#
123 lines
5.3 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|