<?php

namespace app\api\logic;
use app\common\{enum\notice\NoticeEnum, logic\BaseLogic, model\auth\Admin, model\user\User, model\systems\SystemUser, service\sms\SmsDriver};
use app\api\service\JwtTokenService;
use think\facade\Config;

/**
 * 会员逻辑层
 * Class UserLogic
 * @package app\shopapi\logic
 */
class UserLogic extends BaseLogic
{
    //重置登录密码
    public static function resetPassword(array $params): bool
    {
        try {
            //验证手机号码
            $user = User::field('id')->where('phone',$params['phone'])->findOrEmpty();
            if($user->isEmpty()){
                throw new \Exception('该手机号未注册');
            }
            // 校验验证码
            $smsDriver = new SmsDriver();
            if (!$smsDriver->verify($params['phone'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
                throw new \Exception('验证码错误');
            }
            // 重置密码
            $passwordSalt = Config::get('project.unique_identification');
            $password = create_password($params['password'], $passwordSalt);
            // 更新
            $user->password = $password;
            $user->need_reset = 0;
            $user->save();
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    //修该密码
    public static function changePassword(array $params, int $userId): bool
    {
        try {
            $user = User::findOrEmpty($userId);
            if ($user->isEmpty()) {
                throw new \Exception('用户不存在');
            }
            // 密码盐
            $passwordSalt = Config::get('project.unique_identification');
            // 加密原密码
            $oldPassword = create_password($params['old_password'], $passwordSalt);
            if($oldPassword != $user['password']){
                throw new \Exception('原密码不正确');
            }
            // 保存密码
            $password = create_password($params['password'], $passwordSalt);
            $user->password = $password;
            $user->save();
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    //修改手机号
    public static function changeMobile(array $params,int $uid): bool
    {
        try {
            // 校验短信
            $checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'],  NoticeEnum::CHANGE_MOBILE_CAPTCHA);
            if (!$checkSmsCode) {
                throw new \Exception('验证码错误');
            }
            User::update([
                'id' => $uid,
                'phone' => $params['phone'],
            ]);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    //更新用户信息
    public static function updateUser(array $params, int $uid): bool
    {
        if (empty($params)) {
            self::setError('参数列表为空');
            return false;
        }
        $user = User::where('id',$uid)->findOrEmpty();
        if ($user->isEmpty()) {
            self::setError('数据不存在');
            return false;
        }
        $params['id'] = $uid;
        try {
            User::update($params);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    //bind用户信息
    public static function bindSystemUser(array $params, array $userinfo): bool
    {
        try {
            $systemUser = SystemUser::where([
                'app_id'  => $params['app_id'],
                'user_id' => $userinfo['user_id'],
                'system_user_id' => $params['system_user_id']
            ])->findOrEmpty();
            if ($systemUser->isEmpty()) {
                SystemUser::create([
                    'app_id'  => $params['app_id'],
                    'user_id' => $userinfo['user_id'],
                    'system_user_id' => $params['system_user_id'],
                    'phone' => $userinfo['phone'],
                    'create_time' => time(),
                    'update_time' => time()
                ]);
            } else {
                SystemUser::where('id', $systemUser['id'])->update([
                    'phone' => $userinfo['phone'],
                    'create_time' => time(),
                    'update_time' => time()
                ]);
            }
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

}