InstallPaliSeries.php 1.6 KB

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