ProductStore.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Comal.Classes;
  2. using InABox.Core;
  3. namespace Comal.Stores
  4. {
  5. public class ProductStore : BaseProductStore<Product>
  6. {
  7. protected override void AfterSave(Product entity)
  8. {
  9. base.AfterSave(entity);
  10. if (entity.HasOriginalValue("NettCost"))
  11. UpdateProductComponentCost(entity);
  12. if (entity.UnitOfMeasure.HasOriginalValue(nameof(ProductDimensionUnitLink.ID)))
  13. UpdateProductUOMs(entity);
  14. }
  15. private static List<Type>? _stockentitytypes = null;
  16. private void UpdateProductUOMs(Product entity)
  17. {
  18. // If this is a new Product (ie original value is Guid.Empty)
  19. if (entity.GetOriginalValue(x => x.ID, entity.ID) == Guid.Empty)
  20. return;
  21. _stockentitytypes ??= CoreUtils.TypeList(x => x.IsSubclassOf(typeof(StockEntity)));
  22. var _uom = Provider.Query(new Filter<ProductDimensionUnit>(x => x.ID).IsEqualTo(entity.UnitOfMeasure.ID))
  23. .ToObjects<ProductDimensionUnit>().FirstOrDefault() ?? new ProductDimensionUnit();
  24. //List<Task> _tasks = new List<Task>();
  25. foreach (var _stockentitytype in _stockentitytypes)
  26. {
  27. //var _task = Task.Run(() =>
  28. //{
  29. var _children = Provider.Query(
  30. _stockentitytype,
  31. new Filter<StockEntity>(x => x.Product.ID).IsEqualTo(entity.ID),
  32. Columns.None<StockEntity>()
  33. .Add(x => x.ID)
  34. .AddSubColumns(x => x.Dimensions, null)
  35. ).ToObjects(_stockentitytype).OfType<StockEntity>().ToArray();
  36. foreach (var _child in _children)
  37. _child.Dimensions.Unit.CopyFrom(_uom);
  38. var _updates = _children.Where(x => x.IsChanged()).ToArray();
  39. if (_updates.Any())
  40. Provider.Save(_stockentitytype,_updates);
  41. //});
  42. //_tasks.Add(_task);
  43. }
  44. //Task.WaitAll(_queries.ToArray());
  45. }
  46. protected override void AfterDelete(Product entity)
  47. {
  48. base.AfterDelete(entity);
  49. UpdateProductComponentCost(entity);
  50. }
  51. }
  52. }