TermService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Services;
  3. use App\Models\DhammaTerm;
  4. use App\Http\Api\ChannelApi;
  5. use App\Http\Resources\TermResource;
  6. use Illuminate\Http\Request;
  7. class TermService
  8. {
  9. public function attachLocalName(array $categoryData, string $lang): array
  10. {
  11. $allNames = [];
  12. // 收集所有 name
  13. foreach ($categoryData as $item) {
  14. if (!empty($item['category']['name'])) {
  15. $allNames[] = $item['category']['name'];
  16. }
  17. foreach ($item['children'] as $child) {
  18. if (!empty($child['name'])) {
  19. $allNames[] = $child['name'];
  20. }
  21. }
  22. }
  23. // 去重
  24. $allNames = array_values(array_unique($allNames));
  25. // 查词典
  26. $terms = $this->glossaryByLemma($allNames, $lang);
  27. // 构建映射
  28. $termMap = [];
  29. if ($terms) {
  30. foreach ($terms as $term) {
  31. $termMap[$term->word] = $term->meaning;
  32. }
  33. }
  34. // 回填
  35. foreach ($categoryData as &$item) {
  36. $name = $item['category']['name'] ?? null;
  37. $item['category']['local_name'] = $termMap[$name] ?? $name;
  38. foreach ($item['children'] as &$child) {
  39. $childName = $child['name'] ?? null;
  40. $child['local_name'] = $termMap[$childName] ?? $childName;
  41. }
  42. }
  43. unset($item, $child);
  44. return $categoryData;
  45. }
  46. public function glossaryByLemma(array $words, string $lang)
  47. {
  48. $localTermChannel = ChannelApi::getSysChannel(
  49. "_community_term_" . strtolower($lang) . "_"
  50. );
  51. if (!$localTermChannel) {
  52. return null;
  53. }
  54. $result = DhammaTerm::select(['guid', 'word', 'tag', 'meaning', 'other_meaning'])
  55. ->whereIn('word', $words)
  56. ->where('channal', $localTermChannel)
  57. ->get();
  58. return $result;
  59. }
  60. public function getCommunityGlossary($lang)
  61. {
  62. $localTermChannel = ChannelApi::getSysChannel(
  63. "_community_term_" . strtolower($lang) . "_",
  64. "_community_term_en_"
  65. );
  66. $result = DhammaTerm::select(['guid', 'word', 'tag', 'meaning', 'other_meaning'])
  67. ->where('channal', $localTermChannel)
  68. ->get();
  69. return ['items' => $result, 'total' => count($result)];
  70. }
  71. public function getGrammarGlossary($lang)
  72. {
  73. $localTermChannel = ChannelApi::getSysChannel(
  74. "_System_Grammar_Term_" . strtolower($lang) . "_",
  75. "_System_Grammar_Term_en_"
  76. );
  77. $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
  78. ->where('channal', $localTermChannel)
  79. ->get();
  80. return ['items' => $result, 'total' => count($result)];
  81. }
  82. public function getRaw(string $id)
  83. {
  84. $result = DhammaTerm::find($id);
  85. return $result;
  86. }
  87. public function isCommunity(?string $channelId)
  88. {
  89. $channel = ChannelApi::getById($channelId);
  90. if (!$channel) {
  91. return false;
  92. }
  93. if (strpos($channel['name'], '_community_term_') === false) {
  94. return false;
  95. } else {
  96. return true;
  97. }
  98. }
  99. public function communityTerm(string $word, string $lang, string $format)
  100. {
  101. $localTermChannel = ChannelApi::getSysChannel(
  102. "_community_term_" . strtolower($lang) . "_"
  103. );
  104. $result = DhammaTerm::where('word', $word)
  105. ->where('channal', $localTermChannel)
  106. ->first();
  107. if ($result) {
  108. $resource = new TermResource($result);
  109. $urlParam = ['format' => $format];
  110. $fakeRequest = Request::create('', 'GET', $urlParam);
  111. $termArray = $resource->toArray($fakeRequest);
  112. if ($result) {
  113. return $termArray;
  114. } else {
  115. return null;
  116. }
  117. } else {
  118. return null;
  119. }
  120. }
  121. public function communityWiki(string $word, string $lang, string $format)
  122. {
  123. $localTermChannel = ChannelApi::getSysChannel(
  124. "_community_translation_" . strtolower($lang) . "_"
  125. );
  126. $result = DhammaTerm::where('word', $word)
  127. ->where('channal', $localTermChannel)
  128. ->first();
  129. if ($result) {
  130. $resource = new TermResource($result);
  131. $urlParam = ['format' => $format];
  132. $fakeRequest = Request::create('', 'GET', $urlParam);
  133. $termArray = $resource->toArray($fakeRequest);
  134. if ($result) {
  135. return $termArray;
  136. } else {
  137. return null;
  138. }
  139. } else {
  140. return null;
  141. }
  142. }
  143. public function communityTerms(string $lang)
  144. {
  145. $localTermChannel = ChannelApi::getSysChannel(
  146. "_community_term_" . strtolower($lang) . "_"
  147. );
  148. $result = DhammaTerm::where('channal', $localTermChannel)
  149. ->whereNotNull('note')
  150. ->where('note', '<>', '')
  151. ->take(10)
  152. ->orderBy('updated_at', 'desc')
  153. ->get();
  154. return [
  155. "data" => TermResource::collection($result),
  156. "count" => 10
  157. ];
  158. }
  159. public function find(string $id, string $format): ?array
  160. {
  161. $result = DhammaTerm::find($id);
  162. $resource = new TermResource($result);
  163. $urlParam = ['format' => $format];
  164. $fakeRequest = Request::create('', 'GET', $urlParam);
  165. $termArray = $resource->toArray($fakeRequest);
  166. if ($result) {
  167. return $termArray;
  168. } else {
  169. return null;
  170. }
  171. }
  172. public function update(string $id, array $data)
  173. {
  174. DhammaTerm::where('guid', $id)->update($data);
  175. }
  176. }