ExportChannel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * 导出离线用的channel数据
  4. */
  5. namespace App\Console\Commands;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Models\Channel;
  9. use Illuminate\Support\Facades\Log;
  10. class ExportChannel extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'export:channel {db}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '导出离线用的channel数据';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (\App\Tools\Tools::isStop()) {
  41. return 0;
  42. }
  43. Log::debug('task export offline channel-table start');
  44. $exportFile = storage_path('app/public/export/offline/' . $this->argument('db') . '-' . date("Y-m-d") . '.db3');
  45. $dbh = new \PDO('sqlite:' . $exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  46. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  47. $dbh->beginTransaction();
  48. $query = "INSERT INTO channel ( id , name , type , language ,
  49. summary , owner_id , setting,created_at )
  50. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  51. try {
  52. $stmt = $dbh->prepare($query);
  53. } catch (\PDOException $e) {
  54. Log::error($e->getMessage(), ['exception' => $e]);
  55. return 1;
  56. }
  57. $bar = $this->output->createProgressBar(Channel::where('status', 30)->count());
  58. foreach (
  59. Channel::where('status', 30)
  60. ->select([
  61. 'uid',
  62. 'name',
  63. 'type',
  64. 'lang',
  65. 'summary',
  66. 'owner_uid',
  67. 'setting',
  68. 'created_at'
  69. ])
  70. ->cursor() as $row
  71. ) {
  72. $currData = array(
  73. $row->uid,
  74. $row->name,
  75. $row->type,
  76. $row->lang,
  77. $row->summary,
  78. $row->owner_uid,
  79. $row->setting,
  80. $row->created_at,
  81. );
  82. $stmt->execute($currData);
  83. $bar->advance();
  84. }
  85. $dbh->commit();
  86. $bar->finish();
  87. Log::debug('task export offline channel-table finished');
  88. return 0;
  89. }
  90. }