вроде полная реализация круд

This commit is contained in:
sardq 2024-05-28 23:02:22 +04:00
parent 0409356c5e
commit 303d20af01
7 changed files with 120 additions and 73 deletions

View File

@ -4,6 +4,7 @@ using FactoryContracts.SearchModels;
using FactoryContracts.StoragesContracts; using FactoryContracts.StoragesContracts;
using FactoryContracts.ViewModels; using FactoryContracts.ViewModels;
using FactoryDatabaseImplement.Models; using FactoryDatabaseImplement.Models;
using System.Reflection.PortableExecutable;
namespace FactoryDatabaseImplement.Implements namespace FactoryDatabaseImplement.Implements
{ {
@ -97,19 +98,30 @@ namespace FactoryDatabaseImplement.Implements
public PlanProductionViewModel? Update(PlanProductionBindingModel model) public PlanProductionViewModel? Update(PlanProductionBindingModel model)
{ {
using var context = new FactoryDatabase(); using var context = new FactoryDatabase();
var order = context.PlanProductions using var transaction = context.Database.BeginTransaction();
.Include(x => x.Client) try
.Include(x => x.Workpieces)
.ThenInclude(x => x.Workpiece)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{ {
return null; var plan = context.PlanProductions
} .Include(x => x.Client)
order.Update(model); .Include(x => x.Workpieces)
context.SaveChanges(); .ThenInclude(x => x.Workpiece)
return order.GetViewModel; .FirstOrDefault(x => x.Id == model.Id);
} if (plan == null)
{
return null;
}
plan.Update(model);
context.SaveChanges();
plan.UpdateWorkpieces(context, model);
transaction.Commit();
return plan.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
} }
} }

View File

@ -84,11 +84,12 @@ namespace FactoryDatabaseImplement.Models
}; };
public void UpdateWorkpieces(FactoryDatabase context, PlanProductionBindingModel model) public void UpdateWorkpieces(FactoryDatabase context, PlanProductionBindingModel model)
{ {
var planProductionWorkpieces = context.PlanProductionWorkpieces.Where(rec => rec.PlanProductionId == model.Id).ToList(); var planProductionWorkpieces = context.PlanProductionWorkpieces.Where(rec => rec.PlanProductionId == model.Id).ToList();
if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0) if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0 && model.PlanProductionWorkpieces.Count > 0)
{ {
context.PlanProductionWorkpieces.RemoveRange(planProductionWorkpieces.Where(rec => !model.PlanProductionWorkpieces.ContainsKey(rec.WorkpieceId))); context.PlanProductionWorkpieces.RemoveRange(planProductionWorkpieces.Where(rec => !model.PlanProductionWorkpieces.ContainsKey(rec.WorkpieceId)));
context.SaveChanges(); context.SaveChanges();
foreach (var updateWorkpiece in planProductionWorkpieces) foreach (var updateWorkpiece in planProductionWorkpieces)
{ {
updateWorkpiece.Count = model.PlanProductionWorkpieces[updateWorkpiece.WorkpieceId].Item2; updateWorkpiece.Count = model.PlanProductionWorkpieces[updateWorkpiece.WorkpieceId].Item2;

View File

@ -15,8 +15,8 @@ namespace FactoryDatabaseImplement.Models
[Required] [Required]
public int Count { get; set; } public int Count { get; set; }
public virtual PlanProduction PlanProduction { get; set; } = new(); public virtual PlanProduction PlanProduction { get; set; }
public virtual Workpiece Workpiece { get; set; } = new(); public virtual Workpiece Workpiece { get; set; }
} }
} }

View File

@ -155,6 +155,8 @@ namespace FactoryWorkerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult PlanProduction(int id) public IActionResult PlanProduction(int id)
{ {
var workpieces = _logic.GetWorkpieces(Client.user!.Id);
ViewBag.AllWorkpieces = workpieces;
if (id != 0) if (id != 0)
{ {
var value = _logic.GetPlanProduction(id); var value = _logic.GetPlanProduction(id);
@ -186,27 +188,39 @@ namespace FactoryWorkerApp.Controllers
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("Index"); return RedirectToAction("Index");
var plan = _logic.GetPlanProduction(id); ViewBag.PlanProductions = _logic.GetPlanProductions(Client.user!.Id);
ViewBag.PlanProduction = plan; ViewBag.Workpieces = _logic.GetWorkpieces(Client.user!.Id);
var workpieces = _logic.GetWorkpieces(id); return View();
return View(workpieces);
} }
[HttpPost] [HttpPost]
public IActionResult ConnectionPlanProductionWorkpiece(int planProductionId, int workpieceId) public void ConnectionPlanProductionWorkpiece(int planId, int workpieceId, int count)
{ {
if (!IsLoggedIn) if (!IsLoggedIn)
return RedirectToAction("Index"); Response.Redirect("Index");
var plan = _logic.GetPlanProduction(planProductionId); PlanProductionViewModel planProduction = _logic.GetPlanProduction(planId)!;
if (plan == null) WorkpieceViewModel workpiece = _logic.GetWorkpiece(workpieceId)!;
return RedirectToAction("Index"); if(planProduction.PlanProductionWorkpieces.ContainsKey(workpieceId))
PlanProductionBindingModel productBinding = new() { Id = planProductionId, Count = plan.Count, Deadline = plan.Deadline, ClientId= plan.ClientId, ProductionName= plan.ProductionName, PlanProductionWorkpieces = plan.PlanProductionWorkpieces}; planProduction.PlanProductionWorkpieces[workpieceId] = (workpiece, planProduction.PlanProductionWorkpieces[workpieceId].Item2 + count);
//productBinding.PlanProductionWorkpieces.Add(_logic.GetWorkpiece(workpieceId)); else
_logic.UpdatePlanProduction(productBinding); planProduction.PlanProductionWorkpieces.Add(workpieceId, (workpiece, count));
return RedirectToAction("Index"); _logic.UpdatePlanProduction(
new PlanProductionBindingModel
{
Id = planId,
ClientId = Client.user!.Id,
Count = planProduction.Count,
Deadline = planProduction.Deadline,
PlanProductionWorkpieces = planProduction.PlanProductionWorkpieces,
ProductionName = planProduction.ProductionName,
}
);
} Response.Redirect("Index");
[HttpGet]
}
[HttpGet]
public IActionResult Reports() public IActionResult Reports()
{ {
return View(); return View();
@ -304,7 +318,6 @@ namespace FactoryWorkerApp.Controllers
return View(new ExecutionPhaseViewModel()); return View(new ExecutionPhaseViewModel());
} }
[HttpPost] [HttpPost]
// доделать
public IActionResult ExecutionPhase(int id, string name, ExecutionPhaseStatus status, string impFIO, int planId) public IActionResult ExecutionPhase(int id, string name, ExecutionPhaseStatus status, string impFIO, int planId)
{ {
ExecutionPhaseBindingModel model = new ExecutionPhaseBindingModel(); ExecutionPhaseBindingModel model = new ExecutionPhaseBindingModel();

View File

@ -1,7 +1,6 @@
@using FactoryDataModels.Enums @{
@{
ViewData["Title"] = "Connect plan and workpieces"; ViewData["Title"] = "Connect plan and workpieces";
} }
<div class="text-center"> <div class="text-center">
<h3 class="display-4">Привязка заготовок к плану производства:</h3> <h3 class="display-4">Привязка заготовок к плану производства:</h3>
@ -9,41 +8,35 @@
<form method="post"> <form method="post">
<div class="row"> <div class="row">
<div class="col-4">План производства:</div> <div class="col-4">План производства:</div>
<div class="col-8"> <div class="col-8" >
<select id="planproduction" name="planproduction" class="form-control"></select> <select name="planId" id="PlanProductionSelect" class="form-control">
<option value="">Выберите план</option>
@foreach (var planProduction in ViewBag.PlanProductions)
{
<option name ="planId" value="@planProduction.Id">@planProduction.ProductionName</option>
}
</select>
</div> </div>
</div> </div>
<h3>Список заготовок</h3> <div class="row">
<div class="container"> <div class="col-4">Заготовка:</div>
<div>Заготовки</div> <div class="col-8" >
<div class="table-responsive-lg"> <select name ="workpieceId" id="WorkpieceSelect" class="form-control">
<table id="detailsTable" class="display"> <option value="">Выберите заготовку</option>
<thead> @foreach (var workpiece in ViewBag.Workpieces)
<tr> {
<th>Выбор </th> <option name="workpieceId" value="@workpiece.Id">@workpiece.WorkpieceName</option>
<th>Название </th> }
<th>Количество</th> </select>
</tr> </div>
</thead> </div>
<tbody> <div class="row mb-5">
@foreach (var workpiece in Model) <div class="col-4">Количество заготовок:</div>
{ <div class="col-8">
<tr> <input type="number" name="count" id="count" />
<td> </div>
<input type="checkbox" name="workpieces" value="@workpiece.Id" /> </div>
</td> <div class="col-4">
<td>@workpiece.WorkpieceName</td> <input type="submit" value="Добавить" class="btn btn-success" />
<td> </div>
<input type="number" name="count" value="0" min="0" class="form-control" />
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="btn btn-primary" /></div>
</div>
</form> </form>

View File

@ -2,12 +2,40 @@
@model PlanProductionViewModel; @model PlanProductionViewModel;
@{ @{
ViewData["Title"] = "Plan Production"; ViewData["Title"] = "Plan Production";
ViewBag.Workpieces = Model.PlanProductionWorkpieces;
} }
<div class="text-center"> <div class="text-center">
<h2 class="display-4">План производства</h2> <h2 class="display-4">План производства</h2>
</div> </div>
<form method="post"> <form method="post">
<div class="container">
<h2>Заготовки</h2>
<div class="table-responsive-lg">
<table id="workpiecesTable" class="display">
<thead>
<tr>
<th>Название</th>
<th>Количество</th>
</tr>
</thead>
<tbody>
@foreach (var workpiece in ViewBag.Workpieces)
{
<tr data-workpiece-id="@workpiece.Value.Item1.Id">
<td>
<input type="hidden" name="WorkpieceIds" value="@workpiece.Key" />
@workpiece.Value.Item1.WorkpieceName
</td>
<td>
<input type="text" name="counts" value="@workpiece.Value.Item2" min="0" class="form-control product-count" readonly />
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div class="row"> <div class="row">
<div class="col-4">Название:</div> <div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="productionname" id="productionname" value="@Model.ProductionName" /></div> <div class="col-8"><input type="text" name="productionname" id="productionname" value="@Model.ProductionName" /></div>
@ -24,4 +52,4 @@
<div class="col-8"></div> <div class="col-8"></div>
<div class="col-4"><input type="submit" value="Создать" class="btn btn-primary" /></div> <div class="col-4"><input type="submit" value="Создать" class="btn btn-primary" /></div>
</div> </div>
</form> </form>

View File

@ -46,7 +46,7 @@
<option value="@product.Id">@product.ProductName</option> <option value="@product.Id">@product.ProductName</option>
} }
</select> </select>
<button type="button" id="addProduct" class="btn btn-secondary">Добавить деталь</button> <button type="button" id="addProduct" class="btn btn-secondary">Добавить изделие</button>
</div> </div>
<div class="row"> <div class="row">
<div class="col-4">Название:</div> <div class="col-4">Название:</div>