<?php

namespace app\command;

use Exception;
use plugin\admin\app\model\Config;
use app\model\User as UserModel;
use support\Request;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use support\think\Db;
use support\Mattermost;
use app\model\User;


class Tongji extends Command
{
    protected static $defaultName = 'Tongji';
    protected static $defaultDescription = 'Tongji';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addOption('action','a', InputArgument::OPTIONAL, '要做什么操作');
        $this->addOption('user_id','uid', InputArgument::OPTIONAL, 'user_id');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->question($input,$output);
        //$this->role($input,$output);
        //$this->recharge($input,$output);
        //$this->withdrawl($input,$output);
        return self::SUCCESS;
    }
    /**
     * 修复充值统计
     */
    function recharge(InputInterface $input, OutputInterface $output) {
        //购买金额统计
        $recharge = \app\model\Recharge::where('status',\app\enum\RechargeStatus::COMPLETE->value)->select();
        $recharge_result = [];
        $statistics_recharge_times_result = [];
        $user_recharge_total_result = [];
        $team_recharge_total_result = [];
        /**
         * @var \app\model\Recharge $vo
         */
        foreach($recharge as $vo){
            $date = explode(' ',$vo->created_at)[0];
            $recharge_result[$date] += abs($vo->amount);
            $statistics_recharge_times_result[$date] += 1;
            $user_recharge_total_result[$vo->user_id.''] +=abs($vo->amount);
            $parent_id = get_parent_id($vo->user_id);
            if($parent_id){
                //团队提现统计
                $team_recharge_total_result[$parent_id.''] +=abs($vo->amount);
            }
        }
        foreach($recharge_result as $date => $value){
            cache('statistics_recharge_amount_'.$date,$value);
        }
        foreach($statistics_recharge_times_result as $date => $value){
            cache('statistics_recharge_times_'.$date,$value);
        }
        foreach($user_recharge_total_result as $user_id => $value){
            cache('user_recharge_total_'.$user_id,$value);
        }
        foreach($team_recharge_total_result as $user_id => $value){
            cache('team_recharge_total_'.$user_id,$value);
        }
        cp($recharge_result);
    }
    /**
     * 修复提现统计
     */
    function withdrawl(InputInterface $input, OutputInterface $output) {
        //购买金额统计
        $withdrawl = \app\model\Withdrawl::where('status',\app\enum\WithdrawlStatus::COMPLETE->value)->select();
        $withdrawl_result = [];
        $statistics_withdrawl_times_result = [];
        $user_withdrawl_total_result = [];
        $team_withdrawl_total_result = [];
        /**
         * @var \app\model\Withdrawl $vo
         */
        foreach($withdrawl as $vo){
            $date = explode(' ',$vo->created_at)[0];
            $withdrawl_result[$date] += abs($vo->recive_amount);
            $statistics_withdrawl_times_result[$date] += 1;
            $user_withdrawl_total_result[$vo->user_id.''] +=abs($vo->recive_amount);
            $parent_id = get_parent_id($vo->user_id);
            if($parent_id){
                //团队提现统计
                $team_withdrawl_total_result[$parent_id.''] +=abs($vo->recive_amount);
            }
        }
        foreach($withdrawl_result as $date => $value){
            cache('statistics_withdrawl_amount_'.$date,$value);
        }
        foreach($statistics_withdrawl_times_result as $date => $value){
            cache('statistics_withdrawl_times_'.$date,$value);
        }
        foreach($user_withdrawl_total_result as $user_id => $value){
            cache('user_withdrawl_total_'.$user_id,$value);
        }
        foreach($team_withdrawl_total_result as $user_id => $value){
            cache('team_withdrawl_total_'.$user_id,$value);
        }
        cp($withdrawl_result);
    }
    function question(InputInterface $input, OutputInterface $output) {
        $order_list = \app\model\ProductOrder::withJoin([
            'product'=>function($q){
                return $q->field('price,interest_rate');
            }
        ])->where('quantity','>',0)->select();
    }
    /**
     * 修复所有角色购买统计
     */
    function role(InputInterface $input, OutputInterface $output) {
        
    }
    /**
     * 模拟用户注册
     */
    function register(InputInterface $input, OutputInterface $output){
        $last_user_id = UserModel::order('id','desc')->limit(1)->value('id');
        for ($i=$last_user_id+1; $i <= $last_user_id+2; $i++) { 
            $uids = UserModel::where("status",1)->column('id');
            $referrerId = $uids[array_rand($uids)];
            $email = 'test'.$i.'@msn.cn';
            $mobile = '';
            $password = '123456';
            $extends = [ 
                'role_id'   =>  rand(1,3),
                'money'     =>  0,
                'parent_id'   =>  $referrerId
            ];
            $user = \support\Jwt::register($email, $password, $email, $mobile, $extends);
            cp($user['id']);
        }
        return 1;
    }
    function updateRechargeAddress(InputInterface $input, OutputInterface $output){
        $saveData = [];
        $res = post(Config('pay.server').'/RechargeAddress/create',['appid'=>Config('pay.appid')]);
        if($res){
            $res = json_decode($res,true);
            if($res['code'] === 0){
                $saveData['bep_recharge_address']    = $res['data']['BEP-20']['address'];
                $saveData['trc_recharge_address']    = $res['data']['TRC-20']['address'];
                $saveData['decimal_part']            = $res['data']['BEP-20']['decimal_part'];
            }
        }
        UserModel::where('id',123409)->update($saveData);
        return 0;   
    }
    function otop(){
        $secret = 'EJGYB7OZR2W46XRX7VB3PXHSOY4LUAWCA5GTDAVTWKHXNDAAAIIP7AQ3JSO3XZJNX5J5OTIDEQVKLYFYIYNAXSCYF4GNZ2EMA4ORA3Y';
        $totp = \OTPHP\TOTP::create($secret);
        $secret = $totp->getSecret();
        $totp->setLabel('cansnow');
        $totp->setIssuer('DVPN');
        $qrCodeUri =$totp->getProvisioningUri();
        cp($secret);
        cp($qrCodeUri);
        cp('https://api.qrtool.cn/?text='.urlencode($qrCodeUri));
        cp($totp->at(time()));
        if ($totp->verify('535714')) {
            cp('验证成功');
        } else {
            cp('验证失败');
        }
    }
}
