TrxScanner.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;using Edge.Core.IndustryStandardInterface.Pump;
  2. using Dfs.WayneChina.HengshanFPos.FPosDbManager;
  3. using Dfs.WayneChina.SpsDbModels.Models;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Dfs.WayneChina.CardTrxManager.TrxScanner
  11. {
  12. /// <summary>
  13. /// Scans the IC card trx within Hengshan MySQL Sps_db (table TrdInfo)
  14. /// </summary>
  15. public class TrxScanner
  16. {
  17. private FPosDbManager fPosDbManager;
  18. public event EventHandler<MatchingTrxFoundEventArgs> OnMatchingTrxFound;
  19. public event EventHandler<NoMatchFoundEventArgs> OnNoMatchFound;
  20. private static NLog.Logger scannerLogger = NLog.LogManager.LoadConfiguration("Nlog.config").GetLogger("TrxScanner");
  21. private readonly int retryLimit = 30;
  22. public TrxScanner(FPosDbManager fPosDbManager)
  23. {
  24. this.fPosDbManager = fPosDbManager;
  25. }
  26. public void StartScanAsync(int fdcPumpId, int epsSqNo, int fdcSeqNo, int releaseToken, FdcTransaction fdcTrx)
  27. {
  28. bool matchFound = false;
  29. int retries = retryLimit;
  30. //default set to Cash, which should be handled by indoor POS.
  31. var paymentMethod = "CASH";
  32. Task.Run(async () =>
  33. {
  34. using (var db = new SpsDbContext())
  35. {
  36. TTrdinfo matchingRecord = null;
  37. while (retries-- >= 0)
  38. {
  39. try
  40. {
  41. scannerLogger.Debug($"Starting to find a matching record with Fdc Pump Id: {fdcPumpId}, EpsSeqNo: {epsSqNo}");
  42. //
  43. //PaymodeId = 100, customer card payment
  44. //PaymodeId = 103, operator card payment
  45. //
  46. matchingRecord = db.TTrdinfo
  47. .OrderByDescending(t => t.TtctimeEnd)
  48. .FirstOrDefault(t => t.PumpNo == (byte)(fdcPumpId)
  49. && t.PaymodeId.HasValue && (t.PaymodeId.Value == 100 || t.PaymodeId.Value == 103)
  50. && t.SeqNo.HasValue && t.SeqNo == epsSqNo
  51. && t.Mon.Value != 0
  52. && (t.TrdType == 0x00 || t.TrdType == 0x01) // 0x00 = Normal trx; 0x01 = Gray card trx.
  53. && (DateTime.Now - t.Ttctime).TotalMinutes < TimeSpan.FromDays(1).TotalMinutes);
  54. if (matchingRecord != null)
  55. {
  56. scannerLogger.Info("Found the matching record in MySql");
  57. // only Customer IC Card paid trx treated as 'IC'
  58. paymentMethod =
  59. (matchingRecord.CardType.HasValue && (matchingRecord.CardType == (byte)ICCardType.Customer))
  60. ? "IC"
  61. : "CASH";
  62. if (matchingRecord.CardType.HasValue && (matchingRecord.CardType == (byte)ICCardType.Customer))
  63. {
  64. fPosDbManager.SetCardType((ushort)matchingRecord.SeqNo, fdcSeqNo, (int)ICCardType.Customer);
  65. scannerLogger.Info($"match found, set card type to {ICCardType.Customer}");
  66. }
  67. scannerLogger.Debug($" The matching trx is paid with MOP: {paymentMethod}");
  68. matchFound = true;
  69. }
  70. else
  71. {
  72. scannerLogger.Info("No matching trx found in MySql at this round");
  73. }
  74. }
  75. catch (Exception e)
  76. {
  77. scannerLogger.Info("Exception in finding the matching transaction: " + e);
  78. }
  79. if (matchFound || retries == 0)
  80. break;
  81. int round = retryLimit - retries;
  82. if (round <= 5)
  83. {
  84. scannerLogger.Info($"Scan count: {round}");
  85. }
  86. else if (round > 5 && round <= 10)
  87. {
  88. scannerLogger.Info($"# Scan count: {round}");
  89. }
  90. else if (round > 10 && round <= 20)
  91. {
  92. scannerLogger.Info($"## Scan count: {round}");
  93. }
  94. else
  95. {
  96. scannerLogger.Info($"### Scan count: {round}");
  97. }
  98. await Task.Delay(1000);
  99. }
  100. if (matchFound)
  101. {
  102. scannerLogger.Info($"CardNo: {matchingRecord.CardNo}, RealMon: {matchingRecord.RealMon}");
  103. fPosDbManager.SetActualPayAmount((ushort)epsSqNo, fdcSeqNo, (decimal)matchingRecord.RealMon / 100);
  104. scannerLogger.Info($"Set ActualPayAmount { (decimal)matchingRecord.RealMon / 100} to FPOS trx SqNo: {epsSqNo}");
  105. OnMatchingTrxFound?.Invoke(this, new MatchingTrxFoundEventArgs
  106. {
  107. MOP = paymentMethod,
  108. QRCode = matchingRecord.CardNo,
  109. FdcSqNo = fdcSeqNo,
  110. ReleaseToken = releaseToken,
  111. TrdInfo = matchingRecord,
  112. FdcTrx = fdcTrx
  113. });
  114. }
  115. else
  116. {
  117. scannerLogger.Info("Could not find a match, giving up...");
  118. OnNoMatchFound?.Invoke(this, new NoMatchFoundEventArgs
  119. {
  120. FdcSqNo = fdcSeqNo,
  121. ReleaseToken = releaseToken,
  122. FdcTrx = fdcTrx
  123. });
  124. }
  125. }
  126. });
  127. }
  128. }
  129. }