2
0

ExportChapterIndex.php 2.5 KB

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