158 lines
5.9 KiB
C#
158 lines
5.9 KiB
C#
using HardwareShopContracts.BindingModels;
|
||
using HardwareShopContracts.ViewModels;
|
||
using HardwareShopDataModels.Enums;
|
||
using HardwareShopDataModels.Models;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Diagnostics;
|
||
|
||
namespace HardwareShopDatabaseImplement.Models
|
||
{
|
||
public class Purchase : IPurchaseModel
|
||
{
|
||
public int Id { get; set; }
|
||
|
||
public decimal Sum { get; set; }
|
||
|
||
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
|
||
|
||
public DateTime? DatePurchase { get; set; }
|
||
|
||
public int UserID { get; set; }
|
||
|
||
public virtual User User { get; set; }
|
||
|
||
[ForeignKey("PurchaseId")]
|
||
public virtual List<PurchaseBuild>? Builds { get; set; }
|
||
|
||
public Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds
|
||
{
|
||
get
|
||
{
|
||
if (_purchaseBuilds == null)
|
||
{
|
||
_purchaseBuilds = Builds.ToDictionary(recPB => recPB.BuildID, recPB => (recPB.Build as IBuildModel, recPB.Count));
|
||
}
|
||
return _purchaseBuilds;
|
||
}
|
||
}
|
||
|
||
[ForeignKey("PurchaseId")]
|
||
public virtual List<PurchaseGood> Goods { get; set; } = new();
|
||
|
||
public Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
|
||
[NotMapped]
|
||
public Dictionary<int, (IGoodModel, int)>? PurchaseGoods
|
||
{
|
||
get
|
||
{
|
||
if (_purchaseGoods == null)
|
||
{
|
||
_purchaseGoods = Goods.ToDictionary(recPG => recPG.GoodID, recPG => (recPG.Good as IGoodModel, recPG.Count));
|
||
}
|
||
return _purchaseGoods;
|
||
}
|
||
}
|
||
|
||
public static Purchase Create(HardwareShopDatabase context, PurchaseBindingModel model)
|
||
{
|
||
return new Purchase()
|
||
{
|
||
Id = model.Id,
|
||
Sum = model.Sum,
|
||
PurchaseStatus = model.PurchaseStatus,
|
||
DatePurchase = model.DatePurchase,
|
||
UserID = model.UserID,
|
||
Builds = model.PurchaseBuilds.Select(x => new PurchaseBuild
|
||
{
|
||
Build = context.Builds.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList(),
|
||
Goods = model.PurchaseGoods.Select(x => new PurchaseGood
|
||
{
|
||
Good = context.Goods.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
|
||
public void Update(PurchaseBindingModel model)
|
||
{
|
||
PurchaseStatus = model.PurchaseStatus;
|
||
DatePurchase = model.DatePurchase;
|
||
}
|
||
|
||
public PurchaseViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Sum = Sum,
|
||
PurchaseStatus = PurchaseStatus,
|
||
DatePurchase = DatePurchase,
|
||
UserID = UserID,
|
||
UserEmail = User.Email,
|
||
PurchaseBuilds = PurchaseBuilds,
|
||
PurchaseGoods = PurchaseGoods
|
||
};
|
||
|
||
public void UpdateBuilds(HardwareShopDatabase context, PurchaseBindingModel model)
|
||
{
|
||
var purchaseBuilds = context.PurchasesBuilds.Where(rec => rec.PurchaseID == model.Id).ToList();
|
||
if (purchaseBuilds != null && purchaseBuilds.Count > 0)
|
||
{
|
||
context.PurchasesBuilds.RemoveRange(purchaseBuilds.Where(rec => !model.PurchaseBuilds.ContainsKey(rec.BuildID)));
|
||
context.SaveChanges();
|
||
foreach (var updateBuild in purchaseBuilds)
|
||
{
|
||
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildID].Item2;
|
||
model.PurchaseBuilds.Remove(updateBuild.BuildID);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var purchase = context.Purchases.First(x => x.Id == Id);
|
||
foreach (var dc in model.PurchaseBuilds)
|
||
{
|
||
context.PurchasesBuilds.Add(new PurchaseBuild
|
||
{
|
||
Purchase = purchase,
|
||
Build = context.Builds.First(x => x.Id == dc.Key),
|
||
Count = dc.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_purchaseBuilds = null;
|
||
}
|
||
|
||
public void UpdateGoods(HardwareShopDatabase context, PurchaseBindingModel model)
|
||
{
|
||
var purchaseGoods = context.PurchasesGoods.Where(rec => rec.PurchaseID == model.Id).ToList();
|
||
if (purchaseGoods != null && purchaseGoods.Count > 0)
|
||
{ // удалили те в бд, которых нет в модели
|
||
context.PurchasesGoods.RemoveRange(purchaseGoods.Where(rec => !model.PurchaseGoods.ContainsKey(rec.GoodID)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateGood in purchaseGoods)
|
||
{
|
||
updateGood.Count = model.PurchaseGoods[updateGood.GoodID].Item2;
|
||
model.PurchaseGoods.Remove(updateGood.GoodID);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var purchase = context.Purchases.First(x => x.Id == Id);
|
||
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
|
||
foreach (var dc in model.PurchaseGoods)
|
||
{
|
||
context.PurchasesGoods.Add(new PurchaseGood
|
||
{
|
||
Purchase = purchase,
|
||
Good = context.Goods.First(x => x.Id == dc.Key),
|
||
Count = dc.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_purchaseGoods = null;
|
||
}
|
||
}
|
||
}
|