<?php

namespace App\Dao\Channel;

use App\Models\Channel\ChannelUser;

class ChannelUserDao
{
    /**
     * @param $account
     * @param $password
     * @return array
     */
    public function getUserByAccountAndPassword($account, $password): array
    {
        if (strlen($account) < 1 || strlen($password) < 1) {
            return [];
        }

        $result = ChannelUser::where(compact('account', 'password'))->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $account
     * @return array
     */
    public function getUserByAccount($account): array
    {
        if (strlen($account) < 1) {
            return [];
        }

        $result = ChannelUser::where('account', $account)->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $token
     * @return array
     */
    public function getUserByToken($token): array
    {
        if (strlen($token) < 1) {
            return [];
        }

        $result = ChannelUser::where(compact('token'))->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $uid
     * @return array
     */
    public function getUserByUid($uid): array
    {
        if (empty($uid)) {
            return [];
        }

        $result = ChannelUser::where('id', $uid)->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $where
     * @param $data
     * @return bool
     */
    public function updateUserData($where, $data): bool
    {
        if (empty($where) || empty($data)) {
            return false;
        }

        return ChannelUser::where($where)->update($data);
    }

    /**
     * @param $account
     * @param $password
     * @return bool
     */
    public function createUser($account, $password): bool
    {
        if (strlen($account) < 1 || strlen($password) < 1) {
            return false;
        }

        // 创建
        $user             = new ChannelUser();
        $user->nickname   = $account;
        $user->account    = $account;
        $user->phone      = '';
        $user->password   = $password;
        $user->is_enabled = 1;
        $user->save();

        return (bool)$user->id;
    }
}
