* @version : HUOSDK 8.0 */ namespace huo\model\game; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\GameConst; use think\Cache; use tree\Tree; class CategoryModel extends CommonModel { protected $name = 'category'; /** * 基础查询 * * @param $query */ protected function base($query) { $query->where('status', GameConst::CATEGORY_TYPE_DISPLAY); } public function games() { return $this->belongsToMany('huo\model\game\GameModel', 'game_category', 'app_id', 'cate_id'); } /** * @return \think\model\relation\HasMany */ public function appIds() { return self::hasMany('huo\model\game\GamecategoryModel', 'cate_id', 'id'); } public function getImageAttr($value) { if (!empty($value)) { return cmf_get_image_url($value); } else { return null; } } /** * 获取游戏类型ID Name表 * * @param int $type 1 标签 2 类型 * * @return array|bool|mixed */ public function getIdNames($type = 0) { $_map = []; if (!empty($type)) { $_map['type'] = $type; } if (empty($_cates)) { $_cates = $this->where($_map)->column('name', 'id'); } if (empty($_cates)) { return false; } return $_cates; } /** * 更新类型 * * @param $cate_data * @param $cate_id * * @return bool */ public function updateCategory($cate_data, $cate_id) { $_map['id'] = $cate_id; $_data = $cate_data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { if (!empty($cate_data['type'])) { $_type = $cate_data['type']; $_cache_key = CacheConst::CACHE_CATES_PREFIX.$_type; Cache::rm($_cache_key); } return true; } } /** * 添加类型 * * @param $cate_data * * @return bool|mixed */ public function addCategory($cate_data) { if (empty($cate_data)) { return false; } if ($_obj = self::create($cate_data, true)) { if (!empty($cate_data['type'])) { $_type = $cate_data['type']; $_cache_key = CacheConst::CACHE_CATES_PREFIX.$_type; Cache::rm($_cache_key); } return $_obj->id; } else { return false; } } /** * @param int $parent_id * * @param string $order * * @param int $type * * @param array $field * * @param bool $useGlobalScope * * @return array|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getList($parent_id = 0, $order = '-list_order', $type = 2, $field = [], $useGlobalScope = true) { $_field = $field; if (empty($field)) { $_field = ['id', 'name', 'game_cnt', 'image', 'parent_id', 'item_cnt']; } $_map = []; $_map['type'] = $type; if (!empty($parent_id)) { $_map['parent_id'] = $parent_id; } $_order = $this->orderFilter($order); $_datas = $this->useGlobalScope($useGlobalScope)->where($_map)->field($_field)->order($_order)->select() ->toArray(); if (empty($_datas)) { return null; } if (!empty($field)) { $_list = []; foreach ($_datas as $_data) { $_cate = $_data; $_cate['game_cnt'] = (new GamecategoryModel())->where(['cate_id'=>$_data['id']])->count('app_id'); $_list[] = $_cate; } return $_list; } $_list = []; foreach ($_datas as $_data) { $_cate['typeid'] = $_data['id']; $_cate['type_id'] = $_data['id']; $_cate['typename'] = $_data['name']; $_cate['type_name'] = $_data['name']; $_cate['game_cnt'] = $this->getGameCnt($_data['id']); $_cate['icon'] = $_data['image']; $_cate['image'] = $_data['image']; $_cate['item_cnt'] = $_data['item_cnt']; if (0 == $parent_id) { $_cate['sublist'] = $this->getList($_data['id'], $order); $_cate['subcount'] = count($_cate['sublist']); } $_list[] = $_cate; } return $_list; } public function getGameCnt($id = 0) { if (empty($id)) { return 0; } $_map['cate_id'] = $id; $_app_ids = (new GamecategoryModel())->where($_map)->column('app_id'); if (empty($_app_ids)) { return 0; } $_g_map['id'] = ['in', $_app_ids]; $_g_map['status'] = 2; $_g_map['is_delete'] = 2; $_cnt = (new GameModel())->where($_g_map)->count(); return $_cnt; } /** * @param int|array $currentIds * @param string $tpl * * @param bool $useGlobalScope * * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function adminCategoryTableTree($currentIds = 0, $tpl = '', $useGlobalScope = true) { $categories = $this->useGlobalScope($useGlobalScope)->order("list_order DESC")->where( ['type' => GameConst::CATEGORY_TYPE_CATE] )->select()->toArray(); $tree = new Tree(); $tree->icon = ['  │', '  ├─', '  └─']; $tree->nbsp = '  '; if (!is_array($currentIds)) { $currentIds = [$currentIds]; } $newMenus = []; foreach ($categories as $m) { $newMenus[$m['id']] = $m; } foreach ($categories as $key => $item) { $_display = ''.lang('DISPLAY').''; $_hidden = ''.lang('HIDDEN').''; $categories[$key]['status'] = ($item['status'] == GameConst::CATEGORY_TYPE_DISPLAY) ? $_display : $_hidden; $categories[$key]['parent_id_node'] = ($item['parent_id']) ? ' class="child-of-node-'.$item['parent_id'] .' "' : ""; $categories[$key]['style'] = empty($item['parent_id']) ? '' : 'display:none;'; $categories[$key]['url'] = cmf_url('admin/Game.gameCategory/add', ['id' => $item['id']]); $categories[$key]['str_action'] = ' $item['id']]) .'" data-toggle="tooltip" data-original-title="'.lang('ADD') .'" > $item['id']]) .'" data-toggle="tooltip" data-original-title="'.lang('EDIT') .'" > $item['id']]) .'" data-toggle="tooltip" data-original-title="'.lang('DELETE') .'" > '; } $tree->init($categories); if (empty($tpl)) { $tpl = " \$id \$spacer\$name \$status
\$str_action
"; } $treeStr = $tree->getTree(0, $tpl); return $treeStr; } /** * 生成分类 select树形结构 * * @param int $selectId 需要选中的分类 id * @param int $currentCid 需要隐藏的分类 id * * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function adminCategoryTree($selectId = 0, $currentCid = 0) { if (!empty($currentCid)) { $where['id'] = ['neq', $currentCid]; } $categories = $this->order("list_order DESC")->select()->toArray(); $tree = new Tree(); $tree->icon = ['  │', '  ├─', '  └─']; $tree->nbsp = '  '; $newCategories = []; foreach ($categories as $item) { $item['selected'] = $selectId == $item['id'] ? "selected" : ""; array_push($newCategories, $item); } $tree->init($newCategories); $str = ''; $treeStr = $tree->getTree(0, $str); return $treeStr; } public function deleteCategory($_id = 0) { // 先判断是否有子分类 $_gtypes = $this->where(['parent_id' => $_id])->select()->toArray(); if (!empty($_gtypes)) { return false; } $_res = $this->where(['id' => $_id])->delete(); return $_res; } /** * 通过ID查询游戏类型名称 * * @param string $ids * * @return string */ public function getNameByIds($ids = '') { if (empty($ids)) { return ''; } $_cates = $this->getIdNames(); if (empty($_cates)) { return ''; } $_ids_arr = $this->strToArr($ids); $_name_arr = []; foreach ($_ids_arr as $_k => $_v) { $_cate_id = intval($_v); if (empty($_cates[$_cate_id])) { continue; } $_name_arr[$_k] = $_cates[$_cate_id]; } return $this->arrToStr($_name_arr); } /** * 通过ID获取详情 * * @param $id * * @return array * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getInfoById($id) { return self::useGlobalScope(false)->find($id)->toArray(); } /** * 根据类型获取Cate列表 * * @param int $type * * @return array|false */ public function getCatesByType($type = 0) { $_cache_key = CacheConst::CACHE_CATES_PREFIX.$type; $_rdata = Cache::get($_cache_key); if (!empty($_rdata)) { return $_rdata; } $_field = "id type_id,name type_name,game_cnt,image,item_cnt"; $_map['type'] = $type; $_map['status'] = 2; $_order = $this->orderFilter('-list_order'); $_datas = $this->where($_map)->field($_field)->order($_order)->select(); if (is_object($_datas)) { $_datas = $_datas->toArray(); } if (empty($_datas)) { return null; } if (!empty($_datas)) { Cache::set($_cache_key, $_datas); } return $_datas; } }