Update_8_55.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace PRS.Shared;
  11. internal class Update_8_55 : DatabaseUpdateScript
  12. {
  13. public override VersionNumber Version => new(8, 55);
  14. public override bool Update()
  15. {
  16. Logger.Send(LogType.Information, "", $"Updating uninitialised stock movement TransferIDs");
  17. var transactionIDs = DbFactory.NewProvider(Logger.Main)
  18. .Query(
  19. Filter<StockMovement>.Where(x => x.Type).InList(StockMovementType.TransferOut, StockMovementType.TransferIn)
  20. .And(x => x.TransferID).IsEqualTo(null),
  21. Columns.None<StockMovement>()
  22. .Add(x => x.Transaction))
  23. .Rows
  24. .Select(x => x.Get<StockMovement, Guid>(x => x.Transaction))
  25. .ToHashSet();
  26. Logger.Send(LogType.Information, "", $"Found {transactionIDs.Count} uninitialised transactions");
  27. var total = transactionIDs.Count;
  28. while (transactionIDs.Count > 0)
  29. {
  30. Logger.Send(LogType.Information, "", $"Updating Stock Movements: {(double)(total - transactionIDs.Count) / total * 100:F2}%");
  31. var nIDs = Math.Min(transactionIDs.Count, 100);
  32. var ids = new Guid[nIDs];
  33. var i = 0;
  34. foreach(var id in transactionIDs)
  35. {
  36. if (i == nIDs) break;
  37. ids[i] = id;
  38. ++i;
  39. }
  40. foreach(var id in ids)
  41. {
  42. transactionIDs.Remove(id);
  43. }
  44. var movementsTable = DbFactory.NewProvider(Logger.Main)
  45. .Query(
  46. Filter<StockMovement>.Where(x => x.Transaction).InList(ids)
  47. .And(x => x.Type).InList(StockMovementType.TransferOut, StockMovementType.TransferIn)
  48. .And(x => x.TransferID).IsEqualTo(null),
  49. Columns.None<StockMovement>()
  50. .Add(x => x.ID)
  51. .Add(x => x.Transaction)
  52. .Add(x => x.Type)
  53. .Add(x => x.Date)
  54. .Add(x => x.System));
  55. var movements =
  56. movementsTable
  57. .ToObjects<StockMovement>()
  58. .GroupByDictionary(x => x.Transaction);
  59. Logger.Send(LogType.Information, "", $"Updating {movementsTable.Rows.Count} movements");
  60. var updates = new List<StockMovement>();
  61. foreach(var (transaction, transactionMovements) in movements)
  62. {
  63. transactionMovements.SortBy(x => x.Date);
  64. while(transactionMovements.Count > 0)
  65. {
  66. var movement = transactionMovements[0];
  67. transactionMovements.RemoveAt(0);
  68. var otherType = movement.Type == StockMovementType.TransferOut ? StockMovementType.TransferIn : StockMovementType.TransferOut;
  69. var transfer = Guid.NewGuid();
  70. movement.TransferID = transfer;
  71. var first = transactionMovements.FirstOrDefault(x => x.Type == otherType);
  72. if(first is not null)
  73. {
  74. first.TransferID = transfer;
  75. transactionMovements.Remove(first);
  76. updates.Add(movement);
  77. updates.Add(first);
  78. }
  79. }
  80. }
  81. DbFactory.NewProvider(Logger.Main).Save(updates);
  82. }
  83. Logger.Send(LogType.Information, "", $"Finished updating uninitialised stock movement TransferIDs");
  84. return true;
  85. }
  86. }