2
0

UpgradeCommunityTerm.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Tools\Tools;
  5. use App\Models\DhammaTerm;
  6. use App\Http\Api\ChannelApi;
  7. use Illuminate\Support\Str;
  8. class UpgradeCommunityTerm extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'upgrade:community.term {lang}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command 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. $lang = strtolower($this->argument('lang'));
  39. $langFamily = explode('-',$lang)[0];
  40. $localTerm = ChannelApi::getSysChannel(
  41. "_community_term_{$lang}_",
  42. "_community_term_en_"
  43. );
  44. if(!$localTerm){
  45. return 1;
  46. }
  47. $table = DhammaTerm::select('word')->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  48. ->groupBy('word');
  49. $words = $table->get();
  50. $bar = $this->output->createProgressBar(count($words));
  51. foreach ($words as $key => $word) {
  52. $best = DhammaTerm::selectRaw('meaning,count(*) as co')
  53. ->where('word',$word->word)
  54. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  55. ->groupBy('meaning')
  56. ->orderBy('co','desc')
  57. ->first();
  58. if($best){
  59. $term = DhammaTerm::where('channal',$localTerm)->firstOrNew(
  60. [
  61. "word" => $word->word,
  62. "channal" => $localTerm,
  63. ],
  64. [
  65. 'id' =>app('snowflake')->id(),
  66. 'guid' =>Str::uuid(),
  67. 'word_en' =>Tools::getWordEn($word->word),
  68. 'meaning' => '',
  69. 'language' => $this->argument('lang'),
  70. 'owner' => config("app.admin.root_uuid"),
  71. 'editor_id' => 0,
  72. 'create_time' => time()*1000,
  73. ]
  74. );
  75. $term->meaning = $best->meaning;
  76. $term->modify_time = time()*1000;
  77. $term->save();
  78. }
  79. $bar->advance();
  80. }
  81. $bar->finish();
  82. return 0;
  83. }
  84. }