EpsTransactionQuery.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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(GenericSinochemEpsApp.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. List<EpsTransactionModel> epxTrxList = GetCarPlateEpsTrxModelsByTrxStatusCategory(nozzles, topCount, debugLogger);
  41. return epxTrxList;
  42. }
  43. /// <summary>
  44. /// Get car plate trxes by Trx status category.
  45. /// </summary>
  46. private static List<EpsTransactionModel> GetCarPlateEpsTrxModelsByTrxStatusCategory
  47. (
  48. List<int> nozzles,
  49. int topCount,
  50. DebugLogger debugLogger
  51. )
  52. {
  53. StringBuilder sb = new StringBuilder();
  54. // 1) select clause
  55. if (topCount >= 0)
  56. {
  57. sb.Append(string.Format(" select top {0} * from eps_trx where", topCount));
  58. }
  59. else
  60. {
  61. sb.Append(" select * from eps_trx where");
  62. }
  63. // 2) which jihao
  64. sb.Append(" (");
  65. for (int i = 0; i < nozzles.Count; i++)
  66. {
  67. if (i != nozzles.Count - 1)
  68. sb.Append($"jihao = {nozzles[i]} or ");
  69. else
  70. sb.Append($"jihao = {nozzles[i]}");
  71. }
  72. sb.Append(")");
  73. // 3) which status category
  74. sb.Append(" and " + ConvertTrxStatusCategoryToSql());
  75. // 4) car plate mop
  76. sb.Append($" and mop = {(int)EpsTransactionMode.CarPlateMode}");
  77. // 5) order
  78. sb.Append(" order by created_time desc");
  79. debugLogger.Add(sb);
  80. return sqlCmd.ReadData<List<EpsTransactionModel>>(sb.ToString(), DbModelConstructors.ConstructEpsTrxModelList);
  81. }
  82. /// <summary>
  83. /// Trx status category(valid means not Removed or Unknown):
  84. /// 1 -- trx in progress
  85. /// 2 -- trx done and valid
  86. /// </summary>
  87. /// <returns></returns>
  88. private static string ConvertTrxStatusCategoryToSql()
  89. {
  90. //string sqlWhere = "";
  91. string sqlWhere = string.Format
  92. ($"(trx_status = {(int)EpsTrxStatus.BeforeFueling} " +
  93. $"or trx_status = {(int)EpsTrxStatus.Fueling} " +
  94. $"or trx_status = {(int)EpsTrxStatus.FuelingDone} " +
  95. $"or trx_status = {(int)EpsTrxStatus.BeforePayment}" +
  96. $"or trx_status = {(int)EpsTrxStatus.PaymentOk} " +
  97. $"or trx_status = {(int)EpsTrxStatus.PaymentNeedConfirm} " +
  98. $"or trx_status = {(int)EpsTrxStatus.PaymentFailed} " +
  99. $"or trx_status = {(int)EpsTrxStatus.PaymentOkButNeedRefund} " +
  100. $"or trx_status = {(int)EpsTrxStatus.PaymentRefunded})");
  101. //switch (trxStatusCategory)
  102. //{
  103. // case 1:
  104. // sqlWhere = string.Format
  105. // ($"(trx_status = {(int)EpsTrxStatus.BeforeFueling} " +
  106. // $"or trx_status = {(int)EpsTrxStatus.Fueling} " +
  107. // $"or trx_status = {(int)EpsTrxStatus.FuelingDone} " +
  108. // $"or trx_status = {(int)EpsTrxStatus.BeforePayment})");
  109. // break;
  110. // case 2:
  111. // sqlWhere = string.Format
  112. // ($"(trx_status = {(int)EpsTrxStatus.PaymentOk} " +
  113. // $"or trx_status = {(int)EpsTrxStatus.PaymentNeedConfirm} " +
  114. // $"or trx_status = {(int)EpsTrxStatus.PaymentFailed} " +
  115. // $"or trx_status = {(int)EpsTrxStatus.PaymentOkButNeedRefund} " +
  116. // $"or trx_status = {(int)EpsTrxStatus.PaymentRefunded})");
  117. // break;
  118. // default:
  119. // break;
  120. //}
  121. return sqlWhere;
  122. }
  123. /// <summary>
  124. /// Get a list of eps transactions in certain status and after certain date time
  125. /// </summary>
  126. /// <returns></returns>
  127. public static List<EpsTransaction> GetEpsTrxesByTrxStatusAndCreatedTime(EpsTrxStatus trxStatus, DateTime createdTimeStart, bool ignoreCreatedTimeStart)
  128. {
  129. string querySql = "";
  130. if (ignoreCreatedTimeStart)
  131. {
  132. querySql = string.Format(" select * from eps_trx where trx_status = {0}",
  133. (int)trxStatus);
  134. }
  135. else
  136. {
  137. querySql = string.Format(" select * from eps_trx where trx_status = {0} and created_time >= '{1}'",
  138. (int)trxStatus, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss"));
  139. }
  140. return sqlCmd.ReadData<List<EpsTransaction>>(querySql, DbModelConstructors.ConstructEpsTrxList);
  141. }
  142. /// <summary>
  143. /// Get a list of eps transactions in certain notify pos flag and after certain date time
  144. /// </summary>
  145. /// <returns></returns>
  146. public static List<EpsTransaction> GetEpsTrxesByNotifyPosFlagAndCreatedTime(NotifyPosFlag notifyPos, DateTime createdTimeStart)
  147. {
  148. string querySql = string.Format(" select * from eps_trx where notify_pos = {0} and created_time >= '{1}'",
  149. (int)notifyPos, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss"));
  150. return sqlCmd.ReadData<List<EpsTransaction>>(querySql, DbModelConstructors.ConstructEpsTrxList);
  151. }
  152. public static EpsTransactionModel GetEpsTrxByTTC(string ttc, int nozzleId)
  153. {
  154. string querySql = string.Format("select * from eps_trx where ttc = '{0}' and jihao = {1}", ttc, nozzleId);
  155. var epsTrxList = sqlCmd.ReadData<List<EpsTransactionModel>>(querySql, DbModelConstructors.ConstructEpsTrxModelList);
  156. if (epsTrxList != null)
  157. return epsTrxList.FirstOrDefault();
  158. else
  159. return null;
  160. }
  161. public static EpsTransactionModel GetEpsTrxByLiuShuiNO(string liushuino, int nozzleId)
  162. {
  163. string querySql = string.Format("select * from eps_trx where liushuino = '{0}' and jihao = {1}", liushuino, nozzleId);
  164. var epsTrxList = sqlCmd.ReadData<List<EpsTransactionModel>>(querySql, DbModelConstructors.ConstructEpsTrxModelList);
  165. if (epsTrxList != null)
  166. return epsTrxList.FirstOrDefault();
  167. else
  168. return null;
  169. }
  170. public static EpsTransactionModel RefreshEpsTrx(int id)
  171. {
  172. string querySql = string.Format("select * from eps_trx where id = '{0}' ", id);
  173. var epsTrxList = sqlCmd.ReadData<List<EpsTransactionModel>>(querySql, DbModelConstructors.ConstructEpsTrxModelList);
  174. if (epsTrxList != null)
  175. return epsTrxList.FirstOrDefault();
  176. else
  177. return null;
  178. }
  179. public static void UpdateEpsTrxByttc(string ttc, int nozzleId, decimal amount, string openId, string payMethod, EpsTrxStatus trxStatus)
  180. {
  181. string sql = string.Format("update eps_trx" +
  182. " set real_pay_amount = {0},trx_status = {1}, openId = '{2}', payMethod = '{3}'" +
  183. " where ttc = '{4}' and jihao = {5}",
  184. amount, (int)trxStatus, openId, payMethod, ttc, nozzleId);
  185. sqlCmd.UpdateData(sql);
  186. }
  187. public static void UpdateEpsTrxByliushuino(string liushuino, int nozzleId, decimal amount, EpsTrxStatus trxStatus)
  188. {
  189. string sql = string.Format("update eps_trx" +
  190. " set real_pay_amount = {0},trx_status = {1}" +
  191. " where liushuino = '{2}' and jihao = {3}",
  192. amount, (int)trxStatus, liushuino, nozzleId);
  193. sqlCmd.UpdateData(sql);
  194. }
  195. public static void DeleteXiaoFei2(string liushuino, int nozzleId)
  196. {
  197. string sql = string.Format("delete from xiaofei2 where liushuino = '{0}' and jihao = {1}", liushuino, nozzleId);
  198. sqlCmd.DeleteData(sql);
  199. }
  200. /// <summary>
  201. /// not all eps trxes can be removed; need to check eps trx status
  202. /// </summary>
  203. /// <param name="createdTimeStop"></param>
  204. public static void DeleteEpsTrxesBefore(DateTime createdTimeStop)
  205. {
  206. StringBuilder deleteSqlSb = new StringBuilder();
  207. deleteSqlSb.AppendFormat(" delete from eps_trx where created_time < '{0}'", createdTimeStop.ToString("yyyy-MM-dd HH:mm:ss"));
  208. deleteSqlSb.Append(" and (");
  209. deleteSqlSb.AppendFormat("trx_status={0}", (int)EpsTrxStatus.Removed);
  210. deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentRefunded);
  211. deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentFailed);
  212. deleteSqlSb.AppendFormat(" or (trx_status={0} and notify_pos={1})", (int)EpsTrxStatus.PaymentOk, (int)NotifyPosFlag.NotifyOk);
  213. deleteSqlSb.Append(")");
  214. sqlCmd.DeleteData(deleteSqlSb.ToString());
  215. }
  216. public static void DeleteEpsTrxPendingBefore(DateTime createdTimeStop)
  217. {
  218. StringBuilder deleteSqlSb = new StringBuilder();
  219. deleteSqlSb.AppendFormat(" delete from eps_trx where created_time < '{0}'", createdTimeStop.ToString("yyyy-MM-dd HH:mm:ss"));
  220. deleteSqlSb.Append(" and ");
  221. deleteSqlSb.AppendFormat("trx_status={0}", (int)EpsTrxStatus.BeforeFueling);
  222. sqlCmd.DeleteData(deleteSqlSb.ToString());
  223. }
  224. }
  225. }