ExportChannel.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class ExportChannel extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:channel';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '导出离线用的channel数据';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  40. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  41. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  42. $dbh->beginTransaction();
  43. $query = "INSERT INTO channel ( id , name , type , language ,
  44. summary , owner_id , setting,created_at )
  45. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  46. try{
  47. $stmt = $dbh->prepare($query);
  48. }catch(PDOException $e){
  49. Log::info($e);
  50. return 1;
  51. }
  52. $bar = $this->output->createProgressBar(Channel::where('status',30)->count());
  53. foreach (Channel::where('status',30)
  54. ->select(['uid','name','type','lang',
  55. 'summary','owner_uid','setting','created_at'])
  56. ->cursor() as $row) {
  57. $currData = array(
  58. $row->uid,
  59. $row->name,
  60. $row->type,
  61. $row->lang,
  62. $row->summary,
  63. $row->owner_uid,
  64. $row->setting,
  65. $row->created_at,
  66. );
  67. $stmt->execute($currData);
  68. $bar->advance();
  69. }
  70. $dbh->commit();
  71. $bar->finish();
  72. return 0;
  73. }
  74. }