ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceWebApp/Controllers/ReportController.cs

138 lines
4.5 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 CarServiceBusinessLogic.MailWorker;
using CarServiceContracts.BindingModels;
using CarServiceContracts.BusinessLogicsContracts;
using CarServiceWebApp.Models;
using log4net;
using Microsoft.AspNetCore.Mvc;
namespace CarServiceWebApp.Controllers
{
public class ReportController : Controller
{
private readonly IReportLogic _reportLogic;
private readonly IWorkLogic _workLogic;
private readonly AbstractMailWorker _mailWorker;
private static List<int> SelectedWorks = new();
private static ReportBindingModel PaymentsModel = new()
{
FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf"
};
public ReportController(IReportLogic reportLogic, IWorkLogic workLogic, AbstractMailWorker mailWorker)
{
_reportLogic = reportLogic;
_workLogic = workLogic;
_mailWorker = mailWorker;
}
public IActionResult Index()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
return View();
}
public IActionResult ReportRequestsByWorks()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
var requestsByWorks = _reportLogic.GetRequestsByWorks(new() { SelectedWorks = SelectedWorks } );
ViewBag.RequestsByWorks = requestsByWorks;
return View();
}
[HttpGet]
public IActionResult WorksSelection()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
var works = _workLogic.ReadList(new() { Id = CurrentUser.UserId});
ViewBag.Works = works;
return View();
}
[HttpPost]
public IActionResult WorksSelection(List<int> selwor)
{
SelectedWorks = selwor;
return Redirect("~/Report/ReportRequestsByWorks");
}
[HttpGet]
public IActionResult DateSelection()
{
if (CurrentUser.UserId < 1)
{
return Redirect("~/Home/Enter");
}
return View();
}
[HttpPost]
public IActionResult DateSelection(DateTime dateFrom, DateTime dateTo)
{
PaymentsModel.DateFrom = dateFrom;
PaymentsModel.DateTo = dateTo;
return Redirect("~/Report/ReportPayments");
}
public IActionResult ReportPayments()
{
var payments = _reportLogic.GetPayments(PaymentsModel);
ViewBag.Payments = payments;
return View();
}
public IActionResult SaveToWord()
{
_reportLogic.SaveRequestsToWordFile(new ReportBindingModel { SelectedWorks = SelectedWorks, FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportWord.docx" });
return Redirect("~/Report/DownLoadWord");
}
public IActionResult DownLoadWord()
{
string filePath = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportWord.docx";
string fileName = "Отчет.docx";
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/force-download", fileName);
}
public IActionResult SaveToExcel()
{
_reportLogic.SaveRequestsToExcelFile(new ReportBindingModel { SelectedWorks = SelectedWorks, FileName = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportExcel.xlsx" });
return Redirect("~/Report/DownLoadExcel");
}
public IActionResult DownLoadExcel()
{
string filePath = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportExcel.xlsx";
string fileName = "Отчет.xlsx";
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/force-download", fileName);
}
public IActionResult SaveToPdf()
{
_reportLogic.SavePaymentsToPdfFile(PaymentsModel);
return Redirect("~/Report/DownLoadPdf");
}
public IActionResult DownLoadPdf()
{
string filePath = "C:\\Users\\igors\\source\\repos\\ISEbd-21_Melnikov_I.O._CarService\\CarService\\CarServiceWebApp\\Files\\ReportPdf.pdf";
string fileName = "Отчет.pdf";
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/force-download", fileName);
}
public IActionResult Mail() => View();
[HttpPost]
public IActionResult Mail(string MailAddress)
{
_reportLogic.SavePaymentsToPdfFile(PaymentsModel);
_mailWorker.MailSend(new() { MailAddress = MailAddress, Subject = $"Отчет по оплатам с {(PaymentsModel.DateFrom ?? new DateTime()).ToShortDateString()} по {(PaymentsModel.DateTo ?? new DateTime()).ToShortDateString()}" });
return Redirect("~/Report/Index");
}
}
}