ExportChannel.php 2.4 KB

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