Files
junhong_cmp_fiber/internal/model/order.go
huang 1980c846f2 feat: 订单/资产充值/代理充值模型新增 PaymentConfigID 字段
- order.go: Order 模型新增 PaymentConfigID *uint(记录下单时使用的支付配置)
- asset_wallet.go: AssetRechargeRecord 新增 PaymentConfigID *uint
- agent_wallet.go: AgentRechargeRecord 新增 PaymentConfigID *uint
配置切换时旧订单仍按 payment_config_id 加载对应配置验签,解决竞态问题

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-03-16 23:29:52 +08:00

128 lines
6.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 model
import (
"time"
"gorm.io/gorm"
)
// Order 订单模型
// 记录套餐购买订单信息,支持单卡购买和设备购买两种类型
// 买家可以是个人客户(使用卡/设备钱包)或代理商(使用店铺钱包)
type Order struct {
gorm.Model
BaseModel `gorm:"embedded"`
// 订单基础信息
OrderNo string `gorm:"column:order_no;type:varchar(30);uniqueIndex:idx_order_no,where:deleted_at IS NULL;not null;comment:订单号(ORD+时间戳+6位随机数)" json:"order_no"`
OrderType string `gorm:"column:order_type;type:varchar(20);not null;comment:订单类型 single_card-单卡购买 device-设备购买" json:"order_type"`
// 买家信息
BuyerType string `gorm:"column:buyer_type;type:varchar(20);not null;comment:买家类型 personal-个人客户 agent-代理商" json:"buyer_type"`
BuyerID uint `gorm:"column:buyer_id;index:idx_order_buyer;not null;comment:买家ID(个人客户ID或店铺ID)" json:"buyer_id"`
// 关联资源
IotCardID *uint `gorm:"column:iot_card_id;index;comment:IoT卡ID(单卡购买时有值)" json:"iot_card_id,omitempty"`
DeviceID *uint `gorm:"column:device_id;index;comment:设备ID(设备购买时有值)" json:"device_id,omitempty"`
// 金额信息
TotalAmount int64 `gorm:"column:total_amount;type:bigint;not null;comment:订单总金额(分)" json:"total_amount"`
// 支付信息
PaymentMethod string `gorm:"column:payment_method;type:varchar(20);comment:支付方式 wallet-钱包 wechat-微信 alipay-支付宝" json:"payment_method"`
PaymentStatus int `gorm:"column:payment_status;type:int;default:1;not null;index:idx_order_payment_status;comment:支付状态 1-待支付 2-已支付 3-已取消 4-已退款" json:"payment_status"`
PaidAt *time.Time `gorm:"column:paid_at;comment:支付时间" json:"paid_at,omitempty"`
// 佣金信息
CommissionStatus int `gorm:"column:commission_status;type:int;default:1;not null;comment:佣金状态 1-待计算 2-已计算" json:"commission_status"`
CommissionConfigVersion int `gorm:"column:commission_config_version;type:int;default:0;comment:佣金配置版本(订单创建时快照)" json:"commission_config_version"`
SellerShopID *uint `gorm:"column:seller_shop_id;index;comment:销售店铺ID用于成本价差佣金计算" json:"seller_shop_id,omitempty"`
SellerCostPrice int64 `gorm:"column:seller_cost_price;type:bigint;default:0;comment:销售成本价(分,用于计算利润)" json:"seller_cost_price"`
SeriesID *uint `gorm:"column:series_id;index;comment:系列ID用于查询分配配置" json:"series_id,omitempty"`
// 代购信息
IsPurchaseOnBehalf bool `gorm:"column:is_purchase_on_behalf;type:boolean;default:false;comment:是否为代购订单" json:"is_purchase_on_behalf"`
// 操作者信息(谁下的单)
OperatorID *uint `gorm:"column:operator_id;index:idx_orders_operator_id;comment:操作者ID谁下的单" json:"operator_id,omitempty"`
OperatorType string `gorm:"column:operator_type;type:varchar(20);comment:操作者类型platform/agent" json:"operator_type,omitempty"`
// 实际支付金额(可能与订单金额不同,如代理代购场景)
ActualPaidAmount *int64 `gorm:"column:actual_paid_amount;type:bigint;comment:实际支付金额(分)" json:"actual_paid_amount,omitempty"`
// 订单角色(标识订单中的买卖关系)
PurchaseRole string `gorm:"column:purchase_role;type:varchar(50);index:idx_orders_purchase_role;comment:订单角色self_purchase/purchased_by_parent/purchased_by_platform/purchase_for_subordinate" json:"purchase_role,omitempty"`
// 订单超时信息
ExpiresAt *time.Time `gorm:"column:expires_at;comment:订单过期时间(NULL表示不过期)" json:"expires_at,omitempty"`
// 支付配置
PaymentConfigID *uint `gorm:"column:payment_config_id;index;comment:支付配置ID(关联tb_wechat_config.id)" json:"payment_config_id,omitempty"`
}
// TableName 指定表名
func (Order) TableName() string {
return "tb_order"
}
// 订单类型常量
const (
OrderTypeSingleCard = "single_card" // 单卡购买
OrderTypeDevice = "device" // 设备购买
)
// 买家类型常量
const (
BuyerTypePersonal = "personal" // 个人客户
BuyerTypeAgent = "agent" // 代理商
)
// 支付方式常量
const (
PaymentMethodWallet = "wallet" // 钱包支付
PaymentMethodWechat = "wechat" // 微信支付
PaymentMethodAlipay = "alipay" // 支付宝支付
PaymentMethodOffline = "offline" // 线下支付(仅平台代购使用)
)
// 支付状态常量
const (
PaymentStatusPending = 1 // 待支付
PaymentStatusPaid = 2 // 已支付
PaymentStatusCancelled = 3 // 已取消
PaymentStatusRefunded = 4 // 已退款
)
// 佣金状态常量
const (
CommissionStatusPending = 1 // 待计算
CommissionStatusCalculated = 2 // 已计算
)
// 订单角色常量
const (
PurchaseRoleSelfPurchase = "self_purchase" // 自己购买
PurchaseRolePurchasedByParent = "purchased_by_parent" // 上级代理购买
PurchaseRolePurchasedByPlatform = "purchased_by_platform" // 平台代购
PurchaseRolePurchaseForSubordinate = "purchase_for_subordinate" // 给下级购买
)
// OrderItem 订单明细模型
// 记录订单中购买的套餐明细,支持一个订单购买多个套餐
type OrderItem struct {
gorm.Model
BaseModel `gorm:"embedded"`
OrderID uint `gorm:"column:order_id;index:idx_order_item_order_id;not null;comment:订单ID" json:"order_id"`
PackageID uint `gorm:"column:package_id;index;not null;comment:套餐ID" json:"package_id"`
PackageName string `gorm:"column:package_name;type:varchar(100);not null;comment:套餐名称(快照)" json:"package_name"`
Quantity int `gorm:"column:quantity;type:int;default:1;not null;comment:数量" json:"quantity"`
UnitPrice int64 `gorm:"column:unit_price;type:bigint;not null;comment:单价(分)" json:"unit_price"`
Amount int64 `gorm:"column:amount;type:bigint;not null;comment:小计金额(分)" json:"amount"`
}
// TableName 指定表名
func (OrderItem) TableName() string {
return "tb_order_item"
}