| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- package service
- import (
- "errors"
- "log"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/2930134478/AI-CS/backend/models"
- )
- const (
- defaultConversationPageSize = 50
- maxConversationPageSize = 100
- )
- func parseConversationListPagination(page, pageSize int) (int, int) {
- if page < 1 {
- page = 1
- }
- if pageSize <= 0 {
- pageSize = defaultConversationPageSize
- }
- if pageSize > maxConversationPageSize {
- pageSize = maxConversationPageSize
- }
- return page, pageSize
- }
- func (s *ConversationService) buildSummariesBatch(conversations []models.Conversation, userID uint) ([]ConversationSummary, error) {
- if len(conversations) == 0 {
- return []ConversationSummary{}, nil
- }
- ids := make([]uint, len(conversations))
- for i, conv := range conversations {
- ids[i] = conv.ID
- }
- latestMap, err := s.messages.BatchLatestByConversationIDs(ids)
- if err != nil {
- return nil, err
- }
- unreadMap, err := s.messages.BatchCountUnreadBySender(ids, false)
- if err != nil {
- return nil, err
- }
- participatedMap := map[uint]bool{}
- if userID > 0 {
- participatedMap, err = s.messages.BatchHasAgentParticipated(ids, userID)
- if err != nil {
- return nil, err
- }
- }
- result := make([]ConversationSummary, 0, len(conversations))
- for _, conv := range conversations {
- var lastSeen *time.Time
- if conv.LastSeenAt != nil {
- lastSeen = conv.LastSeenAt
- }
- summary := ConversationSummary{
- ID: conv.ID,
- ConversationType: conv.ConversationType,
- VisitorID: conv.VisitorID,
- AgentID: conv.AgentID,
- Status: conv.Status,
- ChatMode: conv.ChatMode,
- CreatedAt: conv.CreatedAt,
- UpdatedAt: conv.UpdatedAt,
- LastSeenAt: lastSeen,
- UnreadCount: unreadMap[conv.ID],
- HasParticipated: participatedMap[conv.ID],
- }
- if message := latestMap[conv.ID]; message != nil {
- var readAt *time.Time
- if message.ReadAt != nil {
- readAt = message.ReadAt
- }
- summary.LastMessage = &LastMessageSummary{
- ID: message.ID,
- Content: message.Content,
- SenderIsAgent: message.SenderIsAgent,
- MessageType: message.MessageType,
- IsRead: message.IsRead,
- ReadAt: readAt,
- CreatedAt: message.CreatedAt,
- }
- }
- result = append(result, summary)
- }
- return result, nil
- }
- // ListConversationsPaginated 分页返回访客会话列表(批量 SQL,无 N+1)。
- func (s *ConversationService) ListConversationsPaginated(userID uint, status string, page, pageSize int) (*ConversationListResult, error) {
- if status == "" {
- status = "open"
- }
- page, pageSize = parseConversationListPagination(page, pageSize)
- offset := (page - 1) * pageSize
- conversations, total, err := s.conversations.ListVisitorForAgentList(status, offset, pageSize)
- if err != nil {
- return nil, err
- }
- items, err := s.buildSummariesBatch(conversations, userID)
- if err != nil {
- return nil, err
- }
- totalUnread, err := s.messages.CountTotalUnreadVisitorForAgentList(status)
- if err != nil {
- totalUnread = 0
- }
- return &ConversationListResult{
- Items: items,
- Total: total,
- Page: page,
- PageSize: pageSize,
- HasMore: int64(offset+len(items)) < total,
- TotalUnread: totalUnread,
- }, nil
- }
- // ListInternalConversationsPaginated 分页返回内部对话列表。
- func (s *ConversationService) ListInternalConversationsPaginated(agentID uint, status string, page, pageSize int) (*ConversationListResult, error) {
- if agentID == 0 {
- return &ConversationListResult{Items: []ConversationSummary{}, Page: 1, PageSize: defaultConversationPageSize}, nil
- }
- if status == "" {
- status = "open"
- }
- page, pageSize = parseConversationListPagination(page, pageSize)
- conversations, err := s.conversations.ListInternalByAgentIDAndStatus(agentID, status)
- if err != nil {
- return nil, err
- }
- total := int64(len(conversations))
- offset := (page - 1) * pageSize
- if offset > len(conversations) {
- offset = len(conversations)
- }
- end := offset + pageSize
- if end > len(conversations) {
- end = len(conversations)
- }
- pageItems := conversations[offset:end]
- items, err := s.buildSummariesBatch(pageItems, agentID)
- if err != nil {
- return nil, err
- }
- return &ConversationListResult{
- Items: items,
- Total: total,
- Page: page,
- PageSize: pageSize,
- HasMore: int64(offset+len(items)) < total,
- }, nil
- }
- // AutoCloseConversationDaysPolicy 自动关闭 stale 会话的策略(数据库覆盖优先于 .env)。
- type AutoCloseConversationDaysPolicy struct {
- EffectiveDays int `json:"effective_days"`
- EnvDays int `json:"env_days"`
- PersistedInDatabase bool `json:"persisted_in_database"`
- }
- func autoCloseConversationDaysFromEnv() int {
- days := 7
- if v := os.Getenv("AUTO_CLOSE_CONVERSATION_DAYS"); v != "" {
- if parsed, err := strconv.Atoi(v); err == nil {
- days = parsed
- }
- }
- return days
- }
- // EffectiveAutoCloseConversationDays 返回当前生效的自动关闭天数(0=禁用)。
- func (s *ConversationService) EffectiveAutoCloseConversationDays() int {
- if s.appSettings != nil {
- if row, err := s.appSettings.Get(models.AppSettingKeyAutoCloseConversationDays); err == nil && row != nil {
- if v := strings.TrimSpace(row.Value); v != "" {
- if parsed, err := strconv.Atoi(v); err == nil && parsed >= 0 {
- return parsed
- }
- }
- }
- }
- return autoCloseConversationDaysFromEnv()
- }
- // GetAutoCloseConversationDaysPolicy 读取自动关闭 stale 会话策略。
- func (s *ConversationService) GetAutoCloseConversationDaysPolicy() AutoCloseConversationDaysPolicy {
- envDays := autoCloseConversationDaysFromEnv()
- policy := AutoCloseConversationDaysPolicy{
- EffectiveDays: envDays,
- EnvDays: envDays,
- }
- if s.appSettings != nil {
- if row, err := s.appSettings.Get(models.AppSettingKeyAutoCloseConversationDays); err == nil && row != nil {
- if v := strings.TrimSpace(row.Value); v != "" {
- policy.PersistedInDatabase = true
- if parsed, err := strconv.Atoi(v); err == nil && parsed >= 0 {
- policy.EffectiveDays = parsed
- }
- }
- }
- }
- return policy
- }
- // SetAutoCloseConversationDaysPolicy 写入自动关闭天数(0=禁用)。
- func (s *ConversationService) SetAutoCloseConversationDaysPolicy(inactiveDays int) error {
- if inactiveDays < 0 {
- return errors.New("inactive_days 不能为负数")
- }
- if s.appSettings == nil {
- return errors.New("配置存储不可用")
- }
- return s.appSettings.SetValue(models.AppSettingKeyAutoCloseConversationDays, strconv.Itoa(inactiveDays))
- }
- // ClearAutoCloseConversationDaysPolicy 删除数据库覆盖,恢复为 .env 默认值。
- func (s *ConversationService) ClearAutoCloseConversationDaysPolicy() error {
- if s.appSettings == nil {
- return errors.New("配置存储不可用")
- }
- return s.appSettings.Delete(models.AppSettingKeyAutoCloseConversationDays)
- }
- // CloseStaleOpenVisitorConversations 关闭超过指定天数未更新的 open 访客会话(仅改 status,不删记录)。
- func (s *ConversationService) CloseStaleOpenVisitorConversations(inactiveDays int) (int64, error) {
- if inactiveDays <= 0 {
- return 0, nil
- }
- cutoff := time.Now().AddDate(0, 0, -inactiveDays)
- return s.conversations.CloseStaleOpenVisitorConversations(cutoff)
- }
- // StartStaleConversationCleanup 启动后台任务:定期关闭长期未活跃的 open 访客会话。
- func (s *ConversationService) StartStaleConversationCleanup() {
- run := func() {
- inactiveDays := s.EffectiveAutoCloseConversationDays()
- if inactiveDays <= 0 {
- return
- }
- n, err := s.CloseStaleOpenVisitorConversations(inactiveDays)
- if err != nil {
- log.Printf("[会话维护] 自动关闭 stale 会话失败: %v", err)
- return
- }
- if n > 0 {
- log.Printf("[会话维护] 已自动关闭 %d 条超过 %d 天未更新的 open 访客会话", n, inactiveDays)
- }
- }
- if days := s.EffectiveAutoCloseConversationDays(); days <= 0 {
- log.Println("[会话维护] 自动关闭 stale 会话已禁用(effective_days=0)")
- } else {
- run()
- }
- go func() {
- ticker := time.NewTicker(24 * time.Hour)
- defer ticker.Stop()
- for range ticker.C {
- run()
- }
- }()
- }
|