From dd664a4ed5acc33eda866561d563f888f369d1be Mon Sep 17 00:00:00 2001 From: sardq Date: Sun, 21 Apr 2024 00:04:11 +0400 Subject: [PATCH] DatabaseImplement Models + Database.cs --- Factory/Factory.sln | 6 + .../BindingModels/WorkpieceBindingModel.cs | 2 +- .../FactoryDatabase.cs | 29 +++++ .../FactoryDatabaseImplement.csproj | 18 +++ .../FactoryDatabaseImplement/Models/Client.cs | 75 +++++++++++ .../Models/ExecutionPhase.cs | 75 +++++++++++ .../Models/MachinePlanProduction.cs | 18 +++ .../Models/PlanProduction.cs | 120 ++++++++++++++++++ .../Models/PlanProductionWorkpiece.cs | 22 ++++ .../Models/Workpiece.cs | 107 ++++++++++++++++ .../Models/WorkpieceProduct.cs | 21 +++ 11 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 Factory/FactoryDatabaseImplement/FactoryDatabase.cs create mode 100644 Factory/FactoryDatabaseImplement/FactoryDatabaseImplement.csproj create mode 100644 Factory/FactoryDatabaseImplement/Models/Client.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/ExecutionPhase.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/MachinePlanProduction.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/PlanProduction.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/Workpiece.cs create mode 100644 Factory/FactoryDatabaseImplement/Models/WorkpieceProduct.cs diff --git a/Factory/Factory.sln b/Factory/Factory.sln index 700e10b..50d71b3 100644 --- a/Factory/Factory.sln +++ b/Factory/Factory.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryContracts", "Factory EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryBuisinessLogic", "FactoryBuisinessLogic\FactoryBuisinessLogic.csproj", "{EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryDatabaseImplement", "FactoryDatabaseImplement\FactoryDatabaseImplement.csproj", "{AC09F9B2-8744-4C66-AD47-33A35613BC40}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Debug|Any CPU.Build.0 = Debug|Any CPU {EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Release|Any CPU.ActiveCfg = Release|Any CPU {EA960B3E-6BFB-4B16-9B0A-E1D603967FEC}.Release|Any CPU.Build.0 = Release|Any CPU + {AC09F9B2-8744-4C66-AD47-33A35613BC40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC09F9B2-8744-4C66-AD47-33A35613BC40}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC09F9B2-8744-4C66-AD47-33A35613BC40}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC09F9B2-8744-4C66-AD47-33A35613BC40}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Factory/FactoryContracts/BindingModels/WorkpieceBindingModel.cs b/Factory/FactoryContracts/BindingModels/WorkpieceBindingModel.cs index 88cd4ad..19a6864 100644 --- a/Factory/FactoryContracts/BindingModels/WorkpieceBindingModel.cs +++ b/Factory/FactoryContracts/BindingModels/WorkpieceBindingModel.cs @@ -12,6 +12,6 @@ namespace FactoryContracts.BindingModels public double Cost { get; set; } - //public Dictionary WorkComponents { get; set; } = new(); + public Dictionary WorkpiecesProducts { get; set; } = new(); } } \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/FactoryDatabase.cs b/Factory/FactoryDatabaseImplement/FactoryDatabase.cs new file mode 100644 index 0000000..805c503 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/FactoryDatabase.cs @@ -0,0 +1,29 @@ +using FactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FactoryDatabaseImplement +{ + public class FactoryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Database=Factory;Username=postgres; Password = postgres"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Clients { set; get; } + public virtual DbSet ExecutionPhases { set; get; } + public virtual DbSet MachinePlanProductions { set; get; } + public virtual DbSet PlanProductions { set; get; } + public virtual DbSet PlanProductionWorkpieces { set; get; } + public virtual DbSet Workpieces { set; get; } + public virtual DbSet WorkpieceProducts { set; get; } + + + + + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/FactoryDatabaseImplement.csproj b/Factory/FactoryDatabaseImplement/FactoryDatabaseImplement.csproj new file mode 100644 index 0000000..0ed8a5f --- /dev/null +++ b/Factory/FactoryDatabaseImplement/FactoryDatabaseImplement.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/Factory/FactoryDatabaseImplement/Models/Client.cs b/Factory/FactoryDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..885bbf9 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/Client.cs @@ -0,0 +1,75 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.ViewModels; +using FactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FactoryDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + + [Required] + public string Login { get; private set; } = string.Empty; + [Required] + public string Email { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + + + [ForeignKey("ClientId")] + public virtual List ExecutionPhases{ get; set; } = new(); + + [ForeignKey("ClientId")] + public virtual List PlanProductions { get; set; } = new(); + + [ForeignKey("ClientId")] + public virtual List Workpieces { get; set; } = new(); + + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + Login = model.Login, + Email= model.Email, + Password = model.Password, + }; + } + + public static Client Create(ClientViewModel model) + { + return new Client + { + Id = model.Id, + Login = model.Login, + Email = model.Email, + Password = model.Password, + }; + } + + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + Login = model.Login; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + Email = Email, + Password = Password, + }; + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/ExecutionPhase.cs b/Factory/FactoryDatabaseImplement/Models/ExecutionPhase.cs new file mode 100644 index 0000000..49cd212 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/ExecutionPhase.cs @@ -0,0 +1,75 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.ViewModels; +using FactoryDataModels.Enums; +using FactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FactoryDatabaseImplement.Models +{ + public class ExecutionPhase : IExecutionPhaseModel + { + public int Id { get; private set; } + [Required] + public int ClientId { get; private set; } + public virtual Client Client { get; private set; } = new(); + + [Required] + public string ExecutionPhaseName { get; private set; } = string.Empty; + [Required] + public string ImplementerFIO { get; private set; } = string.Empty; + [Required] + public ExecutionPhaseStatus Status { get; private set; } = ExecutionPhaseStatus.Неизвестен; + + [ForeignKey("ExecutionPhaseId")] + public virtual List PlanProductions { get; set; } = new(); + + public static ExecutionPhase? Create(ExecutionPhaseBindingModel model) + { + if (model == null) + { + return null; + } + return new ExecutionPhase() + { + Id = model.Id, + ExecutionPhaseName = model.ExecutionPhaseName, + ImplementerFIO = model.ImplementerFIO, + ClientId = model.ClientId, + Status = model.Status, + }; + } + + public static ExecutionPhase Create(ExecutionPhaseViewModel model) + { + return new ExecutionPhase + { + Id = model.Id, + ExecutionPhaseName = model.ExecutionPhaseName, + ImplementerFIO = model.ImplementerFIO, + ClientId = model.ClientId, + Status = model.Status, + }; + } + + public void Update(ExecutionPhaseBindingModel model) + { + if (model == null) + { + return; + } + ExecutionPhaseName = model.ExecutionPhaseName; + ImplementerFIO = model.ImplementerFIO; + Status = model.Status; + } + + public ExecutionPhaseViewModel GetViewModel => new() + { + Id = Id, + ExecutionPhaseName = ExecutionPhaseName, + ImplementerFIO = ImplementerFIO, + ClientId = ClientId, + Status = Status, + }; + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/MachinePlanProduction.cs b/Factory/FactoryDatabaseImplement/Models/MachinePlanProduction.cs new file mode 100644 index 0000000..1c1e91c --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/MachinePlanProduction.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace FactoryDatabaseImplement.Models +{ + public class MachinePlanProduction + { + public int Id { get; set; } + + //[Required] + //public int MachineId { get; set; } + + [Required] + public int PlanProductionId { get; set; } + public virtual PlanProduction PlanProduction { get; set; } = new(); + + //public virtual Machine Machine{ get; set; } = new(); + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs new file mode 100644 index 0000000..338d4db --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs @@ -0,0 +1,120 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.ViewModels; +using FactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FactoryDatabaseImplement.Models +{ + public class PlanProduction : IPlanProductionModel + { + public int Id { get; private set; } + + [Required] + public int ExecutionPhaseId { get; private set; } + public virtual ExecutionPhase ExecutionPhase { get; private set; } = new(); + [Required] + public int ClientId { get; private set; } + public virtual Client Client { get; private set; } = new(); + + [Required] + public string ProductionName { get; private set; } = string.Empty; + [Required] + public int Count { get; private set; } + [Required] + public DateTime Term { get; private set; } + + private Dictionary? _planProductionWorkpieces = null; + + [NotMapped] + public Dictionary PlanProductionWorkpieces + { + get + { + if (_planProductionWorkpieces == null) + { + _planProductionWorkpieces = Workpieces + .ToDictionary(recPC => recPC.WorkpieceId, recPC => (recPC.Workpiece as IWorkpieceModel, recPC.Count)); + } + return _planProductionWorkpieces; + } + } + [ForeignKey("PlanProductionId")] + public virtual List Workpieces{ get; set; } = new(); + [ForeignKey("PlanProductionId")] + public virtual List Machines { get; set; } = new(); + + public static PlanProduction Create(FactoryDatabase context, PlanProductionBindingModel model) + { + if (model == null) + { + return null; + } + return new PlanProduction() + { + Id = model.Id, + ClientId = model.ClientId, + Count = model.Count, + ExecutionPhaseId = model.ExecutionPhaseId, + ProductionName = model.ProductionName, + Term = model.Term, + Workpieces = model.PlanProductionWorkpieces.Select(x => new PlanProductionWorkpiece + { + Workpiece = context.Workpieces.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(PlanProductionBindingModel model) + { + if (model == null) + { + return; + } + Count = model.Count; + ExecutionPhaseId = model.ExecutionPhaseId; + ProductionName = model.ProductionName; + Term = model.Term; + + } + + public PlanProductionBindingModel GetViewModel => new() + { + Id = Id, + ClientId = ClientId, + ExecutionPhaseId = ExecutionPhaseId, + ProductionName = ProductionName, + Term = Term, + Count = Count, + PlanProductionWorkpieces = PlanProductionWorkpieces + }; + public void UpdateWorkpieces(FactoryDatabase context, PlanProductionBindingModel model) + { + var planProductionWorkpieces = context.PlanProductionWorkpieces.Where(rec => rec.PlanProductionId == model.Id).ToList(); + if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0) + { + context.PlanProductionWorkpieces.RemoveRange(planProductionWorkpieces.Where(rec => !model.PlanProductionWorkpieces.ContainsKey(rec.WorkpieceId))); + context.SaveChanges(); + foreach (var updateWorkpiece in planProductionWorkpieces) + { + updateWorkpiece.Count = model.PlanProductionWorkpieces[updateWorkpiece.WorkpieceId].Item2; + model.PlanProductionWorkpieces.Remove(updateWorkpiece.WorkpieceId); + } + context.SaveChanges(); + } + var plan = context.PlanProductions.First(x => x.Id == Id); + foreach (var pc in model.PlanProductionWorkpieces) + { + context.PlanProductionWorkpieces.Add(new PlanProductionWorkpiece + { + PlanProduction = plan, + Workpiece = context.Workpieces.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _planProductionWorkpieces = null; + } + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs b/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs new file mode 100644 index 0000000..2edc666 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace FactoryDatabaseImplement.Models +{ + public class PlanProductionWorkpiece + { + public int Id { get; set; } + + [Required] + public int PlanProductionId { get; set; } + + [Required] + public int WorkpieceId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual PlanProduction PlanProduction { get; set; } = new(); + + public virtual Workpiece Workpiece { get; set; } = new(); + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/Workpiece.cs b/Factory/FactoryDatabaseImplement/Models/Workpiece.cs new file mode 100644 index 0000000..5c68e7e --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/Workpiece.cs @@ -0,0 +1,107 @@ +using FactoryContracts.BindingModels; +using FactoryContracts.ViewModels; +using FactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FactoryDatabaseImplement.Models +{ + public class Workpiece : IWorkpieceModel + { + public int Id { get; set; } + + [Required] + public string WorkpieceName { get; set; } = string.Empty; + [Required] + public int ClientId { get; set; } + public virtual Client Client { get; set; } = new Client(); + [Required] + public string Material { get; set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + private Dictionary? _workpieceProducts = null; + + [NotMapped] + public Dictionary WorkpieceProducts + { + get + { + if (_workpieceProducts == null) + { + _workpieceProducts = Products + .ToDictionary(recPC => recPC.ProductId, recPC => (recPC.Product as IProductModel, recPC.Count)); + } + return _workpieceProducts; + } + } + + [ForeignKey("WorkpieceId")] + public virtual List Products { get; set; } = new(); + [ForeignKey("WorkpieceId")] + public virtual List PlanProductions { get; set; } = new(); + + public static Workpiece Create(FactoryDatabase context, WorkpieceBindingModel model) + { + return new Workpiece() + { + Id = model.Id, + WorkpieceName = model.WorkpieceName, + Cost = model.Cost, + Material = model.Material, + ClientId = model.ClientId, + Products = model.WorkpiecesProducts.Select(x => new WorkpieceProduct + { + Product = context.Products.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(WorkpieceBindingModel model) + { + WorkpieceName = model.WorkpieceName; + Cost = model.Cost; + Material = model.Material; + } + + public WorkpieceBindingModel GetViewModel => new() + { + Id = Id, + WorkpieceName = WorkpieceName, + Cost = Cost, + Material = Material, + ClientId = ClientId, + WorkpiecesProducts = WorkpieceProducts + }; + + public void UpdateProducts(FactoryDatabase context, WorkpieceBindingModel model) + { + var workpieceProducts = context.WorkpieceProducts.Where(rec => rec.WorkpieceId == model.Id).ToList(); + if (workpieceProducts != null && workpieceProducts.Count > 0) + { + context.WorkpieceProducts.RemoveRange(workpieceProducts.Where(rec => !model.WorkpiecesProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + foreach (var updateProduct in workpieceProducts) + { + updateProduct.Count = model.WorkComponents[updateProduct.ProductId].Item2; + model.WorkpiecesProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + var workpiece = context.Workpieces.First(x => x.Id == Id); + foreach (var pc in model.WorkpiecesProducts) + { + context.WorkpieceProducts.Add(new WorkpieceProduct + { + Workpiece = workpiece, + Product = context.Products.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _workpieceProducts = null; + } + } +} \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/WorkpieceProduct.cs b/Factory/FactoryDatabaseImplement/Models/WorkpieceProduct.cs new file mode 100644 index 0000000..d4c3062 --- /dev/null +++ b/Factory/FactoryDatabaseImplement/Models/WorkpieceProduct.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; + +namespace FactoryDatabaseImplement.Models +{ + public class WorkpieceProduct + { + public int Id { get; set; } + + [Required] + public int WorkpieceId { get; set; } + + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Product Product { get; set; } = new(); + + public virtual Workpiece Workpiece { get; set; } = new(); + } +} \ No newline at end of file