<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 角色模型
 * Class Users
 * @package app\admin\model
 */
class SystemRole extends Model
{
    const ROLE_STATUS = 1;

    const IS_SHOW_YES = 1;
    const IS_SHOW_NO  = 2;
    const ROLE_IS_SHOW = [
        self::IS_SHOW_YES => '启用',
        self::IS_SHOW_NO  => '禁用'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_role';

    public $timestamps = false;

    /**
     * Notes:获取角色列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getRoleList(int $page, int $limit, string $keywords)
    {
        $list = static::select('*')
            ->where(['role_status'=>static::ROLE_STATUS])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('role_name', 'like', '%' . $keywords . '%');
            })
            ->orderBy('role_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['role_status'=>static::ROLE_STATUS])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('role_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取角色列表
     * @return array
     * User: ZQ
     * Date: 2022/9/19`
     */
    public static function getRoleAll()
    {
        $list = static::select(['role_id','role_name'])
            ->where(['role_status'=>static::ROLE_STATUS])
            ->orderBy('role_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取角色名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function roleMation($admin_roles)
    {
        $where = [];
        $where['role_status'] = static::ROLE_STATUS;
        $mation = static::where($where)
            ->when($admin_roles != '', function ($query) use ($admin_roles){
                $query->whereIn('role_id', $admin_roles);
            })
            ->get('role_name');
        return $mation;
    }

    /**
     * Notes:获取身份列表
     * @param array $admin_roles
     * @return array
     * User: yym
     * Date: 2022/9/21
     */
    public static function getRuleList(array $admin_roles)
    {
        return static::where(['role_status' => static::ROLE_STATUS, 'role_is_show' => static::IS_SHOW_YES])
            ->whereIn('role_id', $admin_roles)
            ->get()
            ->toArray();
    }
}
