UpgradeGrammarBook.php 4.4 KB

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