2024-04-27 12:35:20 +04:00
|
|
|
|
using Contracts.BindingModels;
|
|
|
|
|
using Contracts.ViewModels;
|
|
|
|
|
using DataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Machine : IMachineModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string Title { get; private set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public string Country { get; private set; } = string.Empty;
|
2024-04-30 23:42:05 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public DateTime DateCreate { get; set; } = DateTime.Now;
|
|
|
|
|
[Required]
|
2024-04-27 12:35:20 +04:00
|
|
|
|
public int UserId { get; private set; }
|
2024-04-27 22:13:33 +04:00
|
|
|
|
private Dictionary<int, IWorkerModel>? _workerMachines = null;
|
2024-04-27 20:23:32 +04:00
|
|
|
|
[NotMapped]
|
2024-04-27 22:13:33 +04:00
|
|
|
|
public Dictionary<int, IWorkerModel>? WorkerMachines
|
2024-04-27 12:35:20 +04:00
|
|
|
|
{
|
2024-04-27 20:23:32 +04:00
|
|
|
|
get
|
2024-04-27 12:35:20 +04:00
|
|
|
|
{
|
2024-04-27 20:23:32 +04:00
|
|
|
|
if (_workerMachines == null)
|
|
|
|
|
{
|
2024-04-27 22:13:33 +04:00
|
|
|
|
_workerMachines = Workers.ToDictionary(recDP => recDP.WorkerId, recDp => (recDp.Worker as IWorkerModel));
|
2024-04-27 20:23:32 +04:00
|
|
|
|
}
|
|
|
|
|
return _workerMachines;
|
2024-04-27 12:35:20 +04:00
|
|
|
|
}
|
2024-04-27 20:23:32 +04:00
|
|
|
|
}
|
2024-04-30 22:38:48 +04:00
|
|
|
|
[ForeignKey("MachineId")]
|
2024-04-27 20:23:32 +04:00
|
|
|
|
public virtual List<WorkerMachine> Workers { get; set; } = new();
|
|
|
|
|
public virtual Guarantor Guarantor { get; set; }
|
2024-04-30 22:38:48 +04:00
|
|
|
|
[ForeignKey("MachineId")]
|
|
|
|
|
public virtual List<Product> Products { get; set; }
|
|
|
|
|
public static Machine Create(MachineBindingModel model, FactoryGoWorkDatabase context)
|
2024-04-27 20:23:32 +04:00
|
|
|
|
{
|
2024-04-27 12:35:20 +04:00
|
|
|
|
return new Machine()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Title = model.Title,
|
|
|
|
|
Country = model.Country,
|
2024-04-27 20:23:32 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
Workers = model.MachineWorker.Select(x => new WorkerMachine
|
|
|
|
|
{
|
|
|
|
|
Worker = context.Workers.First(y => y.Id == x.Key),
|
|
|
|
|
}).ToList(),
|
2024-04-27 12:35:20 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(MachineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
Title = model.Title;
|
|
|
|
|
Country = model.Country;
|
|
|
|
|
}
|
|
|
|
|
public MachineViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Title = Title,
|
|
|
|
|
Country = Country,
|
|
|
|
|
UserId = UserId,
|
2024-04-27 20:23:32 +04:00
|
|
|
|
WorkerMachines = WorkerMachines
|
2024-04-27 12:35:20 +04:00
|
|
|
|
};
|
2024-04-27 20:23:32 +04:00
|
|
|
|
public void UpdateWorkers(FactoryGoWorkDatabase context, MachineBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var machineWorkers = context.WorkerMachines.Where(rec => rec.MachineId == model.Id).ToList();
|
|
|
|
|
if (machineWorkers != null && machineWorkers.Count > 0)
|
|
|
|
|
{
|
2024-04-30 22:38:48 +04:00
|
|
|
|
context.WorkerMachines.RemoveRange(machineWorkers.Where(rec => model.MachineWorker.ContainsKey(rec.WorkerId)));
|
2024-04-27 20:23:32 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var machine = context.Machines.First(x => x.Id == model.Id);
|
|
|
|
|
foreach (var dp in model.MachineWorker)
|
|
|
|
|
{
|
|
|
|
|
context.WorkerMachines.Add(new WorkerMachine
|
|
|
|
|
{
|
|
|
|
|
Machine = machine,
|
2024-04-27 22:13:33 +04:00
|
|
|
|
Worker = context.Workers.First(x => x.Id == dp.Key)
|
2024-04-27 20:23:32 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_workerMachines = null;
|
|
|
|
|
}
|
2024-04-27 12:35:20 +04:00
|
|
|
|
}
|
|
|
|
|
}
|