ClearMerchantData.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\input\Option;
  6. use think\console\Output;
  7. use think\db\PDOConnection;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\Model;
  11. class ClearMerchantData extends Command
  12. {
  13. protected function configure()
  14. {
  15. // 指令配置
  16. $this->setName('clear:merchant')
  17. ->setDescription('清空数据(除系统配置以外的所有数据)');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $flag = $output->confirm($input, '清空数据前务必做好数据库的备份,防止数据被误删 !!!', false);
  22. if (!$flag) return;
  23. $tables = Db::query('SHOW TABLES FROM ' . env('database.database', ''));
  24. $pre = env('database.prefix', '');
  25. $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'];
  26. foreach ($tables as $table) {
  27. $name = array_values($table)[0];
  28. if (!in_array($name, $bakTables)) {
  29. Db::table($name)->delete(true);
  30. }
  31. }
  32. $output->info('删除成功');
  33. }
  34. }