CategoryModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * CategoryModel.php UTF-8
  4. * 游戏分类
  5. *
  6. * @date : 2017/11/23 17:16
  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\constant\GameConst;
  16. use think\Cache;
  17. use tree\Tree;
  18. class CategoryModel extends CommonModel {
  19. protected $name = 'category';
  20. /**
  21. * 基础查询
  22. *
  23. * @param $query
  24. */
  25. protected function base($query) {
  26. $query->where('status', GameConst::CATEGORY_TYPE_DISPLAY);
  27. }
  28. public function games() {
  29. return $this->belongsToMany('huo\model\game\GameModel', 'game_category', 'app_id', 'cate_id');
  30. }
  31. /**
  32. * @return \think\model\relation\HasMany
  33. */
  34. public function appIds() {
  35. return self::hasMany('huo\model\game\GamecategoryModel', 'cate_id', 'id');
  36. }
  37. public function getImageAttr($value) {
  38. if (!empty($value)) {
  39. return cmf_get_image_url($value);
  40. } else {
  41. return null;
  42. }
  43. }
  44. /**
  45. * 获取游戏类型ID Name表
  46. *
  47. * @param int $type 1 标签 2 类型
  48. *
  49. * @return array|bool|mixed
  50. */
  51. public function getIdNames($type = 0) {
  52. $_map = [];
  53. if (!empty($type)) {
  54. $_map['type'] = $type;
  55. }
  56. if (empty($_cates)) {
  57. $_cates = $this->where($_map)->column('name', 'id');
  58. }
  59. if (empty($_cates)) {
  60. return false;
  61. }
  62. return $_cates;
  63. }
  64. /**
  65. * 更新类型
  66. *
  67. * @param $cate_data
  68. * @param $cate_id
  69. *
  70. * @return bool
  71. */
  72. public function updateCategory($cate_data, $cate_id) {
  73. $_map['id'] = $cate_id;
  74. $_data = $cate_data;
  75. $_rs = self::update($_data, $_map, true);
  76. if (false == $_rs) {
  77. return false;
  78. } else {
  79. if (!empty($cate_data['type'])) {
  80. $_type = $cate_data['type'];
  81. $_cache_key = CacheConst::CACHE_CATES_PREFIX.$_type;
  82. Cache::rm($_cache_key);
  83. }
  84. return true;
  85. }
  86. }
  87. /**
  88. * 添加类型
  89. *
  90. * @param $cate_data
  91. *
  92. * @return bool|mixed
  93. */
  94. public function addCategory($cate_data) {
  95. if (empty($cate_data)) {
  96. return false;
  97. }
  98. if ($_obj = self::create($cate_data, true)) {
  99. if (!empty($cate_data['type'])) {
  100. $_type = $cate_data['type'];
  101. $_cache_key = CacheConst::CACHE_CATES_PREFIX.$_type;
  102. Cache::rm($_cache_key);
  103. }
  104. return $_obj->id;
  105. } else {
  106. return false;
  107. }
  108. }
  109. /**
  110. * @param int $parent_id
  111. *
  112. * @param string $order
  113. *
  114. * @param int $type
  115. *
  116. * @param array $field
  117. *
  118. * @param bool $useGlobalScope
  119. *
  120. * @return array|null
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. * @throws \think\exception\DbException
  124. */
  125. public function getList($parent_id = 0, $order = '-list_order', $type = 2, $field = [], $useGlobalScope = true) {
  126. $_field = $field;
  127. if (empty($field)) {
  128. $_field = ['id', 'name', 'game_cnt', 'image', 'parent_id', 'item_cnt'];
  129. }
  130. $_map = [];
  131. $_map['type'] = $type;
  132. if (!empty($parent_id)) {
  133. $_map['parent_id'] = $parent_id;
  134. }
  135. $_order = $this->orderFilter($order);
  136. $_datas = $this->useGlobalScope($useGlobalScope)->where($_map)->field($_field)->order($_order)->select()
  137. ->toArray();
  138. if (empty($_datas)) {
  139. return null;
  140. }
  141. if (!empty($field)) {
  142. $_list = [];
  143. foreach ($_datas as $_data) {
  144. $_cate = $_data;
  145. $_cate['game_cnt'] = (new GamecategoryModel())->where(['cate_id'=>$_data['id']])->count('app_id');
  146. $_list[] = $_cate;
  147. }
  148. return $_list;
  149. }
  150. $_list = [];
  151. foreach ($_datas as $_data) {
  152. $_cate['typeid'] = $_data['id'];
  153. $_cate['type_id'] = $_data['id'];
  154. $_cate['typename'] = $_data['name'];
  155. $_cate['type_name'] = $_data['name'];
  156. $_cate['game_cnt'] = $this->getGameCnt($_data['id']);
  157. $_cate['icon'] = $_data['image'];
  158. $_cate['image'] = $_data['image'];
  159. $_cate['item_cnt'] = $_data['item_cnt'];
  160. if (0 == $parent_id) {
  161. $_cate['sublist'] = $this->getList($_data['id'], $order);
  162. $_cate['subcount'] = count($_cate['sublist']);
  163. }
  164. $_list[] = $_cate;
  165. }
  166. return $_list;
  167. }
  168. public function getGameCnt($id = 0) {
  169. if (empty($id)) {
  170. return 0;
  171. }
  172. $_map['cate_id'] = $id;
  173. $_app_ids = (new GamecategoryModel())->where($_map)->column('app_id');
  174. if (empty($_app_ids)) {
  175. return 0;
  176. }
  177. $_g_map['id'] = ['in', $_app_ids];
  178. $_g_map['status'] = 2;
  179. $_g_map['is_delete'] = 2;
  180. $_cnt = (new GameModel())->where($_g_map)->count();
  181. return $_cnt;
  182. }
  183. /**
  184. * @param int|array $currentIds
  185. * @param string $tpl
  186. *
  187. * @param bool $useGlobalScope
  188. *
  189. * @return string
  190. * @throws \think\db\exception\DataNotFoundException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. * @throws \think\exception\DbException
  193. */
  194. public function adminCategoryTableTree($currentIds = 0, $tpl = '', $useGlobalScope = true) {
  195. $categories = $this->useGlobalScope($useGlobalScope)->order("list_order DESC")->where(
  196. ['type' => GameConst::CATEGORY_TYPE_CATE]
  197. )->select()->toArray();
  198. $tree = new Tree();
  199. $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
  200. $tree->nbsp = '&nbsp;&nbsp;';
  201. if (!is_array($currentIds)) {
  202. $currentIds = [$currentIds];
  203. }
  204. $newMenus = [];
  205. foreach ($categories as $m) {
  206. $newMenus[$m['id']] = $m;
  207. }
  208. foreach ($categories as $key => $item) {
  209. $_display = '<a class="js-ajax-dialog-btn btn btn-xs btn-success" data-msg="确认隐藏?" href="'.url(
  210. 'admin/Game.gameCategory/setStatus',
  211. array('id' => $item['id'], 'status' => GameConst::CATEGORY_TYPE_HIDDEN)
  212. ).'">'.lang('DISPLAY').'</a>';
  213. $_hidden = '<a class="js-ajax-dialog-btn btn btn-xs btn-primary" data-msg="确认显示?" href="'.url(
  214. 'admin/Game.gameCategory/setStatus',
  215. array('id' => $item['id'], 'status' => GameConst::CATEGORY_TYPE_DISPLAY)
  216. ).'">'.lang('HIDDEN').'</a>';
  217. $categories[$key]['status'] = ($item['status'] == GameConst::CATEGORY_TYPE_DISPLAY) ? $_display : $_hidden;
  218. $categories[$key]['parent_id_node'] = ($item['parent_id']) ? ' class="child-of-node-'.$item['parent_id']
  219. .' "' : "";
  220. $categories[$key]['style'] = empty($item['parent_id']) ? '' : 'display:none;';
  221. $categories[$key]['url'] = cmf_url('admin/Game.gameCategory/add', ['id' => $item['id']]);
  222. $categories[$key]['str_action'] = '<a href="'
  223. .url("admin/Game.gameCategory/add", ["parent" => $item['id']])
  224. .'" data-toggle="tooltip" data-original-title="'.lang('ADD')
  225. .'" ><span class="text-info"><i class="fa fa-plus"></i></span></a> <a href="'
  226. .url("admin/Game.gameCategory/edit", ["id" => $item['id']])
  227. .'" data-toggle="tooltip" data-original-title="'.lang('EDIT')
  228. .'" ><span class="text-success"><i class="fa fa-pencil"></i></span></a> <a class="js-ajax-dialog-btn" data-msg='
  229. .lang(
  230. 'confirm to delete'
  231. ).' href="'
  232. .url("admin/Game.gameCategory/delete", ["id" => $item['id']])
  233. .'" data-toggle="tooltip" data-original-title="'.lang('DELETE')
  234. .'" ><span class="text-danger"><i class="fa fa-trash"></i></span></a> ';
  235. }
  236. $tree->init($categories);
  237. if (empty($tpl)) {
  238. $tpl
  239. = "<tr id='node-\$id' \$parent_id_node style='\$style'>
  240. <td style='padding-left:20px;'><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td>
  241. <td>\$id</td>
  242. <td>\$spacer\$name</td>
  243. <td>
  244. <a href='javascript:parent.imagePreviewDialog(\\\"\$image\\\");'>
  245. <img src='\$image' width='40'/>
  246. </a>
  247. </td>
  248. <td>\$status</td>
  249. <td><div class='action-buttons'>\$str_action</div></td>
  250. </tr>";
  251. }
  252. $treeStr = $tree->getTree(0, $tpl);
  253. return $treeStr;
  254. }
  255. /**
  256. * 生成分类 select树形结构
  257. *
  258. * @param int $selectId 需要选中的分类 id
  259. * @param int $currentCid 需要隐藏的分类 id
  260. *
  261. * @return string
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. * @throws \think\exception\DbException
  265. */
  266. public function adminCategoryTree($selectId = 0, $currentCid = 0) {
  267. if (!empty($currentCid)) {
  268. $where['id'] = ['neq', $currentCid];
  269. }
  270. $categories = $this->order("list_order DESC")->select()->toArray();
  271. $tree = new Tree();
  272. $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
  273. $tree->nbsp = '&nbsp;&nbsp;';
  274. $newCategories = [];
  275. foreach ($categories as $item) {
  276. $item['selected'] = $selectId == $item['id'] ? "selected" : "";
  277. array_push($newCategories, $item);
  278. }
  279. $tree->init($newCategories);
  280. $str = '<option value=\"{$id}\" {$selected}>{$spacer}{$name}</option>';
  281. $treeStr = $tree->getTree(0, $str);
  282. return $treeStr;
  283. }
  284. public function deleteCategory($_id = 0) {
  285. // 先判断是否有子分类
  286. $_gtypes = $this->where(['parent_id' => $_id])->select()->toArray();
  287. if (!empty($_gtypes)) {
  288. return false;
  289. }
  290. $_res = $this->where(['id' => $_id])->delete();
  291. return $_res;
  292. }
  293. /**
  294. * 通过ID查询游戏类型名称
  295. *
  296. * @param string $ids
  297. *
  298. * @return string
  299. */
  300. public function getNameByIds($ids = '') {
  301. if (empty($ids)) {
  302. return '';
  303. }
  304. $_cates = $this->getIdNames();
  305. if (empty($_cates)) {
  306. return '';
  307. }
  308. $_ids_arr = $this->strToArr($ids);
  309. $_name_arr = [];
  310. foreach ($_ids_arr as $_k => $_v) {
  311. $_cate_id = intval($_v);
  312. if (empty($_cates[$_cate_id])) {
  313. continue;
  314. }
  315. $_name_arr[$_k] = $_cates[$_cate_id];
  316. }
  317. return $this->arrToStr($_name_arr);
  318. }
  319. /**
  320. * 通过ID获取详情
  321. *
  322. * @param $id
  323. *
  324. * @return array
  325. * @throws \think\Exception
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\ModelNotFoundException
  328. * @throws \think\exception\DbException
  329. */
  330. public function getInfoById($id) {
  331. return self::useGlobalScope(false)->find($id)->toArray();
  332. }
  333. /**
  334. * 根据类型获取Cate列表
  335. *
  336. * @param int $type
  337. *
  338. * @return array|false
  339. */
  340. public function getCatesByType($type = 0) {
  341. $_cache_key = CacheConst::CACHE_CATES_PREFIX.$type;
  342. $_rdata = Cache::get($_cache_key);
  343. if (!empty($_rdata)) {
  344. return $_rdata;
  345. }
  346. $_field = "id type_id,name type_name,game_cnt,image,item_cnt";
  347. $_map['type'] = $type;
  348. $_map['status'] = 2;
  349. $_order = $this->orderFilter('-list_order');
  350. $_datas = $this->where($_map)->field($_field)->order($_order)->select();
  351. if (is_object($_datas)) {
  352. $_datas = $_datas->toArray();
  353. }
  354. if (empty($_datas)) {
  355. return null;
  356. }
  357. if (!empty($_datas)) {
  358. Cache::set($_cache_key, $_datas);
  359. }
  360. return $_datas;
  361. }
  362. }