<?php

namespace app\model;

use app\BaseModel;
use think\Collection;
use think\db\Query;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsToMany;

/**
 * 系统用户模型
 * @property int $user_id
 * @property string $username
 * @property string $password
 * @property string $nickname
 * @property string $avatar
 * @property int $sex
 * @property string $birthday
 * @property string $phone
 * @property string $email
 * @property integer $email_verified
 * @property string $real_name
 * @property string $id_card
 * @property string $introduction
 * @property int $organization_id
 * @property int $status
 * @property int $deleted
 * @property int $tenant_id
 * @property string $create_time
 * @property string $update_time
 * @property string $delete_time
 * @property SysRole[]|Collection $roles
 * @property SysMenu[]|Collection $menus
 */
class SysUser extends BaseModel
{
    use SoftDelete;


    protected $name = "sys_user";
    protected $pk = "user_id";

    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(SysRole::class, SysUserRole::class, 'role_id', 'user_id');
    }

    public function getAuthoritiesAttr(): array
    {
        $menus = [];
        $this->roles->load(['menus'], true);
        foreach ($this->roles as $role) {
            $menus = array_merge($menus, $role->menus->hidden(['pivot'])->where('deleted', 0)->toArray());
        }
        $uniqueMenus = [];
        foreach ($menus as $menu) {
            $uniqueMenus[$menu['menuId']] = $menu;
        }

        $array =  array_values($uniqueMenus);
        usort($array, function($a, $b) {
            return $a['sortNumber'] <=> $b['sortNumber'];
        });
        return $array;
    }

    /**
     * Attr@性别
     * @param $value
     * @param $data
     * @return string
     */
    public function getSexNameAttr($value, $data): string
    {
        return (string)dict_get('sex.' . $data['sex'], '未知');
    }


    public function searchOrganizationIdAttr(Query $query, int $value, array $data): void
    {
        ($value > 0) && $query->where('organization_id', $value);
    }


    public function searchCreateTimeAttr($query, array $value): void
    {
        $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }

    public function searchNicknameAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('nickname', 'like', '%' . $value . '%');
    }

    public function searchUsernameAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('username', 'like', '%' . $value . '%');
    }

    public function searchSexAttr(Query $query, int $value): void
    {
        ($value > 0) && $query->where('sex', '=', $value);
    }

    public function searchStatusAttr(Query $query, int $value): void
    {
        ($value > 0) && $query->where('status', '=', $value);
    }

    public function searchEmailAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('email', 'like', '%' . $value . '%');
    }

    public function searchPhoneAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('phone', 'like', '%' . $value . '%');
    }

    public function searchIdCardAttr(Query $query, string $value): void
    {
        $value != "" && $query->where('id_card', 'like', '%' . $value . '%');
    }

}