<?php
namespace App\Distributor\Repositories;

use App\Models\DistReadStatus as Model;
use Dcat\Admin\Repositories\EloquentRepository;

class DistReadStatus extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;

    /**
     * 标记消息为已读
     *
     * @param int $messageId 消息ID
     * @param int $userId 用户ID
     * @return bool
     */
    public static function markAsRead($messageId, $userId)
    {
        $readStatus = Model::where('message_id', $messageId)
            ->where('user_id', $userId)
            ->first();

        if (!$readStatus) {
            // 如果记录不存在，创建新记录并标记为已读
            $readStatus = Model::create([
                'message_id' => $messageId,
                'user_id' => $userId,
                'is_read' => true,
            ]);

            return $readStatus->save();
        }

// 如果记录已存在，不做任何操作
        return false;
    }
}
