ExportChapterIndex.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $publicChannels = Channel::where('status',30)->select('uid')->get();
  50. $bar = $this->output->createProgressBar(ProgressChapter::whereIn('channel_id',$publicChannels)->count());
  51. foreach (ProgressChapter::whereIn('channel_id',$publicChannels)
  52. ->select(['uid','book','para',
  53. 'lang','title','channel_id',
  54. 'progress','updated_at'])->cursor() as $row) {
  55. $currData = array(
  56. $row->uid,
  57. $row->book,
  58. $row->para,
  59. $row->lang,
  60. $row->title,
  61. $row->channel_id,
  62. $row->progress,
  63. $row->updated_at,
  64. );
  65. $stmt->execute($currData);
  66. $bar->advance();
  67. }
  68. $dbh->commit();
  69. $bar->finish();
  70. return 0;
  71. }
  72. }