<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 角色模型
 * Class Users
 * @package app\admin\model
 */
class SystemMenu extends Model
{
    const STATUS_DEL_NO = '1';
    const STATUS_DEL_YES = '2';
    const STATUS_DEL = [
        self::STATUS_DEL_NO => '正常',
        self::STATUS_DEL_YES => '删除'
    ];

    const MENU_MENU = '1';
    const MENU_RULE = '2';
    const MENU_TYPE = [
        self::MENU_MENU => '菜单',
        self::MENU_RULE => '权限'
    ];

    const MENU_SHOW_NO = '0';
    const MENU_SHOW_YES = '1';
    const MENU_SHOW = [
        self::MENU_SHOW_NO => '隐藏',
        self::MENU_SHOW_YES => '显示'
    ];

    const MENU_LEVEL_ONE = '1';
    const MENU_LEVEL_TWO = '2';
    const MENU_LEVEL_THE = '3';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_menu';

    public $timestamps = false;

    /**
     * Notes:父级模型关联
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: yym
     * Date: 2022/9/21
     */
    public function children()
    {
        $where = array(
            'menu_status' => static::STATUS_DEL_NO,
            'menu_is_show' => static::MENU_SHOW_YES,
            'menu_is_menu' => static::MENU_MENU
        );
        return $this->hasMany(static::class, 'menu_pid', 'menu_id')->where($where)->select(['menu_id', 'menu_pid', 'menu_path', 'menu_icon', 'menu_name', 'menu_route'])->orderBy('menu_sort', 'desc');
    }

    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function getMenuList(string $keywords)
    {
        $list = static::select('*')->where(['menu_status'=>static::STATUS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('menu_name', 'like', '%' . $keywords . '%');
            })
            ->orderBy('menu_create_time','DESC')
            ->get();
        $count = static::where(['menu_status'=>static::STATUS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('menu_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return $list;
    }

    /**
     * Notes:获取菜单
     * @param int $menu_pid
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getMenus(int $menu_pid)
    {
        $list = static::select('*')
            ->where(['menu_pid' => $menu_pid, 'menu_status' => static::STATUS_DEL_NO])
            ->orderBy('menu_sort','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:三级菜单
     * @param int $menu_pid
     * @return array
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function getLevel($menu_pid)
    {
        $list = static::select(['menu_id as value','menu_name as label'])
            ->where(['menu_pid' => $menu_pid, 'menu_status' => static::STATUS_DEL_NO])
            ->orderBy('menu_sort','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取菜单
     * @param int $menus
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function menusMation($menus)
    {
        $list = static::where(['menu_status'=>static::STATUS_DEL_NO])
            ->when($menus != '', function ($query) use ($menus){
                $query->whereIn('menu_id', $menus);
            })
            ->get();
        return $list;
    }

    /**
     * Notes:获取角色对应权限菜单
     * @param array $menu_ids
     * @return array
     * User: yym
     * Date: 2022/9/21
     */
    public static function getMenuListRule(array $menu_ids)
    {
        return static::where(['menu_status' => static::STATUS_DEL_NO])
            ->whereIn('menu_id', $menu_ids)
            ->get()
            ->toArray();
    }

    /**
     * Notes:菜单列表
     * @param array $menu_list_ids
     * @return array
     * User: yym
     * Date: 2022/9/21
     */
    public static function getHomeMenuList(array $menu_list_ids)
    {
        $where = array(
            'menu_status' => static::STATUS_DEL_NO,
            'menu_is_show' => static::MENU_SHOW_YES,
            'menu_is_menu' => static::MENU_MENU,
            'level' => STATIC::MENU_LEVEL_ONE
        );
        return static::where($where)
            ->select(['menu_id', 'menu_pid', 'menu_path', 'menu_icon', 'menu_name', 'menu_route'])
            ->when(!empty($menu_list_ids), function ($query) use ($menu_list_ids){
                $query->whereIn('menu_id', $menu_list_ids);
            })
            ->with(['children' => function($query) use ($menu_list_ids){
                $query->with('children')->when(!empty($menu_list_ids), function ($query1) use ($menu_list_ids){
                    $query1->whereIn('menu_id', $menu_list_ids);
                });
            }])
            ->orderBy('menu_sort', 'desc')
            ->get()
            ->toArray();
    }

    /**
     * Notes:查询指定条件下的某个字段内容
     * @param int $menu_id
     * @param string $field
     * User: yym
     * Date: 2022/9/27
     */
    public static function getParentValue(int $menu_id, string $field = 'level')
    {
        return static::where(['menu_id' => $menu_id])->value($field);
    }

    /**
     * Notes:获取父级菜单编号 处理成数组形式
     * @param int $menu_pid
     * @return array|false|mixed
     * User: yym
     * Date: 2022/9/26
     */
    public function getParentMenuId(int $menu_pid)
    {
        $parent = array();
        $parent = $this->getParentPid($menu_pid);
        if(!empty($parent))
        {
            $parent = array_merge([$menu_pid], $parent);
            array_pop($parent);
        }
        return array_reverse($parent);
    }

    /**
     * Notes:递归查询父级ID
     * @param $menu_id
     * @param array $date
     * @return array|false|mixed
     * User: yym
     * Date: 2022/9/26
     */
    public function getParentPid($menu_id, $date=[]){
        if(empty($menu_id) && empty($date))
        {
            return array();
        }
        if(empty($menu_id) && !empty($date)){
            return $date;
        }

        $parentIds = array();
        $parentIds = SystemMenu::select('menu_pid')->where(['menu_id' => $menu_id, 'menu_status' => SystemMenu::STATUS_DEL_NO])->get()->toArray();

        $subIds = array();
        $subIds = array_column($parentIds, 'menu_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;
        }
    }
}
