package card_redeem_account

import (
	"context"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/utility/config"

	"github.com/gogf/gf/v2/database/gdb"
)

// AddHistory 添加操作历史
func (a *sCardRedeemAccount) AddHistory(ctx context.Context, input *model.RedeemAccountAddHistory, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemAccountHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Insert(do.V1CardRedeemAccountHistory{
		AccountId:        input.AccountId,
		Amount:           input.Amount,
		Balance:          input.Balance,
		EffectiveBalance: input.EffectiveBalance,
		AccountName:      input.AccountName,
		Cookie:           input.Cookie,
		OrderNo:          input.OrderNo,
		Remark:           input.Remark,
		OperationStatus:  input.OperationStatus,
	})
	return err
}

// GetWalletHistory 获取单账户钱包明细
func (a *sCardRedeemAccount) GetWalletHistory(ctx context.Context, input *model.RedeemAccountHistoryQueryInput) (total int, data []*entity.V1CardRedeemAccountHistory, err error) {
	data = make([]*entity.V1CardRedeemAccountHistory, 0)
	m := dao.V1CardRedeemAccountHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	if input.AccountId != "" {
		m = m.Where(dao.V1CardRedeemAccountHistory.Columns().AccountId, input.AccountId)
	}
	err = m.Page(input.Current, input.PageSize).
		OrderDesc(dao.V1CardRedeemAccountHistory.Columns().CreatedAt).
		ScanAndCount(&data, &total, false)
	return
}
