UpgradeGrammarBook.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\ChannelApi;
  4. use App\Models\DhammaTerm;
  5. use App\Models\Relation;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Str;
  8. class UpgradeGrammarBook extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan upgrade:grammar.book
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:grammar.book';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. $lang = 'zh-Hans';
  40. $channelId = ChannelApi::getSysChannel('_System_Grammar_Term_'.strtolower($lang).'_');
  41. if ($channelId === false) {
  42. $this->error('no channel');
  43. return 1;
  44. }
  45. $relations = Relation::get();
  46. $result = [];
  47. foreach ($relations as $key => $relation) {
  48. $from = json_decode($relation->from, true);
  49. $words = [];
  50. if (isset($from['spell']) && ! empty($from['spell'])) {
  51. $words[] = $from['spell'];
  52. }
  53. if (isset($from['case']) && count($from['case']) > 0) {
  54. $words = array_merge($words, $from['case']);
  55. }
  56. if (count($words) === 0) {
  57. continue;
  58. }
  59. $word = implode('.', $words);
  60. if (! isset($result[$word])) {
  61. $result[$word] = [];
  62. }
  63. $result[$word][] = $relation;
  64. }
  65. foreach ($result as $key => $rows) {
  66. $this->info('## '.$key);
  67. $caseLocal = DhammaTerm::where('channal', $channelId)
  68. ->where('word', $key)
  69. ->value('meaning');
  70. if ($caseLocal) {
  71. $title = "**[[{$key}]]** 用法表\n\n";
  72. } else {
  73. $title = "**{$key}** 用法表\n\n";
  74. }
  75. $relations = [];
  76. foreach ($rows as $row) {
  77. if (! isset($relations[$row['name']])) {
  78. $relations[$row['name']] = [];
  79. $local = DhammaTerm::where('channal', $channelId)
  80. ->where('word', $row['name'])
  81. ->first();
  82. if ($local) {
  83. $relations[$row['name']]['meaning'] = $local->meaning;
  84. $relations[$row['name']]['note'] = $local->note;
  85. } else {
  86. $relations[$row['name']]['meaning'] = '';
  87. $relations[$row['name']]['note'] = '';
  88. }
  89. $relations[$row['name']]['to'] = [];
  90. }
  91. $relations[$row['name']]['to'][] = $row['to'];
  92. }
  93. $table = "|名称|解释|\n";
  94. $table .= "| -- | -- |\n";
  95. foreach ($relations as $relation => $value) {
  96. $table .= "| [[{$relation}]] | ".$value['note']." |\n";
  97. }
  98. $table .= "\n\n";
  99. echo $title.$table;
  100. // 更新字典
  101. $newWord = $key.'.relations';
  102. $new = DhammaTerm::firstOrNew([
  103. 'channal' => $channelId,
  104. 'word' => $newWord,
  105. ], [
  106. 'id' => app('snowflake')->id(),
  107. 'guid' => Str::uuid(),
  108. 'create_time' => time() * 1000,
  109. ]);
  110. if (empty($caseLocal)) {
  111. $caseLocal = $key;
  112. }
  113. $owner = ChannelApi::getById($channelId);
  114. if (! $owner) {
  115. $this->error('channel id error '.$channelId);
  116. continue;
  117. }
  118. $new->word_en = strtolower($newWord);
  119. $new->meaning = $caseLocal.'用法表';
  120. $new->note = $table;
  121. $new->language = $lang;
  122. $new->editor_id = 1;
  123. $new->owner = $owner['studio_id'];
  124. $new->modify_time = time() * 1000;
  125. $new->save();
  126. }
  127. return 0;
  128. }
  129. }