Files
gochat/backend/internal/service/sla_event_service.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

107 lines
3.2 KiB
Go

package service
import (
"context"
"fmt"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
applogger "github.com/gochat/gochat/pkg/logger"
)
// SlaEventService 创建和管理SLA事件记录。
// Reference: Chatwoot app/models/sla_event.rb
// 事件类型: frt/nrt/rt missed, 以及hit事件(会话resolved时)
type SlaEventService struct {
slaEventRepo *repository.SlaEventRepo
conversationRepo *repository.ConversationRepo
}
// NewSlaEventService 创建新的SLA事件服务。
func NewSlaEventService(
slaEventRepo *repository.SlaEventRepo,
conversationRepo *repository.ConversationRepo,
) *SlaEventService {
return &SlaEventService{
slaEventRepo: slaEventRepo,
conversationRepo: conversationRepo,
}
}
// NewSlaEventServiceSimple 创建不需要conversationRepo的简化版本(用于内部调用)。
// 内部调用时InboxID从其他地方获取。
func NewSlaEventServiceSimple(slaEventRepo *repository.SlaEventRepo) *SlaEventService {
return &SlaEventService{
slaEventRepo: slaEventRepo,
}
}
// CreateMissedEvent 创建一个SLA阈值missed事件记录。
// Reference: Chatwoot — 当SLA阈值未被满足时,创建missed事件
// eventType: frt/nrt/rt,标识哪个阈值被违反
func (s *SlaEventService) CreateMissedEvent(
ctx context.Context,
appliedSla *model.AppliedSLA,
eventType model.SLAEventType,
) error {
// 获取InboxID: 从会话获取
inboxID := uint(0)
if s.conversationRepo != nil {
conversation, err := s.conversationRepo.FindByID(ctx, appliedSla.ConversationID)
if err == nil {
inboxID = conversation.InboxID
} else {
applogger.L().Errorf("查找Conversation获取InboxID失败: %v", err)
}
}
event := &model.SlaEvent{
AppliedSlaID: appliedSla.ID,
AccountID: appliedSla.AccountID,
ConversationID: appliedSla.ConversationID,
InboxID: inboxID,
SlaPolicyID: appliedSla.SlaPolicyID,
EventType: eventType,
}
if err := s.slaEventRepo.Create(ctx, event); err != nil {
return fmt.Errorf("创建SlaEvent missed记录失败: %w", err)
}
applogger.L().Infof("创建SlaEvent missed: AppliedSLA[%d] eventType=%s", appliedSla.ID, eventType)
return nil
}
// CreateHitEvent 当会话resolved且所有阈值达标时,创建hit事件记录。
// Reference: Chatwoot — 当会话解决且SLA目标全部达标时,创建hit事件
func (s *SlaEventService) CreateHitEvent(
ctx context.Context,
appliedSla *model.AppliedSLA,
) error {
// 获取InboxID: 从会话获取
inboxID := uint(0)
if s.conversationRepo != nil {
conversation, err := s.conversationRepo.FindByID(ctx, appliedSla.ConversationID)
if err == nil {
inboxID = conversation.InboxID
} else {
applogger.L().Errorf("查找Conversation获取InboxID失败: %v", err)
}
}
event := &model.SlaEvent{
AppliedSlaID: appliedSla.ID,
AccountID: appliedSla.AccountID,
ConversationID: appliedSla.ConversationID,
InboxID: inboxID,
SlaPolicyID: appliedSla.SlaPolicyID,
EventType: model.SLAEventFRT, // hit事件使用FRT作为标记类型
}
if err := s.slaEventRepo.Create(ctx, event); err != nil {
return fmt.Errorf("创建SlaEvent hit记录失败: %w", err)
}
applogger.L().Infof("创建SlaEvent hit: AppliedSLA[%d]", appliedSla.ID)
return nil
}