Files
junhong_cmp_fiber/pkg/constants/redis.go
huang 6e2dc325d7 新增钱包、换卡、标签系统的数据模型和规范
本次提交完成 add-wallet-transfer-tag-models 提案的实施和归档:

## 新增功能模块
- 钱包系统:用户/代理钱包管理,支持充值、扣款、退款、乐观锁防并发
- 换卡记录:物联卡更换历史追溯,包含套餐快照(JSONB)
- 标签系统:设备/IoT卡/号卡的统一标签管理
- 运营商渠道:四大运营商(CMCC/CUCC/CTCC/CBN)的渠道管理

## 数据库变更
- 新增 6 张表:tb_wallet, tb_wallet_transaction, tb_recharge_record, tb_card_replacement_record, tb_tag, tb_resource_tag
- 修改 2 张表:tb_carrier(新增渠道字段), tb_order(新增混合支付字段)
- 迁移版本:v6 → v7(执行时间 282.5ms)

## 代码变更
- 新增 8 个 Go 模型(符合统一规范:gorm.Model + BaseModel)
- 新增 40+ 个常量定义(含完整中文注释)
- 新增 7 个 Redis Key 生成函数
- 修复模型规范:移除重复字段,统一使用 gorm.Model 嵌入

## 文档变更
- 新增 3 个业务文档:数据模型设计、字段说明、迁移验证报告
- 更新 AGENTS.md:新增 Model 模型规范和常量注释规范
- 新增 4 个 OpenSpec 规范:wallet, carrier, card-replacement, tag
- 更新 1 个 OpenSpec 规范:iot-order(支持混合支付)

## 验证通过
-  LSP 诊断:所有模型和常量文件无错误
-  OpenSpec 验证:openspec validate --strict 通过
-  迁移执行:表结构创建成功,索引正确
-  提案归档:2026-01-13-add-wallet-transfer-tag-models

变更文件统计:29 个文件,新增 3682 行
2026-01-13 15:47:32 +08:00

99 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package constants
import "fmt"
// RedisAuthTokenKey 生成认证令牌的 Redis 键
func RedisAuthTokenKey(token string) string {
return fmt.Sprintf("auth:token:%s", token)
}
// RedisRateLimitKey 生成限流的 Redis 键
func RedisRateLimitKey(ip string) string {
return fmt.Sprintf("ratelimit:%s", ip)
}
// RedisTaskLockKey 生成任务锁的 Redis 键
// 用途:幂等性控制,防止重复执行
// 过期时间24 小时
func RedisTaskLockKey(requestID string) string {
return fmt.Sprintf("task:lock:%s", requestID)
}
// RedisTaskStatusKey 生成任务状态的 Redis 键
// 用途:存储任务执行状态
// 过期时间7 天
func RedisTaskStatusKey(taskID string) string {
return fmt.Sprintf("task:status:%s", taskID)
}
// RedisShopSubordinatesKey 生成店铺下级 ID 列表的 Redis 键
// 用途:缓存递归查询的下级店铺 ID 列表
// 过期时间30 分钟
func RedisShopSubordinatesKey(shopID uint) string {
return fmt.Sprintf("shop:subordinates:%d", shopID)
}
// RedisVerificationCodeKey 生成验证码的 Redis 键
// 用途:存储手机验证码
// 过期时间5 分钟
func RedisVerificationCodeKey(phone string) string {
return fmt.Sprintf("verification:code:%s", phone)
}
// RedisVerificationCodeLimitKey 生成验证码发送频率限制的 Redis 键
// 用途:限制验证码发送频率
// 过期时间60 秒
func RedisVerificationCodeLimitKey(phone string) string {
return fmt.Sprintf("verification:limit:%s", phone)
}
// ========================================
// 钱包相关 Redis Key
// ========================================
// RedisWalletLockKey 生成钱包操作锁的 Redis 键
// 用途:钱包余额变更时的分布式锁,防止并发冲突
// 过期时间10 秒
func RedisWalletLockKey(walletID uint) string {
return fmt.Sprintf("wallet:lock:%d", walletID)
}
// RedisWalletBalanceKey 生成钱包余额缓存的 Redis 键
// 用途:缓存钱包余额,减少数据库查询
// 过期时间5 分钟
func RedisWalletBalanceKey(walletID uint) string {
return fmt.Sprintf("wallet:balance:%d", walletID)
}
// RedisRechargeOrderKey 生成充值订单缓存的 Redis 键
// 用途:充值订单状态查询缓存
// 过期时间1 小时
func RedisRechargeOrderKey(rechargeNo string) string {
return fmt.Sprintf("recharge:order:%s", rechargeNo)
}
// RedisWalletTransactionKey 生成钱包交易幂等性的 Redis 键
// 用途:防止重复交易
// 过期时间24 小时
func RedisWalletTransactionKey(requestID string) string {
return fmt.Sprintf("wallet:transaction:%s", requestID)
}
// ========================================
// 标签相关 Redis Key
// ========================================
// RedisTagCacheKey 生成标签缓存的 Redis 键
// 用途:缓存热门标签列表
// 过期时间1 小时
func RedisTagCacheKey() string {
return "tag:cache:list"
}
// RedisResourceTagsKey 生成资源标签关联缓存的 Redis 键
// 用途:缓存资源的标签列表
// 过期时间30 分钟
func RedisResourceTagsKey(resourceType string, resourceID uint) string {
return fmt.Sprintf("resource:tags:%s:%d", resourceType, resourceID)
}