<?php

namespace app\business;

use Illuminate\Support\Arr;
use support\Db;
use support\Redis;

class LoginBusiness
{
    /** 注册
     * @param array $param
     * @return void
     */
    static public function login(array $param)
    {
        try {
            $userData = Db::table('wa_users')->where('mobile', $param['mobile'])->first();
            if (empty($userData)) {
                throw new \Exception('账号不存在');
            }
            if ($userData->password != md5($param['password'])) {
                throw new \Exception('密码不正确');
            }
            if (empty($userData->status)) {
                throw new \Exception('账号已被冻结');
            }
            $token = jwtEncode(collect($userData)->toArray());
            $uid   = $userData->id;
            if (Arr::get($param, 'app')) {
                Db::table('wa_users')->where('id', $uid)->update([
                    'is_app' => 1
                ]);
            }
            Db::table('wa_users')->where('id', $uid)->update([
                'last_time' => date('Y-m-d H:i:s'),
            ]);
            Redis::setEx(getenv('PROJECTWEB') . '_' . $uid, 86400, $token);
        } catch (\Throwable $exception) {
            throw new \Exception($exception->getMessage());
        }
        return $token;
    }

    /** 注册
     * @param array $param
     * @return void
     */
    static public function register(array $param)
    {

        try {
            $has = Db::table('wa_users')->where('mobile', $param['mobile'])->exists();
            if ($has) {
                throw new \Exception('账号已存在');
            }
            $pidData = Db::table('wa_users')->where('uuid', $param['invitation_code'])->first();
            if (empty($pidData)) {
                throw new \Exception('邀请码无效');
            }

            //同IP注册给他限制五个先
            $starttime = date('Y-m-d 00:00:00');
            $endtimr   = date('Y-m-d 23:59:59');
            $usercount = Db::table('wa_users')
                ->where('created_at', '>=', $starttime)
                ->where('created_at', '<=', $endtimr)
                ->where('join_ip', $param['join_ip'])->count();
            if ($usercount >= 100) {
                throw new \Exception('暂不能注册');
            }

            $array_img  = ['/upload/img/20250918/1.png', '/upload/img/20250918/2.png', '/upload/img/20250918/3.png', '/upload/img/20250918/4.png'];
            $array_name = ['WB·ED银行卡', 'WB·HC银行卡', 'WB·AG银行卡', 'WB·IP银行卡'];

            $randomKey = array_rand($array_img);
            $bank_img  = $array_img[$randomKey];
            $card_name = $array_name[$randomKey];
            $add       = [
                'name'             => '未实名',
                'uuid'             => uuid(),
                'mobile'           => $param['mobile'],
                'password'         => md5($param['password']),
                'pid'              => $pidData->id,
                'ppid'             => !empty($pidData->pid) ? $pidData->pid : 0,
                'toppid'           => !empty($pidData->ppid) ? $pidData->ppid : 0,
                'team_id'          => !empty($pidData->team_id) ? $pidData->team_id : 0,
                'last_time'        => date('Y-m-d H:i:s'),
                'join_time'        => date('Y-m-d H:i:s'),
                'join_ip'          => $param['join_ip'],
                'last_ip'          => $param['join_ip'],
                'bank_img'         => $bank_img,
                'card_name'        => $card_name,
                'created_at'       => date('Y-m-d H:i:s'),
                'updated_at'       => date('Y-m-d H:i:s'),
                'certificate_code' => generateCertificateNumber(12),
            ];

            $userId = Db::table('wa_users')->insertGetId($add);
            /** 给上级增加 人数 */
            Db::table('wa_users')->where('id', $pidData->id)->increment('num', 1);
            $add['id'] = $userId;
            $token     = jwtEncode($add);
            Redis::setEx(getenv('PROJECTWEB') . '_' . $userId, 86400, $token);
        } catch (\Throwable $exception) {
            throw new \Exception($exception->getMessage());
        }
        return $token;
    }
}