ExportChapterIndex.php 2.6 KB

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