// 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"
)

const TableNameTenantLedger = "tenant_ledgers"

// TenantLedger mapped from table <tenant_ledgers>
type TenantLedger 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_users.tenant_id 一致" json:"tenant_id"`                                                      // 租户ID：多租户隔离关键字段；必须与 tenant_users.tenant_id 一致
	UserID         int64                   `gorm:"column:user_id;type:bigint;not null;comment:用户ID：余额账户归属用户；对应 tenant_users.user_id" json:"user_id"`                                                                 // 用户ID：余额账户归属用户；对应 tenant_users.user_id
	OrderID        int64                   `gorm:"column:order_id;type:bigint;comment:关联订单ID：购买/退款类流水应关联 orders.id；非订单类可为空" json:"order_id"`                                                                         // 关联订单ID：购买/退款类流水应关联 orders.id；非订单类可为空
	Type           consts.TenantLedgerType `gorm:"column:type;type:character varying(32);not null;comment:流水类型：credit_topup/debit_purchase/credit_refund/freeze/unfreeze/adjustment；不同类型决定余额/冻结余额的变更方向" json:"type"` // 流水类型：credit_topup/debit_purchase/credit_refund/freeze/unfreeze/adjustment；不同类型决定余额/冻结余额的变更方向
	Amount         int64                   `gorm:"column:amount;type:bigint;not null;comment:流水金额：分/最小货币单位；通常为正数，方向由 type 决定（由业务层约束）" json:"amount"`                                                                 // 流水金额：分/最小货币单位；通常为正数，方向由 type 决定（由业务层约束）
	BalanceBefore  int64                   `gorm:"column:balance_before;type:bigint;not null;comment:变更前可用余额：用于审计与对账回放" json:"balance_before"`                                                                       // 变更前可用余额：用于审计与对账回放
	BalanceAfter   int64                   `gorm:"column:balance_after;type:bigint;not null;comment:变更后可用余额：用于审计与对账回放" json:"balance_after"`                                                                         // 变更后可用余额：用于审计与对账回放
	FrozenBefore   int64                   `gorm:"column:frozen_before;type:bigint;not null;comment:变更前冻结余额：用于审计与对账回放" json:"frozen_before"`                                                                         // 变更前冻结余额：用于审计与对账回放
	FrozenAfter    int64                   `gorm:"column:frozen_after;type:bigint;not null;comment:变更后冻结余额：用于审计与对账回放" json:"frozen_after"`                                                                           // 变更后冻结余额：用于审计与对账回放
	IdempotencyKey string                  `gorm:"column:idempotency_key;type:character varying(128);not null;comment:幂等键：同一租户同一用户同一业务操作固定；用于防止重复落账（建议由业务层生成）" json:"idempotency_key"`                               // 幂等键：同一租户同一用户同一业务操作固定；用于防止重复落账（建议由业务层生成）
	Remark         string                  `gorm:"column:remark;type:character varying(255);not null;comment:备注：业务说明/后台操作原因等；用于审计" json:"remark"`                                                                    // 备注：业务说明/后台操作原因等；用于审计
	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"`
}

// Quick operations without importing query package
// Update applies changed fields to the database using the default DB.
func (m *TenantLedger) Update(ctx context.Context) (gen.ResultInfo, error) {
	return Q.TenantLedger.WithContext(ctx).Updates(m)
}

// Save upserts the model using the default DB.
func (m *TenantLedger) Save(ctx context.Context) error {
	return Q.TenantLedger.WithContext(ctx).Save(m)
}

// Create inserts the model using the default DB.
func (m *TenantLedger) Create(ctx context.Context) error {
	return Q.TenantLedger.WithContext(ctx).Create(m)
}

// Delete removes the row represented by the model using the default DB.
func (m *TenantLedger) Delete(ctx context.Context) (gen.ResultInfo, error) {
	return Q.TenantLedger.WithContext(ctx).Delete(m)
}

// ForceDelete permanently deletes the row (ignores soft delete) using the default DB.
func (m *TenantLedger) ForceDelete(ctx context.Context) (gen.ResultInfo, error) {
	return Q.TenantLedger.WithContext(ctx).Unscoped().Delete(m)
}

// Reload reloads the model from database by its primary key and overwrites current fields.
func (m *TenantLedger) Reload(ctx context.Context) error {
	fresh, err := Q.TenantLedger.WithContext(ctx).GetByID(m.ID)
	if err != nil {
		return err
	}
	*m = *fresh
	return nil
}
