TermService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. use App\Tools\Tools;
  8. class TermService
  9. {
  10. public function attachLocalName(array $categoryData, string $lang): array
  11. {
  12. $allNames = [];
  13. // 收集所有 name
  14. foreach ($categoryData as $item) {
  15. if (!empty($item['category']['name'])) {
  16. $allNames[] = $item['category']['name'];
  17. }
  18. foreach ($item['children'] as $child) {
  19. if (!empty($child['name'])) {
  20. $allNames[] = $child['name'];
  21. }
  22. }
  23. }
  24. // 去重
  25. $allNames = array_values(array_unique($allNames));
  26. // 查词典
  27. $terms = $this->glossaryByLemma($allNames, $lang);
  28. // 构建映射
  29. $termMap = [];
  30. if ($terms) {
  31. foreach ($terms as $term) {
  32. $termMap[$term->word] = $term->meaning;
  33. }
  34. }
  35. // 回填
  36. foreach ($categoryData as &$item) {
  37. $name = $item['category']['name'] ?? null;
  38. $item['category']['local_name'] = $termMap[$name] ?? $name;
  39. foreach ($item['children'] as &$child) {
  40. $childName = $child['name'] ?? null;
  41. $child['local_name'] = $termMap[$childName] ?? $childName;
  42. }
  43. }
  44. unset($item, $child);
  45. return $categoryData;
  46. }
  47. public function glossaryByLemma(array $words, string $lang)
  48. {
  49. $localTermChannel = ChannelApi::getSysChannel(
  50. "_community_term_" . strtolower($lang) . "_"
  51. );
  52. if (!$localTermChannel) {
  53. return null;
  54. }
  55. $result = DhammaTerm::select(['guid', 'word', 'tag', 'meaning', 'other_meaning'])
  56. ->whereIn('word', $words)
  57. ->where('channal', $localTermChannel)
  58. ->get();
  59. return $result;
  60. }
  61. public function getCommunityGlossary($lang)
  62. {
  63. $localTermChannel = ChannelApi::getSysChannel(
  64. "_community_term_" . strtolower($lang) . "_",
  65. "_community_term_en_"
  66. );
  67. $result = DhammaTerm::select(['guid', 'word', 'tag', 'meaning', 'other_meaning'])
  68. ->where('channal', $localTermChannel)
  69. ->get();
  70. return ['items' => $result, 'total' => count($result)];
  71. }
  72. public function getGrammarGlossary($lang)
  73. {
  74. $localTermChannel = ChannelApi::getSysChannel(
  75. "_System_Grammar_Term_" . strtolower($lang) . "_",
  76. "_System_Grammar_Term_en_"
  77. );
  78. $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
  79. ->where('channal', $localTermChannel)
  80. ->get();
  81. return ['items' => $result, 'total' => count($result)];
  82. }
  83. public function getRaw(string $id)
  84. {
  85. $result = DhammaTerm::find($id);
  86. return $result;
  87. }
  88. public function isCommunity(?string $channelId)
  89. {
  90. $channel = ChannelApi::getById($channelId);
  91. if (!$channel) {
  92. return false;
  93. }
  94. if (strpos($channel['name'], '_community_term_') === false) {
  95. return false;
  96. } else {
  97. return true;
  98. }
  99. }
  100. public function communityTerm(string $word, string $lang, string $format)
  101. {
  102. $localTermChannel = ChannelApi::getSysChannel(
  103. "_community_term_" . strtolower($lang) . "_"
  104. );
  105. $result = DhammaTerm::where('word', $word)
  106. ->where('channal', $localTermChannel)
  107. ->first();
  108. if ($result) {
  109. $resource = new TermResource($result);
  110. $urlParam = ['format' => $format];
  111. $fakeRequest = Request::create('', 'GET', $urlParam);
  112. $termArray = $resource->toArray($fakeRequest);
  113. if ($result) {
  114. return $termArray;
  115. } else {
  116. return null;
  117. }
  118. } else {
  119. return null;
  120. }
  121. }
  122. public function communityWiki(string $word, string $lang, string $format)
  123. {
  124. $localTermChannel = ChannelApi::getSysChannel(
  125. "_community_translation_" . strtolower($lang) . "_"
  126. );
  127. $result = DhammaTerm::where('word', $word)
  128. ->where('channal', $localTermChannel)
  129. ->first();
  130. if ($result) {
  131. $resource = new TermResource($result);
  132. $urlParam = ['format' => $format];
  133. $fakeRequest = Request::create('', 'GET', $urlParam);
  134. $termArray = $resource->toArray($fakeRequest);
  135. if ($result) {
  136. return $termArray;
  137. } else {
  138. return null;
  139. }
  140. } else {
  141. return null;
  142. }
  143. }
  144. public function communityTerms(string $lang)
  145. {
  146. $localTermChannel = ChannelApi::getSysChannel(
  147. "_community_term_" . strtolower($lang) . "_"
  148. );
  149. $result = DhammaTerm::where('channal', $localTermChannel)
  150. ->whereNotNull('note')
  151. ->where('note', '<>', '')
  152. ->take(10)
  153. ->orderBy('updated_at', 'desc')
  154. ->get();
  155. return [
  156. "data" => TermResource::collection($result),
  157. "count" => 10
  158. ];
  159. }
  160. public function find(string $id, string $format): ?array
  161. {
  162. $result = DhammaTerm::find($id);
  163. $resource = new TermResource($result);
  164. $urlParam = ['format' => $format];
  165. $fakeRequest = Request::create('', 'GET', $urlParam);
  166. $termArray = $resource->toArray($fakeRequest);
  167. if ($result) {
  168. return $termArray;
  169. } else {
  170. return null;
  171. }
  172. }
  173. public function update(string $id, array $data)
  174. {
  175. DhammaTerm::where('guid', $id)->update($data);
  176. }
  177. /**
  178. * @param array{
  179. * word: string,
  180. * tag: string,
  181. * channal: string,
  182. * meaning: string,
  183. * other_meaning: string|null,
  184. * note: string|null,
  185. * editor_id: int,
  186. * } $data
  187. * @return string 返回记录的 id
  188. */
  189. public function updateOrCreateByWord(array $data): string
  190. {
  191. $now = time();
  192. $channelInfo = ChannelApi::getById($data['channel_id']);
  193. // 先查询是否存在
  194. $term = DhammaTerm::where('word', $data['word'])
  195. ->where('tag', $data['tag'] ?? null)
  196. ->where('channal', $data['channel_id'])
  197. ->first();
  198. if ($term) {
  199. // 已存在,直接更新
  200. $term->update([
  201. 'meaning' => $data['meaning'],
  202. 'other_meaning' => $data['other_meaning'] ?? null,
  203. 'note' => $data['note'] ?? null,
  204. 'redirect' => $data['redirect'] ?? null,
  205. 'editor_id' => $data['editor_id'],
  206. 'modify_time' => $now,
  207. ]);
  208. } else {
  209. // 不存在,新建(一次性写入所有字段)
  210. $term = new DhammaTerm();
  211. $term->id = app('snowflake')->id();
  212. $term->guid = (string) \Illuminate\Support\Str::uuid();
  213. $term->word = $data['word'];
  214. $term->tag = $data['tag'] ?? null;
  215. $term->channal = $data['channel_id'];
  216. $term->meaning = $data['meaning'];
  217. $term->other_meaning = $data['other_meaning'] ?? null;
  218. $term->note = $data['note'] ?? null;
  219. $term->redirect = $data['redirect'] ?? null;
  220. $term->editor_id = $data['editor_id']; // 注意:需传入 int 类型的 editor id
  221. $term->owner = $channelInfo['studio_id'];
  222. $term->word_en = Tools::getWordEn($data['word']);
  223. $term->language = $channelInfo['lang'] ?? 'zh-Hans';
  224. $term->create_time = $now;
  225. $term->modify_time = $now;
  226. $term->save();
  227. }
  228. return $term->guid;
  229. }
  230. }