using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.ViewModels; using TravelAgencyWebApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using TravelAgencyContracts.BusinessLogicsContracts; using TravelAgencyContracts.SearchModels; using TravelAgencyBusinessLogic.BusinessLogics; using DocumentFormat.OpenXml.Spreadsheet; using TravelAgencyBusinessLogic.OfficePackage; using TravelAgencyBusinessLogic.MailWorker; using DocumentFormat.OpenXml.Wordprocessing; using TravelAgencyDatabaseImplement.Models; namespace TravelAgencyWebApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly IUserLogic _userLogic; private readonly IReportLogic _reportLogic; private readonly ITourLogic _tourLogic; private readonly IGuideLogic _guideLogic; private readonly IExcursionGroupLogic _excursionGroupLogic; private readonly AbstractSaveToPdf _pdfLogic; private readonly AbstractMailWorker _mailLogic; public HomeController(ILogger logger, IUserLogic userLogic, IReportLogic reportLogic, ITourLogic tourLogic, IGuideLogic guideLogic, IExcursionGroupLogic excursionGroupLogic, AbstractSaveToPdf pdfLogic, AbstractMailWorker mailLogic) { _logger = logger; _userLogic = userLogic; _reportLogic = reportLogic; _tourLogic = tourLogic; _guideLogic = guideLogic; _excursionGroupLogic = excursionGroupLogic; _pdfLogic = pdfLogic; _mailLogic = mailLogic; } public IActionResult Index() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(LoggedinUser.User); } [HttpGet] public IActionResult Privacy() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(LoggedinUser.User); } [HttpPost] public void Privacy(string userFIO, string email, string phoneNumber, string password) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (string.IsNullOrEmpty(userFIO) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } _userLogic.Update(new UserBindingModel { Id = LoggedinUser.User.Id, UserFIO = userFIO, Email = email, PhoneNumber = phoneNumber, Password = password }); LoggedinUser.User.UserFIO = userFIO; LoggedinUser.User.Email = email; LoggedinUser.User.PhoneNumber = phoneNumber; LoggedinUser.User.Password = password; Response.Redirect("Privacy"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } [HttpGet] public IActionResult Enter() { if (LoggedinUser.User != null) { throw new Exception("Вы уже авторизовались!"); } return View(); } [HttpPost] public void Enter(string email, string password) { if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } LoggedinUser.User = _userLogic.ReadElement(new UserSearchModel { Email = email, Password = password }); if (LoggedinUser.User == null) { throw new Exception("Неверный логин/пароль"); } Response.Redirect("Index"); } [HttpGet] public IActionResult Register() { if (LoggedinUser.User != null) { throw new Exception("Вы уже зарегистрировались!"); } return View(); } [HttpPost] public void Register(string fio, string email, string password, string phone) { if (string.IsNullOrEmpty(fio) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) { throw new Exception("Введены не все данные!"); } _userLogic.Create(new UserBindingModel { UserFIO = fio, Email = email, PhoneNumber = phone, Password = password }); Response.Redirect("Enter"); } public void Logout() { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } LoggedinUser.User = null; Response.Redirect("Enter"); } [HttpGet] public IActionResult ReportMenu() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpGet] public IActionResult ReportTourPlace() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); return View(); } [HttpGet] public IActionResult ReportTourPeriod() { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } return View(); } [HttpPost] public IActionResult ReportTourPeriod(DateTime dateFrom, DateTime dateTo) { if (LoggedinUser.User == null) { return Redirect("~/Home/Enter"); } if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) { throw new Exception("Введены не все данные!"); } ViewBag.DateFrom = dateFrom; ViewBag.DateTo = dateTo; return View(_reportLogic.GetTourPeriod(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo, UserId = LoggedinUser.User.Id })); } [HttpPost] public IActionResult CreateReport(List tours, string type) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (tours == null) { throw new Exception("Выберите туры!"); } if (type.Equals("docx")) { using (var stream = new MemoryStream()) { _reportLogic.SaveTourPlacesToWordFile(new ReportBindingModel { FileStream = stream, UserId = LoggedinUser.User.Id, Tours = tours }); stream.Seek(0, SeekOrigin.Begin); return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "wordfile.docx"); } } else if (type.Equals("xlsx")) { using (var stream = new MemoryStream()) { _reportLogic.SaveTourPlacesToExcelFile(new ReportBindingModel { FileStream = stream, UserId = LoggedinUser.User.Id, Tours = tours }); stream.Seek(0, SeekOrigin.Begin); return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "excelfile.xlsx"); } } ViewBag.Tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, }); return View("ReportTourPlace", tours); } [HttpPost] public void SentPdfToMail(DateTime dateFrom, DateTime dateTo) { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } if (dateFrom == DateTime.MinValue || dateTo == DateTime.MinValue) { throw new Exception("Введены не все данные!"); } using (MemoryStream memoryStream = new MemoryStream()) { SendMailReport(dateFrom, dateTo, memoryStream); } Response.Redirect("/Home/ReportTourPeriod"); } public void SendMailReport(DateTime? dateFrom, DateTime? dateTo, MemoryStream stream) { var tours = _reportLogic.GetTourPeriod(new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo, UserId = LoggedinUser.User.Id, }); if (tours == null) return; _pdfLogic.CreateDoc(new() { DateFrom = dateFrom!.Value, DateTo = dateTo!.Value, FileName = stream, Tours = tours, Title = "Отчет" }); byte[] report = stream.GetBuffer(); _mailLogic.MailSendAsync(new() { MailAddress = LoggedinUser.User.Email, Subject = "Отчет", FileName = "PdfReport.pdf", Pdf = report }); } [HttpGet] public IActionResult Diagrams() { if (LoggedinUser.User == null) { throw new Exception("Необходимо авторизоваться!"); } ViewBag.RevenuePerMonth = Newtonsoft.Json.JsonConvert.SerializeObject(getRevenues()); ViewBag.TourPopularity = Newtonsoft.Json.JsonConvert.SerializeObject(getTourPopularity()); return View(); } private Dictionary getRevenues() { var tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id, DateFrom = new DateTime(DateTime.Today.Year, 1, 1), DateTo = DateTime.Today }); var excursionGroups = _excursionGroupLogic.ReadList(new ExcursionGroupSearchModel { UserId = LoggedinUser.User.Id }); var revenuePerMonth = new Dictionary(); foreach (var tour in tours) { var month = tour.TourDate.Month; if (!revenuePerMonth.ContainsKey(month)) { revenuePerMonth[month] = 0; } foreach (var eg in excursionGroups) { if (eg.ExcursionGroupTours.ContainsKey(tour.Id)) { revenuePerMonth[month] += tour.Price * eg.ParticipantsAmount; } } } return revenuePerMonth; } private Dictionary getTourPopularity() { var tours = _tourLogic.ReadList(new TourSearchModel { UserId = LoggedinUser.User.Id }); var excursionGroups = _excursionGroupLogic.ReadList(new ExcursionGroupSearchModel { UserId = LoggedinUser.User.Id }); var participantsPerTour = new Dictionary(); foreach (var tour in tours) { if (!participantsPerTour.ContainsKey(tour.TourName)) { participantsPerTour[tour.TourName] = 0; } foreach (var eg in excursionGroups) { if (eg.ExcursionGroupTours.ContainsKey(tour.Id)) { participantsPerTour[tour.TourName] += eg.ParticipantsAmount; } } } return participantsPerTour; } } }