InstallPaliSeries.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $this->info("upgrade pali serieses");
  41. $startTime = time();
  42. DB::transaction(function () {
  43. #删除目标数据库中数据
  44. BookTitle::where('book','>',0)->delete();
  45. // 打开csv文件并读取数据
  46. $strFileName = config("mint.path.pali_title") . "/pali_serieses.csv";
  47. if(!file_exists($strFileName)){
  48. return 1;
  49. }
  50. $inputRow = 0;
  51. if (($fp = fopen($strFileName, "r")) !== false) {
  52. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  53. if($inputRow>0){
  54. $newData = [
  55. 'sn'=>$data[0],
  56. 'book'=>$data[1],
  57. 'paragraph'=>$data[2],
  58. 'title'=>$data[3],
  59. ];
  60. BookTitle::create($newData);
  61. }
  62. $inputRow++;
  63. }
  64. fclose($fp);
  65. Log::info("res load:" .$strFileName);
  66. } else {
  67. $this->error("can not open csv $strFileName");
  68. Log::error("can not open csv $strFileName");
  69. }
  70. });
  71. $this->info("ok");
  72. return 0;
  73. }
  74. }