FunctionThaiLan.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using CommonObj.common;
  2. using CommonObj.lotoModel;
  3. using ResfullApi.Models.bet;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Data;
  9. namespace ApiProcessToken.Models.common
  10. {
  11. public class FunctionThaiLan
  12. {
  13. public static string getGuiId()
  14. {
  15. string _key = Guid.NewGuid().ToString();
  16. return _key;
  17. }
  18. public static List<string> getListTicketFree(redisConnection _redis, int total, string randomId)
  19. {
  20. List<string> _result = new List<string>();
  21. try
  22. {
  23. List<string> _allTernFree = _redis.getAllkeyByPatternRegex(_redis.DB_INDEX_DATA_TICKET_UNIQUE_FREE, "*");
  24. if (_allTernFree == null)
  25. return null;
  26. if(_allTernFree.Count> total)
  27. {
  28. Random rand = new Random();
  29. int position = rand.Next(_allTernFree.Count - 1);
  30. string _code;
  31. for (int i = 0; i < total; i++)
  32. {
  33. _code = _allTernFree[position];
  34. _result.Add(_code);
  35. if (position + 1 >= _allTernFree.Count)
  36. position = 0;
  37. else
  38. position++;
  39. }
  40. }
  41. else
  42. {
  43. for (int i = 0; i < _allTernFree.Count; i++)
  44. {
  45. string _code = _allTernFree[i];
  46. _result.Add(_code);
  47. }
  48. }
  49. }
  50. catch (Exception)
  51. {
  52. }
  53. return _result;
  54. }
  55. public static List<string> getListTicketBuy(redisConnection _redis, buyTicketObj _ticketBuy,string randomId)
  56. {
  57. List<string> _result = new List<string>();
  58. try
  59. {
  60. for (int i = 0; i < _ticketBuy.ticket.Length; i++)
  61. {
  62. string _code = _ticketBuy.ticket[i].code;
  63. if (CommonFunction.check_THAILAN_IS_BUY_CACHE(_redis, _code, randomId) || CommonFunction.IsFixTicket(_redis, _code))
  64. {
  65. //da mua trong cache
  66. //Neu trong cache chua co thi add vao list
  67. if (!_result.Contains(_code))
  68. {
  69. _result.Add(_code);
  70. }
  71. }
  72. }
  73. }
  74. catch (Exception)
  75. {
  76. }
  77. return _result;
  78. }
  79. //Ham nay check trong cache va trong DB
  80. public static List<string> getListTicketBuyCacheDB(redisConnection _redis, buyTicketObj _ticketBuy, string randomId)
  81. {
  82. List<string> _result = new List<string>();
  83. try
  84. {
  85. for (int i = 0; i < _ticketBuy.ticket.Length; i++)
  86. {
  87. string _code = _ticketBuy.ticket[i].code;
  88. if (CommonFunction.check_THAILAN_IS_BUY_CACHE(_redis, _code, randomId))
  89. {
  90. //da mua trong cache
  91. //Neu trong cache chua co thi add vao list
  92. if (!_result.Contains(_code))
  93. {
  94. _result.Add(_code);
  95. }
  96. }
  97. else
  98. {
  99. //Truong hop nay khong ton tai trong cache da mua ve.
  100. // Neu day la cac so co phan cuoi trung voi he thong fix gia thi bat buoc phai vao DB kiem tra laij
  101. string _isCheckDB = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, _redis.REDIS_IS_CHECK_TICKET_DB);
  102. if (!string.IsNullOrEmpty(_isCheckDB)) _isCheckDB = "1";
  103. if (_isCheckDB == "1")
  104. {
  105. if (IsTicketBuyDB(_code, randomId))
  106. {
  107. //da mua trong DB
  108. //Neu trong cache chua co thi add vao list
  109. TimeSpan _timeSpam = new TimeSpan(0, 0, 864000000); //Time out trong 10 ngay
  110. _redis.setByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code + "_" + randomId, _code, _timeSpam);
  111. if (!_result.Contains(_code))
  112. {
  113. _result.Add(_code);
  114. }
  115. }
  116. }
  117. else
  118. {
  119. if (CommonFunction.IsFixTicket(_redis, _code))
  120. {
  121. //Truong hop nay bat buoc phai check DB
  122. if (IsTicketBuyDB(_code, randomId))
  123. {
  124. //da mua trong DB
  125. //Neu trong cache chua co thi add vao list
  126. TimeSpan _timeSpam = new TimeSpan(0, 0, 864000000); //Time out trong 10 ngay
  127. _redis.setByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code + "_" + randomId, _code, _timeSpam);
  128. if (!_result.Contains(_code))
  129. {
  130. _result.Add(_code);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }
  138. catch (Exception)
  139. {
  140. }
  141. return _result;
  142. }
  143. /// <summary>
  144. /// true: da mua ticket trong DB
  145. /// false: chua mua trong DB
  146. /// </summary>
  147. /// <param name="_ticket"></param>
  148. /// <param name="_random"></param>
  149. /// <returns></returns>
  150. public static bool IsTicketBuyDB(string _ticket,string _random)
  151. {
  152. bool _result = true;
  153. try
  154. {
  155. DataSet ds = lotoDataAccess.ThaiLan_checkTicketBuy(_ticket, _random);
  156. if(ds!=null && ds.Tables[0].Rows.Count>0)
  157. {
  158. string status = ds.Tables[0].Rows[0]["status"].ToString();
  159. if (status == "0")
  160. return false;
  161. else
  162. return true;
  163. }
  164. }
  165. catch (Exception)
  166. {
  167. }
  168. return _result;
  169. }
  170. public static bool systemIsOK(redisConnection _redis)
  171. {
  172. bool _result = false;
  173. try
  174. {
  175. if(_redis.isConnet())
  176. {
  177. DataSet ds = lotoDataAccess.checkDB("1");
  178. if(ds!=null & ds.Tables[0].Rows.Count>0)
  179. {
  180. return true;
  181. }
  182. }
  183. }
  184. catch (Exception)
  185. {
  186. }
  187. return _result;
  188. }
  189. }
  190. }