AppMessage.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Module\Ulogic\Models;
  3. use App\Module\AppMessage\Enums\APP_MESSAGE_STATUS;
  4. use App\Module\AppMessage\Enums\APP_MESSAGE_TYPE;
  5. use App\Module\AppMessage\Enums\APP_SENDER_TYPE;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. /**
  9. * 应用消息模型
  10. * field start
  11. * @property int $id
  12. * @property string $type 消息类型:system=系统消息,user=用户消息
  13. * @property int $template_id 模板ID,为空表示非模板消息
  14. * @property string $template_code 模板代码,冗余字段,方便查询
  15. * @property string $title 消息标题
  16. * @property string $content 消息内容
  17. * @property string $content_type 内容类型:text=纯文本,html=富文本,markdown=MD格式,json=JSON格式
  18. * @property array $data 消息数据
  19. * @property object|array $variables_data 模板变量数据,用于存储模板变量的实际值
  20. * @property int $sender_id 发送者ID
  21. * @property string $sender_type 发送者类型:system=系统,user=用户
  22. * @property int $receiver_id 接收者ID,为空表示群发
  23. * @property int $parent_id 父消息ID,用于消息回复
  24. * @property int $allow_reply 是否允许回复:0不允许 1允许
  25. * @property int $is_read 是否已读:0未读 1已读
  26. * @property string $read_at 阅读时间
  27. * @property int $status 状态:0删除 1正常
  28. * @property \Carbon\Carbon $created_at 创建时间
  29. * @property \Carbon\Carbon $updated_at 更新时间
  30. * field end
  31. *
  32. * @property-read \Illuminate\Database\Eloquent\Collection|AppMessageRecipient[] $recipients 消息接收者列表
  33. */
  34. class AppMessage extends Model
  35. {
  36. /**
  37. * 数据表名称
  38. *
  39. * @var string
  40. */
  41. protected $table = 'app_messages';
  42. // attrlist start
  43. protected $fillable = [
  44. 'id',
  45. 'type',
  46. 'template_id',
  47. 'template_code',
  48. 'title',
  49. 'content',
  50. 'content_type',
  51. 'data',
  52. 'variables_data',
  53. 'sender_id',
  54. 'sender_type',
  55. 'receiver_id',
  56. 'parent_id',
  57. 'allow_reply',
  58. 'is_read',
  59. 'read_at',
  60. 'status',
  61. ];
  62. // attrlist end
  63. /**
  64. * 属性类型转换
  65. *
  66. * @var array
  67. */
  68. protected $casts = [
  69. 'data' => 'array',
  70. 'status' => APP_MESSAGE_STATUS::class,
  71. 'type' => APP_MESSAGE_TYPE::class,
  72. 'sender_type' => APP_SENDER_TYPE::class
  73. ];
  74. /**
  75. * 获取消息接收者列表
  76. *
  77. * 一条消息可以有多个接收者,这里定义了一对多关系
  78. *
  79. * @return HasMany
  80. */
  81. public function recipients(): HasMany
  82. {
  83. return $this->hasMany(AppMessageRecipient::class, 'message_id');
  84. }
  85. /**
  86. * 获取指定用户的未读消息数量
  87. *
  88. * @param int $userId 用户ID
  89. * @return int 未读消息数量
  90. */
  91. public static function getUnreadCount(int $userId): int
  92. {
  93. return static::whereHas('recipients', function ($query) use ($userId) {
  94. $query->where('user_id', $userId)
  95. ->where('is_read', false);
  96. })->count();
  97. }
  98. /**
  99. * 将消息标记为已读
  100. *
  101. * @param int $messageId 消息ID
  102. * @param int $userId 用户ID
  103. * @return bool 是否标记成功
  104. */
  105. public static function markAsRead(int $messageId, int $userId): bool
  106. {
  107. return static::whereHas('recipients', function ($query) use ($messageId, $userId) {
  108. $query->where('message_id', $messageId)
  109. ->where('user_id', $userId);
  110. })->update([
  111. 'is_read' => true,
  112. 'read_at' => now()
  113. ]);
  114. }
  115. }