<?php



namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\db\PDOConnection;
use think\Exception;
use think\facade\Db;
use think\Model;

class ClearMerchantData extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('clear:merchant')
            ->setDescription('清空数据(除系统配置以外的所有数据)');
    }

    protected function execute(Input $input, Output $output)
    {
        $flag = $output->confirm($input, '清空数据前务必做好数据库的备份,防止数据被误删 !!!', false);
        if (!$flag) return;
        $tables = Db::query('SHOW TABLES FROM ' . env('database.database', ''));
        $pre = env('database.prefix', '');
        $bakTables = [$pre . 'express', $pre . 'system_admin', $pre . 'system_city', $pre . 'system_config', $pre . 'system_config_classify', $pre . 'system_config_value', $pre . 'system_group', $pre . 'system_group_data', $pre . 'system_menu', $pre . 'system_role', $pre . 'template_message'];

        foreach ($tables as $table) {
            $name = array_values($table)[0];
            if (!in_array($name, $bakTables)) {
                Db::table($name)->delete(true);
            }
        }

        $output->info('删除成功');
    }

}