InstallPaliSeries.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\BookTitle;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class InstallPaliSeries extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'install:pali.series';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  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. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $this->info('upgrade pali serieses');
  42. $startTime = time();
  43. DB::transaction(function () {
  44. // 删除目标数据库中数据
  45. BookTitle::where('book', '>', 0)->delete();
  46. // 打开csv文件并读取数据
  47. $strFileName = config('mint.path.pali_title').'/pali_serieses.csv';
  48. if (! file_exists($strFileName)) {
  49. return 1;
  50. }
  51. $inputRow = 0;
  52. if (($fp = fopen($strFileName, 'r')) !== false) {
  53. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  54. if ($inputRow > 0) {
  55. $newData = [
  56. 'sn' => $data[0],
  57. 'book' => $data[1],
  58. 'paragraph' => $data[2],
  59. 'title' => $data[3],
  60. ];
  61. BookTitle::create($newData);
  62. }
  63. $inputRow++;
  64. }
  65. fclose($fp);
  66. Log::info('res load:'.$strFileName);
  67. } else {
  68. $this->error("can not open csv $strFileName");
  69. Log::error("can not open csv $strFileName");
  70. }
  71. });
  72. $this->info('ok');
  73. return 0;
  74. }
  75. }