<?php

namespace app\admin\model;

use support\Model;

/**
 * 悦享家后台管理员模型
 * Class SystemAdmin
 * @package app\admin\model
 */
class SystemAdmin extends Model
{
    const DISABLED = 0;
    const VALID    = 1;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_admin';

    public $timestamps = false;

    /**
     * Notes:获取管理员列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/9/14
     */
    public static function adminList(string $keywords, int $page, int $limit, $status)
    {
        $list = static::select('*')
            ->where(['admin_is_del'=>0])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('admin_account', 'like', '%' . $keywords . '%');
            })
            ->when($status != '', function ($query) use ($status){
                $query->where('admin_status', $status);
            })
            ->orderBy('admin_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['admin_is_del'=>0])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('admin_account', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取管理员信息
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/9/14
     */
    public static function getInfo(string $account)
    {
        return static::where(['admin_account' => $account])->first();
    }

    /**
     * Notes:获取管理员信息
     * @param int $admin_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/9/14
     */
    public static function getAdminInfo(int $admin_id)
    {
        return static::where(['admin_id' => $admin_id])->first();
    }

    /**
     * Notes:更新内容
     * @param int $admin_id
     * @param array $update
     * @return int
     * User: yym
     * Date: 2022/9/14
     */
    public static function updateInfo(int $admin_id, array $update)
    {
        return static::where(['admin_id' => $admin_id])->update($update);
    }
}
