SentenceService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Channel;
  4. use App\Models\Sentence;
  5. use App\Models\SentHistory;
  6. use Illuminate\Support\Facades\Http;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Str;
  9. class SentenceService
  10. {
  11. protected $timeOut = 30;
  12. public function save(array $data): Sentence
  13. {
  14. $row = Sentence::firstOrNew([
  15. 'book_id' => $data['book_id'],
  16. 'paragraph' => $data['paragraph'],
  17. 'word_start' => $data['word_start'],
  18. 'word_end' => $data['word_end'],
  19. 'channel_uid' => $data['channel_uid'],
  20. ], [
  21. 'id' => app('snowflake')->id(),
  22. 'uid' => Str::uuid(),
  23. ]);
  24. $row->content = $data['content'];
  25. if (isset($data['content_type']) && ! empty($data['content_type'])) {
  26. $row->content_type = $data['content_type'];
  27. }
  28. $row->strlen = mb_strlen($data['content'], 'UTF-8');
  29. $lang = Channel::where('uid', $data['channel_uid'])->value('lang');
  30. $row->language = $lang;
  31. $row->status = $data['status'] ?? 10;
  32. if (isset($data['copy'])) {
  33. // 复制句子,保留原作者信息
  34. $row->editor_uid = $data['editor_uid'];
  35. $row->acceptor_uid = $data['acceptor_uid'];
  36. $row->pr_edit_at = $data['updated_at'];
  37. if (isset($data['fork_from'])) {
  38. $row->fork_at = now();
  39. }
  40. } else {
  41. $row->editor_uid = $data['editor_uid'];
  42. $row->acceptor_uid = null;
  43. $row->pr_edit_at = null;
  44. }
  45. $row->create_time = time() * 1000;
  46. $row->modify_time = time() * 1000;
  47. $row->save();
  48. return $row;
  49. }
  50. /**
  51. * 保存句子并记录一条历史。
  52. * 在 save() 基础上,用持久化后的句子 uid、编辑者、内容写入 SentHistory;
  53. * acceptor_uid / fork_from / pr_from 可选(用于复制、fork、PR 接受等场景)。
  54. */
  55. public function saveWithHistory(array $data): Sentence
  56. {
  57. $row = $this->save($data);
  58. $this->saveHistory(
  59. $row->uid,
  60. $row->editor_uid,
  61. $row->content,
  62. $data['acceptor_uid'] ?? null,
  63. $data['fork_from'] ?? null,
  64. $data['pr_from'] ?? null,
  65. );
  66. return $row;
  67. }
  68. /**
  69. * 写入一条句子编辑历史。
  70. *
  71. * @param string $uid 句子 uid
  72. * @param string $editor 编辑者 user_uid
  73. * @param string $content 本次内容
  74. * @param string|null $user_uid 接受者 user_uid(fork / pr 场景填写)
  75. * @param string|null $fork_from fork 来源
  76. * @param string|null $pr_from pr 来源
  77. */
  78. public function saveHistory($uid, $editor, $content, $user_uid = null, $fork_from = null, $pr_from = null): void
  79. {
  80. $newHis = new SentHistory;
  81. $newHis->id = app('snowflake')->id();
  82. $newHis->sent_uid = $uid;
  83. $newHis->user_uid = $editor;
  84. if (empty($content)) {
  85. $newHis->content = '';
  86. } else {
  87. $newHis->content = $content;
  88. }
  89. if ($fork_from) {
  90. $newHis->fork_from = $fork_from;
  91. $newHis->accepter_uid = $user_uid;
  92. }
  93. if ($pr_from) {
  94. $newHis->pr_from = $pr_from;
  95. $newHis->accepter_uid = $user_uid;
  96. }
  97. $newHis->create_time = time() * 1000;
  98. $newHis->save();
  99. }
  100. /**
  101. * 'sentence' => [
  102. 'book_id' => $sentence['id'][0],
  103. 'paragraph' => $sentence['id'][1],
  104. 'word_start' => $sentence['id'][2],
  105. 'word_end' => $sentence['id'][3],
  106. 'channel_uid' => $channelId,
  107. 'content' => $prompt,
  108. 'content_type' => 'markdown',
  109. 'access_token' => $sentChannelInfo[1] ?? $params['token'],
  110. ],
  111. */
  112. public function saveRpc(string $endpoint, array $sentence, string $editorToken)
  113. {
  114. $url = $endpoint;
  115. Log::info(" sentence update {$url}");
  116. $response = Http::timeout($this->timeOut)
  117. ->withToken($editorToken)
  118. ->post($url, [
  119. 'sentences' => [$sentence],
  120. ]);
  121. if ($response->failed()) {
  122. Log::error(' sentence update failed', [
  123. 'url' => $url,
  124. 'data' => $response->json(),
  125. ]);
  126. throw new DatabaseException('sentence 数据库写入错误');
  127. }
  128. $count = $response->json()['data']['count'];
  129. return $count;
  130. }
  131. }