CacheDictPreference.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Cache;
  6. use App\Models\UserDict;
  7. use Illuminate\Support\Facades\DB;
  8. class CacheDictPreference extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'cache:dict.preference';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $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. $prefix = 'dict-preference';
  39. $words = UserDict::select(['word','language'])
  40. ->groupBy(['word','language'])
  41. ->cursor();
  42. $count = DB::select('SELECT count(*) from (
  43. SELECT word,language from user_dicts group by word,language) T');
  44. $bar = $this->output->createProgressBar($count[0]->count);
  45. foreach ($words as $key => $word) {
  46. $meaning = UserDict::where('word',$word->word)
  47. ->where('language',$word->language)
  48. ->where('source','_PAPER_RICH_')
  49. ->whereNotNull('mean')
  50. ->value('mean');
  51. $meaning = trim($meaning," $");
  52. if(!empty($meaning)){
  53. $m = explode('$',$meaning);
  54. Cache::put("{$prefix}/{$word->word}/{$word->language}",$m[0]);
  55. }
  56. $bar->advance();
  57. }
  58. $bar->finish();
  59. return 0;
  60. }
  61. }