2
0

ExportChapterIndex.php 2.5 KB

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