<?php
declare (strict_types = 1);

namespace app\admin\model\v1;

use app\common\model\SysUserBaseModel;
use think\model\concern\SoftDelete;

/**
 * 用户模型
 * @mixin \think\Model
 */
class SysUserModel extends SysUserBaseModel
{
    // 启用软删除
    use SoftDelete;
    // 软删除字段
    protected $deleteTime = 'deleted_at';

    // 隐藏字段
    protected $hidden = ['password', 'salt'];

    // 关联角色
    public function role()
    {
        return $this->belongsTo(SysRoleModel::class, 'role_id', 'id');
    }

    // 用户名搜索
    public function searchUsernameAttr($query, $value)
    {
        if (empty($value)) {
            return;
        }
        $query->where('username', 'like', "%{$value}%");
    }

    // 用户名查询范围
    public function scopeUsernameOrMobile($query, $username)
    {
        $query->where('username', '=', $username)->whereOr('mobile', '=', $username);
    }

    // 状态查询
    public function scopeStatus($query, $status)
    {
        if (empty($status)) {
            return;
        }
        $query->where('status', '=', $status);
    }
}
