<?php
|
|
namespace app\common\model\plus\business\chat;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 名片聊天参与者模型
|
*/
|
class Participant extends BaseModel
|
{
|
protected $pk = 'participant_id';
|
protected $name = 'business_participant';
|
|
/**
|
* 关联用户
|
*/
|
public function user()
|
{
|
return $this->belongsTo("app\\common\\model\\user\\User", 'user_id', 'user_id');
|
}
|
|
/**
|
* 关联会话
|
*/
|
public function conversation()
|
{
|
return $this->belongsTo("app\\common\\model\\plus\\business\\chat\\Conversation", 'conversation_id', 'conversation_id');
|
}
|
|
/**
|
* 添加参与者
|
* @param int $conversationId 会话ID
|
* @param int $userId 用户ID
|
* @param int $appId 应用ID
|
* @return bool
|
*/
|
public function addParticipant($conversationId, $userId)
|
{
|
// 检查是否已存在
|
$existing = $this->where('conversation_id', $conversationId)
|
->where('user_id', $userId)
|
->find();
|
if ($existing) {
|
return true;
|
}
|
|
$data = [
|
'conversation_id' => $conversationId,
|
'user_id' => $userId,
|
'app_id' => self::$app_id,
|
'is_read' => 0,
|
'join_time' => time(),
|
'last_read_time' => time(),
|
'create_time' => time(),
|
'update_time' => time()
|
];
|
return $this->save($data);
|
}
|
|
/**
|
* 更新参与者最后阅读时间
|
* @param int $conversationId 会话ID
|
* @param int $userId 用户ID
|
* @return bool
|
*/
|
public function updateLastReadTime($conversationId, $userId)
|
{
|
$participant = $this->where('conversation_id', $conversationId)
|
->where('user_id', $userId)
|
->find();
|
|
if ($participant) {
|
$data=['last_read_time'=>time()];
|
return $participant->save($data);
|
}
|
|
return false;
|
}
|
|
/**
|
* 标记会话为已读
|
* @param int $conversationId 会话ID
|
* @param int $userId 用户ID
|
* @return bool
|
*/
|
public function markConversationAsRead($conversationId, $userId)
|
{
|
$participant = $this->where('conversation_id', $conversationId)
|
->where('user_id', $userId)
|
->find();
|
|
if ($participant) {
|
$participant->is_read = 1;
|
$participant->last_read_time = time();
|
$participant->update_time = time();
|
return $participant->save();
|
}
|
return true;
|
}
|
|
/**
|
* 检查用户是否在会话中
|
* @param int $conversationId 会话ID
|
* @param int $userId 用户ID
|
* @return bool
|
*/
|
public function isParticipant($conversationId, $userId)
|
{
|
return $this->where('conversation_id', $conversationId)
|
->where('user_id', $userId)
|
->count() > 0;
|
}
|
|
/**
|
* 获取会话的参与者列表
|
* @param int $conversationId 会话ID
|
* @return array|\think\Collection
|
*/
|
public function getParticipants($conversationId)
|
{
|
return $this->with(['user'])
|
->where('conversation_id', $conversationId)
|
->select();
|
}
|
}
|