版本: v1.0\ 最後更新: 2026-06-13\ 服務核心: aile-service-room(策略工廠模式)
Aile 系統採用策略工廠模式(Strategy Factory Pattern)管理 12 種聊天室類型。每種類型由一個獨立的 RoomStrategy 實現類處理,透過 @Component(RoomType.XXX) 註冊到 RoomFactory.strategyMap,執行時依 RoomModel.type 動態分發。
RoomFactory.strategyMap (Map<type, RoomStrategy>)
│
├── person → PersonRoomImpl
├── accountPerson → AccountPersonRoomImpl
├── friend → FriendRoomImpl
├── group → GroupRoomImpl
├── discuss → DiscussRoomImpl
├── services → ServiceRoomImpl
├── serviceMember → ServiceMemberRoomImpl
├── business → BusinessRoomImpl
├── aileSystem → AileSystemRoomImpl
├── aiwowSystem → AiwowSystemRoomImpl
├── system → SystemRoomImpl
└── lineGroup → LineGroupRoomImplMongoDB 集合:aile.room
@Document("aile.room")
public class RoomModel extends BaseModel {
String name; // 聊天室名稱
String type; // 聊天室類型(12 種之一)
String tenantId; // 租戶 ID
String accountId; // 帳號 ID(創建者)
String ownerId; // 擁有者 ID
Boolean isCustomName; // 是否自定義名稱
String avatarId; // 頭像 ID
String serviceNumberId; // 服務號 ID(服務號相關聊天室使用)
Boolean isExternal; // 服務號是否對外
Long lastSequence; // 最後消息序號
RoomStatus status; // 聊天室狀態
MessageVO lastMessage; // 最後一條消息快照
String homePagePicId; // 首頁背景圖 ID
// 物件(Business)相關
String businessId; // 業務物件 ID
String businessName; // 物件名稱
BusinessStatus businessStatus; // 物件狀態
String businessDescription; // 物件描述
Long businessEndTime; // 物件結束時間
Long businessCompleteTime; // 物件完成時間
String mainRoomId; // 主聊天室 ID(Business 子房用)
String mergeTargetRoomId; // 合併目標聊天室 ID(Saga 合併後使用)
String lineGroupId; // LINE 群組 ID(LineGroup 專用)
}| 狀態 | 說明 |
|---|---|
Enable | 正常啟用 |
Disable | 被禁用(附禁用時長) |
Delete | 已刪除(軟刪除) |
Danger | 不安全,有風險 |
Complaint | 被檢舉 |
Forbid | 被禁言 |
Block | 被封鎖 |
| 狀態 | 說明 |
|---|---|
Pending | 尚未開始 |
Progress | 進行中 |
Revision | 返回修改 |
Complete | 已完成 |
person — 個人聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | PersonRoomImpl |
| 成員範圍 | 僅創建者本人 |
| 用途 | 個人筆記、草稿、待辦提醒、系統通知收納 |
| isMulti | ❌ |
| isAccountRoom | ❌ |
設計意圖: 每個 Account 有一個 person 聊天室(AccountModel.personRoomId 指向),用於系統向該帳號推送個人化消息。
消息發送規則: 僅向房間內的所有成員(即本人)發送。
accountPerson — 帳號個人聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | AccountPersonRoomImpl |
| 成員範圍 | 僅帳號本人 |
| 用途 | 跨租戶的帳號級系統通知(如生態邀請、系統公告) |
| isMulti | ❌ |
| isAccountRoom | ✅ |
設計意圖: 比 person 更上層的帳號級聊天室,不依附於任何租戶。用於 Aile 生態級系統消息。
friend — 好友聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | FriendRoomImpl |
| 成員範圍 | 2 人 |
| 用途 | 員工之間的一對一私聊 |
| isMulti | ❌ |
| isAccountRoom | ❌ |
設計意圖: 租戶內部兩位員工之間的私聊頻道。固定雙人,不可擴充成員。
group — 群組聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | GroupRoomImpl |
| 成員範圍 | 多人(≥3) |
| 用途 | 員工群聊 |
| isMulti | ✅ |
| isAccountRoom | ❌ |
設計意圖: 租戶內部的多人工作群聊。支援 @提及、群公告等功能。
discuss — 討論聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | DiscussRoomImpl |
| 成員範圍 | 多人 |
| 用途 | 主題討論 |
| isMulti | ✅ |
| isAccountRoom | ❌ |
設計意圖: 以特定主題或文件為中心的討論空間。與 group 的區別在於更偏重非同步討論,可附屬於某個主題/任務。
services — 客服聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | ServiceRoomImpl |
| 成員範圍 | 客戶 + 服務號 + 客服人員 |
| 用途 | 客戶與服務號的私聊客服通道 |
| isMulti | ❌ |
| isAccountRoom | ❌ |
設計意圖: 這是 Aile 最核心的聊天室類型。每當客戶透過 LINE / WhatsApp 等渠道聯繫服務號時,系統自動建立一個 services 聊天室。
關鍵行為:
ServiceSession(客服會話)AssistantTriggerPublishService)GatewayFeign 向外部渠道發送ServiceIdentityTag(服務號身份標記)成員構成(MemberGroupType):
| 分組 | 說明 |
|---|---|
ServiceAgent | 當前接待客服 |
ServiceOwner | 服務號 Owner |
ServiceMember | 服務號管理成員 |
ProvisionalMember | 臨時加入的內部成員 |
ProvisionalCustomer | 外部客戶 |
serviceMember — 服務號成員聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | ServiceMemberRoomImpl |
| 成員範圍 | 服務號內部成員 |
| 用途 | 服務號客服之間的內部溝通 |
| isMulti | ✅ |
| isAccountRoom | ❌ |
設計意圖: 每個服務號自動建立一個內部群聊,該服務號的所有客服成員(ServiceMember)自動加入。用於客服之間的轉接溝通、疑難討論。
business — 物件聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | BusinessRoomImpl |
| 成員範圍 | 多人 |
| 用途 | 附屬於業務物件(訂單/案件/表單等)的聊天室 |
| isMulti | ✅ |
| isAccountRoom | ❌ |
設計意圖: 以業務物件(如訂單、案件)為中心建立聊天室。RoomModel 中的 businessId、businessName、businessStatus 欄位用於關聯業務物件及其生命週期狀態。
關鍵特性:
mainRoomId:子聊天室可歸屬於主聊天室(如一個訂單下多個子討論)BusinessStatus 生命週期(Pending → Progress → Revision → Complete)type + mainRoomId + serviceNumberId + businessStatus 組合索引aileSystem — Aile 系統聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | AileSystemRoomImpl |
| 成員範圍 | 系統 + 員工 |
| 用途 | Aile 平台級系統通知 |
| isMulti | ❌ |
| isAccountRoom | ✅ |
設計意圖: 每個員工與 Aile 平台之間的 1:1 系統通知頻道。用於接收平台級公告、安全提醒、帳號通知等。TenantEmployeeModel.systemRoomId 指向這個聊天室。
aiwowSystem — Aiwow 系統聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | AiwowSystemRoomImpl |
| 成員範圍 | 系統 + 員工 |
| 用途 | Aiwow 子系統通知 |
| isMulti | ❌ |
| isAccountRoom | ✅ |
設計意圖: 與 aileSystem 類似,但歸屬於 Aiwow 子系統。用於區分不同來源的系統通知。
system — 通用系統聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | SystemRoomImpl |
| 成員範圍 | 多人 |
| 用途 | 系統級群發通知 |
| isMulti | ❌(不在 isMulti 判斷中) |
| isAccountRoom | ❌ |
設計意圖: 更通用的系統級聊天室,可由系統向多位員工推送消息。消息發送時會排除發送者本人。
lineGroup — LINE 群組聊天室| 屬性 | 值 |
|---|---|
| 策略實現 | LineGroupRoomImpl |
| 成員範圍 | 服務號 + LINE 群組成員 |
| 用途 | LINE 群組在 Aile 中的映射 |
| isMulti | ❌(獨立判斷邏輯) |
| isAccountRoom | ❌ |
設計意圖: 這是為 LINE 渠道群組場景設計的專用聊天室類型。不創建 ServiceSession,不走機器人流程。詳見 line-group-implementation-spec.md。
| 分類 | 類型 | 判斷方法 |
|---|---|---|
| 多人 | group, discuss, business, serviceMember | isMulti(type) |
| 非多人 | person, accountPerson, friend, services, aileSystem, aiwowSystem, system, lineGroup | — |
| 分類 | 類型 | 判斷方法 |
|---|---|---|
| 帳號級 | accountPerson, aileSystem, aiwowSystem | isAccountRoom(type) |
| 租戶級 | 其餘 9 種類型 | — |
| 分類 | 類型 | 判斷方法 |
|---|---|---|
| 通用聊天室 | discuss, friend, group, serviceMember | isCommon(type) |
| 特殊聊天室 | 其餘 8 種類型 | — |
| 關聯類型 | 類型 |
|---|---|
| 強關聯服務號 | services, serviceMember, lineGroup, business |
| 可關聯服務號 | group, discuss |
| 無關聯 | person, accountPerson, friend, aileSystem, aiwowSystem, system |
前端 / API 呼叫
│
└── RoomController.create(dto)
│
├── 依 dto.type 選擇對應 RoomStrategy
│
└── RoomStrategy.createAdpter(adpterDto)
│
├── valid() → 參數校驗
├── create() → 建立 RoomModel + 寫入 MongoDB
├── joinMembers() → 加入初始成員
└── 返回 RoomModelMessageController.send(dto)
│
└── MessageServiceImpl.sendMessage(dto)
│
├── 查找 RoomModel
├── 建立 MessageModel
├── roomFactory.get(roomModel.getType()).sendMessage(room, message)
│ │
│ ├── checkMessage() → 校驗 + 補齊發送者資訊
│ ├── memberFilter() → 過濾接收成員
│ ├── send() → 實際推送(Socket.IO)
│ ├── dispatchSavedMessage() → 離線消息處理
│ └── sendBatchToGateway() → 外部渠道發送(services/lineGroup)
│
└── 寫入 Elasticsearch (index: aile.message)RoomStrategy (interface)
│
└── CommonRoomImpl (abstract) ← 通用邏輯基底
│
├── PersonRoomImpl @Component("person")
├── AccountPersonRoomImpl @Component("accountPerson")
├── FriendRoomImpl @Component("friend")
├── GroupRoomImpl @Component("group")
├── DiscussRoomImpl @Component("discuss")
├── ServiceRoomImpl @Component("services")
├── ServiceMemberRoomImpl @Component("serviceMember")
├── BusinessRoomImpl @Component("business")
├── AileSystemRoomImpl @Component("aileSystem")
├── AiwowSystemRoomImpl @Component("aiwowSystem")
├── SystemRoomImpl @Component("system")
└── LineGroupRoomImpl @Component("lineGroup")每個子類可覆寫的方法:
| 方法 | 說明 |
|---|---|
valid() | 建立聊天室前的參數校驗 |
checkMessage() | 發送前檢查 + 補齊發送者名稱 |
memberFilter() | 過濾接收成員列表 |
send() | 實際推送消息(Socket.IO) |
dispatchSavedMessage() | 離線消息處理 |
| 類型 | 成員規模 | 隸屬層級 | 用途場景 | ServiceSession | 助手觸發 | 外部渠道 |
|---|---|---|---|---|---|---|
person | 1 人 | 租戶 | 個人筆記、系統通知 | — | — | — |
accountPerson | 1 人 | 帳號 | 跨租戶系統通知 | — | — | — |
friend | 2 人 | 租戶 | 員工私聊 | — | — | — |
group | 多人 | 租戶 | 員工群聊 | — | — | — |
discuss | 多人 | 租戶 | 主題討論 | — | — | — |
services | 客戶+服務號 | 租戶 | 客服私聊 | ✅ | ✅ | ✅ |
serviceMember | 多人 | 租戶 | 客服內部群 | — | — | — |
business | 多人 | 租戶 | 業務物件討論 | — | — | — |
aileSystem | 系統+員工 | 帳號 | 平台通知 | — | — | — |
aiwowSystem | 系統+員工 | 帳號 | 子系統通知 | — | — | — |
system | 多人 | 租戶 | 系統群發 | — | — | — |
lineGroup | 服務號+群 | 租戶 | LINE 群組 | — | — | ✅ |
services 聊天室內部使用 MemberGroupType 對成員進行分組管理:
public enum MemberGroupType {
ServiceAgent, // 當前服務人員(接待客服)
ServiceOwner, // 服務號 Owner(最高權限)
ServiceMember, // 服務號成員(管理層)
ProvisionalMember, // 臨時內部成員(轉接/協助)
ProvisionalCustomer // 外部客戶
}分組用途:
不同聊天室類型使用獨立的 Redis Key 前綴進行未讀計數:
| 類型 | Redis Key 前綴 |
|---|---|
| 通用(friend/group/discuss 等) | room:unread:common:* |
| 服務號(services/serviceMember) | room:unread:servicenumber:* |
| LineGroup | room:unread:linegroup:* |
| Business | room:unread:business:* |
| 租戶總計 | room:unread:tenant:total:* |
| 層級 | 檔案 | 說明 |
|---|---|---|
| 類型常量 | aile-api/aile-room-api/.../constant/RoomType.java | 12 種類型定義 + 判斷方法 |
| 核心模型 | aile-api/aile-room-api/.../model/RoomModel.java | 聊天室持久化模型 |
| 狀態枚舉 | aile-api/aile-room-api/.../enums/RoomStatus.java | 7 種狀態 |
| 物件狀態 | aile-api/aile-room-api/.../enums/BusinessStatus.java | 4 種業務狀態 |
| 成員分組 | aile-api/aile-room-api/.../enums/MemberGroupType.java | 5 種成員分組 |
| 工廠 | aile-service/aile-service-room/.../factory/RoomFactory.java | 策略工廠 |
| 策略接口 | aile-service/aile-service-room/.../factory/strategy/RoomStrategy.java | 策略接口 |
| 通用基底 | aile-service/aile-service-room/.../factory/strategy/impl/CommonRoomImpl.java | 抽象基底(所有策略的父類) |
| 客服策略 | aile-service/aile-service-room/.../factory/strategy/impl/ServiceRoomImpl.java | services 最核心策略 |
| 群組策略 | aile-service/aile-service-room/.../factory/strategy/impl/LineGroupRoomImpl.java | lineGroup |
| 物件策略 | aile-service/aile-service-room/.../factory/strategy/impl/BusinessRoomImpl.java | business |
| Redis Key | aile-api/aile-room-api/.../constant/RoomRedisKey.java | 未讀計數 Key 定義 |