GameModel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * GameModel.php UTF-8
  4. * 游戏表
  5. *
  6. * @date : 2020/9/14 15:23
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : H5IOS 1.0
  11. */
  12. namespace huosdk\h5ios\core\model;
  13. use huosdk\h5ios\core\constant\CacheConst;
  14. use huosdk\h5ios\core\constant\CommonConst;
  15. use huosdk\h5ios\core\constant\GameConst;
  16. use Pin;
  17. use think\Cache;
  18. use think\Loader;
  19. class GameModel extends \huo\model\game\GameModel {
  20. protected $name = 'game';
  21. protected $pk = 'id';
  22. /* 开启自动写入时间戳字段 */
  23. protected $autoWriteTimestamp = true;
  24. protected $cache_key_prefix = CacheConst::CACHE_GAME_PREFIX;
  25. use ModelTrait;
  26. protected $type
  27. = [
  28. 'id' => 'integer',
  29. 'name' => 'string',
  30. 'en_name' => 'string',
  31. 'en_abbr' => 'string',
  32. 'app_key' => 'string',
  33. 'tags' => 'string',
  34. 'category' => 'string',
  35. 'classify' => 'integer',
  36. 'icon' => 'string',
  37. 'cp_payback_url' => 'string',
  38. 'cp_id' => 'integer',
  39. 'parent_id' => 'integer',
  40. 'package_name' => 'string',
  41. 'pay_switch' => 'integer',
  42. 'pay_show' => 'integer',
  43. 'float_is_show' => 'integer',
  44. 'status' => 'integer',
  45. 'is_auth' => 'integer',
  46. 'is_online' => 'integer',
  47. 'is_sdk' => 'integer',
  48. 'is_bt' => 'integer',
  49. 'list_order' => 'integer',
  50. 'rise_order' => 'integer',
  51. 'hot_order' => 'integer',
  52. 'like_order' => 'integer',
  53. 'fine_order' => 'integer',
  54. 'publicity' => 'string',
  55. 'language' => 'string',
  56. 'description' => 'string',
  57. 'image' => 'json',
  58. 'apple_id' => 'string',
  59. 'single_tag' => 'string',
  60. 'ext_info' => 'json',
  61. 'is_delete' => 'integer',
  62. 'delete_time' => 'timestamp',
  63. 'add_cp_time' => 'timestamp',
  64. 'run_time' => 'timestamp',
  65. 'create_time' => 'timestamp',
  66. 'update_time' => 'timestamp',
  67. 'promote_switch' => 'integer',
  68. ];
  69. /**
  70. * 添加游戏
  71. *
  72. * @param $data
  73. *
  74. * @return bool|mixed
  75. */
  76. public function addData($data) {
  77. $_data = $data;
  78. Loader::import('pinyin.Pin', '', '.class.php');
  79. $_pin_class = new Pin();
  80. $_data['en_name'] = $_pin_class->pinyin($_data['name']);
  81. return parent::addGames($_data);
  82. }
  83. /**
  84. * 通过ID获取信息
  85. *
  86. * @param int $id 主键ID
  87. *
  88. * @return array|false
  89. */
  90. public function getInfoById($id) {
  91. /* 缓存操作 */
  92. $_single_cache_key = $this->getSingleCacheKey($id);
  93. $_data = Cache::get($_single_cache_key);
  94. if (!empty($_data)) {
  95. return $_data;
  96. }
  97. $_data = parent::getInfoById($id);
  98. if (empty($_data)) {
  99. return [];
  100. }
  101. Cache::set($_single_cache_key, $_data);
  102. return $_data;
  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. if (isset($data['status']) && GameConst::GAME_STATUS_ON == $data['status']) {
  114. $data['run_time'] = time();
  115. }
  116. $_rs = parent::updateGame($data, $id);
  117. if (false === $_rs) {
  118. return false;
  119. }
  120. /* 缓存操作 */
  121. $_single_cache_key = $this->getSingleCacheKey($id);
  122. Cache::rm($_single_cache_key);
  123. /* TAG缓存操作 */
  124. return true;
  125. }
  126. /**
  127. * 删除单条数据
  128. *
  129. * @param int $id ID
  130. * @param bool $is_complete 是否完成删除
  131. *
  132. * @return bool
  133. */
  134. public function deleteData($id, $is_complete = false) {
  135. $data = [
  136. $_data['is_delete'] = CommonConst::CONST_DELETED,
  137. $_data['delete_time'] = time()
  138. ];
  139. $_rs = parent::updateGame($data, $id);
  140. if (false == $_rs) {
  141. return false;
  142. }
  143. /* 缓存操作 */
  144. $_single_cache_key = $this->getSingleCacheKey($id);
  145. Cache::rm($_single_cache_key);
  146. /* TAG缓存操作 */
  147. return $_rs;
  148. }
  149. /**
  150. * 游戏名称同名数量
  151. *
  152. * @param array $where 查询条件
  153. *
  154. * @return int
  155. */
  156. public function getNameCnt($where) {
  157. $_map = $where;
  158. $_cnt = $this->getCnt($_map);
  159. return $_cnt;
  160. }
  161. /**
  162. * 通过条件获取数量
  163. *
  164. * @param array $where 条件
  165. *
  166. * @return int
  167. */
  168. public function getCnt($where = []) {
  169. $_map = $where;
  170. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  171. if (empty($_cnt)) {
  172. return 0;
  173. }
  174. return $_cnt;
  175. }
  176. /**
  177. * 关联H5游戏
  178. */
  179. public function pg() {
  180. return $this->belongsTo(GameModel::class, 'parent_id', 'id')->field('id,name,icon');
  181. }
  182. }