2024-08-16 13:44:43 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Presentation;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using ServiceSourceBusinessLogic.MailKitWorker;
|
|
|
|
|
using ServiceStationContracts.BindingModels;
|
|
|
|
|
using ServiceStationContracts.BusinessLogic;
|
|
|
|
|
using ServiceStationContracts.BusinessLogicContracts;
|
|
|
|
|
using ServiceStationContracts.SearchModels;
|
|
|
|
|
using ServiceStationContracts.ViewModels;
|
|
|
|
|
using ServiceStationDataModels.Models;
|
2024-04-30 21:22:23 +03:00
|
|
|
|
|
|
|
|
|
namespace ServiceSourceRestAPI.Controllers {
|
2024-08-16 13:44:43 +04:00
|
|
|
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
2024-04-30 21:22:23 +03:00
|
|
|
|
public class MainController : Controller {
|
2024-08-16 13:44:43 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IClientLogic _clientLogic;
|
|
|
|
|
private readonly IExecutorLogic _executorLogic;
|
|
|
|
|
private readonly IReportLogic _reportLogic;
|
|
|
|
|
private readonly ITaskLogic _taskLogic;
|
|
|
|
|
private readonly IWorkLogic _workLogic;
|
|
|
|
|
|
|
|
|
|
private readonly AbstractMailWorker _mailWorker;
|
|
|
|
|
|
|
|
|
|
public MainController(ILogger<MainController> logger, IClientLogic clientLogic, IExecutorLogic executorLogic,
|
|
|
|
|
IReportLogic reportLogic, ITaskLogic taskLogic, IWorkLogic workLogic, AbstractMailWorker mailWorker) {
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_clientLogic = clientLogic;
|
|
|
|
|
_executorLogic = executorLogic;
|
|
|
|
|
_reportLogic = reportLogic;
|
|
|
|
|
_taskLogic = taskLogic;
|
|
|
|
|
_workLogic = workLogic;
|
|
|
|
|
_mailWorker = mailWorker;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Register(ExecutorBindingModel model) {
|
|
|
|
|
try {
|
|
|
|
|
_executorLogic.Create(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка регистрации");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public ExecutorViewModel? Login (string email, string password) {
|
|
|
|
|
try {
|
|
|
|
|
return _executorLogic.ReadElement(new ExecutorSearchModel {
|
|
|
|
|
Email = email,
|
|
|
|
|
Password = password
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка входа в систему");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void UpdateData(ExecutorBindingModel model) {
|
|
|
|
|
try {
|
|
|
|
|
_executorLogic.Update(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления данных");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkBindingModel Make_New_Record_Work(WorkFromWebBindingModel model) {
|
|
|
|
|
var record = new WorkBindingModel {
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Date = model.Date,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
ExecutorId = model.ExecutorId,
|
|
|
|
|
TaskId = model.TaskId,
|
|
|
|
|
ClientList = new()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (char id in model.client_ids) {
|
|
|
|
|
if (int.TryParse(id.ToString(), out int _id)) {
|
|
|
|
|
var client = _clientLogic.ReadElement(new ClientSearchModel { Id = _id }) ?? throw new Exception("Ошибка получения данных");
|
|
|
|
|
record.ClientList.Add(client.Id, (client));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void CreateWork(WorkFromWebBindingModel model) {
|
|
|
|
|
var record = Make_New_Record_Work(model);
|
|
|
|
|
try {
|
|
|
|
|
_workLogic.Create(record);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания работы");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void UpdateWork(WorkFromWebBindingModel model) {
|
|
|
|
|
var record = Make_New_Record_Work(model);
|
|
|
|
|
try {
|
|
|
|
|
_workLogic.Update(record);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка обновления работы");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void DeleteWork(WorkBindingModel model) {
|
|
|
|
|
try {
|
|
|
|
|
_workLogic.Delete(model);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка удаления работы");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<WorkViewModel>? GetWorks(int _executorId) {
|
|
|
|
|
try {
|
|
|
|
|
return _workLogic.ReadList(new WorkSearchModel {
|
|
|
|
|
ExecutorId = _executorId
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, $"Ошибка получения работ исполнителя || id = {_executorId}");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public WorkViewModel? GetWork(int _workId) {
|
|
|
|
|
try {
|
|
|
|
|
return _workLogic.ReadElement(new WorkSearchModel { Id = _workId });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, $"Ошибка получения работы || work_id = {_workId}");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<TaskViewModel>? GetTasks() {
|
|
|
|
|
try {
|
|
|
|
|
return _taskLogic.ReadList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, $"Ошибка получения задач");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<ClientViewModel>? GetClients() {
|
|
|
|
|
try {
|
|
|
|
|
return _clientLogic.ReadList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка получения клиентов");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo Post method -- AddClientPoints
|
|
|
|
|
|
|
|
|
|
public List<int> Make_Report_Ids(string _ids) {
|
|
|
|
|
var ids = new List<int>();
|
|
|
|
|
foreach (char id in _ids) {
|
|
|
|
|
if (int.TryParse(id.ToString(), out int _id)) {
|
|
|
|
|
ids.Add(_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public byte[]? CreateWordReport(string _ids) {
|
|
|
|
|
var ids_list = Make_Report_Ids(_ids);
|
|
|
|
|
try {
|
|
|
|
|
var document = _reportLogic.SaveToWordFile(new ReportBindeingModel {
|
|
|
|
|
ids = ids_list
|
|
|
|
|
});
|
|
|
|
|
return document;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания документа");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public byte[]? CreateXlsxReport(string _ids) {
|
|
|
|
|
var ids_list = Make_Report_Ids(_ids);
|
|
|
|
|
try {
|
|
|
|
|
var document = _reportLogic.SaveToExcelFile(new ReportBindeingModel {
|
|
|
|
|
ids = ids_list
|
|
|
|
|
});
|
|
|
|
|
return document;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания документа");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void SendReportPdfMail (ReportBindeingModel model) {
|
|
|
|
|
try {
|
|
|
|
|
var _document = _reportLogic.SaveToPdfFile(model);
|
|
|
|
|
|
|
|
|
|
MemoryStream stream = new();
|
|
|
|
|
_document.Save(stream, true);
|
|
|
|
|
byte[] data = stream.ToArray();
|
|
|
|
|
|
|
|
|
|
_mailWorker.MailSendAsync(new() {
|
|
|
|
|
MailAddress = model.EmailAddress,
|
|
|
|
|
Subject = "Отчет",
|
|
|
|
|
Text = $"Отчет по работам с указания назначенных заданий за период с {model.DateFrom.ToLongDateString()} по " +
|
|
|
|
|
$"{model.DateTo.ToShortDateString()}",
|
|
|
|
|
document = data
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
_logger.LogError(ex, "Ошибка создания/отправки отчета на почту");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-04-30 21:22:23 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|