- Added new structured snapshot types for orders and order items to improve data integrity and clarity. - Updated the Order and OrderItem models to use the new JSONType for snapshots. - Refactored tests to accommodate the new snapshot structure, ensuring compatibility with legacy data. - Enhanced the OrdersSnapshot struct to support multiple snapshot types and maintain backward compatibility. - Introduced new fields for order items and orders to capture detailed snapshot information for auditing and historical display.
74 lines
7.9 KiB
Go
74 lines
7.9 KiB
Go
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
|
||
|
||
package models
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"quyun/v2/database/fields"
|
||
"quyun/v2/pkg/consts"
|
||
|
||
"go.ipao.vip/gen"
|
||
"go.ipao.vip/gen/types"
|
||
)
|
||
|
||
const TableNameOrder = "orders"
|
||
|
||
// Order mapped from table <orders>
|
||
type Order struct {
|
||
ID int64 `gorm:"column:id;type:bigint;primaryKey;autoIncrement:true;comment:主键ID:自增;用于关联订单明细、账本流水、权益等" json:"id"` // 主键ID:自增;用于关联订单明细、账本流水、权益等
|
||
TenantID int64 `gorm:"column:tenant_id;type:bigint;not null;comment:租户ID:多租户隔离关键字段;所有查询/写入必须限定 tenant_id" json:"tenant_id"` // 租户ID:多租户隔离关键字段;所有查询/写入必须限定 tenant_id
|
||
UserID int64 `gorm:"column:user_id;type:bigint;not null;comment:用户ID:下单用户(buyer);余额扣款与权益归属以该 user_id 为准" json:"user_id"` // 用户ID:下单用户(buyer);余额扣款与权益归属以该 user_id 为准
|
||
Type consts.OrderType `gorm:"column:type;type:character varying(32);not null;default:content_purchase;comment:订单类型:content_purchase(购买内容)/topup(充值)等;当前默认 content_purchase" json:"type"` // 订单类型:content_purchase(购买内容)/topup(充值)等;当前默认 content_purchase
|
||
Status consts.OrderStatus `gorm:"column:status;type:character varying(32);not null;default:created;comment:订单状态:created/paid/refunding/refunded/canceled/failed;状态变更需与账本/权益保持一致" json:"status"` // 订单状态:created/paid/refunding/refunded/canceled/failed;状态变更需与账本/权益保持一致
|
||
Currency consts.Currency `gorm:"column:currency;type:character varying(16);not null;default:CNY;comment:币种:当前固定 CNY;金额单位为分" json:"currency"` // 币种:当前固定 CNY;金额单位为分
|
||
AmountOriginal int64 `gorm:"column:amount_original;type:bigint;not null;comment:原价金额:分;未折扣前金额(用于展示与对账)" json:"amount_original"` // 原价金额:分;未折扣前金额(用于展示与对账)
|
||
AmountDiscount int64 `gorm:"column:amount_discount;type:bigint;not null;comment:优惠金额:分;amount_paid = amount_original - amount_discount(下单时快照)" json:"amount_discount"` // 优惠金额:分;amount_paid = amount_original - amount_discount(下单时快照)
|
||
AmountPaid int64 `gorm:"column:amount_paid;type:bigint;not null;comment:实付金额:分;从租户内余额扣款的金额(下单时快照)" json:"amount_paid"` // 实付金额:分;从租户内余额扣款的金额(下单时快照)
|
||
Snapshot types.JSONType[fields.OrdersSnapshot] `gorm:"column:snapshot;type:jsonb;not null;default:{};comment:订单快照:JSON;建议包含 content 标题/定价/折扣、请求来源等,避免改价影响历史展示" json:"snapshot"` // 订单快照:JSON;建议包含 content 标题/定价/折扣、请求来源等,避免改价影响历史展示
|
||
IdempotencyKey string `gorm:"column:idempotency_key;type:character varying(128);not null;comment:幂等键:同一租户同一用户同一业务请求可用;用于防重复下单/重复扣款(建议由客户端生成)" json:"idempotency_key"` // 幂等键:同一租户同一用户同一业务请求可用;用于防重复下单/重复扣款(建议由客户端生成)
|
||
PaidAt time.Time `gorm:"column:paid_at;type:timestamp with time zone;comment:支付/扣款完成时间:余额支付在 debit_purchase 成功后写入" json:"paid_at"` // 支付/扣款完成时间:余额支付在 debit_purchase 成功后写入
|
||
RefundedAt time.Time `gorm:"column:refunded_at;type:timestamp with time zone;comment:退款完成时间:退款落账成功后写入" json:"refunded_at"` // 退款完成时间:退款落账成功后写入
|
||
RefundForced bool `gorm:"column:refund_forced;type:boolean;not null;comment:是否强制退款:true 表示租户管理侧绕过时间窗执行退款(需审计)" json:"refund_forced"` // 是否强制退款:true 表示租户管理侧绕过时间窗执行退款(需审计)
|
||
RefundOperatorUserID int64 `gorm:"column:refund_operator_user_id;type:bigint;comment:退款操作人用户ID:租户管理员/系统;用于审计与追责" json:"refund_operator_user_id"` // 退款操作人用户ID:租户管理员/系统;用于审计与追责
|
||
RefundReason string `gorm:"column:refund_reason;type:character varying(255);not null;comment:退款原因:后台/用户发起退款的原因说明;用于审计" json:"refund_reason"` // 退款原因:后台/用户发起退款的原因说明;用于审计
|
||
CreatedAt time.Time `gorm:"column:created_at;type:timestamp with time zone;not null;default:now();comment:创建时间:默认 now();用于审计与排序" json:"created_at"` // 创建时间:默认 now();用于审计与排序
|
||
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp with time zone;not null;default:now();comment:更新时间:默认 now();状态变更/退款写入时更新" json:"updated_at"` // 更新时间:默认 now();状态变更/退款写入时更新
|
||
Items []*OrderItem `gorm:"foreignKey:OrderID;references:ID" json:"items,omitempty"`
|
||
}
|
||
|
||
// Quick operations without importing query package
|
||
// Update applies changed fields to the database using the default DB.
|
||
func (m *Order) Update(ctx context.Context) (gen.ResultInfo, error) {
|
||
return Q.Order.WithContext(ctx).Updates(m)
|
||
}
|
||
|
||
// Save upserts the model using the default DB.
|
||
func (m *Order) Save(ctx context.Context) error { return Q.Order.WithContext(ctx).Save(m) }
|
||
|
||
// Create inserts the model using the default DB.
|
||
func (m *Order) Create(ctx context.Context) error { return Q.Order.WithContext(ctx).Create(m) }
|
||
|
||
// Delete removes the row represented by the model using the default DB.
|
||
func (m *Order) Delete(ctx context.Context) (gen.ResultInfo, error) {
|
||
return Q.Order.WithContext(ctx).Delete(m)
|
||
}
|
||
|
||
// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
|
||
func (m *Order) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
|
||
return Q.Order.WithContext(ctx).Unscoped().Delete(m)
|
||
}
|
||
|
||
// Reload reloads the model from database by its primary key and overwrites current fields.
|
||
func (m *Order) Reload(ctx context.Context) error {
|
||
fresh, err := Q.Order.WithContext(ctx).GetByID(m.ID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
*m = *fresh
|
||
return nil
|
||
}
|