| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Module\Ulogic\Models;
- use App\Module\AppMessage\Enums\APP_MESSAGE_STATUS;
- use App\Module\AppMessage\Enums\APP_MESSAGE_TYPE;
- use App\Module\AppMessage\Enums\APP_SENDER_TYPE;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- /**
- * 应用消息模型
- * field start
- * @property int $id
- * @property string $type 消息类型:system=系统消息,user=用户消息
- * @property int $template_id 模板ID,为空表示非模板消息
- * @property string $template_code 模板代码,冗余字段,方便查询
- * @property string $title 消息标题
- * @property string $content 消息内容
- * @property string $content_type 内容类型:text=纯文本,html=富文本,markdown=MD格式,json=JSON格式
- * @property array $data 消息数据
- * @property object|array $variables_data 模板变量数据,用于存储模板变量的实际值
- * @property int $sender_id 发送者ID
- * @property string $sender_type 发送者类型:system=系统,user=用户
- * @property int $receiver_id 接收者ID,为空表示群发
- * @property int $parent_id 父消息ID,用于消息回复
- * @property int $allow_reply 是否允许回复:0不允许 1允许
- * @property int $is_read 是否已读:0未读 1已读
- * @property string $read_at 阅读时间
- * @property int $status 状态:0删除 1正常
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- *
- * @property-read \Illuminate\Database\Eloquent\Collection|AppMessageRecipient[] $recipients 消息接收者列表
- */
- class AppMessage extends Model
- {
- /**
- * 数据表名称
- *
- * @var string
- */
- protected $table = 'app_messages';
- // attrlist start
- protected $fillable = [
- 'id',
- 'type',
- 'template_id',
- 'template_code',
- 'title',
- 'content',
- 'content_type',
- 'data',
- 'variables_data',
- 'sender_id',
- 'sender_type',
- 'receiver_id',
- 'parent_id',
- 'allow_reply',
- 'is_read',
- 'read_at',
- 'status',
- ];
- // attrlist end
-
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'data' => 'array',
- 'status' => APP_MESSAGE_STATUS::class,
- 'type' => APP_MESSAGE_TYPE::class,
- 'sender_type' => APP_SENDER_TYPE::class
- ];
- /**
- * 获取消息接收者列表
- *
- * 一条消息可以有多个接收者,这里定义了一对多关系
- *
- * @return HasMany
- */
- public function recipients(): HasMany
- {
- return $this->hasMany(AppMessageRecipient::class, 'message_id');
- }
- /**
- * 获取指定用户的未读消息数量
- *
- * @param int $userId 用户ID
- * @return int 未读消息数量
- */
- public static function getUnreadCount(int $userId): int
- {
- return static::whereHas('recipients', function ($query) use ($userId) {
- $query->where('user_id', $userId)
- ->where('is_read', false);
- })->count();
- }
- /**
- * 将消息标记为已读
- *
- * @param int $messageId 消息ID
- * @param int $userId 用户ID
- * @return bool 是否标记成功
- */
- public static function markAsRead(int $messageId, int $userId): bool
- {
- return static::whereHas('recipients', function ($query) use ($messageId, $userId) {
- $query->where('message_id', $messageId)
- ->where('user_id', $userId);
- })->update([
- 'is_read' => true,
- 'read_at' => now()
- ]);
- }
- }
|