Offer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * 账单信息
  4. * @author solu
  5. */
  6. class Offer extends Model {
  7. protected $tableName = 'offer';
  8. protected $dbKey = 'dw_baccarat';
  9. const STATUS_PLAYING = 0; // 进行中
  10. const STATUS_CLOSE = 1; // 已结算
  11. const STATUS_FINISH = 2; // 已转账
  12. const LIST_RANK = 1; // 巨额奖金
  13. const LIST_PLAYER = 2; // 我的下注
  14. const LIST_ALL = 3; // 全部下注
  15. const REDIS_PRE_LIST_CACHE = 'baccarat:list_cache:';
  16. const REDIS_LIST_CACHE_TTL = 600;
  17. const MAX_LIST_COUNT = 100;
  18. /**
  19. * 获取投注玩家
  20. * @param $gameId
  21. * @return array
  22. */
  23. public function getGamePlayer($gameId) {
  24. $players = [];
  25. $list = $this->objTable->getAll(['game_id' => $gameId, 'state' => [self::STATUS_PLAYING, self::STATUS_CLOSE]], ['_field' => 'player, transfer_trx_id']);
  26. foreach ($list as $v) {
  27. $players[$v['player']] = $v['transfer_trx_id'];
  28. }
  29. return $players;
  30. }
  31. /**
  32. * 下注记录
  33. * @param $gameId
  34. * @return array
  35. */
  36. public function getCurrentList($gameId) {
  37. $list = $this->objTable->getAll(['game_id' => $gameId], [
  38. '_field' => 'id, player, offerall_int, offertype, offeramount_int, transfer_trx_id, update_time_int',
  39. '_sortKey' => 'id desc',
  40. ]);
  41. foreach ($list as $k => $v) {
  42. $v['id'] = intval($v['id']);
  43. $v['offerall'] = intval($v['offerall_int']);
  44. $v['offeramount'] = $v['offeramount_int'];
  45. $v['update_time_int'] = intval($v['update_time_int'] / 1000000);
  46. unset($v['offerall_int'], $v['offeramount_int']);
  47. $list[$k] = $v;
  48. }
  49. return $list ?: [];
  50. }
  51. /**
  52. * 下注列表
  53. * @author solu
  54. * @param $type
  55. * @param int $offset
  56. * @param int $limit
  57. * @param string $player
  58. * @param bool $cache
  59. * @param null|int $nextId
  60. * @return array
  61. */
  62. public function getList($type, $offset = 0, $limit = 25, $player = '', $cache = false, $nextId = null) {
  63. if ($offset + $limit > self::MAX_LIST_COUNT) {
  64. return [];
  65. }
  66. if (null !== $nextId) {
  67. $_limit = $limit;
  68. } else {
  69. $_limit = "{$offset}, {$limit}";
  70. }
  71. $where = [
  72. '_field' => 'id, game_id, player, offerall_int, offertype, offeramount_int, win_int, state, transfer_trx_id, result_trx_id, create_time, update_time_int',
  73. '_limit' => $_limit,
  74. // '_debug' => true,
  75. ];
  76. switch ($type) {
  77. case self::LIST_RANK:
  78. $where['_field'] .= ', ( CAST(win_int AS SIGNED) - CAST(offerall_int AS SIGNED) ) as income';
  79. $where['state'] = self::STATUS_FINISH;
  80. $where['_sortKey'] = 'income DESC';
  81. break;
  82. case self::LIST_PLAYER:
  83. $where['player'] = $player;
  84. $where['_sortKey'] = 'id DESC';
  85. if (null !== $nextId) {
  86. $where['_where'] = "id<{$nextId}";
  87. }
  88. break;
  89. case self::LIST_ALL:
  90. $where['_sortKey'] = 'id DESC';
  91. if (null !== $nextId) {
  92. $where['_where'] = "id<{$nextId}";
  93. }
  94. break;
  95. }
  96. $objRedis = null;
  97. $key = '';
  98. if ($cache) {
  99. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  100. $key = self::getListCacheKey($where);
  101. $json = $objRedis->get($key);
  102. if ($json) {
  103. return json_decode($json, true);
  104. }
  105. }
  106. $list = $this->objTable->getAll($where);
  107. $players = array_column($list, 'player');
  108. $gameIds = array_unique(array_column($list, 'game_id'));
  109. $objAccount = new Account();
  110. $objGame = new Game();
  111. $vipLevels = $objAccount->getVipLevels($players);
  112. $gameInfos = $objGame->getGameRevealInfo($gameIds);
  113. foreach ($list as $k => $v) {
  114. $type == self::LIST_RANK && $v['rank'] = ($offset * $limit) + $k + 1;
  115. $v['vip_level'] = $vipLevels[$v['player']] ?: 0;
  116. $v['update_time'] = round($v['update_time_int'] / 1000000);
  117. if (0 == $v['result_block_num']) {
  118. $v['result_block_num'] = '';
  119. }
  120. $v['game_info'] = $gameInfos[$v['game_id']] ?: null;
  121. $list[$k] = $v;
  122. }
  123. if ($cache) {
  124. $objRedis->setex($key, self::REDIS_LIST_CACHE_TTL, json_encode($list));
  125. }
  126. return $list ?: [];
  127. }
  128. private static function getListCacheKey($where) {
  129. $where = http_build_query($where);
  130. return self::REDIS_PRE_LIST_CACHE . md5($where);
  131. }
  132. }