quanwei
2025-11-15 6f12eadd25c8f5335fd9eccf373bf9835bf3e4c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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, $appId = 10001)
    {
        // 检查是否已存在
        $existing = $this->where('conversation_id', $conversationId)
            ->where('user_id', $userId)
            ->find();
        var_dump($existing);
        if ($existing) {
            return true;
        }
        
        $data = [
            'conversation_id' => $conversationId,
            'user_id' => $userId,
            'app_id' => $appId,
            '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) {
            $participant->last_read_time = time();
            $participant->update_time = time();
            return $participant->save();
        }
        
        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();
    }
}