123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php
- /**
- * CategoryModel.php UTF-8
- * 游戏分类
- *
- * @date : 2017/11/23 17:16
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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 = '<a class="js-ajax-dialog-btn btn btn-xs btn-success" data-msg="确认隐藏?" href="'.url(
- 'admin/Game.gameCategory/setStatus',
- array('id' => $item['id'], 'status' => GameConst::CATEGORY_TYPE_HIDDEN)
- ).'">'.lang('DISPLAY').'</a>';
- $_hidden = '<a class="js-ajax-dialog-btn btn btn-xs btn-primary" data-msg="确认显示?" href="'.url(
- 'admin/Game.gameCategory/setStatus',
- array('id' => $item['id'], 'status' => GameConst::CATEGORY_TYPE_DISPLAY)
- ).'">'.lang('HIDDEN').'</a>';
- $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'] = '<a href="'
- .url("admin/Game.gameCategory/add", ["parent" => $item['id']])
- .'" data-toggle="tooltip" data-original-title="'.lang('ADD')
- .'" ><span class="text-info"><i class="fa fa-plus"></i></span></a> <a href="'
- .url("admin/Game.gameCategory/edit", ["id" => $item['id']])
- .'" data-toggle="tooltip" data-original-title="'.lang('EDIT')
- .'" ><span class="text-success"><i class="fa fa-pencil"></i></span></a> <a class="js-ajax-dialog-btn" data-msg='
- .lang(
- 'confirm to delete'
- ).' href="'
- .url("admin/Game.gameCategory/delete", ["id" => $item['id']])
- .'" data-toggle="tooltip" data-original-title="'.lang('DELETE')
- .'" ><span class="text-danger"><i class="fa fa-trash"></i></span></a> ';
- }
- $tree->init($categories);
- if (empty($tpl)) {
- $tpl
- = "<tr id='node-\$id' \$parent_id_node style='\$style'>
- <td style='padding-left:20px;'><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td>
- <td>\$id</td>
- <td>\$spacer\$name</td>
- <td>
- <a href='javascript:parent.imagePreviewDialog(\\\"\$image\\\");'>
- <img src='\$image' width='40'/>
- </a>
- </td>
- <td>\$status</td>
- <td><div class='action-buttons'>\$str_action</div></td>
- </tr>";
- }
- $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 = '<option value=\"{$id}\" {$selected}>{$spacer}{$name}</option>';
- $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;
- }
- }
|