ExportChapterIndex.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\Channel;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\Log;
  9. class ExportChapterIndex extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:chapter.index';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'export chapter index';
  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 chapter ( id , book , paragraph,
  44. language , title , channel_id , progress,updated_at )
  45. VALUES ( ? , ? , ? , ? , ? , ? , ? , ? )";
  46. try{
  47. $stmt = $dbh->prepare($query);
  48. }catch(PDOException $e){
  49. Log::info($e);
  50. return 1;
  51. }
  52. $publicChannels = Channel::where('status',30)->select('uid')->get();
  53. $rows = ProgressChapter::whereIn('channel_id',$publicChannels)->count();
  54. Cache::put("/export/chapter/count",$rows,3600*10);
  55. $bar = $this->output->createProgressBar($rows);
  56. foreach (ProgressChapter::whereIn('channel_id',$publicChannels)
  57. ->select(['uid','book','para',
  58. 'lang','title','channel_id',
  59. 'progress','updated_at'])->cursor() as $row) {
  60. $currData = array(
  61. $row->uid,
  62. $row->book,
  63. $row->para,
  64. $row->lang,
  65. $row->title,
  66. $row->channel_id,
  67. $row->progress,
  68. $row->updated_at,
  69. );
  70. $stmt->execute($currData);
  71. $bar->advance();
  72. }
  73. $dbh->commit();
  74. $bar->finish();
  75. return 0;
  76. }
  77. }