Files
ai_dianshang/server/internal/model/order.go

146 lines
6.0 KiB
Go
Raw Normal View History

2025-11-17 13:32:54 +08:00
package model
import (
"time"
)
// 订单状态常量
const (
OrderStatusPending = 1 // 未付款
OrderStatusPaid = 2 // 已付款
OrderStatusPreparing = 3 // 待发货
OrderStatusShipped = 4 // 已发货
OrderStatusReceiving = 5 // 待收货
OrderStatusCompleted = 6 // 已完成
OrderStatusCancelled = 7 // 已取消
OrderStatusReturning = 8 // 退货中
OrderStatusRefunded = 9 // 已退款
)
// Cart 购物车
type Cart struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
UserID uint `json:"user_id" gorm:"not null"`
ProductID uint `json:"product_id" gorm:"not null"`
SKUID *uint `json:"sku_id" gorm:"column:sk_uid"`
Quantity int `json:"quantity" gorm:"not null"`
Selected bool `json:"selected" gorm:"default:true"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联数据
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
SKU ProductSKU `json:"sku,omitempty" gorm:"foreignKey:SKUID"`
}
// Order 订单
type Order struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
OrderNo string `json:"order_no" gorm:"size:32;not null;uniqueIndex"`
UserID uint `json:"user_id" gorm:"not null"`
StoreID uint `json:"store_id" gorm:"default:1"`
TotalAmount float64 `json:"total_amount" gorm:"type:decimal(10,2);not null"`
PayAmount float64 `json:"pay_amount" gorm:"type:decimal(10,2);not null"`
DiscountAmount float64 `json:"discount_amount" gorm:"type:decimal(10,2);default:0.00"`
ShippingFee float64 `json:"shipping_fee" gorm:"type:decimal(10,2);default:0.00"`
CouponID *uint `json:"coupon_id"`
CouponAmount float64 `json:"coupon_amount" gorm:"type:decimal(10,2);default:0.00"`
PointsAmount float64 `json:"points_amount" gorm:"type:decimal(10,2);default:0.00"`
Status int `json:"status" gorm:"default:1"` // 统一状态1未付款,2已付款,3待发货,4已发货,5待收货,6已完成,7已取消,8退货中,9已退款
PayStatus int `json:"pay_status" gorm:"default:0"` // 兼容字段建议使用Status统一管理
PayMethod string `json:"pay_method" gorm:"size:20"`
WechatOutTradeNo string `json:"wechat_out_trade_no" gorm:"size:64;index"` // 微信支付商户订单号,每次支付请求唯一
WechatTransactionID string `json:"wechat_transaction_id" gorm:"size:64;index"` // 微信支付交易号,支付成功后由微信返回
PayTime *time.Time `json:"pay_time"`
ShippedAt *time.Time `json:"shipped_at" gorm:"column:shipped_at"`
ReceiveTime *time.Time `json:"receive_time"`
CancelTime *time.Time `json:"cancel_time"`
CancelReason string `json:"cancel_reason" gorm:"size:255"`
RefundAmount float64 `json:"refund_amount" gorm:"type:decimal(10,2);default:0.00"`
RefundReason string `json:"refund_reason" gorm:"size:255"`
RefundTime *time.Time `json:"refund_time"`
RefundedAt *time.Time `json:"refunded_at"`
ReceiverName string `json:"receiver_name" gorm:"size:50;not null"`
ReceiverPhone string `json:"receiver_phone" gorm:"size:20;not null"`
ReceiverAddress string `json:"receiver_address" gorm:"size:255;not null"`
LogisticsCompany string `json:"logistics_company" gorm:"column:logistics_company;size:50"`
LogisticsNo string `json:"logistics_no" gorm:"column:logistics_no;size:100"`
Remark string `json:"remark" gorm:"size:255"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联数据
User User `json:"user,omitempty" gorm:"foreignKey:UserID"`
Store Store `json:"store,omitempty" gorm:"foreignKey:StoreID"`
Coupon Coupon `json:"coupon,omitempty" gorm:"foreignKey:CouponID"`
OrderItems []OrderItem `json:"order_items,omitempty" gorm:"foreignKey:OrderID"`
}
// OrderItem 订单项
type OrderItem struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
OrderID uint `json:"order_id" gorm:"not null"`
ProductID uint `json:"product_id" gorm:"not null"`
SpecID *uint `json:"spec_id" gorm:"column:spec_id"`
SKUID *uint `json:"sku_id" gorm:"column:sk_uid"`
Quantity int `json:"quantity" gorm:"not null"`
Price float64 `json:"price" gorm:"type:decimal(10,2);not null"`
TotalPrice float64 `json:"total_price" gorm:"type:decimal(10,2);not null"`
ProductName string `json:"product_name" gorm:"size:100;not null"`
ProductImage string `json:"product_image" gorm:"size:255"`
SpecName string `json:"spec_name" gorm:"column:spec_name"`
SpecValue string `json:"spec_value" gorm:"column:spec_value"`
SpecInfo JSONMap `json:"spec_info" gorm:"type:json"`
IsCommented bool `json:"is_commented" gorm:"default:false"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 关联
Order Order `json:"order,omitempty" gorm:"foreignKey:OrderID"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
SKU ProductSKU `json:"sku,omitempty" gorm:"foreignKey:SKUID"`
}
// TableName 指定表名
func (Cart) TableName() string {
return "ai_carts"
}
// TableName 指定表名
func (Order) TableName() string {
return "ai_orders"
}
// TableName 指定表名
func (OrderItem) TableName() string {
return "order_items"
}
// GetStatusText 获取订单状态文本
func (o *Order) GetStatusText() string {
switch o.Status {
case OrderStatusPending:
return "未付款"
case OrderStatusPaid:
return "已付款"
case OrderStatusPreparing:
return "待发货"
case OrderStatusShipped:
return "已发货"
case OrderStatusReceiving:
return "待收货"
case OrderStatusCompleted:
return "已完成"
case OrderStatusCancelled:
return "已取消"
case OrderStatusReturning:
return "退货中"
case OrderStatusRefunded:
return "已退款"
default:
return "未知状态"
}
}