using CommonObj.common; using CommonObj.lotoModel; using ResfullApi.Models.bet; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Data; namespace ApiProcessToken.Models.common { public class FunctionThaiLan { public static string getGuiId() { string _key = Guid.NewGuid().ToString(); return _key; } public static List getListTicketFree(redisConnection _redis, int total, string randomId) { List _result = new List(); try { List _allTernFree = _redis.getAllkeyByPatternRegex(_redis.DB_INDEX_DATA_TICKET_UNIQUE_FREE, "*"); if (_allTernFree == null) return null; if(_allTernFree.Count> total) { Random rand = new Random(); int position = rand.Next(_allTernFree.Count - 1); string _code; for (int i = 0; i < total; i++) { _code = _allTernFree[position]; _result.Add(_code); if (position + 1 >= _allTernFree.Count) position = 0; else position++; } } else { for (int i = 0; i < _allTernFree.Count; i++) { string _code = _allTernFree[i]; _result.Add(_code); } } } catch (Exception) { } return _result; } public static List getListTicketBuy(redisConnection _redis, buyTicketObj _ticketBuy,string randomId) { List _result = new List(); try { for (int i = 0; i < _ticketBuy.ticket.Length; i++) { string _code = _ticketBuy.ticket[i].code; if (CommonFunction.check_THAILAN_IS_BUY_CACHE(_redis, _code, randomId) || CommonFunction.IsFixTicket(_redis, _code)) { //da mua trong cache //Neu trong cache chua co thi add vao list if (!_result.Contains(_code)) { _result.Add(_code); } } } } catch (Exception) { } return _result; } //Ham nay check trong cache va trong DB public static List getListTicketBuyCacheDB(redisConnection _redis, buyTicketObj _ticketBuy, string randomId) { List _result = new List(); try { for (int i = 0; i < _ticketBuy.ticket.Length; i++) { string _code = _ticketBuy.ticket[i].code; if (CommonFunction.check_THAILAN_IS_BUY_CACHE(_redis, _code, randomId)) { //da mua trong cache //Neu trong cache chua co thi add vao list if (!_result.Contains(_code)) { _result.Add(_code); } } else { //Truong hop nay khong ton tai trong cache da mua ve. // Neu day la cac so co phan cuoi trung voi he thong fix gia thi bat buoc phai vao DB kiem tra laij string _isCheckDB = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, _redis.REDIS_IS_CHECK_TICKET_DB); if (!string.IsNullOrEmpty(_isCheckDB)) _isCheckDB = "1"; if (_isCheckDB == "1") { if (IsTicketBuyDB(_code, randomId)) { //da mua trong DB //Neu trong cache chua co thi add vao list TimeSpan _timeSpam = new TimeSpan(0, 0, 864000000); //Time out trong 10 ngay _redis.setByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code + "_" + randomId, _code, _timeSpam); if (!_result.Contains(_code)) { _result.Add(_code); } } } else { if (CommonFunction.IsFixTicket(_redis, _code)) { //Truong hop nay bat buoc phai check DB if (IsTicketBuyDB(_code, randomId)) { //da mua trong DB //Neu trong cache chua co thi add vao list TimeSpan _timeSpam = new TimeSpan(0, 0, 864000000); //Time out trong 10 ngay _redis.setByKey(_redis.DB_INDEX_DATA_TICKET_UNIQUE_BUY, _code + "_" + randomId, _code, _timeSpam); if (!_result.Contains(_code)) { _result.Add(_code); } } } } } } } catch (Exception) { } return _result; } /// /// true: da mua ticket trong DB /// false: chua mua trong DB /// /// /// /// public static bool IsTicketBuyDB(string _ticket,string _random) { bool _result = true; try { DataSet ds = lotoDataAccess.ThaiLan_checkTicketBuy(_ticket, _random); if(ds!=null && ds.Tables[0].Rows.Count>0) { string status = ds.Tables[0].Rows[0]["status"].ToString(); if (status == "0") return false; else return true; } } catch (Exception) { } return _result; } public static bool systemIsOK(redisConnection _redis) { bool _result = false; try { if(_redis.isConnet()) { DataSet ds = lotoDataAccess.checkDB("1"); if(ds!=null & ds.Tables[0].Rows.Count>0) { return true; } } } catch (Exception) { } return _result; } } }