From a67e43a4c59d1904a55a8435cd53ca223c27bb07 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Wed, 3 May 2023 19:17:42 +0400 Subject: [PATCH] lab 6 hard complete --- .../SoftwareInstallation/FormMain.Designer.cs | 2 +- .../BusinessLogic/ImplementerLogic.cs | 3 +- .../BusinessLogic/OrderLogic.cs | 62 ++-- .../BusinessLogic/WorkModeling.cs | 38 +- .../20230503134657_InitDb.Designer.cs | 340 ++++++++++++++++++ ...itDatabase.cs => 20230503134657_InitDb.cs} | 6 +- .../Models/Store.cs | 4 +- .../Enums/OrderStatus.cs | 3 +- .../Controllers/ImplementerController.cs | 2 +- 9 files changed, 407 insertions(+), 53 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs rename SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/{20230428121847_InitDatabase.cs => 20230503134657_InitDb.cs} (96%) diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs index fcd09ce..15fb6ce 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs @@ -74,7 +74,7 @@ this.ИзделияToolStripMenuItem, this.КомпонентыToolStripMenuItem, this.клиентыToolStripMenuItem, - this.исполнителиToolStripMenuItem}); + this.исполнителиToolStripMenuItem, this.StoreToolStripMenuItem, this.клиентыToolStripMenuItem}); this.СправочникиToolStripMenuItem.Name = "СправочникиToolStripMenuItem"; diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ImplementerLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ImplementerLogic.cs index 63f16ed..0d8e7bc 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ImplementerLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ImplementerLogic.cs @@ -47,8 +47,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic { throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement. FIO:{FIO}.Id:{ Id}", - model.ImplementerFIO, model.Id); + _logger.LogInformation("ReadElement. FIO:{FIO}.Id:{ Id}", model.ImplementerFIO, model.Id); var element = _implementerStorage.GetElement(model); if (element == null) { diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs index f50f5e4..b26f93f 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs @@ -47,51 +47,46 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); - - if (viewModel == null) + var vmodel = _orderStorage.GetElement(new() { Id = model.Id }); + if (vmodel == null) { throw new ArgumentNullException(nameof(model)); } - - if (viewModel.Status + 1 != newStatus) + if ((int)vmodel.Status + 1 != (int)newStatus && !(vmodel.Status == OrderStatus.Ожидается && newStatus == OrderStatus.Готов)) { - _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); - return false; + throw new InvalidOperationException($"Попытка перевести заказ не в следующий статус: " + + $"Текущий статус: {vmodel.Status} \n" + + $"Планируемый статус: {newStatus} \n" + + $"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}"); } + if (newStatus == OrderStatus.Готов || newStatus == OrderStatus.Ожидается) + { + var vpackage = _packageStorage.GetElement(new() { Id = vmodel.PackageId }); + if (vpackage == null || !_storeLogic.AddPackage(vpackage, vmodel.Count)) + { + _logger.LogWarning($"Не удалось заполнить магазины изделием '{vpackage?.PackageName ?? string.Empty}' из заказа {vmodel.Id}"); + newStatus = OrderStatus.Ожидается; + } + else + { + newStatus = OrderStatus.Готов; + } + } model.Status = newStatus; - - if (model.Status == OrderStatus.Готов) - { - model.DateImplement = DateTime.Now; - - var package = _packageStorage.GetElement(new() { Id = viewModel.PackageId }); - - if (package == null) - { - throw new ArgumentNullException(nameof(package)); - } - - if (!_storeLogic.AddPackage(package, viewModel.Count)) - { - throw new Exception($"AddPackage operation failed. Store is full."); - } - } - else - { - model.DateImplement = viewModel.DateImplement; - } - - CheckModel(model, false); - + model.DateCreate = vmodel.DateCreate; + if (model.DateImplement == null) + model.DateImplement = vmodel.DateImplement; + if (vmodel.ImplementerId.HasValue) + model.ImplementerId = vmodel.ImplementerId; + model.PackageId = vmodel.PackageId; + model.Sum = vmodel.Sum; + model.Count = vmodel.Count; if (_orderStorage.Update(model) == null) { - model.Status--; _logger.LogWarning("Update operation failed"); return false; } - return true; } @@ -107,6 +102,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic public bool FinishOrder(OrderBindingModel model) { + model.DateImplement = DateTime.Now; return StatusUpdate(model, OrderStatus.Выдан); } diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs index 9c9858b..caab419 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs @@ -31,7 +31,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic return; } - var orders = _orderLogic.ReadList(new OrderSearchModel { Statuses = new() { OrderStatus.Принят, OrderStatus.Выполняется } }); + var orders = _orderLogic.ReadList(new OrderSearchModel { Statuses = new() { OrderStatus.Принят, OrderStatus.Выполняется, OrderStatus.Ожидается } }); if (orders == null || orders.Count == 0) { _logger.LogWarning("DoWork. Orders is null or empty"); @@ -54,33 +54,54 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic await Task.Run(() => { - foreach (var order in orders) + foreach (var order in orders.Where(x => x.Status == OrderStatus.Ожидается && x.ImplementerId == implementer.Id)) + { + try + { + _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = order.Id + }); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + }); + + await RunOrderInWork(implementer, orders); + + await Task.Run(() => + { + foreach (var order in orders.Where(x => x.Status == OrderStatus.Принят)) { try { _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); - _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = order.Id, ImplementerId = implementer.Id }); - Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); _orderLogic.DeliveryOrder(new OrderBindingModel { Id = order.Id }); - Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); } - catch (InvalidOperationException ex) { _logger.LogWarning(ex, "Error try get work"); } - catch (Exception ex) { _logger.LogError(ex, "Error while do work"); @@ -97,8 +118,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic return; } try - { - + { var runOrder = await Task.Run(() => allOrders.FirstOrDefault(x => x.ImplementerId == implementer.Id && x.Status == OrderStatus.Выполняется)); if (runOrder == null) { diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs new file mode 100644 index 0000000..aeb1982 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs @@ -0,0 +1,340 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SoftwareInstallationDataBaseImplement; + +#nullable disable + +namespace SoftwareInstallationDataBaseImplement.Migrations +{ + [DbContext(typeof(SoftwareInstallationDataBase))] + [Migration("20230503134657_InitDb")] + partial class InitDb + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("PackageId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Packages"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PackageId"); + + b.ToTable("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Store", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PackageMaxCount") + .HasColumnType("int"); + + b.Property("StoreAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StoreName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Stores"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.StorePackage", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PackageId") + .HasColumnType("int"); + + b.Property("StoreId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.HasIndex("StoreId"); + + b.ToTable("StorePackages"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.PackageComponent", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Component", "Component") + .WithMany("PackageComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("Components") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.StorePackage", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Package", "Package") + .WithMany("StorePackages") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Store", "Store") + .WithMany("Packages") + .HasForeignKey("StoreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + + b.Navigation("Store"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Component", b => + { + b.Navigation("PackageComponents"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Package", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("StorePackages"); + }); + + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Store", b => + { + b.Navigation("Packages"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230428121847_InitDatabase.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs similarity index 96% rename from SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230428121847_InitDatabase.cs rename to SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs index 7d3bf84..5a9be04 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230428121847_InitDatabase.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs @@ -6,11 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace SoftwareInstallationDataBaseImplement.Migrations { /// -<<<<<<<< HEAD:SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230428121847_InitDatabase.cs - public partial class InitDatabase : Migration -======== - public partial class CreateBD : Migration ->>>>>>>> LabRab_6:SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230410192808_CreateBD.cs + public partial class InitDb : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Store.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Store.cs index 991dff2..16c5354 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Store.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Store.cs @@ -29,8 +29,10 @@ namespace SoftwareInstallationDataBaseImplement.Models { if (_storePackages == null) { + using var context = new SoftwareInstallationDataBase(); _storePackages = Packages - .ToDictionary(recPC => recPC.PackageId, recPC => (recPC.Package as IPackageModel, recPC.Count)); + .ToDictionary(x => x.PackageId, x => (context.Packages + .FirstOrDefault(y => y.Id == x.PackageId)! as IPackageModel, x.Count)); } return _storePackages; } diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/Enums/OrderStatus.cs b/SoftwareInstallation/SoftwareInstallationDataModels/Enums/OrderStatus.cs index 7cf4108..19ab735 100644 --- a/SoftwareInstallation/SoftwareInstallationDataModels/Enums/OrderStatus.cs +++ b/SoftwareInstallation/SoftwareInstallationDataModels/Enums/OrderStatus.cs @@ -6,6 +6,7 @@ Принят = 0, Выполняется = 1, Готов = 2, - Выдан = 3 + Выдан = 3, + Ожидается = 4 } } diff --git a/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ImplementerController.cs b/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ImplementerController.cs index f1f2993..0c43146 100644 --- a/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ImplementerController.cs +++ b/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ImplementerController.cs @@ -93,7 +93,7 @@ namespace SoftwareInstallationRestApi.Controllers { try { - _order.FinishOrder(model); + _order.DeliveryOrder(model); } catch (Exception ex) {