2
0

Sentence.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Models;
  3. use App\Services\PaliContentService;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Illuminate\Support\Facades\DB;
  8. class Sentence extends Model
  9. {
  10. use HasFactory;
  11. use SoftDeletes;
  12. protected $fillable = [
  13. 'id',
  14. 'uid',
  15. 'book_id',
  16. 'paragraph',
  17. 'word_start',
  18. 'word_end',
  19. 'channel_uid',
  20. 'editor_uid',
  21. 'content',
  22. 'content_type',
  23. 'strlen',
  24. 'status',
  25. 'create_time',
  26. 'modify_time',
  27. 'language',
  28. ];
  29. protected $primaryKey = 'uid';
  30. protected $casts = [
  31. 'uid' => 'string',
  32. 'channel_uid' => 'string',
  33. ];
  34. /**
  35. * 增改删句子时清阅读缓存:段落成品(L1)删掉;「有译文的段」可能变了才删内容索引(L2)。
  36. * 见 {@see PaliContentService::paragraphChanged()}。改了 book / paragraph / channel 的,新旧两处都要清。
  37. * 提交之后才清,免得并发的读在提交前把旧数据写回缓存。
  38. */
  39. protected static function booted(): void
  40. {
  41. $created = function (Sentence $sentence) {
  42. $now = [(int) $sentence->book_id, (int) $sentence->paragraph, (string) $sentence->channel_uid];
  43. $hasText = (int) $sentence->strlen > 0;
  44. DB::afterCommit(fn () => PaliContentService::paragraphChanged(...$now, indexChanged: true, mayAddBook: $hasText));
  45. };
  46. // updated 事件里 getOriginal() 还是改之前的值(含软删恢复:deleted_at 从有到无)
  47. $updated = function (Sentence $sentence) {
  48. $now = [(int) $sentence->book_id, (int) $sentence->paragraph, (string) $sentence->channel_uid];
  49. $before = [(int) $sentence->getOriginal('book_id'), (int) $sentence->getOriginal('paragraph'), (string) $sentence->getOriginal('channel_uid')];
  50. $hasText = (int) $sentence->strlen > 0;
  51. $hadText = (int) $sentence->getOriginal('strlen') > 0;
  52. $restored = $sentence->getOriginal('deleted_at') !== null && $sentence->deleted_at === null;
  53. DB::afterCommit(function () use ($now, $before, $hasText, $hadText, $restored) {
  54. $moved = $before !== $now;
  55. PaliContentService::paragraphChanged(
  56. ...$now,
  57. indexChanged: $restored || $moved || $hasText !== $hadText,
  58. mayAddBook: $hasText,
  59. mayRemoveBook: ! $moved && $hadText && ! $hasText,
  60. );
  61. if ($moved) {
  62. PaliContentService::paragraphChanged(...$before, indexChanged: true, mayRemoveBook: $hadText);
  63. }
  64. });
  65. };
  66. $removed = function (Sentence $sentence) {
  67. $now = [(int) $sentence->book_id, (int) $sentence->paragraph, (string) $sentence->channel_uid];
  68. DB::afterCommit(fn () => PaliContentService::paragraphChanged(...$now, indexChanged: true, mayRemoveBook: true));
  69. };
  70. static::created($created);
  71. static::updated($updated);
  72. static::deleted($removed);
  73. static::forceDeleted($removed);
  74. }
  75. protected $dates = [
  76. 'created_at',
  77. 'updated_at',
  78. 'deleted_at',
  79. 'fork_at',
  80. ];
  81. /**
  82. * 获取句子所属的频道
  83. */
  84. public function channel()
  85. {
  86. return $this->belongsTo(Channel::class, 'channel_uid', 'uid');
  87. }
  88. public function scopeType($query, $type)
  89. {
  90. return $query->whereHas('channel', function ($q) use ($type) {
  91. $q->where('type', $type);
  92. });
  93. }
  94. public function scopeNissaya($query)
  95. {
  96. return $query->whereHas('channel', function ($q) {
  97. $q->where('type', 'nissaya');
  98. });
  99. }
  100. public function scopeLanguage($query, $language)
  101. {
  102. return $query->whereHas('channel', function ($q) use ($language) {
  103. $q->where('lang', $language);
  104. });
  105. }
  106. }