This commit is contained in:
Дмитрий Блохин 2024-05-29 17:12:35 +04:00
commit ed24d5e00e
8 changed files with 288 additions and 166 deletions

View File

@ -36,7 +36,7 @@ namespace DatabaseImplement.Implements
using var context = new FactoryGoWorkDatabase(); using var context = new FactoryGoWorkDatabase();
if (model.DateFrom.HasValue) if (model.DateFrom.HasValue)
return context.Machines.Where(x => x.UserId == model.UserId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList(); return context.Machines.Where(x => x.UserId == model.UserId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList();
else else
return context.Machines.Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList(); return context.Machines.Where(x => x.UserId == model.UserId).Select(x => x.GetViewModel).ToList();
} }

View File

@ -40,7 +40,7 @@ namespace DatabaseImplement.Models
public virtual List<WorkerMachine> Workers { get; set; } = new(); public virtual List<WorkerMachine> Workers { get; set; } = new();
public virtual Guarantor Guarantor { get; set; } public virtual Guarantor Guarantor { get; set; }
[ForeignKey("MachineId")] [ForeignKey("MachineId")]
public virtual List<Product> Products { get; set; } public virtual List<Product> Products { get; set; } = new();
public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context) public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context)
{ {
return new Machine() return new Machine()

View File

@ -20,19 +20,24 @@ namespace ImplementerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult IndexDetail() public IActionResult IndexDetail()
{ {
if (UserImplementer.user != null) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try
{ {
var list = _data.GetDetails(UserImplementer.user.Id); var list = _data.GetDetails(UserImplementer.user!.Id);
if (list != null) if (list != null)
return View(list); return View(list);
return View(new List<DetailViewModel>());
} }
return RedirectToAction("IndexNonReg"); catch (Exception)
{
return View(new List<DetailViewModel>()); ;
}
return View(new List<DetailViewModel>());
} }
[HttpPost] [HttpPost]
public void IndexDetail(int id) public void IndexDetail(int id)
{ {
if (UserImplementer.user != null) if (IsLoggedIn)
{ {
_data.DeleteDetail(id); _data.DeleteDetail(id);
} }
@ -41,6 +46,10 @@ namespace ImplementerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult CreateDetail(int id) public IActionResult CreateDetail(int id)
{ {
if (!IsLoggedIn)
{
return RedirectToAction("IndexNonReg", "Home");
}
if (id != 0) if (id != 0)
{ {
var value = _data.GetDetail(id); var value = _data.GetDetail(id);
@ -52,24 +61,25 @@ namespace ImplementerApp.Controllers
[HttpPost] [HttpPost]
public IActionResult CreateDetail(DetailBindingModel model) public IActionResult CreateDetail(DetailBindingModel model)
{ {
if (model.Id == 0) try
{ {
model.DateCreate = DateTime.Now; if (model.Id == 0)
model.UserId = UserImplementer.user!.Id; {
try { model.DateCreate = DateTime.Now;
model.UserId = UserId;
if (_data.CreateDetail(model)) if (_data.CreateDetail(model))
return RedirectToAction("IndexDetail"); return RedirectToAction("IndexDetail");
} catch (ArgumentException ex)
{
return RedirectToAction("Error", ex.Message);
} }
} else
else {
if (_data.UpdateDetail(model))
return RedirectToAction("IndexDetail");
}
} catch (Exception)
{ {
if (_data.UpdateDetail(model)) return RedirectToAction("IndexDetail");
return RedirectToAction("IndexDetail");
} }
return View(); return RedirectToAction("IndexDetail");
} }
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

View File

@ -25,8 +25,14 @@ namespace ImplementerApp.Controllers
[HttpPost] [HttpPost]
public JsonResult CheckLogin(string login) public JsonResult CheckLogin(string login)
{ {
var unique = _data.CheckLogin(login); try
return Json(new { isUnique = unique }); {
var unique = _data.CheckLogin(login);
return Json(new { isUnique = unique });
}
catch (Exception) {
return Json(new { isUnique = false });
}
} }
public IActionResult IndexNonReg() public IActionResult IndexNonReg()
{ {
@ -50,13 +56,19 @@ namespace ImplementerApp.Controllers
[HttpPost] [HttpPost]
public void Enter(string login, string password) public void Enter(string login, string password)
{ {
var user = _data.Login(login, password); try {
if (user != null) var user = _data.Login(login, password);
if (user != null)
{
UserImplementer.user = user;
Response.Redirect("Index");
}
Response.Redirect("Enter");
} catch (Exception)
{ {
UserImplementer.user = user; Response.Redirect("IndexNonReg");
Response.Redirect("Index");
} }
Response.Redirect("Enter");
} }
[HttpGet] [HttpGet]
public IActionResult Register() public IActionResult Register()
@ -71,9 +83,15 @@ namespace ImplementerApp.Controllers
[HttpPost] [HttpPost]
public void Register(string name, string login, string email, string password1, string password2) public void Register(string name, string login, string email, string password1, string password2)
{ {
if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 })) try
{ {
Response.Redirect("Index"); if (password1 == password2 && _data.Register(new() { Email = email, Login = login, Name = name, Password = password1 }))
{
Response.Redirect("Index");
}
} catch (Exception)
{
Response.Redirect("IndexNonReg");
} }
} }
[HttpGet] [HttpGet]
@ -88,12 +106,17 @@ namespace ImplementerApp.Controllers
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg");
ImplementerBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name }; try {
if (_data.UpdateUser(user)) ImplementerBindingModel user = new() { Id = id, Login = login, Email = email, Password = password, Name = name };
if (_data.UpdateUser(user))
{
UserImplementer.user = new ImplementerViewModel { Id = id, Login = login, Password = password, Name = name, Email = email };
}
return View(user);
} catch (Exception)
{ {
UserImplementer.user = new ImplementerViewModel { Id = id, Login = login, Password = password, Name = name, Email = email }; return RedirectToAction("IndexNonReg");
} }
return View(user);
} }
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

View File

@ -23,52 +23,70 @@ namespace ImplementerApp.Controllers
{ {
if (IsLoggedIn) if (IsLoggedIn)
{ {
var products = _data.GetProducts(UserImplementer.user!.Id); try {
return View(products); var products = _data.GetProducts(UserImplementer.user!.Id);
return View(products);
} catch (Exception)
{
return RedirectToAction("IndexNonReg", "Home");
}
} }
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg", "Home");
} }
[HttpPost] [HttpPost]
public IActionResult IndexProduct(int id) public IActionResult IndexProduct(int id)
{ {
_data.DeleteProduct(id); try
{
_data.DeleteProduct(id);
}
catch (Exception)
{ }
return RedirectToAction("IndexProduct"); return RedirectToAction("IndexProduct");
} }
[HttpGet] [HttpGet]
public IActionResult CreateProduct(int id) public IActionResult CreateProduct(int id)
{ {
var details = _data.GetDetails(UserImplementer.user!.Id); if (!IsLoggedIn)
ViewBag.AllDetails = details; return RedirectToAction("IndexNonReg", "Home");
if (id != 0) try
{ {
var value = _data.GetProduct(id); var details = _data.GetDetails(UserImplementer.user!.Id);
if (value != null) ViewBag.AllDetails = details;
return View(value); if (id != 0)
{
var value = _data.GetProduct(id);
if (value != null)
return View(value);
}
} }
catch (Exception)
{ }
return View(new ProductViewModel()); return View(new ProductViewModel());
} }
[HttpPost] [HttpPost]
public IActionResult CreateProduct(int id, string title, int[] detailIds, int[] counts) public IActionResult CreateProduct(int id, string title, int[] detailIds, int[] counts)
{ {
ProductBindingModel model = new ProductBindingModel();
model.Id = id;
model.Name = title;
model.UserId = UserImplementer.user!.Id;
var details = _data.GetDetails(UserImplementer.user!.Id);
double sum = 0;
for (int i = 0; i < detailIds.Length; i++)
{
var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
if (counts[i] <= 0)
continue;
model.ProductDetails[detailIds[i]] = (detail, counts[i]);
sum += detail.Cost * counts[i];
}
if (model.ProductDetails.Count == 0)
return RedirectToAction("IndexProduct");
model.Cost = sum;
try try
{ {
ProductBindingModel model = new ProductBindingModel();
model.Id = id;
model.Name = title;
model.UserId = UserImplementer.user!.Id;
var details = _data.GetDetails(UserImplementer.user!.Id);
double sum = 0;
for (int i = 0; i < detailIds.Length; i++)
{
var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
if (counts[i] <= 0)
continue;
model.ProductDetails[detailIds[i]] = (detail, counts[i]);
sum += detail.Cost * counts[i];
}
if (model.ProductDetails.Count == 0)
return RedirectToAction("IndexProduct");
model.Cost = sum;
if (id != 0) if (id != 0)
{ {
_data.UpdateProduct(model); _data.UpdateProduct(model);
@ -89,22 +107,29 @@ namespace ImplementerApp.Controllers
public IActionResult ProductMachineAdd(int id) public IActionResult ProductMachineAdd(int id)
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg", "Home");
var product = _data.GetProduct(id); try
ViewBag.Product = product; {
var machines = _data.GetMachines(); var product = _data.GetProduct(id);
return View(machines); ViewBag.Product = product;
var machines = _data.GetMachines();
return View(machines);
} catch (Exception)
{
return RedirectToAction("IndexProduct");
}
} }
[HttpPost] [HttpPost]
public IActionResult ProductMachineAdd(int productId, int machineId) public IActionResult ProductMachineAdd(int productId, int machineId)
{ {
if (!IsLoggedIn) try {
return RedirectToAction("IndexNonReg"); var product = _data.GetProduct(productId);
var product = _data.GetProduct(productId); if (product == null)
if (product == null) return RedirectToAction("Index");
return RedirectToAction("Index"); ProductBindingModel productBinding = new() { Id = productId, Cost = product.Cost, Name = product.Name, UserId = product.UserId, ProductDetails = product.DetailProducts, MachineId = machineId };
ProductBindingModel productBinding = new() { Id = productId, Cost = product.Cost, Name = product.Name, UserId = product.UserId, ProductDetails = product.DetailProducts, MachineId = machineId }; _data.UpdateProduct(productBinding);
_data.UpdateProduct(productBinding); } catch (Exception)
{ }
return RedirectToAction("IndexProduct"); return RedirectToAction("IndexProduct");
} }

View File

@ -19,66 +19,91 @@ namespace ImplementerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult IndexProduction() public IActionResult IndexProduction()
{ {
if (UserImplementer.user != null) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try
{ {
var productions = _data.GetProductions(UserImplementer.user.Id); var productions = _data.GetProductions(UserImplementer.user!.Id);
return View(productions); return View(productions);
} catch
{
return RedirectToAction("IndexNonReg", "Home");
} }
return RedirectToAction("IndexNonReg");
} }
[HttpPost] [HttpPost]
public IActionResult IndexProduction(int id) public IActionResult IndexProduction(int id)
{ {
_data.DeleteProduction(id); try
return RedirectToAction("IndexProduction"); {
_data.DeleteProduction(id);
return RedirectToAction("IndexProduction");
} catch
{
return RedirectToAction("IndexNonReg");
}
} }
[HttpGet] [HttpGet]
public IActionResult CreateProduction(int id) public IActionResult CreateProduction(int id)
{ {
var details = _data.GetDetails(UserImplementer.user!.Id); if (!IsLoggedIn)
ViewBag.AllDetails = details; return RedirectToAction("IndexNonReg", "Home");
if (id != 0) try
{ {
var value = _data.GetProduction(id); var details = _data.GetDetails(UserImplementer.user!.Id);
if (value != null) ViewBag.AllDetails = details;
return View(value); if (id != 0)
{
var value = _data.GetProduction(id);
if (value != null)
return View(value);
}
} catch
{
return RedirectToAction("IndexProduction");
} }
return View(new ProductionViewModel()); return View(new ProductionViewModel());
} }
[HttpPost] [HttpPost]
public IActionResult CreateProduction(int id, string title, int[] detailIds) public IActionResult CreateProduction(int id, string title, int[] detailIds)
{ {
ProductionBindingModel model = new ProductionBindingModel(); try
model.Id = id;
model.Name = title;
model.UserId = UserImplementer.user!.Id;
var details = _data.GetDetails(UserImplementer.user!.Id);
double sum = 0;
for (int i = 0; i < detailIds.Length; i++)
{ {
var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!; ProductionBindingModel model = new ProductionBindingModel();
model.ProductionDetails[detailIds[i]] = detail; model.Id = id;
sum += detail.Cost; model.Name = title;
} model.UserId = UserImplementer.user!.Id;
model.Cost = sum; var details = _data.GetDetails(UserImplementer.user!.Id);
bool changed = false; double sum = 0;
if (model.ProductionDetails.Count > 0) for (int i = 0; i < detailIds.Length; i++)
{
if (id != 0)
{ {
changed = _data.UpdateProduction(model); var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
model.ProductionDetails[detailIds[i]] = detail;
sum += detail.Cost;
} }
model.Cost = sum;
bool changed = false;
if (model.ProductionDetails.Count > 0)
{
if (id != 0)
{
changed = _data.UpdateProduction(model);
}
else
{
changed = _data.CreateProduction(model);
}
}
if (changed)
return RedirectToAction("IndexProduction");
else else
{ {
changed = _data.CreateProduction(model); ViewBag.AllDetails = details;
return View(model);
} }
} } catch
if (changed)
return RedirectToAction("IndexProduction");
else
{ {
ViewBag.AllDetails = details; return RedirectToAction("IndexProduction");
return View(model);
} }
} }

View File

@ -22,22 +22,12 @@ namespace ImplementerApp.Controllers
public IActionResult DetailTimeChoose() public IActionResult DetailTimeChoose()
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg", "Home");
return View(); return View();
} }
[HttpPost] [HttpPost]
public IActionResult SendReport(DateTime startDate, DateTime endDate)
{
return Ok();
}
[HttpPost]
public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate) public IActionResult TimeReportWeb(DateTime startDate, DateTime endDate)
{ {
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg");
HttpContext.Session.SetString("StartDate", startDate.ToString()); HttpContext.Session.SetString("StartDate", startDate.ToString());
HttpContext.Session.SetString("EndDate", endDate.ToString()); HttpContext.Session.SetString("EndDate", endDate.ToString());
@ -46,6 +36,9 @@ namespace ImplementerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult DetailTimeReport() public IActionResult DetailTimeReport()
{ {
if (!IsLoggedIn)
return RedirectToAction("IndexNonReg", "Home");
try {
var startDateStr = HttpContext.Session.GetString("StartDate"); var startDateStr = HttpContext.Session.GetString("StartDate");
var endDateStr = HttpContext.Session.GetString("EndDate"); var endDateStr = HttpContext.Session.GetString("EndDate");
var startDate = DateTime.Parse(startDateStr); var startDate = DateTime.Parse(startDateStr);
@ -57,85 +50,131 @@ namespace ImplementerApp.Controllers
ViewBag.EndDate = endDate; ViewBag.EndDate = endDate;
return View(values); return View(values);
} catch
{
return RedirectToAction("Index", "Home");
}
} }
[HttpPost] [HttpPost]
public void DetailTimeMail() public void DetailTimeMail()
{ {
var startDateStr = HttpContext.Session.GetString("StartDate"); try
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); 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("DetailTimeReport");
}
catch
{
Response.Redirect("Error");
} }
Response.Redirect("DetailTimeReport");
} }
[HttpGet] [HttpGet]
public IActionResult DetailWorkshopChoose() public IActionResult DetailWorkshopChoose()
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg", "Home");
var details = _data.GetDetails(UserId); try
return View(details); {
var details = _data.GetDetails(UserId);
return View(details);
}
catch
{
return RedirectToAction("Error");
}
} }
[HttpPost] [HttpPost]
public IActionResult DetailWorkshopChoose(List<int> selectedItems, string reportType) public IActionResult DetailWorkshopChoose(List<int> selectedItems, string reportType)
{ {
string value = string.Join("/", selectedItems); try
HttpContext.Session.SetString("Details", value); {
if (reportType.Equals("default")) string value = string.Join("/", selectedItems);
return RedirectToAction("DetailWorkshopReport"); HttpContext.Session.SetString("Details", value);
else if (reportType.Equals("excel")) if (reportType.Equals("default"))
return RedirectToAction("ExcelGenerate"); return RedirectToAction("DetailWorkshopReport");
else else if (reportType.Equals("excel"))
return RedirectToAction("WordGenerate"); return RedirectToAction("ExcelGenerate");
else
return RedirectToAction("WordGenerate");
}
catch
{
return RedirectToAction("Error");
}
} }
public async Task<IActionResult> ExcelGenerate() public async Task<IActionResult> ExcelGenerate()
{ {
var value = HttpContext.Session.GetString("Details"); try {
if (value != null) var value = HttpContext.Session.GetString("Details");
{ if (value != null)
List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
using (MemoryStream memoryStream = new MemoryStream())
{ {
_data.SaveReportExcel(rawReports, memoryStream); List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
memoryStream.Seek(0, SeekOrigin.Begin); using (MemoryStream memoryStream = new MemoryStream())
var outputStream = new MemoryStream(); {
await memoryStream.CopyToAsync(outputStream); _data.SaveReportExcel(rawReports, memoryStream);
outputStream.Seek(0, SeekOrigin.Begin); memoryStream.Seek(0, SeekOrigin.Begin);
return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ReportExcel.xlsx"); 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("DetailWorkshopChoose");
}
catch
{
return RedirectToAction("Error");
} }
return RedirectToAction("DetailWorkshopChoose");
} }
public async Task<IActionResult> WordGenerate() public async Task<IActionResult> WordGenerate()
{ {
var value = HttpContext.Session.GetString("Details"); try {
if (value != null) var value = HttpContext.Session.GetString("Details");
{ if (value != null)
List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
using (MemoryStream memoryStream = new MemoryStream())
{ {
_data.SaveReportWord(rawReports, memoryStream); List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
memoryStream.Seek(0, SeekOrigin.Begin); using (MemoryStream memoryStream = new MemoryStream())
var outputStream = new MemoryStream(); {
await memoryStream.CopyToAsync(outputStream); _data.SaveReportWord(rawReports, memoryStream);
outputStream.Seek(0, SeekOrigin.Begin); memoryStream.Seek(0, SeekOrigin.Begin);
return File(outputStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ReportWord.docx"); 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("DetailWorkshopChoose");
}
catch
{
return RedirectToAction("Error");
} }
return RedirectToAction("DetailWorkshopChoose");
} }
[HttpGet] [HttpGet]
public IActionResult DetailWorkshopReport() public IActionResult DetailWorkshopReport()
{ {
var value = HttpContext.Session.GetString("Details"); if (!IsLoggedIn)
if (value != null) return RedirectToAction("IndexNonReg", "Home");
try
{ {
List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList(); var value = HttpContext.Session.GetString("Details");
var reports = _data.GetWorkshopReports(rawReports); if (value != null)
return View(reports); {
List<int> rawReports = value!.Split('/').Select(x => int.Parse(x)).ToList();
var reports = _data.GetWorkshopReports(rawReports);
return View(reports);
}
}
catch
{
return RedirectToAction("Error");
} }
return View(new List<DetailWorkshopReportViewModel>()); return View(new List<DetailWorkshopReportViewModel>());
} }
@ -147,7 +186,7 @@ namespace ImplementerApp.Controllers
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error() public IActionResult Error()
{ {
return View(); return RedirectToAction("IndexNonReg", "Home");
} }
} }
} }