<?php

namespace App\Services;

use App\Models\NickName;
use App\Models\User;

class UserService
{
    public function updatePassword(User $user, string $password): bool
    {
        $user->password = $password;
        if ($user->save()) {
            return true;
        }
        return false;
    }

    public function updateMobile(User $user, $mobile)
    {
        if ($user->mobile != $mobile) {

        }

        if ($user->mobile == $mobile) {
            return ['error' => '新手机号与原手机号一致'];
        }

        if (User::where(['mobile' => $mobile, 'id' => ['<>', $user->id]])->first()) {
            return ['error' => '手机号已被占用'];
        }
        $user->mobile = $mobile;
        if ($user->save()) {
            return true;
        }
        return ['error' => '手机号修改失败'];
    }

    public function selectUserNickNameById(array $id_arr)
    {
        $list = NickName::query()->whereIn('id', $id_arr)->pluck('name', 'id');
        return $list;
    }

}
