EpsTransactionQuery.cs 7.6 KB

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