ExportChannel.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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';
  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. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  41. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  42. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  43. $dbh->beginTransaction();
  44. $query = "INSERT INTO channel ( id , name , type , language ,
  45. summary , owner_id , setting,created_at )
  46. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  47. try{
  48. $stmt = $dbh->prepare($query);
  49. }catch(PDOException $e){
  50. Log::info($e);
  51. return 1;
  52. }
  53. $bar = $this->output->createProgressBar(Channel::where('status',30)->count());
  54. foreach (Channel::where('status',30)
  55. ->select(['uid','name','type','lang',
  56. 'summary','owner_uid','setting','created_at'])
  57. ->cursor() as $row) {
  58. $currData = array(
  59. $row->uid,
  60. $row->name,
  61. $row->type,
  62. $row->lang,
  63. $row->summary,
  64. $row->owner_uid,
  65. $row->setting,
  66. $row->created_at,
  67. );
  68. $stmt->execute($currData);
  69. $bar->advance();
  70. }
  71. $dbh->commit();
  72. $bar->finish();
  73. return 0;
  74. }
  75. }