feat(csmanualentry): 手动录单支持收号出号分组
- 录单记录新增组别(收号/出号),唯一约束放宽为编号+组别,同一编号两组各可登记一次 - 操作人选项支持组别标记,录单选中后自动带出组别 - 列表新增组别列与筛选,今日统计按收号/出号分列
This commit is contained in:
@@ -2,13 +2,21 @@ package model
|
||||
|
||||
import "time"
|
||||
|
||||
// 录单组别取值。
|
||||
const (
|
||||
CSManualEntryGroupCollect = "collect" // 收号
|
||||
CSManualEntryGroupSell = "sell" // 出号
|
||||
)
|
||||
|
||||
// CSManualEntry 客服手动录单记录。
|
||||
// 客服在后台手动登记一个编号(如 202610120013),该编号与其登录账号绑定,
|
||||
// 记录平台、操作人、登记时间等,供后续查询与按客服统计今日录单量。
|
||||
// 记录组别(收号/出号)、平台、操作人、登记时间等,供后续查询与按客服统计今日录单量。
|
||||
// 同一编号允许收号、出号两组各登记一次,唯一键为 (entry_no, entry_group)。
|
||||
// platform / operator 存名称快照:后台删掉选项也不影响历史记录展示。
|
||||
type CSManualEntry struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
EntryNo string `gorm:"size:64;not null;uniqueIndex" json:"entry_no"`
|
||||
EntryNo string `gorm:"size:64;not null;uniqueIndex:uk_cs_manual_entries_entry_no_group,priority:1" json:"entry_no"`
|
||||
EntryGroup string `gorm:"size:16;not null;default:'collect';uniqueIndex:uk_cs_manual_entries_entry_no_group,priority:2;index" json:"entry_group"`
|
||||
Platform string `gorm:"size:64;not null;default:'';index" json:"platform"`
|
||||
Operator string `gorm:"size:64;not null;default:'';index" json:"operator"`
|
||||
AdminID uint64 `gorm:"not null;index" json:"admin_id"`
|
||||
@@ -25,10 +33,12 @@ func (CSManualEntry) TableName() string {
|
||||
|
||||
// CSManualEntryOption 客服手动录单的可选项配置(平台 / 操作人)。
|
||||
// 仅作为录单表单的下拉数据源,记录里保存的是名称快照。
|
||||
// 操作人选项可通过 group_tag 标记所属组别(收号/出号),录单时用于自动带出组别。
|
||||
type CSManualEntryOption struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
Type string `gorm:"size:16;not null;uniqueIndex:uk_cs_manual_entry_options_type_name,priority:1" json:"type"`
|
||||
Name string `gorm:"size:64;not null;uniqueIndex:uk_cs_manual_entry_options_type_name,priority:2" json:"name"`
|
||||
GroupTag string `gorm:"size:16;not null;default:''" json:"group_tag"`
|
||||
SortOrder int `gorm:"not null;default:0" json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
@@ -8,26 +8,35 @@ const (
|
||||
OptionTypeOperator = "operator" // 操作人
|
||||
)
|
||||
|
||||
// 录单组别。
|
||||
const (
|
||||
EntryGroupCollect = "collect" // 收号
|
||||
EntryGroupSell = "sell" // 出号
|
||||
)
|
||||
|
||||
// CreateRequest 客服手动录单请求。
|
||||
type CreateRequest struct {
|
||||
EntryNo string `json:"entry_no" binding:"required"`
|
||||
Platform string `json:"platform"`
|
||||
Operator string `json:"operator"`
|
||||
Remark string `json:"remark"`
|
||||
EntryNo string `json:"entry_no" binding:"required"`
|
||||
EntryGroup string `json:"entry_group" binding:"required"`
|
||||
Platform string `json:"platform"`
|
||||
Operator string `json:"operator"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// UpdateRequest 管理员修正录单请求。
|
||||
type UpdateRequest struct {
|
||||
EntryNo string `json:"entry_no" binding:"required"`
|
||||
Platform string `json:"platform"`
|
||||
Operator string `json:"operator"`
|
||||
Remark string `json:"remark"`
|
||||
EntryNo string `json:"entry_no" binding:"required"`
|
||||
EntryGroup string `json:"entry_group" binding:"required"`
|
||||
Platform string `json:"platform"`
|
||||
Operator string `json:"operator"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// EntryDTO 录单记录。
|
||||
type EntryDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
EntryNo string `json:"entry_no"`
|
||||
EntryGroup string `json:"entry_group"`
|
||||
Platform string `json:"platform"`
|
||||
Operator string `json:"operator"`
|
||||
AdminID uint64 `json:"admin_id"`
|
||||
@@ -39,13 +48,14 @@ type EntryDTO struct {
|
||||
|
||||
// ListQuery 录单查询条件。
|
||||
type ListQuery struct {
|
||||
EntryNo string
|
||||
Keyword string
|
||||
AdminID uint64
|
||||
StartDate string // YYYY-MM-DD(北京时间,闭区间)
|
||||
EndDate string // YYYY-MM-DD(北京时间,闭区间)
|
||||
Page int
|
||||
PageSize int
|
||||
EntryNo string
|
||||
EntryGroup string
|
||||
Keyword string
|
||||
AdminID uint64
|
||||
StartDate string // YYYY-MM-DD(北京时间,闭区间)
|
||||
EndDate string // YYYY-MM-DD(北京时间,闭区间)
|
||||
Page int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// PaginatedResult 分页结果。
|
||||
@@ -56,12 +66,14 @@ type PaginatedResult struct {
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
// AgentDayStatsDTO 某客服某日录单统计。
|
||||
// AgentDayStatsDTO 某客服某日录单统计(按组分列)。
|
||||
type AgentDayStatsDTO struct {
|
||||
Date string `json:"date"` // YYYY-MM-DD(北京时间)
|
||||
AdminID uint64 `json:"admin_id"`
|
||||
AdminName string `json:"admin_name"`
|
||||
Count int64 `json:"count"`
|
||||
Date string `json:"date"` // YYYY-MM-DD(北京时间)
|
||||
AdminID uint64 `json:"admin_id"`
|
||||
AdminName string `json:"admin_name"`
|
||||
Count int64 `json:"count"` // 总录单量
|
||||
CollectCount int64 `json:"collect_count"` // 收号录单量
|
||||
SellCount int64 `json:"sell_count"` // 出号录单量
|
||||
}
|
||||
|
||||
// CreateResult 录单结果。
|
||||
@@ -74,6 +86,7 @@ type OptionDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
GroupTag string `json:"group_tag"` // 操作人组别标记:collect/sell/空
|
||||
SortOrder int `json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -86,11 +99,23 @@ type OptionListDTO struct {
|
||||
|
||||
// CreateOptionRequest 新增平台 / 操作人选项。
|
||||
type CreateOptionRequest struct {
|
||||
Type string `json:"type" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
GroupTag string `json:"group_tag"` // 仅操作人类型可用,collect/sell/空
|
||||
}
|
||||
|
||||
// UpdateOptionRequest 重命名平台 / 操作人选项。
|
||||
// UpdateOptionRequest 重命名平台 / 操作人选项(可同时更新组别标记)。
|
||||
type UpdateOptionRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
GroupTag string `json:"group_tag"` // 仅操作人类型可用,collect/sell/空
|
||||
}
|
||||
|
||||
// IsValidEntryGroup 判断组别取值是否合法。
|
||||
func IsValidEntryGroup(group string) bool {
|
||||
return group == EntryGroupCollect || group == EntryGroupSell
|
||||
}
|
||||
|
||||
// IsValidGroupTag 判断选项组别标记是否合法(允许为空表示未分组)。
|
||||
func IsValidGroupTag(tag string) bool {
|
||||
return tag == "" || IsValidEntryGroup(tag)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrEntryNoExists = errors.New("manual entry number already exists")
|
||||
ErrEntryNoInvalid = errors.New("entry number is invalid")
|
||||
ErrEntryGroupInvalid = errors.New("entry group is invalid")
|
||||
ErrEntryNotFound = errors.New("manual entry not found")
|
||||
ErrAdminNotFound = errors.New("admin not found")
|
||||
ErrOptionTypeInvalid = errors.New("manual entry option type is invalid")
|
||||
|
||||
@@ -46,8 +46,9 @@ func (h *Handler) List(c *gin.Context) {
|
||||
}
|
||||
page, pageSize := parsePagination(c)
|
||||
query := ListQuery{
|
||||
EntryNo: strings.TrimSpace(c.Query("entry_no")),
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
EntryNo: strings.TrimSpace(c.Query("entry_no")),
|
||||
EntryGroup: strings.TrimSpace(c.Query("entry_group")),
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
StartDate: strings.TrimSpace(c.Query("start_date")),
|
||||
EndDate: strings.TrimSpace(c.Query("end_date")),
|
||||
Page: page,
|
||||
@@ -233,7 +234,9 @@ func writeManualEntryError(c *gin.Context, err error) {
|
||||
case errors.Is(err, ErrEntryNotFound):
|
||||
response.NotFound(c, "录单记录不存在")
|
||||
case errors.Is(err, ErrEntryNoExists):
|
||||
response.Error(c, http.StatusConflict, "entry_no_exists", "该编号已被登记")
|
||||
response.Error(c, http.StatusConflict, "entry_no_exists", "该编号在当前组别下已被登记")
|
||||
case errors.Is(err, ErrEntryGroupInvalid):
|
||||
response.BadRequest(c, "组别不正确,仅支持收号或出号")
|
||||
case errors.Is(err, ErrOptionTypeInvalid):
|
||||
response.BadRequest(c, "选项类型不正确")
|
||||
case errors.Is(err, ErrOptionNameInvalid):
|
||||
|
||||
@@ -34,9 +34,10 @@ func (r *Repository) FindAdminSnapshot(ctx context.Context, adminID uint64) (nam
|
||||
}
|
||||
|
||||
// Create 写入一条录单记录。
|
||||
func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, adminUsername, entryNo, platform, operator, remark string) (*EntryDTO, error) {
|
||||
func (r *Repository) Create(ctx context.Context, adminID uint64, adminName, adminUsername, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
row := model.CSManualEntry{
|
||||
EntryNo: entryNo,
|
||||
EntryGroup: entryGroup,
|
||||
Platform: platform,
|
||||
Operator: operator,
|
||||
AdminID: adminID,
|
||||
@@ -67,15 +68,16 @@ func (r *Repository) FindByID(ctx context.Context, id uint64) (*EntryDTO, error)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// Update 修正录单的编号、平台、操作人与备注。编号唯一冲突返回 ErrEntryNoExists。
|
||||
func (r *Repository) Update(ctx context.Context, id uint64, entryNo, platform, operator, remark string) (*EntryDTO, error) {
|
||||
// Update 修正录单的编号、组别、平台、操作人与备注。同组内编号唯一冲突返回 ErrEntryNoExists。
|
||||
func (r *Repository) Update(ctx context.Context, id uint64, entryNo, entryGroup, platform, operator, remark string) (*EntryDTO, error) {
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntry{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]any{
|
||||
"entry_no": entryNo,
|
||||
"platform": platform,
|
||||
"operator": operator,
|
||||
"remark": remark,
|
||||
"entry_no": entryNo,
|
||||
"entry_group": entryGroup,
|
||||
"platform": platform,
|
||||
"operator": operator,
|
||||
"remark": remark,
|
||||
})
|
||||
if result.Error != nil {
|
||||
if isDuplicateEntry(result.Error) {
|
||||
@@ -116,6 +118,10 @@ func (r *Repository) List(ctx context.Context, query ListQuery) (*PaginatedResul
|
||||
db = db.Where("entry_no = ?", query.EntryNo)
|
||||
countDB = countDB.Where("entry_no = ?", query.EntryNo)
|
||||
}
|
||||
if query.EntryGroup != "" {
|
||||
db = db.Where("entry_group = ?", query.EntryGroup)
|
||||
countDB = countDB.Where("entry_group = ?", query.EntryGroup)
|
||||
}
|
||||
if query.AdminID > 0 {
|
||||
db = db.Where("admin_id = ?", query.AdminID)
|
||||
countDB = countDB.Where("admin_id = ?", query.AdminID)
|
||||
@@ -168,13 +174,17 @@ func (r *Repository) TodayAgentStats(ctx context.Context, day time.Time) ([]Agen
|
||||
end := start.AddDate(0, 0, 1)
|
||||
|
||||
type row struct {
|
||||
AdminID uint64
|
||||
AdminName string
|
||||
Count int64
|
||||
AdminID uint64
|
||||
AdminName string
|
||||
Count int64
|
||||
CollectCount int64
|
||||
SellCount int64
|
||||
}
|
||||
rows := make([]row, 0)
|
||||
if err := r.db.WithContext(ctx).Table("cs_manual_entries").
|
||||
Select("admin_id, MAX(admin_name) AS admin_name, COUNT(*) AS count").
|
||||
Select(`admin_id, MAX(admin_name) AS admin_name, COUNT(*) AS count,
|
||||
SUM(CASE WHEN entry_group = 'collect' THEN 1 ELSE 0 END) AS collect_count,
|
||||
SUM(CASE WHEN entry_group = 'sell' THEN 1 ELSE 0 END) AS sell_count`).
|
||||
Where("created_at >= ? AND created_at < ?", start, end).
|
||||
Group("admin_id").
|
||||
Scan(&rows).Error; err != nil {
|
||||
@@ -190,10 +200,12 @@ func (r *Repository) TodayAgentStats(ctx context.Context, day time.Time) ([]Agen
|
||||
items := make([]AgentDayStatsDTO, 0, len(rows))
|
||||
for _, item := range rows {
|
||||
items = append(items, AgentDayStatsDTO{
|
||||
Date: date,
|
||||
AdminID: item.AdminID,
|
||||
AdminName: item.AdminName,
|
||||
Count: item.Count,
|
||||
Date: date,
|
||||
AdminID: item.AdminID,
|
||||
AdminName: item.AdminName,
|
||||
Count: item.Count,
|
||||
CollectCount: item.CollectCount,
|
||||
SellCount: item.SellCount,
|
||||
})
|
||||
}
|
||||
return items, nil
|
||||
@@ -232,11 +244,12 @@ func (r *Repository) ListAllOptions(ctx context.Context) (*OptionListDTO, error)
|
||||
}
|
||||
|
||||
// CreateOption 新增一个平台 / 操作人选项,重名返回 ErrOptionExists。
|
||||
func (r *Repository) CreateOption(ctx context.Context, optionType, name string) (*OptionDTO, error) {
|
||||
// 操作人选项可通过 groupTag 标记所属组别(collect/sell,空表示未分组)。
|
||||
func (r *Repository) CreateOption(ctx context.Context, optionType, name, groupTag string) (*OptionDTO, error) {
|
||||
if !IsValidOptionType(optionType) {
|
||||
return nil, ErrOptionTypeInvalid
|
||||
}
|
||||
row := model.CSManualEntryOption{Type: optionType, Name: name}
|
||||
row := model.CSManualEntryOption{Type: optionType, Name: name, GroupTag: groupTag}
|
||||
if err := r.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
if isDuplicateEntry(err) {
|
||||
return nil, ErrOptionExists
|
||||
@@ -247,8 +260,8 @@ func (r *Repository) CreateOption(ctx context.Context, optionType, name string)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// UpdateOption 重命名一个选项,重名返回 ErrOptionExists。
|
||||
func (r *Repository) UpdateOption(ctx context.Context, id uint64, name string) (*OptionDTO, error) {
|
||||
// UpdateOption 重命名一个选项并可更新组别标记,重名返回 ErrOptionExists。
|
||||
func (r *Repository) UpdateOption(ctx context.Context, id uint64, name, groupTag string) (*OptionDTO, error) {
|
||||
var row model.CSManualEntryOption
|
||||
if err := r.db.WithContext(ctx).First(&row, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -258,7 +271,10 @@ func (r *Repository) UpdateOption(ctx context.Context, id uint64, name string) (
|
||||
}
|
||||
result := r.db.WithContext(ctx).Model(&model.CSManualEntryOption{}).
|
||||
Where("id = ?", id).
|
||||
Update("name", name)
|
||||
Updates(map[string]any{
|
||||
"name": name,
|
||||
"group_tag": groupTag,
|
||||
})
|
||||
if result.Error != nil {
|
||||
if isDuplicateEntry(result.Error) {
|
||||
return nil, ErrOptionExists
|
||||
@@ -266,6 +282,7 @@ func (r *Repository) UpdateOption(ctx context.Context, id uint64, name string) (
|
||||
return nil, result.Error
|
||||
}
|
||||
row.Name = name
|
||||
row.GroupTag = groupTag
|
||||
dto := toOptionDTO(row)
|
||||
return &dto, nil
|
||||
}
|
||||
@@ -306,6 +323,7 @@ func toDTO(row model.CSManualEntry) EntryDTO {
|
||||
return EntryDTO{
|
||||
ID: row.ID,
|
||||
EntryNo: row.EntryNo,
|
||||
EntryGroup: row.EntryGroup,
|
||||
Platform: row.Platform,
|
||||
Operator: row.Operator,
|
||||
AdminID: row.AdminID,
|
||||
@@ -321,6 +339,7 @@ func toOptionDTO(row model.CSManualEntryOption) OptionDTO {
|
||||
ID: row.ID,
|
||||
Type: row.Type,
|
||||
Name: row.Name,
|
||||
GroupTag: row.GroupTag,
|
||||
SortOrder: row.SortOrder,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -61,36 +62,36 @@ func TestRepositoryOptionCRUDAndEntryPlatformOperator(t *testing.T) {
|
||||
}
|
||||
|
||||
// 1. 选项 CRUD
|
||||
platform, err := repo.CreateOption(ctx, OptionTypePlatform, "网易大神")
|
||||
platform, err := repo.CreateOption(ctx, OptionTypePlatform, "网易大神", "")
|
||||
if err != nil {
|
||||
t.Fatalf("创建平台选项失败: %v", err)
|
||||
}
|
||||
// 同类型第二个平台,用于验证重命名撞名(唯一键是 type+name,跨类型不算冲突)
|
||||
if _, err := repo.CreateOption(ctx, OptionTypePlatform, "交易猫"); err != nil {
|
||||
if _, err := repo.CreateOption(ctx, OptionTypePlatform, "交易猫", ""); err != nil {
|
||||
t.Fatalf("创建第二个平台选项失败: %v", err)
|
||||
}
|
||||
operator, err := repo.CreateOption(ctx, OptionTypeOperator, "技术测试")
|
||||
operator, err := repo.CreateOption(ctx, OptionTypeOperator, "技术测试", "")
|
||||
if err != nil {
|
||||
t.Fatalf("创建操作人选项失败: %v", err)
|
||||
}
|
||||
if _, err := repo.CreateOption(ctx, OptionTypePlatform, "网易大神"); !errors.Is(err, ErrOptionExists) {
|
||||
if _, err := repo.CreateOption(ctx, OptionTypePlatform, "网易大神", ""); !errors.Is(err, ErrOptionExists) {
|
||||
t.Fatalf("重复平台应返回 ErrOptionExists, got %v", err)
|
||||
}
|
||||
if _, err := repo.CreateOption(ctx, "unknown", "x"); !errors.Is(err, ErrOptionTypeInvalid) {
|
||||
if _, err := repo.CreateOption(ctx, "unknown", "x", ""); !errors.Is(err, ErrOptionTypeInvalid) {
|
||||
t.Fatalf("非法类型应返回 ErrOptionTypeInvalid, got %v", err)
|
||||
}
|
||||
// 与平台同名的操作人是允许的(不同 type)
|
||||
if _, err := repo.CreateOption(ctx, OptionTypeOperator, "网易大神"); err != nil {
|
||||
if _, err := repo.CreateOption(ctx, OptionTypeOperator, "网易大神", ""); err != nil {
|
||||
t.Fatalf("跨类型同名应允许: %v", err)
|
||||
}
|
||||
|
||||
if _, err := repo.UpdateOption(ctx, platform.ID, "网易"); err != nil {
|
||||
if _, err := repo.UpdateOption(ctx, platform.ID, "网易", ""); err != nil {
|
||||
t.Fatalf("重命名选项失败: %v", err)
|
||||
}
|
||||
if _, err := repo.UpdateOption(ctx, platform.ID, "交易猫"); !errors.Is(err, ErrOptionExists) {
|
||||
if _, err := repo.UpdateOption(ctx, platform.ID, "交易猫", ""); !errors.Is(err, ErrOptionExists) {
|
||||
t.Fatalf("重命名撞同类型已有名应返回 ErrOptionExists, got %v", err)
|
||||
}
|
||||
if _, err := repo.UpdateOption(ctx, 999999, "任何"); !errors.Is(err, ErrOptionNotFound) {
|
||||
if _, err := repo.UpdateOption(ctx, 999999, "任何", ""); !errors.Is(err, ErrOptionNotFound) {
|
||||
t.Fatalf("重命名不存在选项应返回 ErrOptionNotFound, got %v", err)
|
||||
}
|
||||
|
||||
@@ -106,7 +107,7 @@ func TestRepositoryOptionCRUDAndEntryPlatformOperator(t *testing.T) {
|
||||
}
|
||||
|
||||
// 2. 录单写入平台 / 操作人
|
||||
entry, err := repo.Create(ctx, admin.ID, "客服小张", "cs001", "202610130001", "网易", "技术测试", "备注1")
|
||||
entry, err := repo.Create(ctx, admin.ID, "客服小张", "cs001", "202610130001", EntryGroupCollect, "网易", "技术测试", "备注1")
|
||||
if err != nil {
|
||||
t.Fatalf("录单失败: %v", err)
|
||||
}
|
||||
@@ -115,7 +116,7 @@ func TestRepositoryOptionCRUDAndEntryPlatformOperator(t *testing.T) {
|
||||
}
|
||||
|
||||
// 修正录单的平台与操作人
|
||||
updated, err := repo.Update(ctx, entry.ID, "202610130001", "网易", "客服小李", "备注2")
|
||||
updated, err := repo.Update(ctx, entry.ID, "202610130001", EntryGroupCollect, "网易", "客服小李", "备注2")
|
||||
if err != nil {
|
||||
t.Fatalf("修正录单失败: %v", err)
|
||||
}
|
||||
@@ -154,3 +155,78 @@ func TestRepositoryOptionCRUDAndEntryPlatformOperator(t *testing.T) {
|
||||
t.Fatalf("删除选项后历史录单操作人 = %q, want 客服小李", after.Operator)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryEntryGroup(t *testing.T) {
|
||||
repo, db := newManualEntryTestRepo(t)
|
||||
ctx := t.Context()
|
||||
|
||||
admin := model.AdminUser{Username: "cs002", Nickname: "客服小李", PasswordHash: "x"}
|
||||
if err := db.Create(&admin).Error; err != nil {
|
||||
t.Fatalf("创建管理员失败: %v", err)
|
||||
}
|
||||
|
||||
// 同一编号:收号组登记一次
|
||||
collect, err := repo.Create(ctx, admin.ID, "客服小李", "cs002", "202611050473/0", EntryGroupCollect, "", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("收号组录单失败: %v", err)
|
||||
}
|
||||
if collect.EntryGroup != EntryGroupCollect {
|
||||
t.Fatalf("收号组别 = %q, want collect", collect.EntryGroup)
|
||||
}
|
||||
|
||||
// 同组重复登记应冲突
|
||||
if _, err := repo.Create(ctx, admin.ID, "客服小李", "cs002", "202611050473/0", EntryGroupCollect, "", "", ""); !errors.Is(err, ErrEntryNoExists) {
|
||||
t.Fatalf("同组重复编号应返回 ErrEntryNoExists, got %v", err)
|
||||
}
|
||||
|
||||
// 出号组登记同一编号应成功(编号绑定两个客服/两个组)
|
||||
sell, err := repo.Create(ctx, admin.ID, "客服小李", "cs002", "202611050473/0", EntryGroupSell, "", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("出号组录单失败: %v", err)
|
||||
}
|
||||
if sell.EntryGroup != EntryGroupSell {
|
||||
t.Fatalf("出号组别 = %q, want sell", sell.EntryGroup)
|
||||
}
|
||||
|
||||
// 按组别筛选
|
||||
list, err := repo.List(ctx, ListQuery{EntryNo: "202611050473/0", EntryGroup: EntryGroupSell, Page: 1, PageSize: 20})
|
||||
if err != nil {
|
||||
t.Fatalf("按组别查询失败: %v", err)
|
||||
}
|
||||
if list.Total != 1 || list.Items[0].EntryGroup != EntryGroupSell {
|
||||
t.Fatalf("按出号组查询 = %+v, want 1 条 sell 记录", list.Items)
|
||||
}
|
||||
|
||||
// 今日统计按组分列
|
||||
stats, err := repo.TodayAgentStats(ctx, time.Now())
|
||||
if err != nil {
|
||||
t.Fatalf("今日统计失败: %v", err)
|
||||
}
|
||||
if len(stats) != 1 {
|
||||
t.Fatalf("今日统计客服数 = %d, want 1", len(stats))
|
||||
}
|
||||
if stats[0].Count != 2 || stats[0].CollectCount != 1 || stats[0].SellCount != 1 {
|
||||
t.Fatalf("今日统计 = %+v, want 总量2 收号1 出号1", stats[0])
|
||||
}
|
||||
|
||||
// 修正组别:出号改成收号后与原收号记录冲突
|
||||
if _, err := repo.Update(ctx, sell.ID, "202611050473/0", EntryGroupCollect, "", "", ""); !errors.Is(err, ErrEntryNoExists) {
|
||||
t.Fatalf("改成冲突组别应返回 ErrEntryNoExists, got %v", err)
|
||||
}
|
||||
|
||||
// 操作人选项组别标记
|
||||
operator, err := repo.CreateOption(ctx, OptionTypeOperator, "测试操作", EntryGroupCollect)
|
||||
if err != nil {
|
||||
t.Fatalf("创建操作人选项失败: %v", err)
|
||||
}
|
||||
if operator.GroupTag != EntryGroupCollect {
|
||||
t.Fatalf("操作人组别标记 = %q, want collect", operator.GroupTag)
|
||||
}
|
||||
updated, err := repo.UpdateOption(ctx, operator.ID, "测试操作", EntryGroupSell)
|
||||
if err != nil {
|
||||
t.Fatalf("更新操作人组别失败: %v", err)
|
||||
}
|
||||
if updated.GroupTag != EntryGroupSell {
|
||||
t.Fatalf("更新后组别标记 = %q, want sell", updated.GroupTag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ func (s *Service) Create(ctx context.Context, adminID uint64, req CreateRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entryGroup, err := normalizeEntryGroup(req.EntryGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
platform, err := normalizeOptionName(req.Platform)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -45,7 +49,7 @@ func (s *Service) Create(ctx context.Context, adminID uint64, req CreateRequest)
|
||||
if adminName == "" {
|
||||
adminName = adminUsername
|
||||
}
|
||||
entry, err := s.repo.Create(ctx, adminID, adminName, adminUsername, entryNo, platform, operator, strings.TrimSpace(req.Remark))
|
||||
entry, err := s.repo.Create(ctx, adminID, adminName, adminUsername, entryNo, entryGroup, platform, operator, strings.TrimSpace(req.Remark))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -61,6 +65,10 @@ func (s *Service) Update(ctx context.Context, id uint64, req UpdateRequest) (*En
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entryGroup, err := normalizeEntryGroup(req.EntryGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
platform, err := normalizeOptionName(req.Platform)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -69,7 +77,7 @@ func (s *Service) Update(ctx context.Context, id uint64, req UpdateRequest) (*En
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.Update(ctx, id, entryNo, platform, operator, strings.TrimSpace(req.Remark))
|
||||
return s.repo.Update(ctx, id, entryNo, entryGroup, platform, operator, strings.TrimSpace(req.Remark))
|
||||
}
|
||||
|
||||
// Delete 管理员删除录单记录。
|
||||
@@ -120,7 +128,11 @@ func (s *Service) CreateOption(ctx context.Context, req CreateOptionRequest) (*O
|
||||
if name == "" {
|
||||
return nil, ErrOptionNameInvalid
|
||||
}
|
||||
return s.repo.CreateOption(ctx, optionType, name)
|
||||
groupTag, err := normalizeGroupTag(req.GroupTag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.CreateOption(ctx, optionType, name, groupTag)
|
||||
}
|
||||
|
||||
// UpdateOption 重命名平台 / 操作人选项。
|
||||
@@ -135,7 +147,11 @@ func (s *Service) UpdateOption(ctx context.Context, id uint64, req UpdateOptionR
|
||||
if name == "" {
|
||||
return nil, ErrOptionNameInvalid
|
||||
}
|
||||
return s.repo.UpdateOption(ctx, id, name)
|
||||
groupTag, err := normalizeGroupTag(req.GroupTag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.UpdateOption(ctx, id, name, groupTag)
|
||||
}
|
||||
|
||||
// DeleteOption 删除平台 / 操作人选项。
|
||||
@@ -173,3 +189,21 @@ func normalizeOptionName(raw string) (string, error) {
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// normalizeEntryGroup 规整录单组别:必填且仅允许 collect / sell。
|
||||
func normalizeEntryGroup(raw string) (string, error) {
|
||||
value := strings.TrimSpace(raw)
|
||||
if !IsValidEntryGroup(value) {
|
||||
return "", ErrEntryGroupInvalid
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// normalizeGroupTag 规整选项组别标记:允许为空(未分组),否则仅允许 collect / sell。
|
||||
func normalizeGroupTag(raw string) (string, error) {
|
||||
value := strings.TrimSpace(raw)
|
||||
if !IsValidGroupTag(value) {
|
||||
return "", ErrEntryGroupInvalid
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 客服手动录单:区分「收号 / 出号」两组客服。
|
||||
-- entry_group: collect=收号, sell=出号;唯一约束从 entry_no 放宽为 (entry_no, entry_group),
|
||||
-- 同一编号允许收号、出号各登记一次。
|
||||
-- cs_manual_entry_options.group_tag 给操作人选项打组别标记,录单选操作人后自动带出组别。
|
||||
ALTER TABLE cs_manual_entries
|
||||
ADD COLUMN entry_group VARCHAR(16) NOT NULL DEFAULT 'collect' COMMENT '录单组别: collect=收号, sell=出号' AFTER entry_no,
|
||||
DROP KEY uk_cs_manual_entries_entry_no,
|
||||
ADD UNIQUE KEY uk_cs_manual_entries_entry_no_group (entry_no, entry_group),
|
||||
ADD KEY idx_cs_manual_entries_group (entry_group);
|
||||
|
||||
ALTER TABLE cs_manual_entry_options
|
||||
ADD COLUMN group_tag VARCHAR(16) NOT NULL DEFAULT '' COMMENT '操作人组别标记: collect=收号, sell=出号, 空=未分组' AFTER name;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
-- 注意:若升级后同一编号已在收号/出号两组各登记过,Down 恢复 entry_no 唯一键会因重复失败,
|
||||
-- 需先人工清理重复编号后再执行回滚。
|
||||
ALTER TABLE cs_manual_entry_options
|
||||
DROP COLUMN group_tag;
|
||||
|
||||
ALTER TABLE cs_manual_entries
|
||||
DROP KEY idx_cs_manual_entries_group,
|
||||
DROP KEY uk_cs_manual_entries_entry_no_group,
|
||||
ADD UNIQUE KEY uk_cs_manual_entries_entry_no (entry_no),
|
||||
DROP COLUMN entry_group;
|
||||
Reference in New Issue
Block a user