PsMemWhiteModel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * PsMemWhiteModel.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 think\Cache;
  17. class PsMemWhiteModel extends CommonModel {
  18. protected $name = 'ps_mem_white';
  19. protected $autoWriteTimestamp = true;
  20. protected $key = CacheConst::CACHE_PAY_SWITCH_MEM_WHITE_PREFIX;
  21. /**
  22. * @return \think\model\relation\BelongsTo
  23. */
  24. public function game() {
  25. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->field('id,name');
  26. }
  27. /**
  28. * 获取缓存key
  29. *
  30. * @param $app_id
  31. * @param $mem_id
  32. *
  33. * @return string
  34. */
  35. protected function getKey($app_id, $mem_id) {
  36. $_key = $this->key.$app_id.'_'.$mem_id;
  37. return $_key;
  38. }
  39. public function addData($data) {
  40. $_data = [];
  41. $_data['app_id'] = get_val($data, 'app_id', 0);
  42. $_data['mem_id'] = get_val($data, 'mem_id', 0);
  43. $_data['username'] = get_val($data, 'username', '');
  44. $_data['nickname'] = get_val($data, 'nickname', '');
  45. if ($_obj = self::create($_data, true)) {
  46. $_data['id'] = $_obj->id;
  47. $_key = $this->getKey($_data['app_id'], $_data['mem_id']);
  48. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  49. return $_data;
  50. } else {
  51. return false;
  52. }
  53. }
  54. /**
  55. * 获取详情
  56. *
  57. * @param $app_id
  58. * @param $mem_id
  59. *
  60. * @return mixed
  61. */
  62. public function getInfo($app_id, $mem_id) {
  63. $_key = $this->getKey($app_id, $mem_id);
  64. $_data = Cache::get($_key);
  65. if (!empty($_data)) {
  66. return json_decode($_data, true);
  67. }
  68. $_map = [
  69. 'app_id' => $app_id,
  70. 'mem_id' => $mem_id,
  71. ];
  72. $_data = $this->where($_map)->find();
  73. if (is_object($_data)) {
  74. $_data = $_data->toArray();
  75. }
  76. if (empty($_data)) {
  77. return [];
  78. }
  79. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  80. return $_data;
  81. }
  82. /**
  83. * 修改信息
  84. *
  85. * @param $app_id
  86. * @param $mem_id
  87. * @param $data
  88. *
  89. * @return bool
  90. */
  91. public function updateDataPsMemWhite($app_id, $mem_id, $data) {
  92. $_key = $this->getKey($app_id, $mem_id);
  93. $_data = $this->getInfo($app_id, $mem_id);
  94. $_data = array_merge($_data, $data);
  95. $_map = [
  96. 'app_id' => $app_id,
  97. 'mem_id' => $mem_id,
  98. ];
  99. $_rs = $this->where($_map)->update($_data);
  100. if (false == $_rs) {
  101. return false;
  102. }
  103. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  104. return true;
  105. }
  106. /**
  107. * 根据ID更新数据
  108. *
  109. * @param $id
  110. * @param $data
  111. *
  112. * @return bool
  113. */
  114. public function updateById($id, $data) {
  115. $_data = parent::getInfoById($id);
  116. $_data = array_merge($_data, $data);
  117. $_map = ['id' => $id];
  118. $_rs = $this->where($_map)->update($_data);
  119. if (false == $_rs) {
  120. return false;
  121. }
  122. $_key = $this->getKey($_data['app_id'], $_data['mem_id']);
  123. Cache::set($_key, json_encode($_data), CommonConst::CONST_DAY_SECONDS);
  124. return true;
  125. }
  126. /**
  127. * 删除数据
  128. *
  129. * @param $app_id
  130. * @param $mem_id
  131. *
  132. * @return int
  133. */
  134. public function delData($app_id, $mem_id) {
  135. $_map = ['app_id' => $app_id, 'mem_id' => $mem_id];
  136. $_rs = $this->where($_map)->delete();
  137. if (true == $_rs) {
  138. $_key = $this->getKey($app_id, $mem_id);
  139. Cache::rm($_key);
  140. }
  141. return $_rs;
  142. }
  143. /**
  144. * 获取白名单列表
  145. *
  146. * @param array $param 筛选条件入参
  147. * @param string $page 分页数据
  148. * @param string $order 排序条件
  149. *
  150. * @return array
  151. */
  152. public function getList($param = [], $page = '1,10', $order = '-create_time') {
  153. $_return = ['count' => 0, 'list' => []];
  154. if (empty($param['app_id'])) {
  155. return $_return;
  156. }
  157. $_map = ['app_id' => $param['app_id']];
  158. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  159. $_map['create_time'] = ['between', [strtotime($param['start_time']),
  160. strtotime($param['end_time']) + CommonConst::CONST_DAY_SECONDS]];
  161. } elseif (!empty($param['start_time'])) {
  162. $_map['create_time'] = ['egt', strtotime($param['start_time'])];
  163. } elseif (!empty($param['end_time'])) {
  164. $_map['create_time'] = ['elt', strtotime($param['end_time']) + CommonConst::CONST_DAY_SECONDS];
  165. }
  166. if (!empty($param['mem_id'])) {
  167. $_map['mem_id'] = $param['mem_id'];
  168. }
  169. if (!empty($param['username'])) {
  170. $_map['username'] = $param['username'];
  171. }
  172. if (!empty($param['nickname'])) {
  173. $_map['nickname'] = $param['nickname'];
  174. }
  175. $_count = $this->where($_map)->count();
  176. if (empty($_count)) {
  177. return $_return;
  178. }
  179. $_order = $this->orderFilter($order);
  180. $_data = $this->with('game')->where($_map)->order($_order)->page($page)->select();
  181. if (is_object($_data)) {
  182. $_data = $_data->toArray();
  183. }
  184. if (empty($_data)) {
  185. return $_return;
  186. }
  187. $_return['count'] = $_count;
  188. $_return['list'] = $_data;
  189. return $_return;
  190. }
  191. }