diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/ImplementerLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/ImplementerLogic.cs new file mode 100644 index 0000000..df4119f --- /dev/null +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/ImplementerLogic.cs @@ -0,0 +1,128 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkBusinessLogic.BusinessLogics +{ + public class ImplementerLogic : IImplementerLogic + { + private readonly ILogger _logger; + private readonly IImplementerStorage _implementerStorage; + + public ImplementerLogic(ILogger logger, IImplementerStorage implementerStorage) + { + _logger = logger; + _implementerStorage = implementerStorage; + } + + public List? ReadList(ImplementerSearchModel? model) + { + _logger.LogInformation("ReadList. ImplementerFIO:{ImplementerFIO}.Password:{Password}.Id:{ Id}", model?.ImplementerFIO, model?.Password?.Length, model?.Id); + var list = model == null ? _implementerStorage.GetFullList() : _implementerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ImplementerViewModel? ReadElement(ImplementerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ImplementerFIO:{ImplementerFIO}.Password:{Password}.Id:{ Id}", model?.ImplementerFIO, model?.Password?.Length, model?.Id); + var element = _implementerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ImplementerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_implementerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ImplementerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + throw new ArgumentNullException("Нет ФИО исполнителя", nameof(model.ImplementerFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля исполнителя", nameof(model.Password)); + } + if (model.WorkExperience < 0) + { + throw new ArgumentNullException("Стаж должен быть больше 0", nameof(model.WorkExperience)); + } + if (model.Qualification < 0) + { + throw new ArgumentNullException("Квалификация должна быть положительной", nameof(model.Qualification)); + } + _logger.LogInformation("Implementer. ImplementerFIO:{ImplementerFIO}.Password:{Password}.WorkExperience:{WorkExperience}.Qualification:{Qualification}.Id: { Id}", + model.ImplementerFIO, model.Password, model.WorkExperience, model.Qualification, model.Id); + var element = _implementerStorage.GetElement(new ImplementerSearchModel + { + ImplementerFIO = model.ImplementerFIO + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Исполнитель с таким ФИО уже есть"); + } + } + } +} diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/OrderLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/OrderLogic.cs index df5741a..061a54a 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,14 +17,33 @@ namespace RenovationWorkBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + static readonly object _locker = new object(); public OrderLogic(ILogger logger, IOrderStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id); + var element = _orderStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } public List? ReadList(OrderSearchModel? model) { - _logger.LogInformation("ReadList. OrderId:{Id}", model?.Id); + _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { @@ -50,9 +69,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogics public bool TakeOrderInWork(OrderBindingModel model) { - return ChangeStatus(model, OrderStatus.Выполняется); + lock (_locker) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } } - public bool FinishOrder(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Готов); @@ -98,19 +119,26 @@ namespace RenovationWorkBusinessLogic.BusinessLogics }); if (element == null) { - throw new ArgumentNullException(nameof(element)); + throw new InvalidOperationException(nameof(element)); } model.DateCreate = element.DateCreate; + model.ClientId = element.ClientId; model.RepairId = element.RepairId; model.DateImplement = element.DateImplement; + if (!model.ImplementerId.HasValue) + { + model.ImplementerId = element.ImplementerId; + } model.Status = element.Status; model.Count = element.Count; model.Sum = element.Sum; if (requiredStatus - model.Status == 1) { model.Status = requiredStatus; - if (model.Status == OrderStatus.Выдан) + if (model.Status == OrderStatus.Готов) + { model.DateImplement = DateTime.Now; + } if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); @@ -119,7 +147,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogics return true; } _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); - throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); + throw new InvalidOperationException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); } } } diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/WorkModeling.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/WorkModeling.cs new file mode 100644 index 0000000..8277e32 --- /dev/null +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogics/WorkModeling.cs @@ -0,0 +1,139 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkBusinessLogic.BusinessLogics +{ + public class WorkModeling : IWorkProcess + { + private readonly ILogger _logger; + + private readonly Random _rnd; + + private IOrderLogic? _orderLogic; + + public WorkModeling(ILogger logger) + { + _logger = logger; + _rnd = new Random(1000); + } + + public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic) + { + _orderLogic = orderLogic; + var implementers = implementerLogic.ReadList(null); + if (implementers == null) + { + _logger.LogWarning("DoWork. Implementers is null"); + return; + } + var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); + if (orders == null || orders.Count == 0) + { + _logger.LogWarning("DoWork. Orders is null or empty"); + return; + } + _logger.LogDebug("DoWork for {Count} orders", orders.Count); + foreach (var implementer in implementers) + { + Task.Run(() => WorkerWorkAsync(implementer, orders)); + } + } + + private async Task WorkerWorkAsync(ImplementerViewModel implementer, List orders) + { + if (_orderLogic == null || implementer == null) + { + return; + } + await RunOrderInWork(implementer); + + await Task.Run(() => + { + foreach (var order in orders) + { + 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.FinishOrder(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)); + } + }); + } + + private async Task RunOrderInWork(ImplementerViewModel implementer) + { + if (_orderLogic == null || implementer == null) + { + return; + } + try + { + var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel + { + ImplementerId = implementer.Id, + Status = OrderStatus.Выполняется + })); + if (runOrder == null) + { + return; + } + + _logger.LogDebug("DoWork. Worker {Id} back to order {Order}", implementer.Id, runOrder.Id); + // доделываем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = runOrder.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"); + throw; + } + } + } +} diff --git a/RenovationWork/RenovationWorkContracts/BindingModels/ImplementerBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..c46ff46 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkDataModels.Models; + +namespace RenovationWorkContracts.BindingModels +{ + public class ImplementerBindingModel : IImplementerModel + { + public int Id { get; set; } + public string ImplementerFIO { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int WorkExperience { get; set; } + public int Qualification { get; set; } + } +} diff --git a/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs index f96158f..fc790cf 100644 --- a/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs +++ b/RenovationWork/RenovationWorkContracts/BindingModels/OrderBindingModel.cs @@ -13,6 +13,7 @@ namespace RenovationWorkContracts.BindingModels public int Id { get; set; } public int RepairId { get; set; } public int ClientId { get; set; } + public int? ImplementerId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IImplementerLogic.cs b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..38e3573 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,20 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.BusinessLogicsContracts +{ + public interface IImplementerLogic + { + List? ReadList(ImplementerSearchModel? model); + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + bool Create(ImplementerBindingModel model); + bool Update(ImplementerBindingModel model); + bool Delete(ImplementerBindingModel model); + } +} diff --git a/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IOrderLogic.cs b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IOrderLogic.cs index f289da4..8c32da3 100644 --- a/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -16,5 +16,6 @@ namespace RenovationWorkContracts.BusinessLogicsContracts bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); bool DeliveryOrder(OrderBindingModel model); + OrderViewModel? ReadElement(OrderSearchModel model); } } diff --git a/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IWorkProcess.cs b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IWorkProcess.cs new file mode 100644 index 0000000..be8733a --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IWorkProcess.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.BusinessLogicsContracts +{ + public interface IWorkProcess + { + void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic); + } +} diff --git a/RenovationWork/RenovationWorkContracts/SearchModels/ImplementerSearchModel.cs b/RenovationWork/RenovationWorkContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..35730fd --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } + public string? Password { get; set; } + } +} diff --git a/RenovationWork/RenovationWorkContracts/SearchModels/OrderSearchModel.cs b/RenovationWork/RenovationWorkContracts/SearchModels/OrderSearchModel.cs index 6520241..24b401b 100644 --- a/RenovationWork/RenovationWorkContracts/SearchModels/OrderSearchModel.cs +++ b/RenovationWork/RenovationWorkContracts/SearchModels/OrderSearchModel.cs @@ -1,4 +1,5 @@ -using System; +using RenovationWorkDataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,6 +11,8 @@ namespace RenovationWorkContracts.SearchModels { public int? Id { get; set; } public int? ClientId { get; set; } + public OrderStatus? Status { get; set; } + public int? ImplementerId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/RenovationWork/RenovationWorkContracts/StoragesContracts/IImplementerStorage.cs b/RenovationWork/RenovationWorkContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..f729d1b --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,21 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.StoragesContracts +{ + public interface IImplementerStorage + { + List GetFullList(); + List GetFilteredList(ImplementerSearchModel model); + ImplementerViewModel? GetElement(ImplementerSearchModel model); + ImplementerViewModel? Insert(ImplementerBindingModel model); + ImplementerViewModel? Update(ImplementerBindingModel model); + ImplementerViewModel? Delete(ImplementerBindingModel model); + } +} diff --git a/RenovationWork/RenovationWorkContracts/ViewModels/ImplementerViewModel.cs b/RenovationWork/RenovationWorkContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..a854d63 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkDataModels.Models; + +namespace RenovationWorkContracts.ViewModels +{ + public class ImplementerViewModel : IImplementerModel + { + public int Id { get; set; } + + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + + [DisplayName("Стаж работы")] + public int WorkExperience { get; set; } + + [DisplayName("Квалификация")] + public int Qualification { get; set; } + } +} diff --git a/RenovationWork/RenovationWorkContracts/ViewModels/OrderViewModel.cs b/RenovationWork/RenovationWorkContracts/ViewModels/OrderViewModel.cs index 82e02cd..9f8ff81 100644 --- a/RenovationWork/RenovationWorkContracts/ViewModels/OrderViewModel.cs +++ b/RenovationWork/RenovationWorkContracts/ViewModels/OrderViewModel.cs @@ -13,6 +13,9 @@ namespace RenovationWorkContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } + public int? ImplementerId { get; set; } + [DisplayName("Исполнитель")] + public string? ImplementerFIO { get; set; } = null; public int ClientId { get; set; } [DisplayName("ФИО клиента")] diff --git a/RenovationWork/RenovationWorkDataModels/Models/IImplementerModel.cs b/RenovationWork/RenovationWorkDataModels/Models/IImplementerModel.cs new file mode 100644 index 0000000..a8f1882 --- /dev/null +++ b/RenovationWork/RenovationWorkDataModels/Models/IImplementerModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDataModels.Models +{ + public interface IImplementerModel : IId + { + string ImplementerFIO { get; } + + string Password { get; } + + int WorkExperience { get; } + + int Qualification { get; } + } +} diff --git a/RenovationWork/RenovationWorkDataModels/Models/IOrderModel.cs b/RenovationWork/RenovationWorkDataModels/Models/IOrderModel.cs index ada8324..d9f83f5 100644 --- a/RenovationWork/RenovationWorkDataModels/Models/IOrderModel.cs +++ b/RenovationWork/RenovationWorkDataModels/Models/IOrderModel.cs @@ -10,6 +10,7 @@ namespace RenovationWorkDataModels.Models public interface IOrderModel : IId { int RepairId { get; } + int? ImplementerId { get; } int ClientId { get; } int Count { get; } double Sum { get; } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/ImplementerStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..b561b8f --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,84 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return new(); + } + using var context = new RenovationWorkDatabase(); + return context.Implementers.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)).Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) + { + return null; + } + using var context = new RenovationWorkDatabase(); + return context.Implementers.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO && (!string.IsNullOrEmpty(model.Password) ? x.Password == model.Password : true)) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + using var context = new RenovationWorkDatabase(); + context.Implementers.Add(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (implementer == null) + { + return null; + } + implementer.Update(model); + context.SaveChanges(); + return implementer.GetViewModel; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var implementer = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id); + if (implementer != null) + { + context.Implementers.Remove(implementer); + context.SaveChanges(); + return implementer.GetViewModel; + } + return null; + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs index dd86f9b..9f4b5cb 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -17,31 +17,33 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFullList() { using var context = new RenovationWorkDatabase(); - return context.Orders.Include(x => x.Repair).Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(y => y.Implementer).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) { using var context = new RenovationWorkDatabase(); - if (model.DateFrom.HasValue) + if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue && !model.Status.HasValue) { - return context.Orders.Include(x => x.Repair).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); + return new(); } - if (model.ClientId.HasValue) - { - return context.Orders.Include(x => x.Repair).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); - } - return context.Orders.Include(x => x.Repair).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(x => x.Implementer).Where(x => + (model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) || + (model.ClientId.HasValue && x.ClientId == model.ClientId) || + (model.Status.HasValue && x.Status == model.Status)).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && (!model.ImplementerId.HasValue || !model.Status.HasValue)) { return new(); } using var context = new RenovationWorkDatabase(); - return context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id) || + (model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && x.Status == model.Status)) + ?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) @@ -65,7 +67,7 @@ namespace RenovationWorkDatabaseImplement.Implements { return null; } - order.Update(model); + order.Update(context, model); context.SaveChanges(); return order.GetViewModel; } diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.Designer.cs new file mode 100644 index 0000000..fd4a7bc --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.Designer.cs @@ -0,0 +1,257 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + [Migration("20240515110134_CreateImplementers")] + partial class CreateImplementers + { + /// + 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("RenovationWorkDatabaseImplement.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("RenovationWorkDatabaseImplement.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("RenovationWorkDatabaseImplement.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("RenovationWorkDatabaseImplement.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("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Order") + .HasForeignKey("ImplementerId"); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Order"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.cs new file mode 100644 index 0000000..c76b659 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20240515110134_CreateImplementers.cs @@ -0,0 +1,67 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + /// + public partial class CreateImplementers : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ImplementerId", + table: "Orders", + type: "int", + nullable: true); + + migrationBuilder.CreateTable( + name: "Implementers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ImplementerFIO = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + WorkExperience = table.Column(type: "int", nullable: false), + Qualification = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Implementers", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ImplementerId", + table: "Orders", + column: "ImplementerId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + table: "Orders", + column: "ImplementerId", + principalTable: "Implementers", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Implementers"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ImplementerId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ImplementerId", + table: "Orders"); + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs index 2a44ce3..f7b1ded 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs @@ -67,6 +67,33 @@ namespace RenovationWorkDatabaseImplement.Migrations b.ToTable("Components"); }); + modelBuilder.Entity("RenovationWorkDatabaseImplement.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("RenovationWorkDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -87,6 +114,9 @@ namespace RenovationWorkDatabaseImplement.Migrations b.Property("DateImplement") .HasColumnType("datetime2"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("RepairId") .HasColumnType("int"); @@ -100,6 +130,8 @@ namespace RenovationWorkDatabaseImplement.Migrations b.HasIndex("ClientId"); + b.HasIndex("ImplementerId"); + b.HasIndex("RepairId"); b.ToTable("Orders"); @@ -159,6 +191,10 @@ namespace RenovationWorkDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("RenovationWorkDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Order") + .HasForeignKey("ImplementerId"); + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") .WithMany("Orders") .HasForeignKey("RepairId") @@ -167,6 +203,8 @@ namespace RenovationWorkDatabaseImplement.Migrations b.Navigation("Client"); + b.Navigation("Implementer"); + b.Navigation("Repair"); }); @@ -199,6 +237,11 @@ namespace RenovationWorkDatabaseImplement.Migrations b.Navigation("RepairComponents"); }); + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Order"); + }); + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => { b.Navigation("Components"); diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Implementer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..7757338 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,70 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; set; } + + [Required] + public string ImplementerFIO { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + [Required] + public int WorkExperience { get; set; } + + [Required] + public int Qualification { get; set; } + + [ForeignKey("ImplementerId")] + public virtual List Order { get; set; } = new(); + + public static Implementer? Create(ImplementerBindingModel? model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + WorkExperience = model.WorkExperience, + Qualification = model.Qualification + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + WorkExperience = model.WorkExperience; + Qualification = model.Qualification; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + WorkExperience = WorkExperience, + Qualification = Qualification + }; + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs index 7e845d9..551539b 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Models/Order.cs @@ -20,6 +20,10 @@ namespace RenovationWorkDatabaseImplement.Models public virtual Client Client { get; private set; } = new(); + public int? ImplementerId { get; private set; } + + public virtual Implementer? Implementer { get; set; } = new(); + [Required] public int RepairId { get; private set; } @@ -46,6 +50,8 @@ namespace RenovationWorkDatabaseImplement.Models Id = model.Id, ClientId = model.ClientId, Client = context.Clients.First(x => x.Id == model.ClientId), + ImplementerId = model.ImplementerId, + Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null, RepairId = model.RepairId, Repair = context.Repairs.First(x => x.Id == model.RepairId), Count = model.Count, @@ -56,7 +62,7 @@ namespace RenovationWorkDatabaseImplement.Models }; } - public void Update(OrderBindingModel? model) + public void Update(RenovationWorkDatabase context, OrderBindingModel? model) { if (model == null) { @@ -64,6 +70,8 @@ namespace RenovationWorkDatabaseImplement.Models } Status = model.Status; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; + Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null; } public OrderViewModel GetViewModel => new() @@ -71,6 +79,8 @@ namespace RenovationWorkDatabaseImplement.Models Id = Id, ClientId = ClientId, ClientFIO = Client.ClientFIO, + ImplementerId = ImplementerId, + ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null, RepairId = RepairId, RepairName = Repair.RepairName, Count = Count, diff --git a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs index 2468625..65ea90c 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs @@ -18,5 +18,6 @@ namespace RenovationWorkDatabaseImplement public virtual DbSet RepairComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } } } \ No newline at end of file diff --git a/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs b/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs index 14824a6..7902d35 100644 --- a/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs +++ b/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs @@ -10,10 +10,12 @@ namespace RenovationWorkFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string RepairFileName = "Repair.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Repairs { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } public static DataFileSingleton GetInstance() { @@ -28,6 +30,7 @@ namespace RenovationWorkFileImplement public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); private DataFileSingleton() { @@ -35,6 +38,7 @@ namespace RenovationWorkFileImplement Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/RenovationWork/RenovationWorkFileImplement/Implements/ImplementerStorage.cs b/RenovationWork/RenovationWorkFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..0cf2f62 --- /dev/null +++ b/RenovationWork/RenovationWorkFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,99 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + if (model.ImplementerFIO != null) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (model.Id.HasValue) + { + return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null) + { + return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel; + } + if (model.ImplementerFIO != null) + { + return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + } + return null; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + _source.Implementers.Remove(res); + _source.SaveImplementers(); + } + return res?.GetViewModel; + } + } +} diff --git a/RenovationWork/RenovationWorkFileImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkFileImplement/Implements/OrderStorage.cs index 6edf299..d60d72d 100644 --- a/RenovationWork/RenovationWorkFileImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkFileImplement/Implements/OrderStorage.cs @@ -34,6 +34,12 @@ namespace RenovationWorkFileImplement.Implements return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); } + if (!model.ImplementerId.HasValue && !model.Id.HasValue) + { + return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList(); + + } + if (model.Id.HasValue) { return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); @@ -43,6 +49,11 @@ namespace RenovationWorkFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue) + { + return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } + if (!model.Id.HasValue) { return new(); diff --git a/RenovationWork/RenovationWorkFileImplement/Models/Implementer.cs b/RenovationWork/RenovationWorkFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..5956127 --- /dev/null +++ b/RenovationWork/RenovationWorkFileImplement/Models/Implementer.cs @@ -0,0 +1,87 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace RenovationWorkFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("FIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/RenovationWork/RenovationWorkFileImplement/Models/Order.cs b/RenovationWork/RenovationWorkFileImplement/Models/Order.cs index 3952a24..541fed9 100644 --- a/RenovationWork/RenovationWorkFileImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkFileImplement/Models/Order.cs @@ -15,6 +15,7 @@ namespace RenovationWorkFileImplement.Models { public int Id { get; private set; } public int ClientId { get; private set; } + public int? ImplementerId { get; set; } public int RepairId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -32,6 +33,7 @@ namespace RenovationWorkFileImplement.Models { Id = model.Id, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, RepairId = model.RepairId, Count = model.Count, Sum = model.Sum, @@ -52,6 +54,7 @@ namespace RenovationWorkFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), RepairId = Convert.ToInt32(element.Element("RepairId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), @@ -76,6 +79,7 @@ namespace RenovationWorkFileImplement.Models { Id = Id, ClientId = ClientId, + ImplementerId = ImplementerId, RepairId = RepairId, Count = Count, Sum = Sum, @@ -87,6 +91,7 @@ namespace RenovationWorkFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("ClientId", ClientId.ToString()), + new XElement("ImplementerId", ImplementerId), new XElement("RepairId", RepairId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs b/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs index 40d11c8..074e18c 100644 --- a/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs +++ b/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs @@ -9,6 +9,7 @@ namespace RenovationWorkListImplement public List Orders { get; set; } public List Repairs { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { @@ -16,6 +17,7 @@ namespace RenovationWorkListImplement Orders = new List(); Repairs = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() diff --git a/RenovationWork/RenovationWorkListImplement/Implements/ImplementerStorage.cs b/RenovationWork/RenovationWorkListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..5695d02 --- /dev/null +++ b/RenovationWork/RenovationWorkListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,123 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null && x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + { + return x.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + + List result = new(); + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + } + return res?.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + } +} diff --git a/RenovationWork/RenovationWorkListImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkListImplement/Implements/OrderStorage.cs index 5ff47c8..4944f62 100644 --- a/RenovationWork/RenovationWorkListImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkListImplement/Implements/OrderStorage.cs @@ -53,6 +53,16 @@ namespace RenovationWorkListImplement.Implements } } } + else if (model.ImplementerId.HasValue && !model.Id.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ImplementerId == model.ImplementerId) + { + result.Add(GetViewModel(order)); + } + } + } else if (model.Id.HasValue) { foreach (var order in _source.Orders) @@ -100,6 +110,11 @@ namespace RenovationWorkListImplement.Implements { return AttachRepairName(order.GetViewModel); } + + else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId) + { + return GetViewModel(order); + } } return null; } diff --git a/RenovationWork/RenovationWorkListImplement/Models/Implementer.cs b/RenovationWork/RenovationWorkListImplement/Models/Implementer.cs new file mode 100644 index 0000000..6e196de --- /dev/null +++ b/RenovationWork/RenovationWorkListImplement/Models/Implementer.cs @@ -0,0 +1,60 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkListImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/RenovationWork/RenovationWorkListImplement/Models/Order.cs b/RenovationWork/RenovationWorkListImplement/Models/Order.cs index 987b255..ff53003 100644 --- a/RenovationWork/RenovationWorkListImplement/Models/Order.cs +++ b/RenovationWork/RenovationWorkListImplement/Models/Order.cs @@ -14,6 +14,7 @@ namespace RenovationWorkListImplement.Models { public int Id { get; private set; } public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } public int RepairId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -31,6 +32,7 @@ namespace RenovationWorkListImplement.Models { Id = model.Id, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, RepairId = model.RepairId, Count = model.Count, Sum = model.Sum, @@ -54,6 +56,7 @@ namespace RenovationWorkListImplement.Models { Id = Id, ClientId = ClientId, + ImplementerId = ImplementerId, RepairId = RepairId, Count = Count, Sum = Sum, diff --git a/RenovationWork/RenovationWorkRestApi/Controllers/ImplementerController.cs b/RenovationWork/RenovationWorkRestApi/Controllers/ImplementerController.cs new file mode 100644 index 0000000..38d6f75 --- /dev/null +++ b/RenovationWork/RenovationWorkRestApi/Controllers/ImplementerController.cs @@ -0,0 +1,105 @@ +using Microsoft.AspNetCore.Mvc; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Enums; + +namespace RenovationWorkRestApi.Controllers +{ + public class ImplementerController : Controller + { + private readonly ILogger _logger; + + private readonly IOrderLogic _order; + + private readonly IImplementerLogic _logic; + + public ImplementerController(IOrderLogic order, IImplementerLogic logic, ILogger logger) + { + _logger = logger; + _order = order; + _logic = logic; + } + + [HttpGet] + public ImplementerViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ImplementerSearchModel + { + ImplementerFIO = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка авторизации сотрудника"); + throw; + } + } + + [HttpGet] + public List? GetNewOrders() + { + try + { + return _order.ReadList(new OrderSearchModel + { + Status = OrderStatus.Принят + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения новых заказов"); + throw; + } + } + + [HttpGet] + public OrderViewModel? GetImplementerOrder(int implementerId) + { + try + { + return _order.ReadElement(new OrderSearchModel + { + ImplementerId = implementerId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения текущего заказа исполнителя"); + throw; + } + } + + [HttpPost] + public void TakeOrderInWork(OrderBindingModel model) + { + try + { + _order.TakeOrderInWork(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка перевода заказа с №{Id} в работу", model.Id); + throw; + } + } + + [HttpPost] + public void FinishOrder(OrderBindingModel model) + { + try + { + _order.FinishOrder(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа с №{Id}", model.Id); + throw; + } + } + } +} diff --git a/RenovationWork/RenovationWorkRestApi/Program.cs b/RenovationWork/RenovationWorkRestApi/Program.cs index ba9c9e6..4f65402 100644 --- a/RenovationWork/RenovationWorkRestApi/Program.cs +++ b/RenovationWork/RenovationWorkRestApi/Program.cs @@ -11,11 +11,13 @@ builder.Logging.AddLog4Net("log4net.config"); // Add services to the container. builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddControllers(); diff --git a/RenovationWork/RenovationWorkView/FormImplementer.Designer.cs b/RenovationWork/RenovationWorkView/FormImplementer.Designer.cs new file mode 100644 index 0000000..ed2c0af --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementer.Designer.cs @@ -0,0 +1,167 @@ +namespace RenovationWorkView +{ + partial class FormImplementer + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.textBoxFIO = new System.Windows.Forms.TextBox(); + this.labelFIO = new System.Windows.Forms.Label(); + this.textBoxPassword = new System.Windows.Forms.TextBox(); + this.labelPassword = new System.Windows.Forms.Label(); + this.labelWorkExperience = new System.Windows.Forms.Label(); + this.numericUpDownWorkExperience = new System.Windows.Forms.NumericUpDown(); + this.numericUpDownQualification = new System.Windows.Forms.NumericUpDown(); + this.labelQualification = new System.Windows.Forms.Label(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWorkExperience)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownQualification)).BeginInit(); + this.SuspendLayout(); + // + // textBoxFIO + // + this.textBoxFIO.Location = new System.Drawing.Point(157, 12); + this.textBoxFIO.Name = "textBoxFIO"; + this.textBoxFIO.Size = new System.Drawing.Size(382, 27); + this.textBoxFIO.TabIndex = 3; + // + // labelFIO + // + this.labelFIO.AutoSize = true; + this.labelFIO.Location = new System.Drawing.Point(12, 15); + this.labelFIO.Name = "labelFIO"; + this.labelFIO.Size = new System.Drawing.Size(139, 20); + this.labelFIO.TabIndex = 2; + this.labelFIO.Text = "ФИО исполнителя:"; + // + // textBoxPassword + // + this.textBoxPassword.Location = new System.Drawing.Point(157, 50); + this.textBoxPassword.Name = "textBoxPassword"; + this.textBoxPassword.Size = new System.Drawing.Size(205, 27); + this.textBoxPassword.TabIndex = 5; + // + // labelPassword + // + this.labelPassword.AutoSize = true; + this.labelPassword.Location = new System.Drawing.Point(12, 53); + this.labelPassword.Name = "labelPassword"; + this.labelPassword.Size = new System.Drawing.Size(65, 20); + this.labelPassword.TabIndex = 4; + this.labelPassword.Text = "Пароль:"; + // + // labelWorkExperience + // + this.labelWorkExperience.AutoSize = true; + this.labelWorkExperience.Location = new System.Drawing.Point(12, 99); + this.labelWorkExperience.Name = "labelWorkExperience"; + this.labelWorkExperience.Size = new System.Drawing.Size(105, 20); + this.labelWorkExperience.TabIndex = 6; + this.labelWorkExperience.Text = "Опыт работы:"; + // + // numericUpDownWorkExperience + // + this.numericUpDownWorkExperience.Location = new System.Drawing.Point(157, 97); + this.numericUpDownWorkExperience.Name = "numericUpDownWorkExperience"; + this.numericUpDownWorkExperience.Size = new System.Drawing.Size(124, 27); + this.numericUpDownWorkExperience.TabIndex = 8; + // + // numericUpDownQualification + // + this.numericUpDownQualification.Location = new System.Drawing.Point(157, 142); + this.numericUpDownQualification.Name = "numericUpDownQualification"; + this.numericUpDownQualification.Size = new System.Drawing.Size(124, 27); + this.numericUpDownQualification.TabIndex = 10; + // + // labelQualification + // + this.labelQualification.AutoSize = true; + this.labelQualification.Location = new System.Drawing.Point(12, 144); + this.labelQualification.Name = "labelQualification"; + this.labelQualification.Size = new System.Drawing.Size(114, 20); + this.labelQualification.TabIndex = 9; + this.labelQualification.Text = "Квалификация:"; + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(422, 203); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(136, 40); + this.buttonCancel.TabIndex = 12; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(265, 203); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(130, 40); + this.buttonSave.TabIndex = 11; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // FormImplementer + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(570, 255); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.numericUpDownQualification); + this.Controls.Add(this.labelQualification); + this.Controls.Add(this.numericUpDownWorkExperience); + this.Controls.Add(this.labelWorkExperience); + this.Controls.Add(this.textBoxPassword); + this.Controls.Add(this.labelPassword); + this.Controls.Add(this.textBoxFIO); + this.Controls.Add(this.labelFIO); + this.Name = "FormImplementer"; + this.Text = "Исполнитель"; + this.Load += new System.EventHandler(this.FormImplementer_Load); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWorkExperience)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownQualification)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private TextBox textBoxFIO; + private Label labelFIO; + private TextBox textBoxPassword; + private Label labelPassword; + private Label labelWorkExperience; + private NumericUpDown numericUpDownWorkExperience; + private NumericUpDown numericUpDownQualification; + private Label labelQualification; + private Button buttonCancel; + private Button buttonSave; + } +} \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormImplementer.cs b/RenovationWork/RenovationWorkView/FormImplementer.cs new file mode 100644 index 0000000..ed8f555 --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementer.cs @@ -0,0 +1,103 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using RenovationWorkContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RenovationWorkView +{ + public partial class FormImplementer : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + + public FormImplementer(ILogger logger, IImplementerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormImplementer_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение исполнителя"); + var view = _logic.ReadElement(new ImplementerSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxFIO.Text = view.ImplementerFIO; + textBoxPassword.Text = view.Password; + numericUpDownWorkExperience.Value = view.WorkExperience; + numericUpDownQualification.Value = view.Qualification; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxFIO.Text)) + { + MessageBox.Show("Заполните ФИО", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPassword.Text)) + { + MessageBox.Show("Заполните пароль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение исполнителя"); + try + { + var model = new ImplementerBindingModel + { + Id = _id ?? 0, + ImplementerFIO = textBoxFIO.Text, + Password = textBoxPassword.Text, + WorkExperience = (int)numericUpDownWorkExperience.Value, + Qualification = (int)numericUpDownQualification.Value + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при создании или обновлении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/RenovationWork/RenovationWorkView/FormImplementer.resx b/RenovationWork/RenovationWorkView/FormImplementer.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementer.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormImplementers.Designer.cs b/RenovationWork/RenovationWorkView/FormImplementers.Designer.cs new file mode 100644 index 0000000..aa04ca9 --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementers.Designer.cs @@ -0,0 +1,130 @@ +namespace RenovationWorkView +{ + partial class FormImplementers + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ToolsPanel = new System.Windows.Forms.Panel(); + this.buttonRef = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.ToolsPanel.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // ToolsPanel + // + this.ToolsPanel.Controls.Add(this.buttonRef); + this.ToolsPanel.Controls.Add(this.buttonDel); + this.ToolsPanel.Controls.Add(this.buttonUpd); + this.ToolsPanel.Controls.Add(this.buttonAdd); + this.ToolsPanel.Location = new System.Drawing.Point(608, 12); + this.ToolsPanel.Name = "ToolsPanel"; + this.ToolsPanel.Size = new System.Drawing.Size(180, 426); + this.ToolsPanel.TabIndex = 3; + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(31, 206); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(126, 36); + this.buttonRef.TabIndex = 3; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.buttonRef_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(31, 142); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(126, 36); + this.buttonDel.TabIndex = 2; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(31, 76); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(126, 36); + this.buttonUpd.TabIndex = 1; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.buttonUpd_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(31, 16); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(126, 36); + this.buttonAdd.TabIndex = 0; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); + // + // dataGridView + // + this.dataGridView.AllowUserToAddRows = false; + this.dataGridView.AllowUserToDeleteRows = false; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.ReadOnly = true; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(590, 426); + this.dataGridView.TabIndex = 2; + // + // FormImplementers + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ToolsPanel); + this.Controls.Add(this.dataGridView); + this.Name = "FormImplementers"; + this.Text = "Исполнители"; + this.Load += new System.EventHandler(this.FormImplementers_Load); + this.ToolsPanel.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Panel ToolsPanel; + private Button buttonRef; + private Button buttonDel; + private Button buttonUpd; + private Button buttonAdd; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormImplementers.cs b/RenovationWork/RenovationWorkView/FormImplementers.cs new file mode 100644 index 0000000..75f3b89 --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementers.cs @@ -0,0 +1,117 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RenovationWorkView +{ + public partial class FormImplementers : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + + public FormImplementers(ILogger logger, IImplementerLogic implementerLogic) + { + InitializeComponent(); + _logger = logger; + _logic = implementerLogic; + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ImplementerFIO"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка исполнителей"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки исполнителей"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void FormImplementers_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void buttonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление исполнителя"); + try + { + if (!_logic.Delete(new ImplementerBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void buttonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/RenovationWork/RenovationWorkView/FormImplementers.resx b/RenovationWork/RenovationWorkView/FormImplementers.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/RenovationWork/RenovationWorkView/FormImplementers.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormMain.Designer.cs b/RenovationWork/RenovationWorkView/FormMain.Designer.cs index 737b0ad..8fa8871 100644 --- a/RenovationWork/RenovationWorkView/FormMain.Designer.cs +++ b/RenovationWork/RenovationWorkView/FormMain.Designer.cs @@ -43,6 +43,8 @@ this.buttonIssuedOrder = new System.Windows.Forms.Button(); this.clientsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.buttonRef = new System.Windows.Forms.Button(); + this.startingworkToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.implementersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -52,11 +54,12 @@ this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.bookToolStripMenuItem, - this.reportsToolStripMenuItem}); + this.reportsToolStripMenuItem, + this.startingworkToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Padding = new System.Windows.Forms.Padding(5, 2, 0, 2); - this.menuStrip1.Size = new System.Drawing.Size(1149, 24); + this.menuStrip1.Size = new System.Drawing.Size(1356, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // @@ -65,7 +68,8 @@ this.bookToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.consumablesToolStripMenuItem, this.repairsToolStripMenuItem, - this.clientsToolStripMenuItem}); + this.clientsToolStripMenuItem, + this.implementersToolStripMenuItem}); this.bookToolStripMenuItem.Name = "bookToolStripMenuItem"; this.bookToolStripMenuItem.Size = new System.Drawing.Size(87, 20); this.bookToolStripMenuItem.Text = "Справочник"; @@ -73,14 +77,14 @@ // consumablesToolStripMenuItem // this.consumablesToolStripMenuItem.Name = "consumablesToolStripMenuItem"; - this.consumablesToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.consumablesToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.consumablesToolStripMenuItem.Text = "Расходные материалы"; this.consumablesToolStripMenuItem.Click += new System.EventHandler(this.СonsumablesToolStripMenuItem_Click); // // repairsToolStripMenuItem // this.repairsToolStripMenuItem.Name = "repairsToolStripMenuItem"; - this.repairsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.repairsToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.repairsToolStripMenuItem.Text = "Ремотные работы"; this.repairsToolStripMenuItem.Click += new System.EventHandler(this.RepairsToolStripMenuItem_Click); // @@ -126,12 +130,12 @@ this.dataGridView.ReadOnly = true; this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(855, 286); + this.dataGridView.Size = new System.Drawing.Size(1098, 286); this.dataGridView.TabIndex = 1; // // buttonCreateOrder // - this.buttonCreateOrder.Location = new System.Drawing.Point(898, 53); + this.buttonCreateOrder.Location = new System.Drawing.Point(1128, 53); this.buttonCreateOrder.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCreateOrder.Name = "buttonCreateOrder"; this.buttonCreateOrder.Size = new System.Drawing.Size(216, 22); @@ -142,7 +146,7 @@ // // buttonTakeOrderInWork // - this.buttonTakeOrderInWork.Location = new System.Drawing.Point(898, 92); + this.buttonTakeOrderInWork.Location = new System.Drawing.Point(1128, 92); this.buttonTakeOrderInWork.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; this.buttonTakeOrderInWork.Size = new System.Drawing.Size(216, 22); @@ -153,7 +157,7 @@ // // buttonOrderReady // - this.buttonOrderReady.Location = new System.Drawing.Point(898, 129); + this.buttonOrderReady.Location = new System.Drawing.Point(1128, 129); this.buttonOrderReady.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonOrderReady.Name = "buttonOrderReady"; this.buttonOrderReady.Size = new System.Drawing.Size(216, 22); @@ -164,7 +168,7 @@ // // buttonIssuedOrder // - this.buttonIssuedOrder.Location = new System.Drawing.Point(898, 169); + this.buttonIssuedOrder.Location = new System.Drawing.Point(1128, 169); this.buttonIssuedOrder.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonIssuedOrder.Name = "buttonIssuedOrder"; this.buttonIssuedOrder.Size = new System.Drawing.Size(216, 22); @@ -175,7 +179,7 @@ // // buttonRef // - this.buttonRef.Location = new System.Drawing.Point(898, 210); + this.buttonRef.Location = new System.Drawing.Point(1128, 210); this.buttonRef.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRef.Name = "buttonRef"; this.buttonRef.Size = new System.Drawing.Size(216, 22); @@ -191,11 +195,25 @@ this.clientsToolStripMenuItem.Text = "Клиент"; this.clientsToolStripMenuItem.Click += new System.EventHandler(this.ClientToolStripMenuItem_Click); // + // implementersToolStripMenuItem + // + this.implementersToolStripMenuItem.Name = "implementersToolStripMenuItem"; + this.implementersToolStripMenuItem.Size = new System.Drawing.Size(149, 22); + this.implementersToolStripMenuItem.Text = "Исполнители"; + this.implementersToolStripMenuItem.Click += new System.EventHandler(this.ImplementersToolStripMenuItem_Click); + // + // startingworkToolStripMenuItem + // + this.startingworkToolStripMenuItem.Name = "startingworkToolStripMenuItem"; + this.startingworkToolStripMenuItem.Size = new System.Drawing.Size(92, 20); + this.startingworkToolStripMenuItem.Text = "Запуск работ"; + this.startingworkToolStripMenuItem.Click += new System.EventHandler(this.StartingworkToolStripMenuItem_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1149, 319); + this.ClientSize = new System.Drawing.Size(1356, 319); this.Controls.Add(this.buttonRef); this.Controls.Add(this.buttonIssuedOrder); this.Controls.Add(this.buttonOrderReady); @@ -233,5 +251,7 @@ private ToolStripMenuItem componentsRepairToolStripMenuItem; private ToolStripMenuItem ordersToolStripMenuItem; private ToolStripMenuItem clientsToolStripMenuItem; + private ToolStripMenuItem implementersToolStripMenuItem; + private ToolStripMenuItem startingworkToolStripMenuItem; } } \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormMain.cs b/RenovationWork/RenovationWorkView/FormMain.cs index 250a492..5609658 100644 --- a/RenovationWork/RenovationWorkView/FormMain.cs +++ b/RenovationWork/RenovationWorkView/FormMain.cs @@ -19,13 +19,15 @@ namespace RenovationWorkView private readonly ILogger _logger; private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; + private readonly IWorkProcess _workProcess; - public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic) + public FormMain(ILogger logger, IOrderLogic orderLogic, IReportLogic reportLogic, IWorkProcess workProcess) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportLogic; + _workProcess = workProcess; } private void FormMain_Load(object sender, EventArgs e) @@ -43,6 +45,7 @@ namespace RenovationWorkView dataGridView.DataSource = list; dataGridView.Columns["RepairId"].Visible = false; dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["ImplementerId"].Visible = false; dataGridView.Columns["RepairName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } @@ -203,5 +206,20 @@ namespace RenovationWorkView form.ShowDialog(); } } + + private void ImplementersToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormImplementers)); + if (service is FormImplementers form) + { + form.ShowDialog(); + } + } + + private void StartingworkToolStripMenuItem_Click(object sender, EventArgs e) + { + _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); + MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + } } } diff --git a/RenovationWork/RenovationWorkView/Program.cs b/RenovationWork/RenovationWorkView/Program.cs index 9f2b610..a149db2 100644 --- a/RenovationWork/RenovationWorkView/Program.cs +++ b/RenovationWork/RenovationWorkView/Program.cs @@ -43,11 +43,14 @@ namespace RenovationWorkView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -63,6 +66,8 @@ namespace RenovationWorkView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); } }