123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\common\model;
- /**
- * 商品分类模型
- * Class Category
- * @package app\common\model
- */
- class Category extends BaseModel
- {
- // 定义表名
- protected $name = 'category';
- // 定义主键
- protected $pk = 'category_id';
- /**
- * 分类图片
- * @return \think\model\relation\HasOne
- */
- public function image()
- {
- return $this->hasOne('UploadFile', 'file_id', 'image_id');
- }
- public function goods()
- {
- return $this->hasMany(GoodsCategoryRel::class, 'category_id', 'category_id');
- }
- /**
- * 分类详情
- * @param int|array $where
- * @param array $with
- * @return array|null|static
- */
- public static function detail($where, array $with = [])
- {
- return static::get($where, $with);
- }
- /**
- * 获取列表记录
- * @param array $param
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList(array $param = [])
- {
- $list = $this->getAll($param);
- return $this->getTreeData($list);
- }
- /**
- * 获取所有记录
- * @param array $param
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- protected function getAll(array $param = [])
- {
- // 默认查询参数
- $params = $this->setQueryDefaultValue($param, [
- 'status' => -1 // 状态(1显示 0隐藏 -1全部)
- ]);
- // 设置检索条件
- $filter = [];
- $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
- // 查询列表数据
- $list = $this->with(['image'])
- ->withCount(['goods'])
- ->where($filter)
- ->order(['sort', 'create_time'])
- ->select();
- if (!empty($list)) {
- foreach($list as $item) {
- $item->goods_count = GoodsCategoryRel::alias('gcr')
- ->leftJoin('goods', 'goods.goods_id=gcr.goods_id')
- ->where('goods.is_delete', 0)
- ->where('goods.status', 10)
- ->where('gcr.category_id', $item->category_id)
- ->count();
- }
- }
- return $list;
- }
- /**
- * 获取树状列表
- * @param $list
- * @param int $parentId
- * @return array
- */
- private function getTreeData($list, int $parentId = 0)
- {
- $data = [];
- foreach ($list as $key => $item) {
- if ($item['parent_id'] == $parentId) {
- $children = $this->getTreeData($list, $item['category_id']);
- !empty($children) && $item['children'] = $children;
- $data[] = $item;
- unset($list[$key]);
- }
- }
- return $data;
- }
- }
|