FPosDbManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Dfs.WayneChina.HengshanFPos.FPosDbManager.Model;
  5. namespace Dfs.WayneChina.HengshanFPos.FPosDbManager
  6. {
  7. /// <summary>
  8. /// Sequence number manager
  9. /// </summary>
  10. public class FPosDbManager
  11. {
  12. //private FPosDbContext _context;
  13. //private object syncObj = new object();
  14. NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("HengshanPos");
  15. public FPosDbManager(FPosDbContext context)
  16. {
  17. //_context = context;
  18. }
  19. public bool ReserveTransaction(ushort fPosSqNo, int fcSqNo)
  20. {
  21. using (var _context = new FPosDbContext())
  22. {
  23. var trx = _context.FPosTransactions.FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  24. if (trx != null)
  25. {
  26. if (trx.Reserved)
  27. {
  28. logger.Info($"Current transaction FPosSqNo: {trx.FPosSqNo}, FcSqNo: {trx.FcSqNo} is already reserved by another entity.");
  29. return false;
  30. }
  31. try
  32. {
  33. trx.Reserved = true;
  34. _context.FPosTransactions.Attach(trx);
  35. var entry = _context.Entry(trx);
  36. entry.Property(p => p.Reserved).IsModified = true;
  37. _context.SaveChanges();
  38. logger.Info($"FPos SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, Reserved set to: true");
  39. return true;
  40. }
  41. catch (Exception ex)
  42. {
  43. logger.Info($"Exception in ReserveTransaction: {ex}");
  44. return false;
  45. }
  46. }
  47. else
  48. {
  49. logger.Info($"ReserveTransaction, FPos SqNo {fPosSqNo}, FC SqNo {fcSqNo}, the trx record not found!");
  50. }
  51. return false;
  52. }
  53. }
  54. public bool UnreserveTransaction(ushort fPosSqNo, int fcSqNo)
  55. {
  56. using (var _context = new FPosDbContext())
  57. {
  58. var trx = _context.FPosTransactions.FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  59. if (trx != null)
  60. {
  61. try
  62. {
  63. trx.Reserved = false;
  64. _context.FPosTransactions.Attach(trx);
  65. var entry = _context.Entry(trx);
  66. entry.Property(p => p.Reserved).IsModified = true;
  67. _context.SaveChanges();
  68. logger.Info($"FPos SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, Reserved set to: false");
  69. return true;
  70. }
  71. catch (Exception ex)
  72. {
  73. logger.Info($"Exception in ReserveTransaction: {ex}");
  74. return false;
  75. }
  76. }
  77. else
  78. {
  79. logger.Info($"UnreserveTransaction, FPos SqNo {fPosSqNo}, FC SqNo {fcSqNo}, the trx record not found!");
  80. }
  81. return false;
  82. }
  83. }
  84. public void AddMapping(ushort epsSqNo, int fcSqNo, int pumpId, int nozzleId, int token, decimal fillingAmount)
  85. {
  86. using (var _context = new FPosDbContext())
  87. {
  88. var mapper = new FPosTransaction
  89. {
  90. Reserved = false,
  91. PumpId = pumpId,
  92. NozzleId = nozzleId,
  93. FPosSqNo = epsSqNo,
  94. FcSqNo = fcSqNo,
  95. ReleaseToken = token,
  96. FillingAmount = fillingAmount,
  97. PosItemId = Guid.Empty,
  98. CloudTrxId = Guid.Empty,
  99. Submitted = false
  100. };
  101. logger.Info($"Adding mapping, FPos SqNo={epsSqNo}, FC SqNo={fcSqNo}, PumpId={pumpId}, NozzleId={nozzleId}");
  102. try
  103. {
  104. _context.FPosTransactions.Add(mapper);
  105. _context.SaveChanges();
  106. }
  107. catch (Exception ex)
  108. {
  109. logger.Info($"Exception in inserting a new mapper: {ex.Message}");
  110. }
  111. }
  112. }
  113. #region Un-used
  114. /// <summary>
  115. /// Returns the sequence number of the current fueling or a new one for a new fueling.
  116. /// </summary>
  117. /// <param name="pumpId">Pump id (fueling point id).</param>
  118. /// <param name="nozzleId">Nozzle id of the current pump.</param>
  119. /// <returns>Sequence number.</returns>
  120. public ushort GetSqNo(int pumpId, int nozzleId)
  121. {
  122. using (var _context = new FPosDbContext())
  123. {
  124. try
  125. {
  126. logger.Info($"Checking pumpId = {pumpId} and nozzleId = {nozzleId}");
  127. var sqNoList = _context.SequenceNumbers
  128. .OrderByDescending(s => s.TimeStamp)
  129. .ToList();
  130. var sqNo = sqNoList
  131. .Where(s => s.Ongoing && s.PumpId == pumpId && s.NozzleId == nozzleId)
  132. .FirstOrDefault();
  133. if (sqNo != null)
  134. {
  135. return sqNo.SqNo;
  136. }
  137. logger.Info($"No running transaction for current pumpId: {pumpId} and nozzleId: {nozzleId}, will add a new one.");
  138. var newSqNo = new SequenceNumber
  139. {
  140. PumpId = pumpId,
  141. NozzleId = nozzleId,
  142. Ongoing = true, //Field value in DB: 1
  143. TimeStamp = DateTime.Now
  144. };
  145. _context.SequenceNumbers.Add(newSqNo);
  146. _context.SaveChanges();
  147. logger.Info($"The newly-added SqNo is: {newSqNo.SqNo}");
  148. return newSqNo.SqNo;
  149. }
  150. catch (Exception ex)
  151. {
  152. logger.Info($"Exception in getting new Sequence number: {ex.Message}");
  153. return 0;
  154. }
  155. }
  156. }
  157. #endregion
  158. public ushort GetNewSqNo(int pumpId, int nozzleId)
  159. {
  160. using (var _context = new FPosDbContext())
  161. {
  162. try
  163. {
  164. var newSqNo = new SequenceNumber
  165. {
  166. PumpId = pumpId,
  167. NozzleId = nozzleId,
  168. Ongoing = true, //Field value in DB: 1
  169. TimeStamp = DateTime.Now
  170. };
  171. _context.SequenceNumbers.Add(newSqNo);
  172. _context.SaveChanges();
  173. logger.Info($"Got new SqNo: {newSqNo.SqNo}");
  174. return newSqNo.SqNo;
  175. }
  176. catch (Exception ex)
  177. {
  178. logger.Info($"Exception in creating new SqNo: {ex.Message}");
  179. return 0;
  180. }
  181. }
  182. }
  183. public ushort GetCurrentSqNo(int pumpId, int nozzleId)
  184. {
  185. logger.Info($"Retrieving current SqNo for pumpId = {pumpId} and nozzleId = {nozzleId}");
  186. using (var _context = new FPosDbContext())
  187. {
  188. var sqNoList = _context.SequenceNumbers
  189. .OrderByDescending(s => s.TimeStamp)
  190. .ToList();
  191. var sqNo = sqNoList
  192. .Where(s => s.Ongoing && s.PumpId == pumpId && s.NozzleId == nozzleId)
  193. .FirstOrDefault();
  194. if (sqNo != null)
  195. {
  196. logger.Info($"SqNo for current fueling: {sqNo.SqNo}");
  197. return sqNo.SqNo;
  198. }
  199. return 0;
  200. }
  201. }
  202. public ushort SetFillingDone(int pumpId, int nozzleId)
  203. {
  204. using (var _context = new FPosDbContext())
  205. {
  206. try
  207. {
  208. var sqNo = _context.SequenceNumbers
  209. .Where(s => s.Ongoing && s.PumpId == pumpId && s.NozzleId == nozzleId)
  210. .OrderByDescending(n => n.SqNo)
  211. .FirstOrDefault();
  212. if (sqNo == null)
  213. {
  214. logger.Info($"could not get a SqNo for pump id: {pumpId}, nozzle id: {nozzleId} ");
  215. }
  216. sqNo.Ongoing = false;
  217. _context.SequenceNumbers.Attach(sqNo);
  218. var entry = _context.Entry(sqNo);
  219. entry.Property(p => p.Ongoing).IsModified = true;
  220. _context.SaveChanges();
  221. logger.Info($"Pump id: {pumpId}, nozzle id: {nozzleId}, filling done, ongoing set to false");
  222. return sqNo.SqNo;
  223. }
  224. catch (Exception ex)
  225. {
  226. logger.Info($"Exception in setting filling to be done: {ex.Message}");
  227. return 0;
  228. }
  229. }
  230. }
  231. public bool SetCardType(ushort fPosSqNo, int fcSqNo, int cardType)
  232. {
  233. using (var _context = new FPosDbContext())
  234. {
  235. var trx = _context.FPosTransactions
  236. .FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  237. if (trx != null)
  238. {
  239. try
  240. {
  241. trx.CardType = cardType;
  242. _context.FPosTransactions.Attach(trx);
  243. var entry = _context.Entry(trx);
  244. entry.Property(p => p.CardType).IsModified = true;
  245. _context.SaveChanges();
  246. logger.Info($"pump id:{trx.PumpId}, nozzle id:{trx.NozzleId}, FPOS SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, CardType set to {cardType}");
  247. return true;
  248. }
  249. catch (Exception ex)
  250. {
  251. logger.Info($"Exception in setting card type: {ex}");
  252. return false;
  253. }
  254. }
  255. else
  256. {
  257. logger.Info($"Set card type, Could not find the match in FPos database with FPos SqNo:{fPosSqNo} and FC SqNo:{fcSqNo}");
  258. return false;
  259. }
  260. }
  261. }
  262. public bool SetActualPayAmount(ushort fPosSqNo, int fcSqNo, decimal amount)
  263. {
  264. using (var _context = new FPosDbContext())
  265. {
  266. var trx = _context.FPosTransactions
  267. .FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  268. if (trx != null)
  269. {
  270. try
  271. {
  272. trx.ActualPayAmount = amount;
  273. _context.FPosTransactions.Attach(trx);
  274. var entry = _context.Entry(trx);
  275. entry.Property(p => p.ActualPayAmount).IsModified = true;
  276. _context.SaveChanges();
  277. logger.Info($"Entry pump id:{trx.PumpId}, nozzle id:{trx.NozzleId}, FPOS SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, ActualPayAmount=>{amount}");
  278. return true;
  279. }
  280. catch (Exception ex)
  281. {
  282. logger.Info($"Exception in setting actual payment amount: {ex}");
  283. return false;
  284. }
  285. }
  286. else
  287. {
  288. logger.Info($"Could not find the match in FPos database with FPos SqNo:{fPosSqNo} and FC SqNo:{fcSqNo}");
  289. return false;
  290. }
  291. }
  292. }
  293. public FPosTransaction GetCurrentTrx(ushort currentFPosSqNo, int currentFdcSqNo)
  294. {
  295. using (var _context = new FPosDbContext())
  296. {
  297. return _context.FPosTransactions
  298. .FirstOrDefault(t => t.Reserved && t.FPosSqNo == currentFPosSqNo && t.FcSqNo == currentFdcSqNo);
  299. }
  300. }
  301. public bool SetPosItemId(ushort fPosSqNo, int fcSqNo, Guid itemId)
  302. {
  303. using (var _context = new FPosDbContext())
  304. {
  305. var trx = _context.FPosTransactions
  306. .FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  307. if (trx != null)
  308. {
  309. try
  310. {
  311. trx.PosItemId = itemId;
  312. _context.FPosTransactions.Attach(trx);
  313. var entry = _context.Entry(trx);
  314. entry.Property(p => p.PosItemId).IsModified = true;
  315. _context.SaveChanges();
  316. logger.Info($"FPos SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, PosItemId set to {itemId}");
  317. return true;
  318. }
  319. catch (Exception ex)
  320. {
  321. logger.Info($"Exception in setting PosItemId: {ex}");
  322. return false;
  323. }
  324. }
  325. else
  326. {
  327. logger.Info($"Set PosItemId, FPos SqNo {fPosSqNo}, FC SqNo {fcSqNo}, the trx record not found!");
  328. }
  329. return false;
  330. }
  331. }
  332. public bool SetTransactionId(ushort fPosSqNo, int fcSqNo, Guid trxId)
  333. {
  334. using (var _context = new FPosDbContext())
  335. {
  336. var trx = _context.FPosTransactions
  337. .FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  338. if (trx != null)
  339. {
  340. try
  341. {
  342. trx.CloudTrxId = trxId;
  343. _context.FPosTransactions.Attach(trx);
  344. var entry = _context.Entry(trx);
  345. entry.Property(p => p.CloudTrxId).IsModified = true;
  346. _context.SaveChanges();
  347. logger.Info($"FPos SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, CloudTrxId set to {trxId}");
  348. return true;
  349. }
  350. catch (Exception ex)
  351. {
  352. logger.Info($"Exception in setting CloudTrxId: {ex}");
  353. return false;
  354. }
  355. }
  356. else
  357. {
  358. logger.Info($"Set CloudTrxId, FPos SqNo {fPosSqNo}, FC SqNo {fcSqNo}, the trx record not found!");
  359. }
  360. return false;
  361. }
  362. }
  363. public bool SetTransactionSubmitted(ushort fPosSqNo, int fcSqNo, bool submitted)
  364. {
  365. using (var _context = new FPosDbContext())
  366. {
  367. var trx = _context.FPosTransactions
  368. .FirstOrDefault(t => t.FPosSqNo == fPosSqNo && t.FcSqNo == fcSqNo);
  369. if (trx != null)
  370. {
  371. try
  372. {
  373. trx.Submitted = submitted;
  374. _context.FPosTransactions.Attach(trx);
  375. var entry = _context.Entry(trx);
  376. entry.Property(p => p.Submitted).IsModified = true;
  377. _context.SaveChanges();
  378. logger.Info($"FPos SqNo:{fPosSqNo}, FC SqNo:{fcSqNo}, Submitted set to {submitted}");
  379. return true;
  380. }
  381. catch (Exception ex)
  382. {
  383. logger.Info($"Exception in setting Submitted: {ex}");
  384. return false;
  385. }
  386. }
  387. else
  388. {
  389. logger.Info($"Set Submitted, FPos SqNo {fPosSqNo}, FC SqNo {fcSqNo}, the trx record not found!");
  390. }
  391. return false;
  392. }
  393. }
  394. public void CleanSqNo()
  395. {
  396. using (var _context = new FPosDbContext())
  397. {
  398. try
  399. {
  400. var oldSqNos = _context
  401. .SequenceNumbers
  402. .Where(sq => sq.TimeStamp < DateTime.Now.Subtract(new TimeSpan(0, 1, 0)));
  403. var mappers = new List<FPosTransaction>();
  404. foreach (var sqNo in oldSqNos.ToList())
  405. {
  406. var mapper = _context.FPosTransactions
  407. .FirstOrDefault(m => m.FPosSqNo == sqNo.SqNo && m.Submitted);
  408. if (mapper != null)
  409. mappers.Add(mapper);
  410. }
  411. _context.SequenceNumbers.RemoveRange(oldSqNos.ToArray());
  412. _context.FPosTransactions.RemoveRange(mappers.ToArray());
  413. _context.SaveChanges();
  414. }
  415. catch (Exception ex)
  416. {
  417. logger.Info($"Exception in deleting submitted trx sequence no: {ex.Message}");
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Get the POS sequence number.
  423. /// </summary>
  424. /// <param name="pumpId">pump id.</param>
  425. /// <param name="nozzleId">nozzle id.</param>
  426. /// <param name="fcSqNo">FDC transaction sequence number.</param>
  427. /// <returns></returns>
  428. public int GetSqNoByMapping(int pumpId, int nozzleId, int fcSqNo)
  429. {
  430. using (var _context = new FPosDbContext())
  431. {
  432. try
  433. {
  434. var mapper = _context.FPosTransactions
  435. .OrderBy(s => s.Id)
  436. .FirstOrDefault(s => s.PumpId == pumpId && s.NozzleId == nozzleId && s.FcSqNo == fcSqNo);
  437. if (mapper != null)
  438. {
  439. return mapper.FPosSqNo;
  440. }
  441. else
  442. {
  443. logger.Info($"Could not find the POS SqNo by pumpId={pumpId} and nozzleId={nozzleId} for FcSqNo={fcSqNo}");
  444. return 0;
  445. }
  446. }
  447. catch (Exception ex)
  448. {
  449. logger.Info($"Exception in get SqNo by FcSqNo={fcSqNo} from mapping table: {ex.Message}");
  450. return 0;
  451. }
  452. }
  453. }
  454. }
  455. }