UpgradeWordPart.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WordPart;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. class UpgradeWordPart extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'upgrade:wordpart';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. #载入csv数据
  37. $csvFile = config("app.path.dict_text") .'/system/part.csv';
  38. if (($fp = fopen($csvFile, "r")) !== false) {
  39. Log::info("csv load:" . $csvFile);
  40. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  41. WordPart::updateOrCreate(['word' => $data[0],],['weight' => $data[1],]);
  42. }
  43. fclose($fp);
  44. } else {
  45. $this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  46. Log::error( "can not open csv file. filename=" . $csvFile) ;
  47. }
  48. $this->info('ok');
  49. return 0;
  50. }
  51. }