JobScopeStatusStore.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Comal.Classes;
  2. using InABox.Core;
  3. namespace Comal.Stores;
  4. internal class JobScopeStatusStore : BaseStore<JobScopeStatus>
  5. {
  6. protected override void BeforeSave(JobScopeStatus entity)
  7. {
  8. base.BeforeSave(entity);
  9. var others = Provider.Query(new Filter<JobScopeStatus>(x => x.ID).IsNotEqualTo(entity.ID)
  10. .And(new Filter<JobScopeStatus>(x=>x.ContractDefault).IsEqualTo(true)
  11. .Or(x=>x.VariationDefault).IsEqualTo(true)
  12. .Or(x=>x.AdjustmentDefault).IsEqualTo(true)
  13. )
  14. ).ToObjects<JobScopeStatus>().ToList();
  15. var otherContracts = others.Where(x=>x.ContractDefault);
  16. if (entity.ContractDefault)
  17. {
  18. foreach (var otherContract in otherContracts)
  19. otherContract.ContractDefault = false;
  20. }
  21. else if (!otherContracts.Any())
  22. entity.ContractDefault = true;
  23. var otherVariations = others.Where(x=>x.VariationDefault);
  24. if (entity.VariationDefault)
  25. {
  26. foreach (var otherVariation in otherVariations)
  27. otherVariation.VariationDefault = false;
  28. }
  29. else if (!otherVariations.Any())
  30. entity.VariationDefault = true;
  31. var otherAdjustments = others.Where(x=>x.AdjustmentDefault);
  32. if (entity.AdjustmentDefault)
  33. {
  34. foreach (var otherAdjustment in otherAdjustments)
  35. otherAdjustment.AdjustmentDefault = false;
  36. }
  37. else if (!otherAdjustments.Any())
  38. entity.AdjustmentDefault = true;
  39. Provider.Save(others.Where(x=>x.IsChanged()));
  40. }
  41. }