DhammaTerm.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Contracts\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * 术语 / 百科词条(`dhamma_terms` 表)。主键是 guid(uuid),另有雪花 `id` 列。
  11. *
  12. * 注意列名拼写:channel 存在 `channal` 列。channal 为空的是 studio 级通用术语(owner 名下所有 channel 共用)。
  13. * `word` + `tag`(义项)在一个 channel 内唯一,引用方写成 `[[word@tag]]`;v3 建立后不准改,
  14. * 要改就新建一条、把旧的 `redirect` 到新的(类似维基百科的重定向)。
  15. *
  16. * v3 新增的列(v2 不读):co_editor_uid、quality、summary、content_type、version,
  17. * 分类在 {@see TermCategory}(term_categories 表)。v3 读写见 `App\Services\V3\TermService`。
  18. */
  19. class DhammaTerm extends Model
  20. {
  21. use HasFactory;
  22. use SoftDeletes;
  23. /** 词条质量 */
  24. public const QUALITIES = ['featured', 'standard', 'draft', 'pending'];
  25. /** 正文方言:旧方言(v2 写入的都是它)与 v3 的 markdown-e2 */
  26. public const CONTENT_TYPES = ['markdown', 'markdown-e2'];
  27. public const CONTENT_TYPE_V3 = 'markdown-e2';
  28. protected $primaryKey = 'guid';
  29. protected $keyType = 'string';
  30. protected $casts = [
  31. 'guid' => 'string',
  32. 'version' => 'integer',
  33. 'modify_time' => 'integer',
  34. 'create_time' => 'integer',
  35. ];
  36. public $incrementing = false;
  37. protected $fillable = ['id', 'guid', 'word', 'word_en', 'meaning', 'channal', 'language', 'owner', 'editor_id', 'editor_uid', 'create_time', 'modify_time'];
  38. /** 所属 channel(channal 列);studio 级术语为空 */
  39. public function channel(): BelongsTo
  40. {
  41. return $this->belongsTo(Channel::class, 'channal', 'uid');
  42. }
  43. /**
  44. * 所属 channel 的 uid;studio 级术语给 null。
  45. *
  46. * studio 级术语的 channal 有两种写法:NULL,以及 2022 年以前 v2 写入的空串(库里一千多条),
  47. * 判断「是不是 studio 级」一律走这里,不要直接比 `channal !== null`。
  48. */
  49. public function channelUid(): ?string
  50. {
  51. return $this->channal === null || $this->channal === '' ? null : (string) $this->channal;
  52. }
  53. /**
  54. * 只要 studio 级术语(channal 为 NULL 或空串,见 {@see self::channelUid()})。
  55. *
  56. * @param Builder<DhammaTerm> $query
  57. */
  58. public function scopeStudioLevel(Builder $query): void
  59. {
  60. $query->where(fn (Builder $q) => $q->whereNull('channal')->orWhere('channal', ''));
  61. }
  62. /** 编辑者(人类用户)。editor_uid 对 user_infos.userid */
  63. public function user(): BelongsTo
  64. {
  65. return $this->belongsTo(UserInfo::class, 'editor_uid', 'userid');
  66. }
  67. /** 编辑者(AI 助手)。存量数据里 AI 写的行 editor_uid 直接是模型 */
  68. public function aiModel(): BelongsTo
  69. {
  70. return $this->belongsTo(AiModel::class, 'editor_uid', 'uid');
  71. }
  72. /** 协作者:人类授权代写的 AI 模型。此时 editor 是授权人 */
  73. public function coEditor(): BelongsTo
  74. {
  75. return $this->belongsTo(AiModel::class, 'co_editor_uid', 'uid');
  76. }
  77. /** 百科分类,按正文里出现的顺序 */
  78. public function categories(): HasMany
  79. {
  80. return $this->hasMany(TermCategory::class, 'term_guid', 'guid')->orderBy('position');
  81. }
  82. /** 实际方言:v2 写入的行库里是默认值 markdown */
  83. public function effectiveContentType(): string
  84. {
  85. return $this->content_type ?: 'markdown';
  86. }
  87. /** 乐观锁用的 ETag:v3 修改 +version,v2 修改只动 modify_time,两者都要算进去 */
  88. public function etag(): string
  89. {
  90. return '"'.((int) $this->version).'.'.((int) $this->modify_time).'"';
  91. }
  92. }