using FruitBank.Common.Interfaces; using LinqToDB.Mapping; using Mango.Nop.Core.Dtos; using Mango.Nop.Core.Extensions; using Mango.Nop.Core.Interfaces.ForeignKeys; using Newtonsoft.Json; //using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; using System.ComponentModel.DataAnnotations.Schema; using System.Linq.Expressions; namespace FruitBank.Common.Dtos; public class ProductDto : MgProductDto, IProductDto { [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] private static Expression> RelationWithGenericAttribute => (orderItemDto, genericAttributeDto) => orderItemDto.Id == genericAttributeDto.EntityId && genericAttributeDto.KeyGroup == "Product";// nameof(Product); [Association(ThisKey = nameof(Id), OtherKey = nameof(GenericAttributeDto.EntityId), ExpressionPredicate = nameof(RelationWithGenericAttribute), CanBeNull = false)] public List GenericAttributes { get; set; } public ProductDto() :base() { } public ProductDto(int productId) : base(productId) { } //public ProductDto(Product product) : base(product) //{ } [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public bool IsMeasurable { get => GenericAttributes.GetValueOrDefault(nameof(IMeasurable.IsMeasurable)); set { throw new Exception($"ProductDto.IsMeasurable not set"); //Direkt legyen exception! - J. var ga = GenericAttributes.SingleOrDefault(x => x.Key == nameof(IMeasurable.IsMeasurable))!; ga.Value = value.ToString(); } } [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public double Tare { get => GenericAttributes.GetValueOrDefault(nameof(ITare.Tare)); //get => CommonHelper.To(GenericAttributes.SingleOrDefault(x => x.Key == nameof(ITare.Tare))?.Value ?? "0"); set => throw new Exception($"ProductDto.Tare not set"); } [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public double NetWeight { get => GenericAttributes.GetValueOrDefault(nameof(IMeasuringNetWeight.NetWeight)); set => throw new Exception($"ProductDto.NetWeight not set"); } [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public int IncomingQuantity { get => GenericAttributes.GetValueOrDefault(nameof(IIncomingQuantity.IncomingQuantity)); set => throw new Exception($"ProductDto.IncomingQuantity not set"); //set //{ // var ga = GenericAttributes.FirstOrDefault(ga => ga.Key == nameof(IIncomingQuantity.IncomingQuantity)) ?? // GenericAttributes.AddNewGenericAttribute("Product", nameof(IIncomingQuantity.IncomingQuantity), value.ToString(), Id); // ga.Value = value.ToString(); //} } [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public int AvailableQuantity => StockQuantity + IncomingQuantity; [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public double AverageWeight => GenericAttributes.GetValueOrDefault(nameof(IProductDto.AverageWeight)); [NotColumn, NotMapped, JsonIgnore, System.Text.Json.Serialization.JsonIgnore] public double AverageWeightTreshold => GenericAttributes.GetValueOrDefault(nameof(IProductDto.AverageWeightTreshold)); public bool HasMeasuringValues() => Id > 0 && NetWeight != 0 && IsMeasurable; }