AgentGameSwitchModel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * AgentGameSwitchModel.php UTF-8
  4. * 渠道游戏切量控制
  5. *
  6. * @date : 2019/11/11 15:27
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoAgentSwitch\model;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class AgentGameSwitchModel extends CommonModel {
  18. protected $name = 'agent_game_switch';
  19. protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_SWITCH_PREFIX;
  20. /**
  21. * 获取单条记录缓存key
  22. *
  23. * @param int $id ID
  24. *
  25. * @return string
  26. */
  27. protected function getSingleCacheKey($id) {
  28. return $this->cache_key_prefix.$id;
  29. }
  30. /**
  31. * 根据mem_id 获取缓存key
  32. *
  33. * @param int $agent_id 渠道id
  34. * @param int $app_id 游戏id
  35. *
  36. * @return string
  37. */
  38. public function getCacheKeyByAgentApp($agent_id, $app_id) {
  39. $_key = $this->cache_key_prefix.'agent_'.$agent_id.'_app_'.$app_id;
  40. return $_key;
  41. }
  42. /**
  43. * 添加数据
  44. *
  45. * @param array $data 需要添加的数据
  46. *
  47. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  48. */
  49. public function addData($data) {
  50. if (empty($data)) {
  51. return false;
  52. }
  53. $_data = $data;
  54. $_model = new static();
  55. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  56. if (false !== $_rs) {
  57. return $_model->getLastInsID();
  58. }
  59. return false;
  60. }
  61. /**
  62. * 通过ID获取信息
  63. *
  64. * @param int $id 主键ID
  65. *
  66. * @return array
  67. */
  68. public function getInfoById($id) {
  69. /* 缓存操作 */
  70. $_single_cache_key = $this->getSingleCacheKey($id);
  71. $_data = Cache::get($_single_cache_key);
  72. if (!empty($_data)) {
  73. return $_data;
  74. }
  75. $_data = parent::getInfoById($id);
  76. if (empty($_data)) {
  77. return [];
  78. }
  79. Cache::set($_single_cache_key, $_data);
  80. return $_data;
  81. }
  82. /**
  83. * 更新数据
  84. *
  85. * @param array $data 数据
  86. * @param int|array $id 主集ID
  87. *
  88. * @return bool
  89. */
  90. public function updateData($data, $id) {
  91. $_old_data = $this->getInfoById($id);
  92. $_map = ['id' => $id];
  93. $_data = $data;
  94. $_model = new static();
  95. $_rs = $_model->allowField(true)->isUpdate(true)->save($_data, $_map);
  96. if (false === $_rs) {
  97. return false;
  98. }
  99. /* 缓存操作 */
  100. $_single_cache_key = $this->getSingleCacheKey($id);
  101. Cache::rm($_single_cache_key);
  102. if ((isset($data['app_id']) && $data['app_id'] != $_old_data['app_id'])
  103. || (isset($data['agent_id']) && $data['agent_id'] != $_old_data['agent_id'])) {
  104. $_cache_key = $this->getCacheKeyByAgentApp($_old_data['agent_id'], $_old_data['app_id']);
  105. Cache::rm($_cache_key);
  106. }
  107. return true;
  108. }
  109. /**
  110. * 删除单条数据 渠道数据不能删除
  111. *
  112. * @param int $id ID
  113. *
  114. * @param bool $is_complete
  115. *
  116. * @return bool
  117. */
  118. public function deleteData($id, $is_complete = true) {
  119. $_old_data = $this->getInfoById($id);
  120. $_rs = parent::deleteData($id, $is_complete);
  121. if (false == $_rs) {
  122. return false;
  123. }
  124. /* 缓存操作 */
  125. $_single_cache_key = $this->getSingleCacheKey($id);
  126. Cache::rm($_single_cache_key);
  127. $_cache_key = $this->getCacheKeyByAgentApp($_old_data['agent_id'], $_old_data['app_id']);
  128. Cache::rm($_cache_key);
  129. return $_rs;
  130. }
  131. /**
  132. * 根据渠道和游戏获取id
  133. *
  134. * @param int $agent_id 渠道id
  135. * @param int $app_id 游戏id
  136. *
  137. * @return int|mixed
  138. */
  139. public function getIdByAgentApp($agent_id, $app_id) {
  140. $_cache_key = $this->getCacheKeyByAgentApp($agent_id, $app_id);
  141. $_id = Cache::get($_cache_key);
  142. if (!empty($_id)) {
  143. return $_id;
  144. }
  145. $_map = ['agent_id' => $agent_id, 'app_id' => $app_id];
  146. $_id = $this->where($_map)->value('id');
  147. if (empty($_id)) {
  148. return CommonConst::CONST_ZERO;
  149. }
  150. Cache::set($_cache_key, $_id);
  151. return $_id;
  152. }
  153. /**
  154. * 根据渠道和游戏获取数据
  155. *
  156. * @param int $agent_id 渠道id
  157. * @param int $app_id 游戏id
  158. *
  159. * @return array
  160. */
  161. public function getInfoByAgentApp($agent_id, $app_id) {
  162. $_id = $this->getIdByAgentApp($agent_id, $app_id);
  163. return $this->getInfoById($_id);
  164. }
  165. }