feat: Refactor order snapshot handling and introduce structured snapshot types

- 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.
This commit is contained in:
2025-12-22 21:11:33 +08:00
parent 618fe116ba
commit 683965ae39
8 changed files with 239 additions and 171 deletions

View File

@@ -8,6 +8,8 @@ import (
"context"
"time"
"quyun/v2/database/fields"
"go.ipao.vip/gen"
"go.ipao.vip/gen/types"
)
@@ -16,18 +18,18 @@ const TableNameOrderItem = "order_items"
// OrderItem mapped from table <order_items>
type OrderItem 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多租户隔离关键字段必须与 orders.tenant_id 一致" json:"tenant_id"` // 租户ID多租户隔离关键字段必须与 orders.tenant_id 一致
UserID int64 `gorm:"column:user_id;type:bigint;not null;comment:用户ID下单用户buyer冗余字段用于查询加速与审计" json:"user_id"` // 用户ID下单用户buyer冗余字段用于查询加速与审计
OrderID int64 `gorm:"column:order_id;type:bigint;not null;comment:订单ID关联 orders.id用于聚合订单明细" json:"order_id"` // 订单ID关联 orders.id用于聚合订单明细
ContentID int64 `gorm:"column:content_id;type:bigint;not null;comment:内容ID关联 contents.id用于生成/撤销 content_access" json:"content_id"` // 内容ID关联 contents.id用于生成/撤销 content_access
ContentUserID int64 `gorm:"column:content_user_id;type:bigint;not null;comment:内容作者用户ID用于后续分成/对账扩展;当前可为 0 或写入内容创建者" json:"content_user_id"` // 内容作者用户ID用于后续分成/对账扩展;当前可为 0 或写入内容创建者
AmountPaid int64 `gorm:"column:amount_paid;type:bigint;not null;comment:该行实付金额:分;通常等于订单 amount_paid单内容场景" json:"amount_paid"` // 该行实付金额:分;通常等于订单 amount_paid单内容场景
Snapshot types.JSON `gorm:"column:snapshot;type:jsonb;not null;default:{};comment:内容快照JSON建议包含 title/price/discount 等,用于历史展示与审计" json:"snapshot"` // 内容快照JSON建议包含 title/price/discount 等,用于历史展示与审计
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()
Order *Order `gorm:"foreignKey:OrderID;references:ID" json:"order,omitempty"`
Content *Content `gorm:"foreignKey:ContentID;references:ID" json:"content,omitempty"`
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多租户隔离关键字段必须与 orders.tenant_id 一致" json:"tenant_id"` // 租户ID多租户隔离关键字段必须与 orders.tenant_id 一致
UserID int64 `gorm:"column:user_id;type:bigint;not null;comment:用户ID下单用户buyer冗余字段用于查询加速与审计" json:"user_id"` // 用户ID下单用户buyer冗余字段用于查询加速与审计
OrderID int64 `gorm:"column:order_id;type:bigint;not null;comment:订单ID关联 orders.id用于聚合订单明细" json:"order_id"` // 订单ID关联 orders.id用于聚合订单明细
ContentID int64 `gorm:"column:content_id;type:bigint;not null;comment:内容ID关联 contents.id用于生成/撤销 content_access" json:"content_id"` // 内容ID关联 contents.id用于生成/撤销 content_access
ContentUserID int64 `gorm:"column:content_user_id;type:bigint;not null;comment:内容作者用户ID用于后续分成/对账扩展;当前可为 0 或写入内容创建者" json:"content_user_id"` // 内容作者用户ID用于后续分成/对账扩展;当前可为 0 或写入内容创建者
AmountPaid int64 `gorm:"column:amount_paid;type:bigint;not null;comment:该行实付金额:分;通常等于订单 amount_paid单内容场景" json:"amount_paid"` // 该行实付金额:分;通常等于订单 amount_paid单内容场景
Snapshot types.JSONType[fields.OrderItemsSnapshot] `gorm:"column:snapshot;type:jsonb;not null;default:{};comment:内容快照JSON建议包含 title/price/discount 等,用于历史展示与审计" json:"snapshot"` // 内容快照JSON建议包含 title/price/discount 等,用于历史展示与审计
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()
Order *Order `gorm:"foreignKey:OrderID;references:ID" json:"order,omitempty"`
Content *Content `gorm:"foreignKey:ContentID;references:ID" json:"content,omitempty"`
}
// Quick operations without importing query package