<?php
namespace app\command;

use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use think\db\PDOConnection;
use support\think\Db;

class Clear extends Command
{
    protected static $defaultName = 'clear';
    protected static $defaultDescription = '数据库缓存';
    protected function configure()
    {
        $this->setDescription('clear database.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $action = 'all';
        if($action == 'all'){
            Db::name('address')->where('id','>',0)->delete();
            Db::name('recharge')->where('id','>',0)->delete();
            Db::name('transfer')->where('id','>',0)->delete();
            Db::name('user')->where('id','>',0)->delete();
            Db::name('user_extend')->where('user_id','>',0)->delete();
            Db::name('user_team')->where('descendant_id|ancestor_id','>',0)->delete();
            Db::name('withdrawl')->where('id','>',0)->delete();
            Db::name('work_record')->where('id','>',0)->delete();
        }else{
            $list = \app\model\User::order('id','asc')->select();
            foreach($list as $k=>$user){
                Db::name('address')->where('user_id',$user->id)->delete();
                Db::name('transfer')->where('user_id',$user->id)->delete();

                Db::name('recharge')->where('user_id',$user->id)->delete();
                Db::name('record')->where('user_id',$user->id)->delete();
                Db::name('withdrawl')->where('user_id',$user->id)->delete();
                Db::name('user_extend')->where('user_id',$user->id)->delete();
                Db::name('user_team')->where('descendant_id|ancestor_id','=',$user->id)->delete();
                Db::name('withdrawl')->where('user_id',$user->id)->delete();
                Db::name('work_record')->where('user_id',$user->id)->delete();
                Db::name('user')->where('id',$user->id)->delete();

            }
        }
        $output->writeln('<info>Succeed!</info>');
        return self::SUCCESS;
    }

    protected function buildModelSchema(string $class): void
    {

        
    }
}