<?php

namespace app\controller;

use app\api\repositories\UsersRepositories;
use app\business\BankCardBusiness;
use app\business\LoginBusiness;
use app\business\StreamBusiness;
use app\business\UserIdentityBusiness;
use Illuminate\Support\Arr;
use Matrix\Exception;
use Respect\Validation\Validator;
use support\Db;
use support\Redis;
use support\Request;
use hg\apidoc\annotation as Apidoc;
use Throwable;
use Webman\Captcha\CaptchaBuilder;
use Webman\Captcha\PhraseBuilder;
use function Symfony\Component\String\s;

#[Apidoc\Title("用户信息")]
#[Apidoc\Group("User")]
#[Apidoc\Sort(5)]
class UserController
{

    #[Apidoc\Title("用户信息")]
    #[Apidoc\Url("api/user.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "uuid", type: "int", require: true, desc: 'UUID', default: '100001')]
    #[Apidoc\Returned(name: "mobile", type: "string", require: true, desc: '账号', default: '15800000000')]
    #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '名称', default: '主号')]
    #[Apidoc\Returned(name: "money", type: "string", require: true, desc: 'USD', default: '0.00')]
    #[Apidoc\Returned(name: "money_one", type: "string", require: true, desc: '基金分红钱包', default: '0.00')]
    #[Apidoc\Returned(name: "money_two", type: "string", require: true, desc: '银行卡余额', default: '0.00')]
    #[Apidoc\Returned(name: "money_four", type: "string", require: true, desc: '银行卡基础额度', default: '0.00')]
    #[Apidoc\Returned(name: "equity_value", type: "string", require: true, desc: '汇率', default: '0.00')]
    #[Apidoc\Returned(name: "money_five", type: "string", require: true, desc: 'wroldcion币', default: '0.00')]
    #[Apidoc\Returned(name: "coin_value", type: "string", require: true, desc: 'wroldcion币单枚价值', default: '0.00')]
    #[Apidoc\Returned(name: "zong_money_five", type: "string", require: true, desc: 'wroldcion币总价值', default: '0.00')]
    #[Apidoc\Returned(name: "total_value", type: "string", require: true, desc: '世界币总市值', default: '0.00')]
    #[Apidoc\Returned(name: "turnover", type: "string", require: true, desc: '世界币成交量', default: '0.00')]
    #[Apidoc\Returned(name: "realname_reward", type: "string", require: true, desc: '签到奖励--wroldcion币', default: '0.00')]
    #[Apidoc\Returned(name: "register_award", type: "string", require: true, desc: '实名赠送(wroldcion币)', default: '0.00')]
    #[Apidoc\Returned(name: "invitation_award", type: "string", require: true, desc: '邀请奖励(wroldcion币)', default: '0.00')]
    #[Apidoc\Returned(name: "raffle_num", type: "string", require: true, desc: '助力次数', default: '0.00')]
    #[Apidoc\Returned(name: "effective_is_num", type: "string", require: true, desc: '有效邀请人数', default: '0.00')]
    #[Apidoc\Returned(name: "bank_img", type: "string", require: true, desc: '银行图片', default: '0.00')]
    #[Apidoc\Returned(name: "card_code", type: "string", require: true, desc: '卡号', default: '0.00')]
    #[Apidoc\Returned(name: "total_sign", type: "int", require: true, desc: '签到总天数', default: '')]
    #[Apidoc\Returned(name: "day_is_sign", type: "int", require: true, desc: '今日是否签到', default: '')]
    #[Apidoc\Returned(name: "user_identity", type: "array", desc: '实名信息', default: '', children: [
        ['name' => "name", 'type' => 'string', 'require' => true, 'default' => '张三1', 'desc' => '姓名'],
        ['name' => "number", 'type' => 'string', 'require' => true, 'default' => '500221233312836451', 'desc' => '身份证号'],
    ])]
    #[Apidoc\Returned(name: "bank_card", type: "string", desc: '银行卡', default: '', children: [
        ['name' => "affiliated_bank", 'type' => 'string', 'require' => true, 'default' => '中国银行1', 'desc' => '归属银行'],
        ['name' => "account_holder", 'type' => 'string', 'require' => true, 'default' => '张三', 'desc' => '开户人'],
        ['name' => "card_number", 'type' => 'string', 'require' => true, 'default' => '500221233312836451', 'desc' => '卡号'],
    ])]
    public function userData(Request $request)
    {
        //查询今日是否已签到
        $day_is_sign = Db::table('wa_stream')
            ->where('type', streamType2)
            ->whereBetween('add_time', [strtotime(date('Y-m-d')) . ' 00:00:00', strtotime(date('Y-m-d') . ' 23:59:59')])
            ->where('user_id', $request->user_data['id'])
            ->first();
        $total_sign  = Db::table('wa_stream')->where('type', streamType2)->where('user_id', $request->user_data['id'])->count();

        $system = Db::table('wa_system')->first();

        $is_buy_licai = Db::table('wa_payorder')
            ->where('user_id', $request->user_data['id'])
            ->where('is_pay', 2)
            ->where('goods_type', 1)
            ->exists();

        $mining_count = Db::table('wa_sign_record')
            ->where('uid', $request->user_data['id'])
            ->where('type', 2)
            ->count();

        $is_buy_ambassador = Db::table('wa_payorder')
            ->where('user_id', $request->user_data['id'])
            ->where('is_pay', 2)
            ->where('goods_type', 2)
            ->exists();
        $is_buy_wealth     = Db::table('wa_payorder')
            ->where('user_id', $request->user_data['id'])
            ->where('is_pay', 2)
            ->where('goods_type', 4)
            ->exists();

        $arr = [
            'img'                 => !empty($request->user_data['img']) ? imageToBase64($request->user_data['img']) : '',
            'uuid'                => $request->user_data['uuid'],
            'mobile'              => $request->user_data['mobile'],
            'name'                => $request->user_data['name'],
            'is_autonym'          => $request->user_data['is_autonym'],
            'is_num'              => $request->user_data['is_num'],
            'money'               => $request->user_data['money'],
            'money_one'           => $request->user_data['money_one'],
            'money_two'           => $request->user_data['money_two'],
            'money_four'          => $request->user_data['money_four'],
            'money_five'          => $request->user_data['money_five'],
            'zong_money_five'     => bcmul($request->user_data['money_five'], $system->coin_value, 2),
            'raffle_num'          => $request->user_data['raffle_num'],
            'money_six'           => $request->user_data['money_six'],
            'money_seven'         => $request->user_data['money_seven'],
            'money_eight'         => $request->user_data['money_eight'],
            'money_nine'          => $request->user_data['money_nine'],
            'money_ten'           => $request->user_data['money_ten'],
            'money_eleven'        => $request->user_data['money_eleven'],
            'buy_num'             => $request->user_data['buy_num'],
            'card_code'           => $request->user_data['card_code'],
            'bank_img'            => imageToBase64($request->user_data['bank_img']),
            'day_is_sign'         => $day_is_sign ? true : false,
            'is_buy_licai'        => $is_buy_licai ? true : false,
            'is_buy_ambassador'   => $is_buy_ambassador ? true : false,
            'is_buy_wealth'       => $is_buy_wealth ? true : false,
            'total_sign'          => $total_sign,
            'equity_value'        => $system->equity_value,
            'coin_value'          => $system->coin_value,
            'total_value'         => $system->total_value,
            'turnover'            => $system->turnover,
            'realname_reward'     => $system->realname_reward,
            'register_award'      => $system->register_award,
            'invitation_award'    => $system->invitation_award,
            'overall_progress'    => $system->overall_progress,
            'completion_progress' => $system->completion_progress,
            'mining_count'        => $mining_count,
            'user_identity'       => UserIdentityBusiness::data(['uid' => $request->user_data['id']]),
            'bank_card'           => BankCardBusiness::data(['uid' => $request->user_data['id']]),
        ];

        $has = Db::table('wa_stream')
            ->where('user_id', $request->user_data['id'])
            ->where('type', streamType33)
            ->exists();
        if (empty($has)) {
            StreamBusiness::addStream($request->user_data['id'], $request->user_data['money_ten'], streamType33, moldType10, moldTypefild10);
        }
        $has = Db::table('wa_stream')
            ->where('user_id', $request->user_data['id'])
            ->where('type', streamType34)
            ->exists();
        if (empty($has)) {
            StreamBusiness::addStream($request->user_data['id'], 10000, streamType34, moldType10, moldTypefild10);
        }

        $has       = Db::table('wa_stream')
            ->where('user_id', $request->user_data['id'])
            ->where('type', streamType35)
            ->exists();
        $orderlist = Db::table('wa_payorder')
            ->where('user_id', $request->user_data['id'])
            ->where('goods_type', 3)
            ->where('is_pay', 2)
            ->where('created_at', '>=', '2025-12-02 00:00:00')
            ->where('created_at', '<=', '2025-12-02 23:59:59')
            ->exists();
        if (empty($has) && !empty($orderlist)) {
            StreamBusiness::addStream($request->user_data['id'], 20, streamType35, moldType7, moldTypefild7);
        }
        $rs_refresh = Db::table('wa_users')->where('id', $request->user_data['id'])->value('is_refresh');
        if ($rs_refresh == 0) {
            $ranking = mt_rand(880000, 900000);
            Db::table('wa_user_social_cark')->where('user_id', $request->user_data['id'])->update(['ranking' => $ranking]);
            Db::table('wa_users')->where('id', $request->user_data['id'])->update(['is_refresh' => 1]);
        }

        return success($arr);
    }

    #[Apidoc\Title("签到列表")]
    #[Apidoc\Url("api/user/signlist.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "money", type: "string", desc: '签到奖励', default: '')]
    #[Apidoc\Returned(name: "realname_reward", type: "string", desc: '签到奖励--wroldcion币', default: '')]
    #[Apidoc\Returned(name: "day", type: "string", desc: '天数', default: '')]
    #[Apidoc\Returned(name: "has", type: "string", desc: '是否签到', default: 'true  已签到  false 未签到')]
    #[Apidoc\Returned(name: "signcount", type: "string", desc: '累计签到', default: 'true  已签到  false 未签到')]
    #[Apidoc\Returned(name: "continuity", type: "string", desc: '连续签到', default: 'true  已签到  false 未签到')]
    #[Apidoc\Returned(name: "is_sign", type: "string", desc: '今日是否签到', default: 'true  已签到  false 未签到')]
    public function signlist(Request $request)
    {
        $system = Db::table('wa_system')->first();
        $arr[]  = date('Y-m-d', strtotime('monday this week'));
        $arr[]  = date('Y-m-d', strtotime('monday this week +1 day'));
        $arr[]  = date('Y-m-d', strtotime('monday this week +2 day'));
        $arr[]  = date('Y-m-d', strtotime('monday this week +3 day'));
        $arr[]  = date('Y-m-d', strtotime('monday this week +4 day'));
        $arr[]  = date('Y-m-d', strtotime('monday this week +5 day'));
        $arr[]  = date('Y-m-d', strtotime('sunday this week'));
        $array  = [];
        foreach ($arr as $k => $v) {
            $array[] = [
                'day' => date('d', strtotime($v)),
                'has' => Db::table('wa_stream')
                    ->where('user_id', $request->user_data['id'])
                    ->where('type', streamType2)
                    ->whereBetween('add_time', [strtotime($v . ' 00:00:00'), strtotime($v . ' 23:59:59')])->exists()
            ];
        }
        $signcount = Db::table('wa_stream')->where('type', streamType2)->where('user_id', $request->user_data['id'])->count();
        $is_sign   = Db::table('wa_stream')
            ->where('user_id', $request->user_data['id'])
            ->where('type', streamType2)
            ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
            ->exists();
        return success([
            'list'                => $array,
            'signcount'           => $signcount,
            'continuity'          => $request->user_data['continuity'],
            'money'               => $system->realname_reward,
            'original_sign_award' => $system->sign_award,
            'is_sign'             => $is_sign,
        ]);

//        $param              = $request->param_data;
//        $param['user_data'] = $request->user_data;
//        $month_num          = date('t');
//        $res                = [];
//        //查询当前用户当月签到记录
//        $this_month_sign = Db::table('wa_stream')
//            ->where('type', streamType2)
//            ->where('user_id', $param['user_data']['id'])
//            ->whereRaw("DATE_FORMAT(created_at,'%Y-%m')=?", date('Y-m'))
//            ->get();
//        for ($i = 1; $i <= $month_num; $i++) {
//            $date       = $i > 9 ? date('Y-m-') . $i : date('Y-m-') . '0' . $i;
//            $check_sign = collect($this_month_sign)
//                ->where('created_at', '>=', $date . ' 00:00:00')
//                ->where('created_at', '<=', $date . ' 23:59:59')
//                ->exists();
//            $res[]      = [
//                'day' => $i,
//                'has' => $check_sign,
//            ];
//        }
//        $signcount = Db::table('wa_stream')
//            ->where('type', streamType2)
//            ->where('user_id', $request->user_data['id'])
//            ->count();
//        $is_sign   = Db::table('wa_stream')
//            ->where('user_id', $request->user_data['id'])
//            ->where('type', streamType2)
//            ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
//            ->exists();
//        return success([
//            'list'       => $res,
//            'signcount'  => $signcount,
//            'continuity' => $request->user_data['continuity'],
//            'is_sign'    => $is_sign,
//        ]);

    }

    #[Apidoc\Title("签到")]
    #[Apidoc\Url("api/user/sign.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "money", type: "string", desc: '奖励金额', default: '888')]
    #[Apidoc\Returned(name: "signcount", type: "string", desc: '累计签到天数', default: '888')]
    public function sign(Request $request)
    {
        Db::beginTransaction();
        try {
            $zuotian = Db::table('wa_stream')
                ->where('user_id', $request->user_data['id'])
                ->where('type', streamType2)
                ->whereBetween('add_time', [bcsub(strtotime(date('Y-m-d') . ' 00:00:00'), 86400), bcadd(strtotime(date('Y-m-d') . ' 23:59:59'), 86400)])
                ->exists();
            if ($zuotian) {
                $continuity = bcadd($request->user_data['continuity'], 1);
            } else {
                $continuity = 1;
            }

            $has = Db::table('wa_stream')
                ->where('user_id', $request->user_data['id'])
                ->where('type', streamType2)
                ->whereBetween('add_time', [strtotime(date('Y-m-d') . ' 00:00:00'), strtotime(date('Y-m-d') . ' 23:59:59')])
                ->exists();
            if ($has) {
                throw new \Exception('今日已签到,请不要重复签到');
            }

            $system = Db::table('wa_system')->first();
            if (!empty($system->sign_award)) {
                StreamBusiness::addStream($request->user_data['id'], $system->sign_award, streamType2, moldType1, moldTypefild1);
            }
            if (!empty($system->realname_reward)) {
                StreamBusiness::addStream($request->user_data['id'], $system->realname_reward, streamType2, moldType5, moldTypefild5);
            }
            Db::table('wa_users')->where(['id' => $request->user_data['id']])->update(['continuity' => $continuity]);

            $signcount = Db::table('wa_stream')->where('type', streamType2)->where('user_id', $request->user_data['id'])->count();

            $userdata = Db::table('wa_users')->where(['id' => $request->user_data['id']])->first();
            if ($continuity >= 3 && $request->user_data['is_boost'] == 1 && $userdata->is_effective == 0 && !empty($request->user_data['pid'])) {
                Db::table('wa_users')->where('id', $request->user_data['pid'])->increment('effective_is_num', 1);
                Db::table('wa_users')->where(['id' => $request->user_data['id']])->update(['is_effective' => 1]);
            }

        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success([
            'money'     => $system->sign_award,
            'signcount' => $signcount
        ], '签到成功');
    }

    #[Apidoc\Title("我的团队头数据")]
    #[Apidoc\Url("api/user/team.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "num", type: "int", require: true, desc: '团队总人数', default: '1')]
    #[Apidoc\Returned(name: "earnings", type: "int", require: true, desc: '总拥金收益', default: '0')]
    #[Apidoc\Returned(name: "dayearnings", type: "int", require: true, desc: '今日收益', default: '0')]
    #[Apidoc\Returned(name: "one", type: "int", require: true, desc: '一级人数', default: '0')]
    #[Apidoc\Returned(name: "two", type: "int", require: true, desc: '二级人数', default: '0')]
    #[Apidoc\Returned(name: "three", type: "int", require: true, desc: '三级人数', default: '0')]
    #[Apidoc\Returned(name: "rebate", type: "int", require: true, desc: '一级返佣', default: '0')]
    #[Apidoc\Returned(name: "rebate_one", type: "int", require: true, desc: '二级返佣', default: '0')]
    #[Apidoc\Returned(name: "rebate_two", type: "int", require: true, desc: '三级返佣', default: '0')]
    public function team(Request $request)
    {
        $param = $request->user_data;

        $data   = Db::table('wa_users')->where(function ($query) use ($param) {
            $query->orWhere('pid', $param['id']);
            $query->orWhere('ppid', $param['id']);
            $query->orWhere('toppid', $param['id']);
        });
        $system = Db::table('wa_system')->first();
        /** @var  $num  团队总人数 */
        $num = (clone $data)->count();
//        $stream = Db::table('wa_stream')
//            ->where('mold', moldType1)
//            ->where('type', streamType14)
//            ->where('user_id', $request->user_data['id']);

        return success([
            'num'         => $num,
            'earnings'    => 0,
            'dayearnings' => 0,
            'one'         => Db::table('wa_users')->where('pid', $param['id'])->count(),
            'two'         => Db::table('wa_users')->where('ppid', $param['id'])->count(),
            'three'       => Db::table('wa_users')->where('toppid', $param['id'])->count(),
            'rebate'      => $system->rebate,
            'rebate_one'  => $system->rebate_one,
            'rebate_two'  => $system->rebate_two,
        ]);

    }

    #[Apidoc\Title("我的团队列表数据")]
    #[Apidoc\Url("api/user/team_list.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Param("page", type: "int", require: true, desc: "页面", mock: 1)]
    #[Apidoc\Param("limit", type: "int", require: true, desc: "输出条数", mock: 10)]
    #[Apidoc\Param("grade", type: "int", require: true, desc: "等级 1一级 2二级 3三级", mock: 1)]
    #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '名称', default: '未实名')]
    #[Apidoc\Returned(name: "mobile", type: "string", require: true, desc: '电话', default: '158****0002')]
    #[Apidoc\Returned(name: "join_time", type: "string", require: true, desc: '注册时间', default: '2024-07-08 01:20:47')]
    #[Apidoc\Returned(name: "invest_money", type: "string", require: true, desc: '消费金额', default: '0.00')]
    public function teamList(Request $request)
    {
        try {
            $param              = $request->param_data;
            $param['user_data'] = $request->user_data;
            Validator::input($param, [
                'page'  => Validator::notEmpty()->intType()->setName('页面'),
                'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
                'grade' => Validator::notEmpty()->intType()->setName('等级'),
            ]);
            $data = Db::table('wa_users')->where(function ($query) use ($param) {
                if (Arr::get($param, 'grade')) {
                    if ($param['grade'] == 1) {
                        $query->where('pid', $param['user_data']['id']);
                    } elseif ($param['grade'] == 2) {
                        $query->where('ppid', $param['user_data']['id']);
                    } elseif ($param['grade'] == 3) {
                        $query->where('toppid', $param['user_data']['id']);
                    }
                } else {
                    $query->where('pid', $param['user_data']['id']);
                }
            })->select(['name', 'mobile', 'join_time', 'invest_money'])
                ->orderByDesc('id')
                ->paginate(Arr::get($param, 'limit', 10), ['*'], 'page', Arr::get($param, 'page'))
                ->toArray();
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]->mobile = substr_replace($v->mobile, '****', 3, 4);
            }

        } catch (\Throwable $exception) {
            return new Exception($exception->getMessage());
        }

        return success($data['data'], '成功', 200, $data['total']);
    }

//        #[Apidoc\Title("我的投资")]
//        #[Apidoc\Url("api/user/mygoods")]
//        #[Apidoc\Method("POST")]
//        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
//        #[Apidoc\Param("page", type: "int", require: true, desc: "页面", mock: 1)]
//        #[Apidoc\Param("limit", type: "int", require: true, desc: "输出条数", mock: 10)]
//        #[Apidoc\Returned(name: "id", type: "string", require: true, desc: '购买记录ID', default: '485')]
//        #[Apidoc\Returned(name: "goods_id", type: "string", require: true, desc: '产品ID', default: '5')]
//        #[Apidoc\Returned(name: "img", type: "string", require: true, desc: '封面图', default: '')]
//        #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '产品名称', default: '')]
//        #[Apidoc\Returned(name: "type", type: "string", require: true, desc: '产品类型 1 慈善 2助力', default: '')]
//        #[Apidoc\Returned(name: "price", type: "string", require: true, desc: '价格', default: '')]
//        #[Apidoc\Returned(name: "pay_price", type: "string", require: true, desc: '通道支付金额', default: '')]
//        #[Apidoc\Returned(name: "period", type: "string", require: true, desc: '周期', default: '')]
//        #[Apidoc\Returned(name: "bl", type: "string", require: true, desc: '比例', default: '')]
//        #[Apidoc\Returned(name: "bonus", type: "string", require: true, desc: '每日分红金额', default: '')]
//        #[Apidoc\Returned(name: "predict", type: "string", require: true, desc: '预计收益', default: '')]
//        #[Apidoc\Returned(name: "created_at", type: "string", require: true, desc: '购买时间', default: '')]
//        #[Apidoc\Returned(name: "expiretime", type: "string", require: true, desc: '到期时间戳', default: '')]
//        #[Apidoc\Returned(name: "is_grant", type: "string", require: true, desc: '是否退本 1未退本  2已退本', default: '')]
//        public function mygoods(Request $request)
//        {
//            try {
//                $param              = Arr::only($request->all(), ['page', 'limit', 'grade']);
//                $param['user_data'] = $request->user_data;
//                Validator::input($param, [
//                    'page'  => Validator::notEmpty()->intType()->setName('页码'),
//                    'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
//                ]);
//                $data = Db::table('wa_my_goods')
//                    ->join('wa_goods', 'wa_goods.id', '=', 'wa_my_goods.goods_id')
//                    ->where('wa_my_goods.user_id', $param['user_data']['id'])
//                    ->select(['wa_my_goods.id', 'wa_my_goods.goods_id', 'wa_goods.img', 'wa_goods.type', 'wa_goods.pay_price', 'wa_goods.price', 'wa_goods.name', 'wa_goods.period', 'wa_goods.bl', 'wa_goods.bonus', 'wa_goods.predict', 'wa_goods.bonus', 'wa_my_goods.created_at', 'wa_my_goods.expiretime', 'wa_my_goods.is_grant'])
//                    ->orderByDesc('wa_my_goods.id')
//                    ->paginate(Arr::get($param, 'limit', 10))
//                    ->toArray();
//                foreach ($data['data'] as $k => $v) {
//                    $data['data'][$k]->img = getenv('IMG') . $v->img;
//                }
//            } catch (\Throwable $exception) {
//                return error($exception->getMessage());
//            }
//
//            return success($data['data'], '成功', 200, $data['total']);
//        }

    #[Apidoc\Title("退出登录")]
    #[Apidoc\Url("api/user/quit.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    public function quit(Request $request)
    {
        Redis::del($request->user_data['id']);
        return success([], '已退出登录!');
    }

    #[Apidoc\Title("修改密码")]
    #[Apidoc\Url("api/user/save.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Param("type", type: "int", require: true, desc: "类型 1修改名称  2 修改密码 ", mock: 1)]
    #[Apidoc\Param("name", type: "string", require: true, desc: "名称", mock: 1)]
    #[Apidoc\Param("old_password", type: "string", require: true, desc: "旧密码", mock: 1)]
    #[Apidoc\Param("password", type: "string", require: true, desc: "新密码", mock: 10)]
    public function save(Request $request)
    {
        Db::beginTransaction();
        try {
            $param = $request->param_data;
            Validator::input($param, [
                'type' => Validator::notEmpty()->intType()->setName('操作类型'),
            ]);
            if (Arr::get($param, 'type') == 1) {
                Validator::input($param, [
                    'name' => Validator::notEmpty()->setName('操作类型'),
                ]);
                $arr['name'] = $param['name'];

            } elseif (Arr::get($param, 'type') == 2) {
                Validator::input($param, [
                    'old_password' => Validator::notEmpty()->setName('旧密码'),
                    'password'     => Validator::notEmpty()->setName('新密码'),
                ]);
                if ($request->user_data['password'] != md5($param['old_password'])) {
                    throw new \Exception('密码验证失败');
                }
                $arr['password'] = md5($param['password']);
            }
            if (empty($arr)) {
                throw new \Exception('非法操作');
            }
            Db::table('wa_users')->where('id', $request->user_data['id'])->update($arr);
        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success([], '修改成功');
    }


    #[Apidoc\Title("统计数据")]
    #[Apidoc\Url("api/user/cfxstat.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Returned(name: "coin_value", type: "string", require: true, desc: 'wroldcion币单枚价值', default: '0.00')]
    #[Apidoc\Returned(name: "total_value", type: "string", require: true, desc: '世界币总市值', default: '0.00')]
    #[Apidoc\Returned(name: "turnover", type: "string", require: true, desc: '世界币成交量', default: '0.00')]
    #[Apidoc\Returned(name: "realname_reward", type: "string", require: true, desc: '签到奖励--wroldcion币', default: '0.00')]
    #[Apidoc\Returned(name: "register_award", type: "string", require: true, desc: '实名赠送(wroldcion币)', default: '0.00')]
    #[Apidoc\Returned(name: "invitation_award", type: "string", require: true, desc: '邀请奖励(wroldcion币)', default: '0.00')]
    public function cfxstat(Request $request)
    {
        Db::beginTransaction();
        try {
            $system     = Db::table('wa_system')->first();
            $cfxdata    = Db::table('wa_cfx_data')->orderByRaw('id')->limit(30)
                ->get()
                ->toArray();
            $categories = [];
            $series     = [];
            foreach ($cfxdata as $k => $v) {
                if (empty($v->created_at)) {
                    $categories[$k] = date('m/d', $v->createtime);
                } else {
                    $categories[$k] = date('m/d', strtotime($v->created_at));
                }
                $series[] = [$categories[$k], $v->open, $v->close, $v->up, $v->lower];
            }
            $list = [
                'series' => $series,
            ];
            $data = [
                'list'                      => $list,
                'coin_value'                => $system->coin_value,
                'total_value'               => $system->total_value,
                'turnover'                  => $system->turnover,
                'realname_reward'           => $system->realname_reward,
                'original_realname_reward'  => $system->original_realname_reward,
                'register_award'            => $system->register_award,
                'original_register_award'   => $system->original_register_award,
                'invitation_award'          => $system->invitation_award,
                'original_invitation_award' => $system->original_invitation_award,
            ];
        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success($data, '获取成功');
    }

}
