Files
2026-09-15 10:35:56 +08:00

28 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\CustomerMessage\Model;
use Magento\Framework\App\ResourceConnection;
class MessageProvider
{
public function __construct(private readonly ResourceConnection $resource)
{
}
public function getForCustomer(int $customerId, int $storeId, int $limit = 50): array
{
$connection = $this->resource->getConnection();
return $connection->fetchAll($connection->select()->from(['recipient'=>$this->resource->getTableName('esun_customer_message_recipient')], ['recipient_id','is_read','read_at'])
->joinInner(['message'=>$this->resource->getTableName('esun_customer_message')], 'message.message_id=recipient.message_id', ['message_id','title','content','message_type','created_at','expires_at'])
->where('recipient.customer_id=?', $customerId)->where('message.is_active=?', 1)
->where('message.store_id IN (?)', [0,$storeId])->where('(message.expires_at IS NULL OR message.expires_at > UTC_TIMESTAMP())')
->order('recipient.is_read ASC')->order('message.created_at DESC')->limit(max(1, min(100, $limit))));
}
public function markRead(int $recipientId, int $customerId): bool
{
return (bool)$this->resource->getConnection()->update($this->resource->getTableName('esun_customer_message_recipient'), ['is_read'=>1,'read_at'=>gmdate('Y-m-d H:i:s')], ['recipient_id=?'=>$recipientId,'customer_id=?'=>$customerId]);
}
}