ExportChannel.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $this->info('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. $total = Channel::where('status', 30)->count();
  58. $channels= Channel::where('status', 30)
  59. ->select([
  60. 'uid',
  61. 'name',
  62. 'type',
  63. 'lang',
  64. 'summary',
  65. 'owner_uid',
  66. 'setting',
  67. 'created_at'
  68. ])
  69. ->cursor();
  70. foreach ($channels as $key => $row ) {
  71. $currData = array(
  72. $row->uid,
  73. $row->name,
  74. $row->type,
  75. $row->lang,
  76. $row->summary,
  77. $row->owner_uid,
  78. $row->setting,
  79. $row->created_at,
  80. );
  81. $stmt->execute($currData);
  82. if($key % 30 ===0){
  83. $precent = (int)($key*100/$total);
  84. $this->line("[{$precent}%]");
  85. }
  86. }
  87. $dbh->commit();
  88. $this->info('task export offline channel-table finished');
  89. return 0;
  90. }
  91. }