feat: 实现套餐管理模块,包含套餐系列、双状态管理、废弃模型清理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m24s

- 新增套餐系列管理 (CRUD + 状态切换)
- 新增套餐管理 (CRUD + 启用/禁用 + 上架/下架双状态)
- 清理 8 个废弃分佣模型及对应数据库表
- Package 模型新增建议成本价、建议售价、上架状态字段
- 完整的 Store/Service/Handler 三层实现
- 包含单元测试和集成测试
- 归档 add-package-module change
- 新增多个 OpenSpec changes (订单支付、店铺套餐分配、一次性分佣、卡设备系列绑定)
This commit is contained in:
2026-01-27 19:55:47 +08:00
parent 30a0717316
commit 79c061b6fa
70 changed files with 7554 additions and 244 deletions

View File

@@ -14,7 +14,6 @@ import (
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// EmailPayload 邮件任务载荷(测试用)
type EmailPayload struct {
RequestID string `json:"request_id"`
To string `json:"to"`
@@ -23,21 +22,21 @@ type EmailPayload struct {
CC []string `json:"cc,omitempty"`
}
// TestTaskSubmit 测试任务提交
func TestTaskSubmit(t *testing.T) {
// 创建 Redis 客户端
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
// 清理测试数据
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
// 创建 Asynq 客户端
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = client.Close() }()
@@ -66,20 +65,21 @@ func TestTaskSubmit(t *testing.T) {
assert.Equal(t, constants.DefaultRetryMax, info.MaxRetry)
}
// TestTaskPriority 测试任务优先级
func TestTaskPriority(t *testing.T) {
// 创建 Redis 客户端
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
// 创建 Asynq 客户端
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = client.Close() }()
@@ -113,18 +113,21 @@ func TestTaskPriority(t *testing.T) {
}
}
// TestTaskRetry 测试任务重试机制
func TestTaskRetry(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = client.Close() }()
@@ -150,20 +153,22 @@ func TestTaskRetry(t *testing.T) {
assert.Equal(t, 30*time.Second, info.Timeout)
}
// TestTaskIdempotency 测试任务幂等性键
func TestTaskIdempotency(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
requestID := "idempotent-test-001"
requestID := "idempotent-test-" + time.Now().Format("20060102150405.000")
lockKey := constants.RedisTaskLockKey(requestID)
rdb.Del(ctx, lockKey)
t.Cleanup(func() { rdb.Del(ctx, lockKey) })
// 第一次设置锁(模拟任务开始执行)
result, err := rdb.SetNX(ctx, lockKey, "1", 24*time.Hour).Result()
require.NoError(t, err)
assert.True(t, result, "第一次设置锁应该成功")
@@ -185,15 +190,16 @@ func TestTaskIdempotency(t *testing.T) {
assert.LessOrEqual(t, ttl.Hours(), 24.0)
}
// TestTaskStatusTracking 测试任务状态跟踪
func TestTaskStatusTracking(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
taskID := "task-123456"
statusKey := constants.RedisTaskStatusKey(taskID)
@@ -217,18 +223,21 @@ func TestTaskStatusTracking(t *testing.T) {
assert.Greater(t, ttl.Hours(), 24.0*6)
}
// TestQueueInspection 测试队列检查
func TestQueueInspection(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = rdb.Close() }()
ctx := context.Background()
rdb.FlushDB(ctx)
cleanTestKeys(t, rdb, ctx)
client := asynq.NewClient(asynq.RedisClientOpt{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = client.Close() }()
@@ -249,9 +258,10 @@ func TestQueueInspection(t *testing.T) {
require.NoError(t, err)
}
// 创建 Inspector 检查队列
inspector := asynq.NewInspector(asynq.RedisClientOpt{
Addr: "localhost:6379",
Addr: testRedisAddr,
Password: testRedisPasswd,
DB: testRedisDB,
})
defer func() { _ = inspector.Close() }()
@@ -262,7 +272,6 @@ func TestQueueInspection(t *testing.T) {
assert.Equal(t, 0, info.Active)
}
// TestTaskSerialization 测试任务序列化
func TestTaskSerialization(t *testing.T) {
tests := []struct {
name string
@@ -291,17 +300,14 @@ func TestTaskSerialization(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 序列化
payloadBytes, err := sonic.Marshal(tt.payload)
require.NoError(t, err)
assert.NotEmpty(t, payloadBytes)
// 反序列化
var decoded EmailPayload
err = sonic.Unmarshal(payloadBytes, &decoded)
require.NoError(t, err)
// 验证
assert.Equal(t, tt.payload.RequestID, decoded.RequestID)
assert.Equal(t, tt.payload.To, decoded.To)
assert.Equal(t, tt.payload.Subject, decoded.Subject)
@@ -310,3 +316,19 @@ func TestTaskSerialization(t *testing.T) {
})
}
}
func cleanTestKeys(t *testing.T, rdb *redis.Client, ctx context.Context) {
t.Helper()
prefix := "test:task:" + t.Name() + ":"
keys, err := rdb.Keys(ctx, prefix+"*").Result()
if err != nil {
return
}
if len(keys) > 0 {
rdb.Del(ctx, keys...)
}
asynqKeys, _ := rdb.Keys(ctx, "asynq:*").Result()
if len(asynqKeys) > 0 {
rdb.Del(ctx, asynqKeys...)
}
}