quanwei
2025-11-27 4711bb8fb2fb16c4eb1cdf6c0314069d85e77a67
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
<?php
 
namespace app\common\model\plus\business\chat;
 
use app\common\model\BaseModel;
 
/**
 * 名片聊天会话模型
 */
class Conversation extends BaseModel
{
    protected $pk = 'conversation_id';
    protected $name = 'business_conversation';
 
    /**
     * 关联名片
     */
    public function businessCard()
    {
        return $this->belongsTo("app\\common\\model\\plus\\business\\Business", 'business_card_id', 'business_card_id');
    }
 
    /**
     * 关联参与者
     */
    public function participants()
    {
        return $this->hasMany("app\\common\\model\\plus\\business\\chat\\Participant", 'conversation_id', 'conversation_id');
    }
 
    /**
     * 关联消息
     */
    public function messages()
    {
        return $this->hasMany("app\\common\\model\\plus\\business\\chat\\Chat", 'conversation_id', 'conversation_id');
    }
 
    /**
     * 创建会话
     * @param int $businessCardId 名片ID
     * @param array $participants 参与者数组
     * @param int $appId 应用ID
     * @return int|false
     */
    public function createConversation($businessCardId, $participants = [])
    {
        // 检查是否已存在相同名片的会话
        $existing = $this->where('business_card_id', $businessCardId)->find();
        if ($existing) {
            return $existing->conversation_id;
        }
 
        $data = [
            'business_card_id' => $businessCardId,
        ];
        try {
            $this->save($data);
            $conversationId = $this->conversation_id;
            // 添加参与者
            foreach ($participants as $userId) {
                (new Participant())->addParticipant($conversationId, $userId);
            }
            return $conversationId;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
 
    /**
     * 获取用户相关的会话列表
     * @return array|\think\Paginator
     */
    public function getUserConversations($param = [])
    {
        return $this->with(['businessCard', 'participants.user'])
            ->order('update_time', 'desc')
            ->paginate($param);
    }
 
    /**
     * 根据名片ID获取会话
     * @param int $businessCardId 名片ID
     * @return array|null
     */
    public function getConversationByBusinessCard($businessCardId)
    {
        return $this->with(['businessCard', 'participants.user'])
            ->where('business_card_id', $businessCardId)
            ->find();
    }
 
    /**
     * 更新会话时间
     * @param int $conversationId 会话ID
     * @return bool
     */
    public function updateConversationTime($conversationId)
    {
        $conversation = $this->find($conversationId);
        if ($conversation) {
            $conversation->update_time = time();
            return $conversation->save();
        }
        return false;
    }
}