using Contracts.BindingModels; using Contracts.BusinessLogicsContracts; using Contracts.ViewModels; using DocumentFormat.OpenXml.Packaging; using GuarantorAPP.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace GuarantorAPP.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly GuarantorData _data; public HomeController(ILogger logger, GuarantorData data) { _logger = logger; _data = data; } private bool IsLoggedIn { get { return UserGuarantor.user != null; } } private int UserId { get { return UserGuarantor.user!.Id; } } [HttpPost] public JsonResult CheckLogin(string login) { try { var unique = _data.CheckLogin(login); return Json(new { isUnique = unique }); } catch (Exception) { return Json(new {isUnique = false}); } } public IActionResult IndexNonReg() { if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } public IActionResult Index() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); return View(); } [HttpGet] public IActionResult Enter() { if (!IsLoggedIn) return View(); return RedirectToAction("Index"); } [HttpPost] public void Enter(string login, string password) { try { var user = _data.Login(login, password); if (user != null) { UserGuarantor.user = user; Response.Redirect("Index"); } Response.Redirect("Enter"); } catch (Exception) { Response.Redirect("IndexNonReg"); } } [HttpGet] public IActionResult Register() { return View(); } public IActionResult Logout() { UserGuarantor.user = null; return RedirectToAction("IndexNonReg"); } [HttpPost] public void Register(string name, string login, string email, string password1, string password2) { try { if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) { Response.Redirect("Index"); } } catch (Exception) { Response.Redirect("IndexNonReg"); } } [HttpGet] public IActionResult IndexMachine() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var machines = _data.GetMachines(UserGuarantor.user!.Id); return View(machines); } catch { return RedirectToAction("IndexNonReg"); } } [HttpPost] public IActionResult IndexMachine(int id) { try { _data.DeleteMachine(id); return RedirectToAction("IndexMachine"); } catch { return RedirectToAction("IndexNonReg"); } } [HttpGet] public IActionResult CreateMachine(int id) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var workers = _data.GetWorkers(UserGuarantor.user!.Id); ViewBag.AllWorkers = workers; if (id != 0) { var value = _data.GetMachine(id); if (value != null) return View(value); } } catch { return RedirectToAction("IndexMachine"); } return View(new MachineViewModel()); } [HttpPost] public IActionResult CreateMachine(int id, string title, string country, int[] workerIds) { try { MachineBindingModel model = new MachineBindingModel(); model.Id = id; model.Title = title; model.Country = country; model.DateCreate = DateTime.Now; model.UserId = UserGuarantor.user!.Id; var workers = _data.GetWorkers(UserGuarantor.user!.Id); for (int i = 0; i < workerIds.Length; i++) { var worker = workers!.FirstOrDefault(x => x.Id == workerIds[i]); model.MachineWorker[workerIds[i]] = worker; } bool changed = false; if (model.MachineWorker.Count > 0) { if (id != 0) { changed = _data.UpdateMachine(model); } else { changed = _data.CreateMachine(model); } } if (changed) return RedirectToAction("IndexMachine"); else { ViewBag.AllWorkers = workers; return View(model); } } catch { return RedirectToAction("IndexMachine"); } } [HttpGet] public IActionResult IndexWorker() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var list = _data.GetWorkers(UserGuarantor.user!.Id); if (list != null) return View(list); } catch (Exception) { return View(new List()); ; } return View(new List()); } [HttpPost] public void IndexWorker(int id) { if (IsLoggedIn) { _data.DeleteWorker(id); } Response.Redirect("IndexWorker"); } [HttpGet] public IActionResult CreateWorker(int id) { if (!IsLoggedIn) { return RedirectToAction("IndexNonReg"); } if (id != 0) { var value = _data.GetWorker(id); if (value != null) return View(value); } return View(new WorkerViewModel()); } [HttpPost] public IActionResult CreateWorker(WorkerBindingModel model) { try { if (model.Id == 0) { model.UserId = UserId; if (_data.CreateWorker(model)) return RedirectToAction("IndexWorker"); } else { if (_data.UpdateWorker(model)) return RedirectToAction("IndexWorker"); } } catch (Exception) { return RedirectToAction("IndexWorker"); } return RedirectToAction("IndexWorker"); } [HttpGet] public IActionResult IndexWorkshop() { if (IsLoggedIn) { try { var workshops = _data.GetWorkshops(UserGuarantor.user!.Id); return View(workshops); } catch (Exception) { return RedirectToAction("IndexNonReg"); } } return RedirectToAction("IndexNonReg"); } [HttpPost] public IActionResult IndexWorkshop(int id) { try { _data.DeleteWorkshop(id); } catch (Exception) { } return RedirectToAction("IndexWorkshop"); } [HttpGet] public IActionResult CreateWorkshop(int id) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var workers = _data.GetWorkers(UserGuarantor.user!.Id); ViewBag.AllWorkers = workers; if (id != 0) { var value = _data.GetWorkshop(id); if (value != null) return View(value); } } catch (Exception) { } return View(new WorkshopViewModel()); } [HttpPost] public IActionResult CreateWorkshop(int id, string title, string address, string director, int[] workerIds) { try { WorkshopBindingModel model = new WorkshopBindingModel(); model.Id = id; model.Title = title; model.Address = address; model.Director = director; model.DateCreate = DateTime.Now; model.UserId = UserGuarantor.user!.Id; var workers = _data.GetWorkers(UserGuarantor.user!.Id); for (int i = 0; i < workerIds.Length; i++) { var worker = workers!.FirstOrDefault(x => x.Id == workerIds[i])!; model.WorkshopWorker[workerIds[i]] = (worker); } if (model.WorkshopWorker.Count == 0) return RedirectToAction("IndexWorkshop"); if (id != 0) { _data.UpdateWorkshop(model); } else { _data.CreateWorkshop(model); } } catch (InvalidOperationException ex) { ViewBag.ErrorMessage = "Такое название Цеха уже сущетсвует"; return View(); } return RedirectToAction("IndexWorkshop"); } [HttpGet] public IActionResult Privacy() { if (IsLoggedIn) return View(UserGuarantor.user); return RedirectToAction("IndexNonReg"); } [HttpPost] public IActionResult Privacy(int id, string login, string email, string password, string name) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { GuarantorBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name }; if (_data.UpdateUser(user)) { UserGuarantor.user = new GuarantorViewModel { Id = id, Login = login, Password = password, Name = name, Email = email }; } return View(user); } catch (Exception) { return RedirectToAction("IndexNonReg"); } } [HttpGet] public IActionResult MachineWorkshopTimeChoose() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); return View(); } [HttpPost] public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate) { HttpContext.Session.SetString("StartDate", startDate.ToString()); HttpContext.Session.SetString("EndDate", endDate.ToString()); return RedirectToAction("MachineWorkshopTimeReport"); } [HttpGet] public IActionResult MachineWorkshopTimeReport() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var startDateStr = HttpContext.Session.GetString("StartDate"); var endDateStr = HttpContext.Session.GetString("EndDate"); var startDate = DateTime.Parse(startDateStr); var endDate = DateTime.Parse(endDateStr).AddDays(1); var values = _data.GetTimeReport(startDate, endDate, UserId); ViewBag.StartDate = startDate; ViewBag.EndDate = endDate; return View(values); } catch { return RedirectToAction("Index"); } } [HttpPost] public void MachineWorkshopTimeMail() { try { var startDateStr = HttpContext.Session.GetString("StartDate"); var endDateStr = HttpContext.Session.GetString("EndDate"); var startDate = DateTime.Parse(startDateStr); var endDate = DateTime.Parse(endDateStr).AddDays(1); using (MemoryStream memoryStream = new MemoryStream()) { _data.SendMailReport(startDate, endDate, UserId, memoryStream); } Response.Redirect("MachineWorkshopTimeReport"); } catch { Response.Redirect("Error"); } } [HttpGet] public IActionResult WorkerProductChoose() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var workers = _data.GetWorkers(UserId); return View(workers); } catch { return RedirectToAction("Error"); } } [HttpPost] public IActionResult WorkerProductChoose(List selectedItems, string reportType) { try { string value = string.Join("/", selectedItems); HttpContext.Session.SetString("Workers", value); if (reportType.Equals("default")) return RedirectToAction("WorkerProductReport"); else if (reportType.Equals("excel")) return RedirectToAction("ExcelGenerate"); else return RedirectToAction("WordGenerate"); } catch { return RedirectToAction("Error"); } } public async Task ExcelGenerate() { try { var value = HttpContext.Session.GetString("Workers"); if (value != null) { List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); using (MemoryStream memoryStream = new MemoryStream()) { _data.SaveReportExcel(rawReports, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); var outputStream = new MemoryStream(); await memoryStream.CopyToAsync(outputStream); outputStream.Seek(0, SeekOrigin.Begin); return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportExcel.xlsx"); } } return RedirectToAction("WorkerProductChoose"); } catch { RedirectToAction("Error"); } } public async Task WordGenerate() { try { var value = HttpContext.Session.GetString("Workers"); if (value != null) { List rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); using (MemoryStream memoryStream = new MemoryStream()) { _data.SaveReportWord(rawReports, memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); var outputStream = new MemoryStream(); await memoryStream.CopyToAsync(outputStream); outputStream.Seek(0, SeekOrigin.Begin); return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ReportWord.docx"); } } return RedirectToAction("WorkerProductChoose"); } catch { return RedirectToAction("Error"); } } [HttpGet] public IActionResult WorkerProductReport() { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var value = HttpContext.Session.GetString("Workers"); if (value != null) { List rawReports = value!.Split(',').Select(x => int.Parse(x)).ToList(); var reports = _data.GetProductReports(rawReports); return View(reports); } } catch { return RedirectToAction("Error"); } return View(new List()); } public IActionResult ReportsMenu() { return View(); } [HttpGet] public IActionResult WorkshopProductionAdd(int id) { if (!IsLoggedIn) return RedirectToAction("IndexNonReg"); try { var workshop = _data.GetWorkshop(id); ViewBag.Workshop = workshop; var productions = _data.GetProductions(); return View(productions); } catch (Exception) { return RedirectToAction("IndexWorkshop"); } } [HttpPost] public IActionResult WorkshopProductionAdd(int workshopId, int productionId) { try { var workshop = _data.GetWorkshop(workshopId); if (workshop == null) return RedirectToAction("Index"); WorkshopBindingModel workshopBinding = new() { Id = workshopId, Title = workshop.Title, Address = workshop.Address, Director = workshop.Director, UserId = workshop.UserId, ProductionId = workshop.ProductionId, WorkshopWorker = workshop.WorkerWorkshops }; _data.UpdateWorkshop(workshopBinding); } catch (Exception) { } return RedirectToAction("IndexWorkshop"); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error(string ex) { return View(ex); } } }