SentenceService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Sentence;
  4. use App\Models\SentHistory;
  5. use App\Models\Channel;
  6. use Illuminate\Support\Str;
  7. class SentenceService
  8. {
  9. public function save($data)
  10. {
  11. $row = Sentence::firstOrNew([
  12. "book_id" => $data['book_id'],
  13. "paragraph" => $data['paragraph'],
  14. "word_start" => $data['word_start'],
  15. "word_end" => $data['word_end'],
  16. "channel_uid" => $data['channel_uid'],
  17. ], [
  18. "id" => app('snowflake')->id(),
  19. "uid" => Str::uuid(),
  20. ]);
  21. $row->content = $data['content'];
  22. if (isset($data['content_type']) && !empty($data['content_type'])) {
  23. $row->content_type = $data['content_type'];
  24. }
  25. $row->strlen = mb_strlen($data['content'], "UTF-8");
  26. $lang = Channel::where('uid', $data['channel_uid'])->value('lang');
  27. $row->language = $lang;
  28. $row->status = $data['status'] ?? 10;
  29. if (isset($data['copy'])) {
  30. //复制句子,保留原作者信息
  31. $row->editor_uid = $data["editor_uid"];
  32. $row->acceptor_uid = $data["acceptor_uid"];
  33. $row->pr_edit_at = $data["updated_at"];
  34. if (isset($data['fork_from'])) {
  35. $row->fork_at = now();
  36. }
  37. } else {
  38. $row->editor_uid = $data["editor_uid"];
  39. $row->acceptor_uid = null;
  40. $row->pr_edit_at = null;
  41. }
  42. $row->create_time = time() * 1000;
  43. $row->modify_time = time() * 1000;
  44. $row->save();
  45. }
  46. }