using Dfs.WayneChina.SinochemEps; using SinoChemCommonUtilities; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using Wayne.Lib.Log; namespace SinochemInternetPlusApp { class EpsTransactionQuery { private static SqlCommandUtility sqlCmd = new SqlCommandUtility(SinochemEpsApp.PosDatabaseConnString); /// /// Get a readonly list of all eps transactions /// /// public static List GetAllEpsTrxModels() { string querySql = string.Format(" select * from eps_trx "); return sqlCmd.ReadData>(querySql, DbModelConstructors.ConstructEpsTrxModelList); } /// /// Get a readonly list of the eps transactions for a certain fueling point /// /// /// public static List GetAllEpsTrxModelsOf(int nozzleId) { string querySql = string.Format(" select * from eps_trx where jihao={0} ", nozzleId); return sqlCmd.ReadData>(querySql, DbModelConstructors.ConstructEpsTrxModelList); } /// /// Get a readonly list of the car plate eps transactions for a list of fueling points, except for invalid transactions /// /// /// public static List GetValidCarPlateEpsTrxModels(List nozzles, int topCount, DebugLogger debugLogger) { const int TrxInProgressCategory = 1; const int TrxAlreadyDoneCategory = 2; List epxTrxList = GetCarPlateEpsTrxModelsByTrxStatusCategory(nozzles, -1, TrxInProgressCategory, debugLogger); List alreadyDoneTrxList = GetCarPlateEpsTrxModelsByTrxStatusCategory(nozzles, topCount, TrxAlreadyDoneCategory, debugLogger); epxTrxList.AddRange(alreadyDoneTrxList); return epxTrxList; } /// /// Get car plate trxes by Trx status category. /// private static List GetCarPlateEpsTrxModelsByTrxStatusCategory ( List nozzles, int topCount, int trxStatusCategory, DebugLogger debugLogger ) { StringBuilder sb = new StringBuilder(); // 1) select clause if (topCount >= 0) { sb.Append(string.Format(" select top {0} * from eps_trx where", topCount)); } else { sb.Append(" select * from eps_trx where"); } // 2) which jihao sb.Append(" ("); for (int i = 0; i < nozzles.Count; i++) { if (i != nozzles.Count - 1) sb.Append($"jihao = {nozzles[i]} or "); else sb.Append($"jihao = {nozzles[i]}"); } sb.Append(")"); // 3) which status category sb.Append(" and " + ConvertTrxStatusCategoryToSql(trxStatusCategory)); // 4) car plate mop sb.Append($" and mop = {(int)TransactionMode.CarPlateMode}"); // 5) order sb.Append(" order by created_time desc"); debugLogger.Add(sb); return sqlCmd.ReadData>(sb.ToString(), DbModelConstructors.ConstructEpsTrxModelList); } /// /// Trx status category(valid means not Removed or Unknown): /// 1 -- trx in progress /// 2 -- trx done and valid /// /// private static string ConvertTrxStatusCategoryToSql(int trxStatusCategory) { string sqlWhere = ""; switch (trxStatusCategory) { case 1: sqlWhere = string.Format ($"(trx_status = {(int)EpsTrxStatus.BeforeFueling} " + $"or trx_status = {(int)EpsTrxStatus.Fueling} " + $"or trx_status = {(int)EpsTrxStatus.FuelingDone} " + $"or trx_status = {(int)EpsTrxStatus.BeforePayment})"); break; case 2: sqlWhere = string.Format ($"(trx_status = {(int)EpsTrxStatus.PaymentOk} " + $"or trx_status = {(int)EpsTrxStatus.PaymentNeedConfirm} " + $"or trx_status = {(int)EpsTrxStatus.PaymentFailed} " + $"or trx_status = {(int)EpsTrxStatus.PaymentOkButNeedRefund} " + $"or trx_status = {(int)EpsTrxStatus.PaymentRefunded})"); break; default: break; } return sqlWhere; } /// /// Get a list of eps transactions in certain status and after certain date time /// /// public static List GetEpsTrxesByTrxStatusAndCreatedTime(EpsTrxStatus trxStatus, DateTime createdTimeStart, bool ignoreCreatedTimeStart) { string querySql = ""; if (ignoreCreatedTimeStart) { querySql = string.Format(" select * from eps_trx where trx_status = {0}", (int)trxStatus); } else { querySql = string.Format(" select * from eps_trx where trx_status = {0} and created_time >= '{1}'", (int)trxStatus, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss")); } return sqlCmd.ReadData>(querySql, DbModelConstructors.ConstructEpsTrxList); } /// /// Get a list of eps transactions in certain notify pos flag and after certain date time /// /// public static List GetEpsTrxesByNotifyPosFlagAndCreatedTime(NotifyPosFlag notifyPos, DateTime createdTimeStart) { string querySql = string.Format(" select * from eps_trx where notify_pos = {0} and created_time >= '{1}'", (int)notifyPos, createdTimeStart.ToString("yyyy-MM-dd HH:mm:ss")); return sqlCmd.ReadData>(querySql, DbModelConstructors.ConstructEpsTrxList); } /// /// not all eps trxes can be removed; need to check eps trx status /// /// public static void DeleteEpsTrxesBefore(DateTime createdTimeStop) { StringBuilder deleteSqlSb = new StringBuilder(); deleteSqlSb.AppendFormat(" delete from eps_trx where created_time < '{0}'", createdTimeStop.ToString("yyyy-MM-dd HH:mm:ss")); deleteSqlSb.Append(" and ("); deleteSqlSb.AppendFormat("trx_status={0}", (int)EpsTrxStatus.Removed); deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentRefunded); deleteSqlSb.AppendFormat(" or trx_status={0}", (int)EpsTrxStatus.PaymentFailed); deleteSqlSb.AppendFormat(" or (trx_status={0} and notify_pos={1})", (int)EpsTrxStatus.PaymentOk, (int)NotifyPosFlag.NotifyOk); deleteSqlSb.Append(")"); sqlCmd.DeleteData(deleteSqlSb.ToString()); } } }