96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
|
using UniversityContracts.BindingModels;
|
|||
|
using UniversityContracts.ViewModels;
|
|||
|
using UniversityDataModels;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.Linq.Expressions;
|
|||
|
using UniversityDataModels.ProxyModels;
|
|||
|
|
|||
|
namespace UniversityDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Purchase : IPurchaseModel
|
|||
|
{
|
|||
|
public int ClientId { get; private set; }
|
|||
|
public DateOnly DatePurchase { get; private set; }
|
|||
|
private Dictionary<int, ClassByPurchaseModel>? _cachedOperations;
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, ClassByPurchaseModel> ClassModel => _cachedOperations ??= Class.Select(x => (ClassByPurchaseModel)x).ToDictionary(x => x.ClassId, x => x);
|
|||
|
[NotMapped]
|
|||
|
public List<CostByPurchaseModel>? CostsModel => null;
|
|||
|
public int Id { get; private set; }
|
|||
|
[Required]
|
|||
|
public Client? Client { get; private set; }
|
|||
|
[Required]
|
|||
|
public List<ClassByPurchase> Operations { get; private set; } = new();
|
|||
|
[Required]
|
|||
|
public List<CostByPurchase> Costs { get; private set; } = new();
|
|||
|
|
|||
|
public static Purchase Create(PurchaseBindingModel model)
|
|||
|
{
|
|||
|
return new Purchase()
|
|||
|
{
|
|||
|
ClientId = model.ClientId,
|
|||
|
Id = model.Id,
|
|||
|
DatePurchase = model.DatePurchase,
|
|||
|
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(PurchaseBindingModel model)
|
|||
|
{
|
|||
|
DatePurchase = model.DatePurchase;
|
|||
|
}
|
|||
|
|
|||
|
public PurchaseViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
OperationsModel = OperationsModel,
|
|||
|
Id = Id,
|
|||
|
DatePurchase = DatePurchase,
|
|||
|
ClientId = ClientId,
|
|||
|
ClientPhoneNumber = Client?.PhoneNumber ?? string.Empty,
|
|||
|
|
|||
|
CostViewModels = Costs?
|
|||
|
.Select(x => x.Cost.GetViewModel)
|
|||
|
.ToList() ?? new(),
|
|||
|
OperationViewModels = Operations?
|
|||
|
.Select(x => x.Operation?.GetViewModel)
|
|||
|
.ToList() ?? new()
|
|||
|
};
|
|||
|
|
|||
|
public PurchaseViewModel GetViewModel2 => new()
|
|||
|
{
|
|||
|
OperationsModel = OperationsModel,
|
|||
|
Id = Id,
|
|||
|
DatePurchase = DatePurchase,
|
|||
|
ClientId = ClientId,
|
|||
|
ClientPhoneNumber = Client?.PhoneNumber ?? string.Empty,
|
|||
|
|
|||
|
CostViewModels = Costs?
|
|||
|
.Select(x => x.Cost.GetViewModel)
|
|||
|
.ToList() ?? new()
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateOperations(UniversityDB context, PurchaseBindingModel model)
|
|||
|
{
|
|||
|
var oldOperations = context.OperationByPurchases.Where(x => x.PurchaseId == model.Id).ToDictionary(x => x.OperationId, x => x);
|
|||
|
var newOperations = model.OperationsModel.ToDictionary(
|
|||
|
x => x.Key,
|
|||
|
x => new ClassnByPurchase()
|
|||
|
{
|
|||
|
OperationId = x.Key,
|
|||
|
PurchaseId = Id,
|
|||
|
CountOperations = x.Value.CountOperations
|
|||
|
}
|
|||
|
);
|
|||
|
|
|||
|
context.RemoveRange(oldOperations.Where(x => !newOperations.ContainsKey(x.Key)).Select(x => x.Value));
|
|||
|
context.AddRange (newOperations.Where(x => !oldOperations.ContainsKey(x.Key)).Select(x => x.Value));
|
|||
|
oldOperations
|
|||
|
.Where(x => newOperations.ContainsKey(x.Key))
|
|||
|
.Select(x => x.Value).ToList()
|
|||
|
.ForEach(x => x.CountOperations = newOperations[x.OperationId].CountOperations);
|
|||
|
context.SaveChanges();
|
|||
|
_cachedOperations = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|