123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace app\admin\model;
- use support\Db;
- use support\Model;
- /**
- * 分类模型
- * Class Users
- * @package app\admin\model
- */
- class Category extends Model
- {
- const STATUS_DEL_NO = '0';
- const STATUS_DEL_YES = '1';
- const STATUS_DEL = [
- self::STATUS_DEL_NO => '正常',
- self::STATUS_DEL_YES => '删除'
- ];
- const CATEGORY_RECOMMEND_NO = '0';
- const CATEGORY_RECOMMEND_YES = '1';
- const CATEGORY_SHOW = [
- self::CATEGORY_RECOMMEND_NO => '不推荐',
- self::CATEGORY_RECOMMEND_YES => '推荐'
- ];
- /**
- * The table associated with the model.
- *
- * @var string
- */
- protected $table = 'category';
- public $timestamps = false;
- /**
- * Notes:模型关联
- * @return \Illuminate\Database\Eloquent\Relations\HasMany
- * User: yym
- * Date: 2022/11/4
- */
- public function children()
- {
- return $this->hasMany(Category::class, 'category_pid', 'category_id')->where(['category_del' => Category::STATUS_DEL_NO])->select(['category_id', 'category_pid', 'category_name', 'category_icon', 'category_type', 'category_open_type', 'category_open_url', 'category_recommend', 'category_sort', Db::raw('FROM_UNIXTIME(category_create_time) as category_create_time')])->orderBy('category_sort', 'desc');
- }
- /**
- * Notes:获取菜单列表
- * @param string $keywords
- * @param int $page
- * @param int $limit
- * @return array
- * User: ZQ
- * Date: 2022/9/14
- */
- public static function getCategoryList(string $keywords)
- {
- return static::select(['*', Db::raw('FROM_UNIXTIME(category_create_time) as category_create_time')])->where(['category_del' => static::STATUS_DEL_NO])
- ->when($keywords != '', function ($query) use ($keywords){
- $query->where('category_name', 'like', '%' . $keywords . '%');
- })
- ->when($keywords == '', function ($query) use ($keywords){
- $query->where('category_pid', 0);
- })
- ->with(['children' => function($query){
- $query->with(['children' => function($query1){
- $query1->with(['children' => function($query2){
- $query2->with(['children' => function($query3){
- $query3->with(['children' => function($query4){
- $query4->with(['children']);
- }]);
- }]);
- }]);
- }]);
- }])
- ->orderBy('category_sort','DESC')
- ->orderBy('category_create_time','DESC')
- ->get()
- ->toArray();
- }
- /**
- * Notes:获取菜单
- * @param int $category_pid
- * @return array
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function getCategorys(int $category_pid)
- {
- $list = static::select('*')
- ->where(['category_pid' => $category_pid, 'category_del' => static::STATUS_DEL_NO])
- ->orderBy('category_sort','DESC')
- ->get();
- return $list;
- }
- /**
- * Notes:三级菜单
- * @param int $category_pid
- * @return array
- * User: ZQ
- * Date: 2022/9/15
- */
- public static function getLevel($category_pid)
- {
- $list = static::select(['category_id as value','category_name as label'])
- ->where(['category_pid' => $category_pid, 'category_del' => static::STATUS_DEL_NO])
- ->orderBy('category_sort','DESC')
- ->get();
- return $list;
- }
- /**
- * Notes:获取菜单
- * @param int $categorys
- * @return array
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function categorysMation($categorys)
- {
- $list = static::where(['category_del'=>static::STATUS_DEL_NO])
- ->when($categorys != '', function ($query) use ($categorys){
- $query->whereIn('category_id', $categorys);
- })
- ->get();
- return $list;
- }
- /**
- * Notes:获取角色对应权限菜单
- * @param array $category_ids
- * @return array
- * User: yym
- * Date: 2022/9/21
- */
- public static function getCategoryListRule(array $category_ids)
- {
- return static::where(['category_del' => static::STATUS_DEL_NO])
- ->whereIn('category_id', $category_ids)
- ->get()
- ->toArray();
- }
- /**
- * Notes:菜单列表
- * @param array $category_list_ids
- * @return array
- * User: yym
- * Date: 2022/9/21
- */
- public static function getHomeCategoryList(array $category_list_ids)
- {
- $where = array(
- 'category_del' => static::STATUS_DEL_NO,
- 'level' => STATIC::MENU_LEVEL_ONE
- );
- return static::where($where)
- ->select(['category_id', 'category_pid', 'category_path', 'category_icon', 'category_name', 'category_route'])
- ->when(!empty($category_list_ids), function ($query) use ($category_list_ids){
- $query->whereIn('category_id', $category_list_ids);
- })
- ->with(['children' => function($query) use ($category_list_ids){
- $query->with('children')->when(!empty($category_list_ids), function ($query1) use ($category_list_ids){
- $query1->whereIn('category_id', $category_list_ids);
- });
- }])
- ->get()
- ->toArray();
- }
- /**
- * Notes:查询指定条件下的某个字段内容
- * @param int $category_id
- * @param string $field
- * User: yym
- * Date: 2022/9/27
- */
- public static function getParentValue(int $category_id, string $field = 'level')
- {
- return static::where(['category_id' => $category_id])->value($field);
- }
- /**
- * Notes:获取父级菜单编号 处理成数组形式
- * @param int $category_pid
- * @return array|false|mixed
- * User: yym
- * Date: 2022/9/26
- */
- public function getParentCategoryId(int $category_pid)
- {
- $parent = array();
- $parent = $this->getParentPid($category_pid);
- if(!empty($parent))
- {
- $parent = array_merge([$category_pid], $parent);
- array_pop($parent);
- }
- return array_reverse($parent);
- }
- /**
- * Notes:递归查询父级ID
- * @param $category_id
- * @param array $date
- * @return array|false|mixed
- * User: yym
- * Date: 2022/9/26
- */
- public function getParentPid($category_id, $date=[]){
- if(empty($category_id) && empty($date))
- {
- return array();
- }
- if(empty($category_id) && !empty($date)){
- return $date;
- }
- $parentIds = array();
- $parentIds = Category::select('category_pid')->where(['category_id' => $category_id, 'category_del' => Category::STATUS_DEL_NO])->get()->toArray();
- $subIds = array();
- $subIds = array_column($parentIds, 'category_pid');
- $subIds = array_unique($subIds);
- if(count($subIds)>0)
- {
- foreach($subIds as $val)
- {
- $date[] = $val;
- $date = $this->getParentPid($val, $date); //注意写$date 返回给上级
- }
- }
- if(count($date)>0)
- {
- return $date;
- }else{
- return false;
- }
- }
- }
|