ExportChapterIndex.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. class ExportChapterIndex extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'export:chapter.index';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'export chapter index';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $exportFile = storage_path('app/public/export/offline/sentence-'.date("Y-m-d").'.db3');
  37. $dbh = new \PDO('sqlite:'.$exportFile, "", "", array(\PDO::ATTR_PERSISTENT => true));
  38. $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
  39. $dbh->beginTransaction();
  40. $query = "INSERT INTO chapter ( id , book , paragraph,
  41. language , title , channel_id , progress,updated_at )
  42. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  43. try{
  44. $stmt = $dbh->prepare($query);
  45. }catch(PDOException $e){
  46. Log::info($e);
  47. return 1;
  48. }
  49. $bar = $this->output->createProgressBar(ProgressChapter::count());
  50. foreach (ProgressChapter::select(['uid','book','para',
  51. 'lang','title','channel_id','progress','updated_at'])->cursor() as $row) {
  52. $currData = array(
  53. $row->uid,
  54. $row->book,
  55. $row->para,
  56. $row->lang,
  57. $row->title,
  58. $row->channel_id,
  59. $row->progress,
  60. $row->updated_at,
  61. );
  62. $stmt->execute($currData);
  63. $bar->advance();
  64. }
  65. $dbh->commit();
  66. $bar->finish();
  67. return 0;
  68. }
  69. }