GameServerSwitchModel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * GameServerSwitchModel.php UTF-8
  4. * 区服支付切换
  5. *
  6. * @date : 2018/12/22 11:37
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\game;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use huolib\constant\OrderConst;
  17. use think\Cache;
  18. class GameServerSwitchModel extends CommonModel {
  19. protected $name = 'game_server_switch';
  20. protected $autoWriteTimestamp = true;
  21. protected $key = CacheConst::CACHE_GAME_SERVER_PAY_SWITCH_PREFIX;
  22. /**
  23. * 获取缓存key
  24. *
  25. * @param $app_id
  26. * @param $server_id
  27. *
  28. * @return string
  29. */
  30. protected function getKey($app_id, $server_id) {
  31. $_key = $this->key.$app_id.'_'.$server_id;
  32. return $_key;
  33. }
  34. public function addData($data) {
  35. $_data = [];
  36. $_data['app_id'] = get_val($data, 'app_id', '');
  37. $_data['server_id'] = get_val($data, 'server_id', 0);
  38. $_data['server_name'] = get_val($data, 'server_name', '');
  39. $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_YES);
  40. if ($_obj = self::create($_data, true)) {
  41. $_data['id'] = $_obj->id;
  42. $_key = $this->getKey($_data['app_id'], $_data['server_id']);
  43. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  44. return $_data;
  45. } else {
  46. return false;
  47. }
  48. }
  49. /**
  50. * 获取详情
  51. *
  52. * @param $app_id
  53. * @param $server_id
  54. *
  55. * @return mixed
  56. */
  57. public function getInfo($app_id, $server_id) {
  58. $_key = $this->getKey($app_id, $server_id);
  59. $_data = Cache::get($_key);
  60. if (!empty($_data)) {
  61. return json_decode($_data, true);
  62. }
  63. $_map = [
  64. 'app_id' => $app_id,
  65. 'server_id' => $server_id,
  66. ];
  67. $_data = $this->where($_map)->find();
  68. if (is_object($_data)) {
  69. $_data = $_data->toArray();
  70. }
  71. if (empty($_data)) {
  72. return [];
  73. }
  74. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  75. return $_data;
  76. }
  77. /**
  78. * 修改信息
  79. *
  80. * @param $app_id
  81. * @param $server_id
  82. * @param $data
  83. *
  84. * @return bool
  85. */
  86. public function updateDataGss($app_id, $server_id, $data) {
  87. $_key = $this->getKey($app_id, $server_id);
  88. $_data = $this->getInfo($app_id, $server_id);
  89. $_data = array_merge($_data, $data);
  90. $_map = [
  91. 'app_id' => $app_id,
  92. 'server_id' => $server_id,
  93. ];
  94. $_rs = $this->where($_map)->update($_data);
  95. if (false == $_rs) {
  96. return false;
  97. }
  98. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  99. return true;
  100. }
  101. /**
  102. * 根据ID更新数据
  103. *
  104. * @param $id
  105. * @param $data
  106. *
  107. * @return bool
  108. */
  109. public function updateById($id, $data) {
  110. $_data = parent::getInfoById($id);
  111. $_data = array_merge($_data, $data);
  112. $_map = ['id' => $id];
  113. $_rs = $this->where($_map)->update($_data);
  114. if (false == $_rs) {
  115. return false;
  116. }
  117. $_key = $this->getKey($_data['app_id'], $_data['server_id']);
  118. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  119. return true;
  120. }
  121. /**
  122. * 获取所有区服id_name
  123. *
  124. * @param $app_id
  125. *
  126. * @return array
  127. */
  128. public function getIdName($app_id) {
  129. $_map = [
  130. 'app_id' => $app_id,
  131. ];
  132. $_array = $this->where($_map)->column('server_name', 'id');
  133. return $_array;
  134. }
  135. /**
  136. * 获取标记为不切换的id
  137. *
  138. * @param $app_id
  139. *
  140. * @return array
  141. */
  142. public function getSwitchNo($app_id) {
  143. $_map = [
  144. 'app_id' => $app_id,
  145. 'is_switch' => OrderConst::PAY_SWITCH_NO
  146. ];
  147. $_array = $this->where($_map)->column('id');
  148. return $_array;
  149. }
  150. }