DhammaTerm.php 3.1 KB

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