feat: add TenantLedger model and query generation

- Introduced TenantLedger model with fields for managing tenant transactions, including ID, TenantID, UserID, OrderID, transaction Type, Amount, and balance details.
- Implemented CRUD operations for TenantLedger with methods for Create, Update, Delete, and Reload.
- Generated query methods for TenantLedger to facilitate database interactions, including filtering, pagination, and aggregation functions.
- Established relationships with Order model for foreign key references.
This commit is contained in:
2025-12-18 13:12:26 +08:00
parent f93caefcb2
commit 1da84f2af3
42 changed files with 6468 additions and 265 deletions

View File

@@ -0,0 +1,72 @@
// 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/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.JSON `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
}