Files
junhong_cmp_fiber/pkg/constants/wallet.go
huang e661b59bb9
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m58s
feat: 实现订单超时自动取消功能,支持钱包余额解冻和 Asynq Scheduler 统一调度
- 新增 expires_at 字段和复合索引,待支付订单 30 分钟超时自动取消
- 实现 cancelOrder/unfreezeWalletForCancel 钱包余额解冻逻辑
- 创建 Asynq 定时任务(order_expire/alert_check/data_cleanup)
- 将原有 time.Ticker 轮询迁移至 Asynq Scheduler 统一调度
- 同步 delta specs 到 main specs 并归档变更
2026-02-28 17:16:15 +08:00

198 lines
7.0 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"
// ========================================
// 钱包系统常量定义
// ========================================
// ========== 代理钱包常量 ==========
// 代理钱包类型
const (
AgentWalletTypeMain = "main" // 主钱包
AgentWalletTypeCommission = "commission" // 分佣钱包
)
// 代理钱包状态
const (
AgentWalletStatusNormal = 1 // 正常
AgentWalletStatusFrozen = 2 // 冻结
AgentWalletStatusClosed = 3 // 关闭
)
// 代理钱包交易类型
const (
AgentTransactionTypeRecharge = "recharge" // 充值
AgentTransactionTypeDeduct = "deduct" // 扣款
AgentTransactionTypeRefund = "refund" // 退款
AgentTransactionTypeCommission = "commission" // 分佣
AgentTransactionTypeWithdrawal = "withdrawal" // 提现
)
// 代理钱包交易子类型(当 transaction_type = "deduct" 用于订单支付时)
const (
WalletTransactionSubtypeSelfPurchase = "self_purchase" // 自购
WalletTransactionSubtypePurchaseForSubordinate = "purchase_for_subordinate" // 给下级代理购买
)
// 代理充值订单号前缀
const (
AgentRechargeOrderPrefix = "ARCH" // 代理充值订单号前缀
)
// 代理充值金额限制(单位:分)
const (
AgentRechargeMinAmount = 10000 // 最小充值金额100元
AgentRechargeMaxAmount = 100000000 // 最大充值金额1000000元
)
// ========== 卡钱包常量 ==========
// 卡钱包资源类型
const (
CardWalletResourceTypeIotCard = "iot_card" // 物联网卡钱包
CardWalletResourceTypeDevice = "device" // 设备钱包(多卡共享)
)
// 卡钱包状态
const (
CardWalletStatusNormal = 1 // 正常
CardWalletStatusFrozen = 2 // 冻结
CardWalletStatusClosed = 3 // 关闭
)
// 卡钱包交易类型
const (
CardTransactionTypeRecharge = "recharge" // 充值
CardTransactionTypeDeduct = "deduct" // 扣款
CardTransactionTypeRefund = "refund" // 退款
)
// 卡充值订单号前缀
const (
CardRechargeOrderPrefix = "CRCH" // 卡充值订单号前缀
)
// 卡充值金额限制(单位:分)
const (
CardRechargeMinAmount = 100 // 最小充值金额1元
CardRechargeMaxAmount = 10000000 // 最大充值金额100000元
)
// ========== 通用常量 ==========
// 交易状态(代理钱包和卡钱包通用)
const (
TransactionStatusSuccess = 1 // 成功
TransactionStatusFailed = 2 // 失败
TransactionStatusProcessing = 3 // 处理中
)
// 充值状态(代理钱包和卡钱包通用)
const (
RechargeStatusPending = 1 // 待支付
RechargeStatusPaid = 2 // 已支付
RechargeStatusCompleted = 3 // 已完成
RechargeStatusClosed = 4 // 已关闭
RechargeStatusRefunded = 5 // 已退款
)
// 充值支付方式
const (
RechargeMethodAlipay = "alipay" // 支付宝
RechargeMethodWechat = "wechat" // 微信
RechargeMethodBank = "bank" // 银行转账(仅代理钱包支持)
RechargeMethodOffline = "offline" // 线下(仅代理钱包支持)
)
// 关联业务类型
const (
ReferenceTypeOrder = "order" // 订单
ReferenceTypeCommission = "commission" // 分佣
ReferenceTypeWithdrawal = "withdrawal" // 提现
ReferenceTypeTopup = "topup" // 充值
)
// ========== Redis Key 生成函数 ==========
// RedisAgentWalletBalanceKey 代理钱包余额缓存 Key
// 格式agent_wallet:balance:{shop_id}:{wallet_type}
// TTL300 秒5 分钟)
func RedisAgentWalletBalanceKey(shopID uint, walletType string) string {
return fmt.Sprintf("agent_wallet:balance:%d:%s", shopID, walletType)
}
// RedisAgentWalletLockKey 代理钱包分布式锁 Key
// 格式agent_wallet:lock:{shop_id}:{wallet_type}
// TTL10 秒
func RedisAgentWalletLockKey(shopID uint, walletType string) string {
return fmt.Sprintf("agent_wallet:lock:%d:%s", shopID, walletType)
}
// RedisCardWalletBalanceKey 卡钱包余额缓存 Key
// 格式card_wallet:balance:{resource_type}:{resource_id}
// TTL180 秒3 分钟)
func RedisCardWalletBalanceKey(resourceType string, resourceID uint) string {
return fmt.Sprintf("card_wallet:balance:%s:%d", resourceType, resourceID)
}
// RedisCardWalletLockKey 卡钱包分布式锁 Key
// 格式card_wallet:lock:{resource_type}:{resource_id}
// TTL10 秒
func RedisCardWalletLockKey(resourceType string, resourceID uint) string {
return fmt.Sprintf("card_wallet:lock:%s:%d", resourceType, resourceID)
}
// ========== 兼容性别名(待清理)==========
// 以下常量保留用于向后兼容,待旧代码清理后删除
// WalletTypeMain 主钱包(已废弃,使用 AgentWalletTypeMain
const WalletTypeMain = AgentWalletTypeMain
// WalletTypeCommission 分佣钱包(已废弃,使用 AgentWalletTypeCommission
const WalletTypeCommission = AgentWalletTypeCommission
// WalletResourceTypeIotCard 物联网卡钱包(已废弃,使用 CardWalletResourceTypeIotCard
const WalletResourceTypeIotCard = CardWalletResourceTypeIotCard
// WalletResourceTypeDevice 设备钱包(已废弃,使用 CardWalletResourceTypeDevice
const WalletResourceTypeDevice = CardWalletResourceTypeDevice
// WalletResourceTypeShop 店铺钱包(已废弃,代理钱包不再使用 resource_type
const WalletResourceTypeShop = "shop"
// WalletStatusNormal 钱包状态-正常(已废弃,使用 AgentWalletStatusNormal 或 CardWalletStatusNormal
const WalletStatusNormal = AgentWalletStatusNormal
// WalletStatusFrozen 钱包状态-冻结(已废弃,使用 AgentWalletStatusFrozen 或 CardWalletStatusFrozen
const WalletStatusFrozen = AgentWalletStatusFrozen
// WalletStatusClosed 钱包状态-关闭(已废弃,使用 AgentWalletStatusClosed 或 CardWalletStatusClosed
const WalletStatusClosed = AgentWalletStatusClosed
// TransactionTypeRecharge 交易类型-充值(已废弃,使用 AgentTransactionTypeRecharge 或 CardTransactionTypeRecharge
const TransactionTypeRecharge = AgentTransactionTypeRecharge
// TransactionTypeDeduct 交易类型-扣款(已废弃,使用 AgentTransactionTypeDeduct 或 CardTransactionTypeDeduct
const TransactionTypeDeduct = AgentTransactionTypeDeduct
// TransactionTypeRefund 交易类型-退款(已废弃,使用 AgentTransactionTypeRefund 或 CardTransactionTypeRefund
const TransactionTypeRefund = AgentTransactionTypeRefund
// TransactionTypeCommission 交易类型-分佣(已废弃,使用 AgentTransactionTypeCommission
const TransactionTypeCommission = AgentTransactionTypeCommission
// TransactionTypeWithdrawal 交易类型-提现(已废弃,使用 AgentTransactionTypeWithdrawal
const TransactionTypeWithdrawal = AgentTransactionTypeWithdrawal
// RechargeOrderPrefix 充值订单号前缀(已废弃,使用 AgentRechargeOrderPrefix 或 CardRechargeOrderPrefix
const RechargeOrderPrefix = "RCH"
// RechargeMinAmount 最小充值金额(已废弃,使用 AgentRechargeMinAmount 或 CardRechargeMinAmount
const RechargeMinAmount = CardRechargeMinAmount
// RechargeMaxAmount 最大充值金额(已废弃,使用 AgentRechargeMaxAmount 或 CardRechargeMaxAmount
const RechargeMaxAmount = CardRechargeMaxAmount