refactor: move batch schedule response mapping to builder
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
package builders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cs-agent/internal/pkg/dto/response"
|
||||||
|
"cs-agent/internal/services"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BuildAgentTeamScheduleBatchPreviewResponse(result *services.AgentTeamScheduleBatchPreviewResult) *response.AgentTeamScheduleBatchPreviewResponse {
|
||||||
|
if result == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
items := make([]response.AgentTeamScheduleBatchPreviewItem, 0, len(result.Items))
|
||||||
|
for _, item := range result.Items {
|
||||||
|
items = append(items, response.AgentTeamScheduleBatchPreviewItem{
|
||||||
|
TeamID: item.TeamID,
|
||||||
|
TeamName: item.TeamName,
|
||||||
|
Date: item.Date.Format(time.DateOnly),
|
||||||
|
Weekday: item.Weekday,
|
||||||
|
StartAt: item.StartAt.Format(time.DateTime),
|
||||||
|
EndAt: item.EndAt.Format(time.DateTime),
|
||||||
|
Remark: item.Remark,
|
||||||
|
Conflict: item.Conflict,
|
||||||
|
ConflictReason: item.ConflictReason,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return &response.AgentTeamScheduleBatchPreviewResponse{
|
||||||
|
Total: result.Total,
|
||||||
|
Conflict: result.Conflict,
|
||||||
|
Items: items,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildAgentTeamScheduleBatchGenerateResponse(result *services.AgentTeamScheduleBatchGenerateResult) *response.AgentTeamScheduleBatchGenerateResponse {
|
||||||
|
if result == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &response.AgentTeamScheduleBatchGenerateResponse{Created: result.Created}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package dashboard
|
package dashboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cs-agent/internal/builders"
|
||||||
"cs-agent/internal/models"
|
"cs-agent/internal/models"
|
||||||
"cs-agent/internal/pkg/constants"
|
"cs-agent/internal/pkg/constants"
|
||||||
"cs-agent/internal/pkg/dto/request"
|
"cs-agent/internal/pkg/dto/request"
|
||||||
@@ -66,7 +67,7 @@ func (c *AgentTeamScheduleController) PostBatch_preview() *web.JsonResult {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return web.JsonError(err)
|
return web.JsonError(err)
|
||||||
}
|
}
|
||||||
return web.JsonData(ret)
|
return web.JsonData(builders.BuildAgentTeamScheduleBatchPreviewResponse(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AgentTeamScheduleController) PostBatch_generate() *web.JsonResult {
|
func (c *AgentTeamScheduleController) PostBatch_generate() *web.JsonResult {
|
||||||
@@ -82,7 +83,7 @@ func (c *AgentTeamScheduleController) PostBatch_generate() *web.JsonResult {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return web.JsonError(err)
|
return web.JsonError(err)
|
||||||
}
|
}
|
||||||
return web.JsonData(ret)
|
return web.JsonData(builders.BuildAgentTeamScheduleBatchGenerateResponse(ret))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AgentTeamScheduleController) GetBy(id int64) *web.JsonResult {
|
func (c *AgentTeamScheduleController) GetBy(id int64) *web.JsonResult {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"cs-agent/internal/models"
|
"cs-agent/internal/models"
|
||||||
"cs-agent/internal/pkg/dto"
|
"cs-agent/internal/pkg/dto"
|
||||||
"cs-agent/internal/pkg/dto/request"
|
"cs-agent/internal/pkg/dto/request"
|
||||||
"cs-agent/internal/pkg/dto/response"
|
|
||||||
"cs-agent/internal/pkg/enums"
|
"cs-agent/internal/pkg/enums"
|
||||||
"cs-agent/internal/pkg/errorsx"
|
"cs-agent/internal/pkg/errorsx"
|
||||||
"cs-agent/internal/pkg/utils"
|
"cs-agent/internal/pkg/utils"
|
||||||
@@ -32,6 +31,28 @@ type agentTeamScheduleService struct {
|
|||||||
|
|
||||||
const maxAgentTeamScheduleBatchItems = 500
|
const maxAgentTeamScheduleBatchItems = 500
|
||||||
|
|
||||||
|
type AgentTeamScheduleBatchPreviewResult struct {
|
||||||
|
Total int
|
||||||
|
Conflict bool
|
||||||
|
Items []AgentTeamScheduleBatchPreviewItem
|
||||||
|
}
|
||||||
|
|
||||||
|
type AgentTeamScheduleBatchPreviewItem struct {
|
||||||
|
TeamID int64
|
||||||
|
TeamName string
|
||||||
|
Date time.Time
|
||||||
|
Weekday int
|
||||||
|
StartAt time.Time
|
||||||
|
EndAt time.Time
|
||||||
|
Remark string
|
||||||
|
Conflict bool
|
||||||
|
ConflictReason string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AgentTeamScheduleBatchGenerateResult struct {
|
||||||
|
Created int
|
||||||
|
}
|
||||||
|
|
||||||
type batchScheduleCandidate struct {
|
type batchScheduleCandidate struct {
|
||||||
TeamID int64
|
TeamID int64
|
||||||
TeamName string
|
TeamName string
|
||||||
@@ -163,7 +184,7 @@ func (s *agentTeamScheduleService) DeleteAgentTeamSchedule(id int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *agentTeamScheduleService) BatchPreview(req request.AgentTeamScheduleBatchRequest, operator *dto.AuthPrincipal) (*response.AgentTeamScheduleBatchPreviewResponse, error) {
|
func (s *agentTeamScheduleService) BatchPreview(req request.AgentTeamScheduleBatchRequest, operator *dto.AuthPrincipal) (*AgentTeamScheduleBatchPreviewResult, error) {
|
||||||
if operator == nil {
|
if operator == nil {
|
||||||
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
||||||
}
|
}
|
||||||
@@ -172,10 +193,10 @@ func (s *agentTeamScheduleService) BatchPreview(req request.AgentTeamScheduleBat
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conflicts := s.findBatchConflict(candidates)
|
conflicts := s.findBatchConflict(candidates)
|
||||||
return buildBatchPreviewResponse(candidates, conflicts), nil
|
return buildBatchPreviewResult(candidates, conflicts), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *agentTeamScheduleService) BatchGenerate(req request.AgentTeamScheduleBatchRequest, operator *dto.AuthPrincipal) (*response.AgentTeamScheduleBatchGenerateResponse, error) {
|
func (s *agentTeamScheduleService) BatchGenerate(req request.AgentTeamScheduleBatchRequest, operator *dto.AuthPrincipal) (*AgentTeamScheduleBatchGenerateResult, error) {
|
||||||
if operator == nil {
|
if operator == nil {
|
||||||
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
||||||
}
|
}
|
||||||
@@ -220,7 +241,7 @@ func (s *agentTeamScheduleService) BatchGenerate(req request.AgentTeamScheduleBa
|
|||||||
for i := range schedules {
|
for i := range schedules {
|
||||||
s.dispatchPendingConversationsIfActive(&schedules[i])
|
s.dispatchPendingConversationsIfActive(&schedules[i])
|
||||||
}
|
}
|
||||||
return &response.AgentTeamScheduleBatchGenerateResponse{Created: len(schedules)}, nil
|
return &AgentTeamScheduleBatchGenerateResult{Created: len(schedules)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *agentTeamScheduleService) buildScheduleModel(id, teamID int64, startAt, endAt, remark string) (*models.AgentTeamSchedule, error) {
|
func (s *agentTeamScheduleService) buildScheduleModel(id, teamID int64, startAt, endAt, remark string) (*models.AgentTeamSchedule, error) {
|
||||||
@@ -380,8 +401,8 @@ func combineDateAndClock(date, clock time.Time) time.Time {
|
|||||||
return time.Date(year, month, day, hour, minute, second, 0, time.Local)
|
return time.Date(year, month, day, hour, minute, second, 0, time.Local)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildBatchPreviewResponse(candidates []batchScheduleCandidate, conflicts map[int]string) *response.AgentTeamScheduleBatchPreviewResponse {
|
func buildBatchPreviewResult(candidates []batchScheduleCandidate, conflicts map[int]string) *AgentTeamScheduleBatchPreviewResult {
|
||||||
items := make([]response.AgentTeamScheduleBatchPreviewItem, 0, len(candidates))
|
items := make([]AgentTeamScheduleBatchPreviewItem, 0, len(candidates))
|
||||||
hasConflict := false
|
hasConflict := false
|
||||||
for i, candidate := range candidates {
|
for i, candidate := range candidates {
|
||||||
conflictReason := conflicts[i]
|
conflictReason := conflicts[i]
|
||||||
@@ -389,19 +410,19 @@ func buildBatchPreviewResponse(candidates []batchScheduleCandidate, conflicts ma
|
|||||||
if conflict {
|
if conflict {
|
||||||
hasConflict = true
|
hasConflict = true
|
||||||
}
|
}
|
||||||
items = append(items, response.AgentTeamScheduleBatchPreviewItem{
|
items = append(items, AgentTeamScheduleBatchPreviewItem{
|
||||||
TeamID: candidate.TeamID,
|
TeamID: candidate.TeamID,
|
||||||
TeamName: candidate.TeamName,
|
TeamName: candidate.TeamName,
|
||||||
Date: candidate.Date.Format(time.DateOnly),
|
Date: candidate.Date,
|
||||||
Weekday: weekdayForBatchRequest(candidate.Date),
|
Weekday: weekdayForBatchRequest(candidate.Date),
|
||||||
StartAt: candidate.StartAt.Format(time.DateTime),
|
StartAt: candidate.StartAt,
|
||||||
EndAt: candidate.EndAt.Format(time.DateTime),
|
EndAt: candidate.EndAt,
|
||||||
Remark: candidate.Remark,
|
Remark: candidate.Remark,
|
||||||
Conflict: conflict,
|
Conflict: conflict,
|
||||||
ConflictReason: conflictReason,
|
ConflictReason: conflictReason,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return &response.AgentTeamScheduleBatchPreviewResponse{
|
return &AgentTeamScheduleBatchPreviewResult{
|
||||||
Total: len(items),
|
Total: len(items),
|
||||||
Conflict: hasConflict,
|
Conflict: hasConflict,
|
||||||
Items: items,
|
Items: items,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"cs-agent/internal/models"
|
"cs-agent/internal/models"
|
||||||
"cs-agent/internal/pkg/dto"
|
"cs-agent/internal/pkg/dto"
|
||||||
"cs-agent/internal/pkg/dto/request"
|
"cs-agent/internal/pkg/dto/request"
|
||||||
"cs-agent/internal/pkg/dto/response"
|
|
||||||
"cs-agent/internal/pkg/enums"
|
"cs-agent/internal/pkg/enums"
|
||||||
"cs-agent/internal/services"
|
"cs-agent/internal/services"
|
||||||
|
|
||||||
@@ -214,9 +213,9 @@ func TestAgentTeamScheduleServiceBatchPreviewExpandsSharedRule(t *testing.T) {
|
|||||||
teamID int64
|
teamID int64
|
||||||
date string
|
date string
|
||||||
}
|
}
|
||||||
itemsByKey := make(map[previewKey]response.AgentTeamScheduleBatchPreviewItem)
|
itemsByKey := make(map[previewKey]services.AgentTeamScheduleBatchPreviewItem)
|
||||||
for _, item := range preview.Items {
|
for _, item := range preview.Items {
|
||||||
itemsByKey[previewKey{teamID: item.TeamID, date: item.Date}] = item
|
itemsByKey[previewKey{teamID: item.TeamID, date: item.Date.Format(time.DateOnly)}] = item
|
||||||
}
|
}
|
||||||
expected := []struct {
|
expected := []struct {
|
||||||
teamID int64
|
teamID int64
|
||||||
@@ -235,8 +234,8 @@ func TestAgentTeamScheduleServiceBatchPreviewExpandsSharedRule(t *testing.T) {
|
|||||||
t.Fatalf("expected preview item for teamID=%d date=%s, got %+v", want.teamID, wantDate, preview.Items)
|
t.Fatalf("expected preview item for teamID=%d date=%s, got %+v", want.teamID, wantDate, preview.Items)
|
||||||
}
|
}
|
||||||
if item.Weekday != want.weekday ||
|
if item.Weekday != want.weekday ||
|
||||||
item.StartAt != formatTestDateTime(want.date, "09:00:00") ||
|
item.StartAt.Format(time.DateTime) != formatTestDateTime(want.date, "09:00:00") ||
|
||||||
item.EndAt != formatTestDateTime(want.date, "18:00:00") ||
|
item.EndAt.Format(time.DateTime) != formatTestDateTime(want.date, "18:00:00") ||
|
||||||
item.Remark != "工作日白班" {
|
item.Remark != "工作日白班" {
|
||||||
t.Fatalf("unexpected preview item for teamID=%d date=%s: %+v", want.teamID, wantDate, item)
|
t.Fatalf("unexpected preview item for teamID=%d date=%s: %+v", want.teamID, wantDate, item)
|
||||||
}
|
}
|
||||||
@@ -334,7 +333,7 @@ func TestAgentTeamScheduleServiceBatchPreviewMarksConflicts(t *testing.T) {
|
|||||||
if !preview.Conflict {
|
if !preview.Conflict {
|
||||||
t.Fatalf("expected preview conflict, got %+v", preview)
|
t.Fatalf("expected preview conflict, got %+v", preview)
|
||||||
}
|
}
|
||||||
itemsByTeamID := make(map[int64]response.AgentTeamScheduleBatchPreviewItem)
|
itemsByTeamID := make(map[int64]services.AgentTeamScheduleBatchPreviewItem)
|
||||||
for _, item := range preview.Items {
|
for _, item := range preview.Items {
|
||||||
itemsByTeamID[item.TeamID] = item
|
itemsByTeamID[item.TeamID] = item
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user