2024-03-11 12:12:21 +04:00
|
|
|
|
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();
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
[ForeignKey("FurnitureId")]
|
|
|
|
|
public virtual List<ShopFurniture> Shops { get; set; } = new();
|
|
|
|
|
|
2024-03-11 12:12:21 +04:00
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-05-11 23:46:17 +04:00
|
|
|
|
foreach(var mwp in model.FurnitureWorkPieces)
|
2024-03-11 12:12:21 +04:00
|
|
|
|
{
|
|
|
|
|
context.FurnitureWorkPieces.Add(new FurnitureWorkPiece
|
|
|
|
|
{
|
|
|
|
|
Furniture = furniture,
|
2024-05-11 23:46:17 +04:00
|
|
|
|
WorkPiece = context.WorkPieces.First(x => x.Id == mwp.Key),
|
|
|
|
|
Count = mwp.Value.Item2
|
2024-03-11 12:12:21 +04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_furnitureWorkPieces = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|