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:
@@ -14,6 +14,7 @@ import (
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/fields"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
@@ -28,6 +29,17 @@ import (
|
||||
"go.ipao.vip/gen/types"
|
||||
)
|
||||
|
||||
func newOrderSnapshot(kind consts.OrderType, payload any) types.JSONType[fields.OrdersSnapshot] {
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil || len(b) == 0 {
|
||||
b = []byte("{}")
|
||||
}
|
||||
return types.NewJSONType(fields.OrdersSnapshot{
|
||||
Kind: string(kind),
|
||||
Data: b,
|
||||
})
|
||||
}
|
||||
|
||||
// AdminOrderExportCSV 租户管理员导出订单列表(CSV 文本)。
|
||||
func (s *order) AdminOrderExportCSV(ctx context.Context, tenantID int64, filter *dto.AdminOrderListFilter) (*dto.AdminOrderExportResponse, error) {
|
||||
if tenantID <= 0 {
|
||||
@@ -305,76 +317,6 @@ func (s *order) AdminBatchTopupUsers(
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PurchaseOrderSnapshot 为“内容购买订单”的下单快照(用于历史展示与争议审计)。
|
||||
type PurchaseOrderSnapshot struct {
|
||||
// ContentID 内容ID。
|
||||
ContentID int64 `json:"content_id"`
|
||||
// ContentTitle 内容标题(下单时快照,避免事后改名影响历史订单展示)。
|
||||
ContentTitle string `json:"content_title"`
|
||||
// ContentUserID 内容作者用户ID(用于审计与后续分成扩展)。
|
||||
ContentUserID int64 `json:"content_user_id"`
|
||||
// ContentVisibility 下单时的可见性快照。
|
||||
ContentVisibility consts.ContentVisibility `json:"content_visibility"`
|
||||
// PreviewSeconds 下单时的试看秒数快照。
|
||||
PreviewSeconds int32 `json:"preview_seconds"`
|
||||
// PreviewDownloadable 下单时的试看是否可下载快照(当前固定为 false)。
|
||||
PreviewDownloadable bool `json:"preview_downloadable"`
|
||||
// Currency 币种:当前固定 CNY(金额单位为分)。
|
||||
Currency consts.Currency `json:"currency"`
|
||||
// PriceAmount 基础价格(分)。
|
||||
PriceAmount int64 `json:"price_amount"`
|
||||
// DiscountType 折扣类型(none/percent/amount)。
|
||||
DiscountType consts.DiscountType `json:"discount_type"`
|
||||
// DiscountValue 折扣值(percent=0-100;amount=分)。
|
||||
DiscountValue int64 `json:"discount_value"`
|
||||
// DiscountStartAt 折扣开始时间(可选)。
|
||||
DiscountStartAt *time.Time `json:"discount_start_at,omitempty"`
|
||||
// DiscountEndAt 折扣结束时间(可选)。
|
||||
DiscountEndAt *time.Time `json:"discount_end_at,omitempty"`
|
||||
// AmountOriginal 原价金额(分)。
|
||||
AmountOriginal int64 `json:"amount_original"`
|
||||
// AmountDiscount 优惠金额(分)。
|
||||
AmountDiscount int64 `json:"amount_discount"`
|
||||
// AmountPaid 实付金额(分)。
|
||||
AmountPaid int64 `json:"amount_paid"`
|
||||
// PurchaseAt 下单时间(逻辑时间)。
|
||||
PurchaseAt time.Time `json:"purchase_at"`
|
||||
// PurchaseIdempotency 幂等键(可选)。
|
||||
PurchaseIdempotency string `json:"purchase_idempotency_key,omitempty"`
|
||||
// PurchasePricingNotes 价格计算补充说明(可选,便于排查争议)。
|
||||
PurchasePricingNotes string `json:"purchase_pricing_notes,omitempty"`
|
||||
}
|
||||
|
||||
// OrderItemSnapshot 为“订单明细”的内容快照。
|
||||
type OrderItemSnapshot struct {
|
||||
// ContentID 内容ID。
|
||||
ContentID int64 `json:"content_id"`
|
||||
// ContentTitle 内容标题快照。
|
||||
ContentTitle string `json:"content_title"`
|
||||
// ContentUserID 内容作者用户ID。
|
||||
ContentUserID int64 `json:"content_user_id"`
|
||||
// AmountPaid 该行实付金额(分)。
|
||||
AmountPaid int64 `json:"amount_paid"`
|
||||
}
|
||||
|
||||
// TopupOrderSnapshot 为“后台充值订单”的快照(用于审计与追责)。
|
||||
type TopupOrderSnapshot struct {
|
||||
// OperatorUserID 充值操作人用户ID(租户管理员)。
|
||||
OperatorUserID int64 `json:"operator_user_id"`
|
||||
// TargetUserID 充值目标用户ID(租户成员)。
|
||||
TargetUserID int64 `json:"target_user_id"`
|
||||
// Amount 充值金额(分)。
|
||||
Amount int64 `json:"amount"`
|
||||
// Currency 币种:当前固定 CNY(金额单位为分)。
|
||||
Currency consts.Currency `json:"currency"`
|
||||
// Reason 充值原因(可选,强烈建议填写用于审计)。
|
||||
Reason string `json:"reason,omitempty"`
|
||||
// IdempotencyKey 幂等键(可选)。
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
// TopupAt 充值时间(逻辑时间)。
|
||||
TopupAt time.Time `json:"topup_at"`
|
||||
}
|
||||
|
||||
// PurchaseContentParams 定义“租户内使用余额购买内容”的入参。
|
||||
type PurchaseContentParams struct {
|
||||
// TenantID 租户 ID(多租户隔离范围)。
|
||||
@@ -409,17 +351,6 @@ type order struct {
|
||||
ledger *ledger
|
||||
}
|
||||
|
||||
func marshalSnapshot(v any) types.JSON {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return types.JSON([]byte("{}"))
|
||||
}
|
||||
if len(b) == 0 {
|
||||
return types.JSON([]byte("{}"))
|
||||
}
|
||||
return types.JSON(b)
|
||||
}
|
||||
|
||||
// AdminTopupUser 租户管理员给租户成员充值(增加该租户下的可用余额)。
|
||||
func (s *order) AdminTopupUser(
|
||||
ctx context.Context,
|
||||
@@ -475,7 +406,7 @@ func (s *order) AdminTopupUser(
|
||||
}
|
||||
|
||||
// 先落订单(paid),再写入账本(credit_topup),确保“订单可追溯 + 账本可对账”。
|
||||
snapshot := marshalSnapshot(&TopupOrderSnapshot{
|
||||
snapshot := newOrderSnapshot(consts.OrderTypeTopup, &fields.OrdersTopupSnapshot{
|
||||
OperatorUserID: operatorUserID,
|
||||
TargetUserID: targetUserID,
|
||||
Amount: amount,
|
||||
@@ -1061,7 +992,7 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
discountEndAt = &t
|
||||
}
|
||||
|
||||
purchaseSnapshot := marshalSnapshot(&PurchaseOrderSnapshot{
|
||||
purchaseSnapshot := newOrderSnapshot(consts.OrderTypeContentPurchase, &fields.OrdersContentPurchaseSnapshot{
|
||||
ContentID: content.ID,
|
||||
ContentTitle: content.Title,
|
||||
ContentUserID: content.UserID,
|
||||
@@ -1080,7 +1011,7 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
PurchaseAt: now,
|
||||
PurchaseIdempotency: params.IdempotencyKey,
|
||||
})
|
||||
itemSnapshot := marshalSnapshot(&OrderItemSnapshot{
|
||||
itemSnapshot := types.NewJSONType(fields.OrderItemsSnapshot{
|
||||
ContentID: content.ID,
|
||||
ContentTitle: content.Title,
|
||||
ContentUserID: content.UserID,
|
||||
@@ -1303,7 +1234,7 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
t := price.DiscountEndAt
|
||||
discountEndAt = &t
|
||||
}
|
||||
purchaseSnapshot := marshalSnapshot(&PurchaseOrderSnapshot{
|
||||
purchaseSnapshot := newOrderSnapshot(consts.OrderTypeContentPurchase, &fields.OrdersContentPurchaseSnapshot{
|
||||
ContentID: content.ID,
|
||||
ContentTitle: content.Title,
|
||||
ContentUserID: content.UserID,
|
||||
@@ -1321,7 +1252,7 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
|
||||
AmountPaid: amountPaid,
|
||||
PurchaseAt: now,
|
||||
})
|
||||
itemSnapshot := marshalSnapshot(&OrderItemSnapshot{
|
||||
itemSnapshot := types.NewJSONType(fields.OrderItemsSnapshot{
|
||||
ContentID: content.ID,
|
||||
ContentTitle: content.Title,
|
||||
ContentUserID: content.UserID,
|
||||
|
||||
Reference in New Issue
Block a user