feat: 新增数据仓库层实现

- 新增MessageRepository用于消息数据管理
- 新增PaymentRepository用于支付和订单管理
- 新增ProfileRepository用于用户资料管理
- 优化ConversationRepository实现
This commit is contained in:
徐在坤
2026-01-21 18:17:34 +08:00
parent 6351981555
commit 018f4dc901
4 changed files with 128 additions and 1 deletions

View File

@@ -1,11 +1,14 @@
package com.huaga.life_echo.data.repository
import com.huaga.life_echo.data.database.*
import com.huaga.life_echo.network.ApiService
import com.huaga.life_echo.network.models.ConversationListItemDto
import kotlinx.coroutines.flow.Flow
class ConversationRepository(
private val conversationDao: ConversationDao,
private val segmentDao: ConversationSegmentDao
private val segmentDao: ConversationSegmentDao,
private val apiService: ApiService
) {
fun getAllConversations(): Flow<List<Conversation>> {
return conversationDao.getAllConversations()
@@ -34,5 +37,32 @@ class ConversationRepository(
suspend fun insertSegments(segments: List<ConversationSegment>) {
segmentDao.insertSegments(segments)
}
/**
* 从API同步对话列表
*/
suspend fun syncConversations() {
val result = apiService.getConversationList()
result.getOrNull()?.let { conversations ->
// 将DTO转换为Entity并保存到数据库
conversations.forEach { dto ->
val conversation = Conversation(
id = dto.id,
userId = "", // 需要从TokenManager获取
startedAt = dto.latestMessageTime,
endedAt = null,
durationSeconds = 0,
summary = dto.latestMessagePreview,
currentTopic = null,
conversationStage = null,
avatarUrl = dto.avatarUrl,
title = dto.title,
latestMessagePreview = dto.latestMessagePreview,
latestMessageTime = dto.latestMessageTime
)
conversationDao.insertConversation(conversation)
}
}
}
}

View File

@@ -0,0 +1,42 @@
package com.huaga.life_echo.data.repository
import com.huaga.life_echo.data.database.Message
import com.huaga.life_echo.data.database.MessageDao
import com.huaga.life_echo.network.ApiService
import com.huaga.life_echo.network.models.MessageDto
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
class MessageRepository(
private val messageDao: MessageDao,
private val apiService: ApiService
) {
fun getMessagesByConversationId(conversationId: String): Flow<List<Message>> {
return messageDao.getMessagesByConversationId(conversationId)
}
suspend fun syncMessages(conversationId: String) {
val result = apiService.getMessages(conversationId)
result.getOrNull()?.let { messages ->
val dbMessages = messages.map { dto ->
Message(
id = dto.id,
conversationId = dto.conversationId,
content = dto.content,
senderType = dto.senderType,
timestamp = dto.timestamp,
messageType = dto.messageType
)
}
messageDao.insertMessages(dbMessages)
}
}
suspend fun insertMessage(message: Message) {
messageDao.insertMessage(message)
}
suspend fun insertMessages(messages: List<Message>) {
messageDao.insertMessages(messages)
}
}

View File

@@ -0,0 +1,34 @@
package com.huaga.life_echo.data.repository
import com.huaga.life_echo.network.ApiService
import com.huaga.life_echo.network.models.OrderDto
import com.huaga.life_echo.network.models.PlanDto
import com.huaga.life_echo.network.models.QuotaCheckDto
class PaymentRepository(
private val apiService: ApiService
) {
suspend fun getPlans(): Result<List<PlanDto>> {
return apiService.getPlans()
}
suspend fun getCurrentPlan(): Result<PlanDto> {
return apiService.getCurrentPlan()
}
suspend fun checkQuota(): Result<QuotaCheckDto> {
return apiService.checkQuota()
}
suspend fun createOrder(planId: String): Result<OrderDto> {
return apiService.createOrder(planId)
}
suspend fun getOrders(): Result<List<OrderDto>> {
return apiService.getOrders()
}
suspend fun getOrderById(orderId: String): Result<OrderDto> {
return apiService.getOrderById(orderId)
}
}

View File

@@ -0,0 +1,21 @@
package com.huaga.life_echo.data.repository
import com.huaga.life_echo.network.ApiService
import com.huaga.life_echo.network.models.FAQDto
import com.huaga.life_echo.network.models.UserProfileDto
class ProfileRepository(
private val apiService: ApiService
) {
suspend fun getUserProfile(): Result<UserProfileDto> {
return apiService.getUserProfile()
}
suspend fun getFAQs(): Result<List<FAQDto>> {
return apiService.getFAQs()
}
suspend fun submitFeedback(content: String, contact: String?): Result<Unit> {
return apiService.submitFeedback(content, contact)
}
}