UpgradeGrammarBook.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Http;
  5. use App\Http\Api\ChannelApi;
  6. use App\Models\DhammaTerm;
  7. use Illuminate\Support\Str;
  8. use App\Tools\Tools;
  9. class UpgradeGrammarBook extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan upgrade:grammar.book
  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. $cases = ['nom','acc','gen','dat','abl','inst','loc'];
  46. foreach ($cases as $key => $case) {
  47. $this->info('case='.$case);
  48. $title = "**[[{$case}]]** 用法表\n\n";
  49. $caseLocal = DhammaTerm::where('channal',$channelId)
  50. ->where('word',$case)
  51. ->value('meaning');
  52. $url = config('app.url')."/api/v2/relation?ui-lang={$lang}&from={$case}";
  53. $response = Http::get($url);
  54. if($response->successful()){
  55. $this->info("get data ok");
  56. $data = $response['data']['rows'];
  57. $relations = [];
  58. foreach ($data as $key => $row) {
  59. if(!isset($relations[$row['name']])){
  60. $relations[$row['name']] = array();
  61. $local = DhammaTerm::where('channal',$channelId)
  62. ->where('word',$row['name'])
  63. ->first();
  64. if($local){
  65. $relations[$row['name']]['meaning']=$local->meaning;
  66. $relations[$row['name']]['note']=$local->note;
  67. }else{
  68. $relations[$row['name']]['meaning']='';
  69. $relations[$row['name']]['note']='';
  70. }
  71. $relations[$row['name']]['to']=array();
  72. }
  73. $relations[$row['name']]['to'][] = $row['to'];
  74. }
  75. $table = "|名称|解释|\n";
  76. $table .= "| -- | -- |\n";
  77. foreach ($relations as $relation => $value) {
  78. $table .= "| [[{$relation}]] | ". $value['note']." |\n";
  79. }
  80. $table .= "\n\n";
  81. echo $title.$table;
  82. //更新字典
  83. $newWord = $case.'.relations';
  84. $new = DhammaTerm::firstOrNew([
  85. 'channal' => $channelId,
  86. 'word' => $newWord,
  87. ],[
  88. 'id'=>app('snowflake')->id(),
  89. 'guid'=>Str::uuid(),
  90. 'create_time'=>time()*1000,
  91. ]);
  92. if(empty($caseLocal)){
  93. $caseLocal = $case;
  94. }
  95. $owner = ChannelApi::getById($channelId);
  96. if(!$owner){
  97. $this->error('channel id error '.$channelId);
  98. continue;
  99. }
  100. $new->word_en = strtolower($newWord);
  101. $new->meaning =$caseLocal.'用法表';
  102. $new->note = $table;
  103. $new->language = $lang;
  104. $new->editor_id = 1;
  105. $new->owner = $owner['studio_id'];
  106. $new->modify_time = time()*1000;
  107. $new->save();
  108. }
  109. }
  110. return 0;
  111. }
  112. }