GamecategoryModel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * GamecategoryModel.php UTF-8
  4. * 游戏与类型关联表
  5. *
  6. * @date : 2017/11/23 17:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@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\tool\Time;
  16. use huomp\model\game\GameMiniModel;
  17. use think\Cache;
  18. class GamecategoryModel extends CommonModel {
  19. protected $name = 'game_category';
  20. protected $cache_tag = CacheConst::TAG_GAME_LIST;
  21. protected $tgi_tag = CacheConst::TAG_GAME_LIST_INFO;
  22. /**
  23. * 关联游戏
  24. */
  25. public function game() {
  26. return $this->hasone(GameModel::className(), 'id', 'app_id')->field(
  27. 'id,name,classify as classify_label,status as status_label,icon'
  28. );
  29. }
  30. /**
  31. * 关联game表
  32. *
  33. * @return mixed
  34. */
  35. public function gmini() {
  36. return $this->hasone(GameMiniModel::className(), 'app_id', 'app_id')->field('app_id,mini_app_id');
  37. }
  38. public function joingame() {
  39. return $this->belongsTo('huo\model\game\GameModel', 'app_id', 'id')->setEagerlyType(0);
  40. }
  41. /**
  42. * 关联game_ext表
  43. *
  44. * @return mixed
  45. */
  46. public function gameext() {
  47. return $this->belongsTo('huo\model\game\GameextModel', 'app_id', 'app_id')->setEagerlyType(0);
  48. }
  49. /**
  50. * 关联game_rate表
  51. *
  52. * @return mixed
  53. */
  54. public function gamerate() {
  55. return $this->belongsTo('huo\model\rate\GameRateModel', 'app_id', 'app_id')->setEagerlyType(0);
  56. }
  57. /**
  58. * 关联game_rate表
  59. *
  60. * @return mixed
  61. */
  62. public function gamemini() {
  63. return $this->belongsTo('huomp\model\game\GameMiniModel', 'app_id', 'app_id')->field('app_id,mini_app_id,need_popup,entrance_image');
  64. }
  65. /**
  66. * 关联game_version表
  67. *
  68. * @return \think\model\relation\HasMany
  69. */
  70. public function gv() {
  71. return $this->hasMany('huo\model\game\GameversionModel', 'app_id', 'app_id')->field('app_id,package_url');
  72. }
  73. /**
  74. * 根据app_id 获取cate_id数组
  75. *
  76. * @param $app_id
  77. *
  78. * @return array
  79. */
  80. public function getCateIdsByAppId($app_id) {
  81. $_map = ['app_id' => $app_id];
  82. return self::where($_map)->column('cate_id');
  83. }
  84. /**
  85. * 添加数据
  86. *
  87. * @param $data
  88. *
  89. * @return bool
  90. */
  91. public function addData($data) {
  92. if (empty($data)) {
  93. return false;
  94. }
  95. $_data = $data;
  96. $_obj = self::create($_data, true);
  97. if ($_obj) {
  98. Cache::clear($this->cache_tag);
  99. Cache::clear($this->tgi_tag);
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * 更新数据
  106. *
  107. * @param array $data 数据
  108. * @param int $id ID
  109. *
  110. * @return bool
  111. */
  112. public function updateData($data, $id) {
  113. $_map['id'] = $id;
  114. $_data = $data;
  115. $_rs = self::update($_data, $_map, true);
  116. if (false == $_rs) {
  117. return false;
  118. }
  119. Cache::clear($this->cache_tag);
  120. Cache::clear($this->tgi_tag);
  121. return true;
  122. }
  123. /**
  124. * 删除数据
  125. *
  126. * @param array $_map 删除条件
  127. *
  128. * @return int
  129. */
  130. public function deleteDataByWhere($_map) {
  131. $_rs = self::where($_map)->delete();
  132. if ($_rs) {
  133. Cache::clear($this->cache_tag);
  134. Cache::clear($this->tgi_tag);
  135. }
  136. return $_rs;
  137. }
  138. /**
  139. * 判断条件下信息是否存在
  140. *
  141. * @param $cate_id
  142. * @param $app_id
  143. *
  144. * @return bool
  145. */
  146. public function isGameExist($cate_id, $app_id) {
  147. $_map = ['cate_id' => $cate_id, 'app_id' => $app_id];
  148. $_cache_key = $this->tgi_tag.md5(json_encode($_map));
  149. list($_start_time, $_end_time) = Time::today();
  150. $_diff_time = $_end_time - time();
  151. $_rs = self::where($_map)->cache($_cache_key, $_diff_time, $this->tgi_tag)->find();
  152. if (is_object($_rs)) {
  153. $_rs = $_rs->toArray();
  154. }
  155. if (empty($_rs)) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * 获取类下的游戏ids
  162. *
  163. * @param $cate_id
  164. *
  165. * @return array
  166. */
  167. public function getAppIdsByCateId($cate_id) {
  168. return self::where(['cate_id' => $cate_id])->column('app_id');
  169. }
  170. }