EpsTransactionQuery.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Dfs.WayneChina.SinochemEps;
  2. using SinoChemCommonUtilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Wayne.Lib.Log;
  10. namespace SinochemInternetPlusApp
  11. {
  12. class EpsTransactionQuery
  13. {
  14. private static SqlCommandUtility sqlCmd = new SqlCommandUtility(SinochemEpsApp.PosDatabaseConnString);
  15. /// <summary>
  16. /// Get a readonly list of all eps transactions
  17. /// </summary>
  18. /// <returns></returns>
  19. public static List<EpsTransactionModel> GetAllEpsTrxModels()
  20. {
  21. string querySql = string.Format(" select * from eps_trx ");
  22. return sqlCmd.ReadData<List<EpsTransactionModel>>(querySql, DbModelConstructors.ConstructEpsTrxModelList);
  23. }
  24. /// <summary>
  25. /// Get a readonly list of the eps transactions for a certain fueling point
  26. /// </summary>
  27. /// <param name="nozzleId"></param>
  28. /// <returns></returns>
  29. public static List<EpsTransactionModel> GetAllEpsTrxModelsOf(int nozzleId)
  30. {
  31. string querySql = string.Format(" select * from eps_trx where jihao={0} ", nozzleId);
  32. return sqlCmd.ReadData<List<EpsTransactionModel>>(querySql, DbModelConstructors.ConstructEpsTrxModelList);
  33. }
  34. /// <summary>
  35. /// Get a readonly list of the car plate eps transactions for a list of fueling points, except for invalid transactions
  36. /// </summary>
  37. /// <param name="nozzles"></param>
  38. /// <returns></returns>
  39. public static List<EpsTransactionModel> GetValidCarPlateEpsTrxModels(List<int> nozzles, int topCount, DebugLogger debugLogger)
  40. {
  41. const int TrxInProgressCategory = 1;
  42. const int TrxAlreadyDoneCategory = 2;
  43. List<EpsTransactionModel> epxTrxList = GetCarPlateEpsTrxModelsByTrxStatusCategory(nozzles, -1, TrxInProgressCategory, debugLogger);
  44. List<EpsTransactionModel> alreadyDoneTrxList = GetCarPlateEpsTrxModelsByTrxStatusCategory(nozzles, topCount, TrxAlreadyDoneCategory, debugLogger);
  45. epxTrxList.AddRange(alreadyDoneTrxList);
  46. return epxTrxList;
  47. }
  48. /// <summary>
  49. /// Get car plate trxes by Trx status category.
  50. /// </summary>
  51. private static List<EpsTransactionModel> GetCarPlateEpsTrxModelsByTrxStatusCategory
  52. (
  53. List<int> nozzles,
  54. int topCount,
  55. int trxStatusCategory,
  56. DebugLogger debugLogger
  57. )
  58. {
  59. StringBuilder sb = new StringBuilder();
  60. // 1) select clause
  61. if (topCount >= 0)
  62. {
  63. sb.Append(string.Format(" select top {0} * from eps_trx where", topCount));
  64. }
  65. else
  66. {
  67. sb.Append(" select * from eps_trx where");
  68. }
  69. // 2) which jihao
  70. sb.Append(" (");
  71. for (int i = 0; i < nozzles.Count; i++)
  72. {
  73. if (i != nozzles.Count - 1)
  74. sb.Append($"jihao = {nozzles[i]} or ");
  75. else
  76. sb.Append($"jihao = {nozzles[i]}");
  77. }
  78. sb.Append(")");
  79. // 3) which status category
  80. sb.Append(" and " + ConvertTrxStatusCategoryToSql(trxStatusCategory));
  81. // 4) car plate mop
  82. sb.Append($" and mop = {(int)TransactionMode.CarPlateMode}");
  83. // 5) order
  84. sb.Append(" order by created_time desc");
  85. debugLogger.Add(sb);
  86. return sqlCmd.ReadData<List<EpsTransactionModel>>(sb.ToString(), DbModelConstructors.ConstructEpsTrxModelList);
  87. }
  88. /// <summary>
  89. /// Trx status category(valid means not Removed or Unknown):
  90. /// 1 -- trx in progress
  91. /// 2 -- trx done and valid
  92. /// </summary>
  93. /// <returns></returns>
  94. private static string ConvertTrxStatusCategoryToSql(int trxStatusCategory)
  95. {
  96. string sqlWhere = "";
  97. switch (trxStatusCategory)
  98. {
  99. case 1:
  100. sqlWhere = string.Format
  101. ($"(trx_status = {(int)EpsTrxStatus.BeforeFueling} " +
  102. $"or trx_status = {(int)EpsTrxStatus.Fueling} " +
  103. $"or trx_status = {(int)EpsTrxStatus.FuelingDone} " +
  104. $"or trx_status = {(int)EpsTrxStatus.BeforePayment})");
  105. break;
  106. case 2:
  107. sqlWhere = string.Format
  108. ($"(trx_status = {(int)EpsTrxStatus.PaymentOk} " +
  109. $"or trx_status = {(int)EpsTrxStatus.PaymentNeedConfirm} " +
  110. $"or trx_status = {(int)EpsTrxStatus.PaymentFailed} " +
  111. $"or trx_status = {(int)EpsTrxStatus.PaymentOkButNeedRefund} " +
  112. $"or trx_status = {(int)EpsTrxStatus.PaymentRefunded})");
  113. break;
  114. default:
  115. break;
  116. }
  117. return sqlWhere;
  118. }
  119. /// <summary>
  120. /// Get a list of eps transactions in certain status and after certain date time
  121. /// </summary>
  122. /// <returns></returns>
  123. public static List<EpsTransaction> GetEpsTrxesByTrxStatusAndCreatedTime(EpsTrxStatus trxStatus, DateTime createdTimeStart, bool ignoreCreatedTimeStart)
  124. {
  125. string querySql = "";
  126. if (ignoreCreatedTimeStart)
  127. {
  128. querySql = string.Format(" select * from eps_trx where trx_status = {0}",
  129. (int)trxStatus);
  130. }
  131. else
  132. {
  133. querySql = string.Format(" select * from eps_trx where trx_status = {0} and created_time >= '{1}'",
  134. (int)trxStatus, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss"));
  135. }
  136. return sqlCmd.ReadData<List<EpsTransaction>>(querySql, DbModelConstructors.ConstructEpsTrxList);
  137. }
  138. /// <summary>
  139. /// Get a list of eps transactions in certain notify pos flag and after certain date time
  140. /// </summary>
  141. /// <returns></returns>
  142. public static List<EpsTransaction> GetEpsTrxesByNotifyPosFlagAndCreatedTime(NotifyPosFlag notifyPos, DateTime createdTimeStart)
  143. {
  144. string querySql = string.Format(" select * from eps_trx where notify_pos = {0} and created_time >= '{1}'",
  145. (int)notifyPos, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss"));
  146. return sqlCmd.ReadData<List<EpsTransaction>>(querySql, DbModelConstructors.ConstructEpsTrxList);
  147. }
  148. /// <summary>
  149. /// not all eps trxes can be removed; need to check eps trx status
  150. /// </summary>
  151. /// <param name="createdTimeStop"></param>
  152. public static void DeleteEpsTrxesBefore(DateTime createdTimeStop)
  153. {
  154. StringBuilder deleteSqlSb = new StringBuilder();
  155. deleteSqlSb.AppendFormat(" delete from eps_trx where created_time < '{0}'", createdTimeStop.ToString("yyyy-MM-dd HH:mm:ss"));
  156. deleteSqlSb.Append(" and (");
  157. deleteSqlSb.AppendFormat("trx_status={0}", (int)EpsTrxStatus.Removed);
  158. deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentRefunded);
  159. deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentFailed);
  160. deleteSqlSb.AppendFormat(" or (trx_status={0} and notify_pos={1})", (int)EpsTrxStatus.PaymentOk, (int)NotifyPosFlag.NotifyOk);
  161. deleteSqlSb.Append(")");
  162. sqlCmd.DeleteData(deleteSqlSb.ToString());
  163. }
  164. }
  165. }