InstallPaliSeries.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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:pali.series';
  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. 'sn'=>$data[0],
  53. 'book'=>$data[1],
  54. 'paragraph'=>$data[2],
  55. 'title'=>$data[3],
  56. ];
  57. BookTitle::create($newData);
  58. }
  59. $inputRow++;
  60. }
  61. fclose($fp);
  62. Log::info("res load:" .$strFileName);
  63. } else {
  64. $this->error("can not open csv $strFileName");
  65. Log::error("can not open csv $strFileName");
  66. }
  67. });
  68. $this->info("ok");
  69. return 0;
  70. }
  71. }