123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace app\common\Service\Goods;
- use app\common\model\Category;
- use app\common\Enum\StatusEnum;
- class CategoryService
- {
- /**
- * 获取栏目所有子级的ID
- * @param mixed $ids 栏目ID或集合ID
- * @param bool $withself 是否包含自身
- * @return array
- */
- public static function getCategoryChildrenIds($ids, $withself = true)
- {
- // $cacheName = 'shop-childrens-' . $ids . '-' . $withself;
- // $result = Cache::get($cacheName);
- // if ($result === false) {
- $categoryList = Category::where('status', StatusEnum::ENABLED)
- ->order('weigh desc,id desc')
- ->select();
- $result = [];
- $tree = \fast\Tree::instance();
- $tree->init(collection($categoryList)->toArray(), 'pid');
- $CategoryIds = is_array($ids) ? $ids : explode(',', $ids);
- foreach ($CategoryIds as $index => $CategoryId) {
- $result = array_merge($result, $tree->getChildrenIds($CategoryId, $withself));
- }
- // Cache::set($cacheName, $result);
- // }
- return $result;
- }
- /**
- * 根据分类ID获取分类信息
- * @param array $arrCategoryIds 分类ID集合
- * @return array
- */
- public static function getCategoryByIds($arrCategoryIds = []){
- $categoryList = Category::where('id', 'IN', $arrCategoryIds)
- ->where('status', StatusEnum::ENABLED)
- ->order('weigh desc,id desc')
- ->field('id,name,image,weigh,pid')
- ->select();
- $arrCategory = collection($categoryList)->toArray();
- return $arrCategory;
- }
-
- }
|