118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
|
using FurnitureAssemblyContracts.BindingModels;
|
|||
|
using FurnitureAssemblyContracts.ViewModels;
|
|||
|
using FurnitureAssemblyDataModels.Models;
|
|||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|||
|
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;
|
|||
|
|
|||
|
namespace FurnitureAssemblyDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Furniture : IFurnitureModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string FurnitureName { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public double Price { get; set; }
|
|||
|
|
|||
|
public Dictionary<int, (IWorkpieceModel, int)>? _furnitureWorkpieces = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (IWorkpieceModel, int)> FurnitureWorkpieces
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_furnitureWorkpieces == null)
|
|||
|
{
|
|||
|
_furnitureWorkpieces = Workpieces
|
|||
|
.ToDictionary(recPC => recPC.WorkpieceId, recPC => (recPC.Workpiece as IWorkpieceModel, recPC.Count));
|
|||
|
}
|
|||
|
|
|||
|
return _furnitureWorkpieces;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Для реализации связи многие ко многим с заготовками
|
|||
|
[ForeignKey("FurnitureId")]
|
|||
|
public virtual List<FurnitureWorkpiece> Workpieces { get; set; } = new();
|
|||
|
|
|||
|
[ForeignKey("FurnitureId")]
|
|||
|
public virtual List<Order> Orders { get; set; } = new();
|
|||
|
|
|||
|
public static Furniture Create(FurnitureAssemblyDatabase context, FurnitureBindingModel model)
|
|||
|
{
|
|||
|
return new Furniture()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
FurnitureName = model.FurnitureName,
|
|||
|
Price = model.Price,
|
|||
|
Workpieces = model.FurnitureWorkpieces.Select(x => new FurnitureWorkpiece
|
|||
|
{
|
|||
|
Workpiece = context.Workpieces.First(y => y.Id == x.Key),
|
|||
|
Count = x.Value.Item2
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(FurnitureBindingModel model)
|
|||
|
{
|
|||
|
FurnitureName = model.FurnitureName;
|
|||
|
Price = model.Price;
|
|||
|
}
|
|||
|
|
|||
|
public FurnitureViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
FurnitureName = FurnitureName,
|
|||
|
Price = Price,
|
|||
|
FurnitureWorkpieces = FurnitureWorkpieces
|
|||
|
};
|
|||
|
|
|||
|
Dictionary<int, (IWorkpieceModel, int)> IFurnitureModel.FurnitureWorkpieces => throw new NotImplementedException();
|
|||
|
|
|||
|
public void UpdateWorkpieces(FurnitureAssemblyDatabase context, FurnitureBindingModel model)
|
|||
|
{
|
|||
|
var furnitureWorkpieces = context.FurnitureWorkpieces.Where(rec => rec.FurnitureId == model.Id).ToList();
|
|||
|
|
|||
|
if (furnitureWorkpieces != null && furnitureWorkpieces.Count > 0)
|
|||
|
{
|
|||
|
// удалили те, которых нет в модели
|
|||
|
context.FurnitureWorkpieces.RemoveRange(furnitureWorkpieces.Where(rec => !model.FurnitureWorkpieces.ContainsKey(rec.FurnitureId)));
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
// обновили количество у существующих записей
|
|||
|
foreach (var updateFurniture in furnitureWorkpieces)
|
|||
|
{
|
|||
|
updateFurniture.Count = model.FurnitureWorkpieces[updateFurniture.FurnitureId].Item2;
|
|||
|
model.FurnitureWorkpieces.Remove(updateFurniture.FurnitureId);
|
|||
|
}
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
var furniture = context.Furnitures.First(x => x.Id == Id);
|
|||
|
|
|||
|
foreach (var pc in model.FurnitureWorkpieces)
|
|||
|
{
|
|||
|
context.FurnitureWorkpieces.Add(new FurnitureWorkpiece
|
|||
|
{
|
|||
|
Furniture = furniture,
|
|||
|
Workpiece = context.Workpieces.First(x => x.Id == pc.Key),
|
|||
|
Count = pc.Value.Item2
|
|||
|
});
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
_furnitureWorkpieces = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|