SetoutStore.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Syncfusion.Pdf.Parsing;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Drawing.Imaging;
  9. using InABox.Database;
  10. using System;
  11. namespace Comal.Stores
  12. {
  13. public class SetoutStore : BaseStore<Setout>
  14. {
  15. protected override void AfterSave(Setout entity)
  16. {
  17. if (entity.Group.ID != Guid.Empty)
  18. {
  19. var table = Provider.Query<SetoutGroup>(new Filter<SetoutGroup>(x => x.ID).IsEqualTo(entity.Group.ID)
  20. .And(x => x.OptimizationDocument.ID).IsNotEqualTo(Guid.Empty),
  21. Columns.None<SetoutGroup>().Add(x => x.OptimizationDocument.ID));
  22. if (!table.Rows.Any())
  23. return;
  24. var docID = table.Rows.FirstOrDefault().Get<SetoutGroup, Guid>(x => x.OptimizationDocument.ID);
  25. UpdateOptimisationDocument(FindSubStore<SetoutDocument>, entity, docID);
  26. }
  27. base.AfterSave(entity);
  28. }
  29. /// <summary>
  30. /// Update the optimisation document ID for a setout. The setout must have a non-empty <see cref="Entity.ID"/>.
  31. /// </summary>
  32. /// <param name="docStore"></param>
  33. /// <param name="setout">Setout with non-empty <see cref="Entity.ID"/></param>
  34. /// <param name="optimisationDocumentID"></param>
  35. public void UpdateOptimisationDocument(Func<IStore<SetoutDocument>> docStore, Setout setout, Guid optimisationDocumentID)
  36. {
  37. if (optimisationDocumentID == Guid.Empty) return;
  38. var ids = new List<Guid>();
  39. var docsTable = Provider.Query<SetoutDocument>(
  40. new Filter<SetoutDocument>(x => x.EntityLink.ID).IsEqualTo(setout.ID),
  41. Columns.None<SetoutDocument>().Add(x => x.DocumentLink.ID)
  42. );
  43. foreach (var row in docsTable.Rows)
  44. ids.Add(row.Get<SetoutDocument, Guid>(x => x.DocumentLink.ID));
  45. if (!ids.Contains(optimisationDocumentID))
  46. {
  47. var setoutdoc = new SetoutDocument();
  48. setoutdoc.EntityLink.ID = setout.ID;
  49. setoutdoc.DocumentLink.ID = optimisationDocumentID;
  50. docStore().Save(setoutdoc, "Added optimsation document from Group");
  51. }
  52. }
  53. protected override void BeforeDelete(Setout entity)
  54. {
  55. UnlinkTrackingKanban<SetoutKanban, Setout, SetoutLink>(entity);
  56. }
  57. }
  58. }