package card_redeem_order

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"
	"github.com/gogf/gf/v2/os/glog"
)

func (h *sCardRedeemOrder) AddHistory(ctx context.Context, input *model.RedeemCardOrderAddHistory, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	if _, err = m.Insert(do.V1CardRedeemOrderHistory{
		OrderNo:         input.OrderNo,
		AccountId:       input.AccountId,
		Amount:          input.Amount,
		Remark:          input.Remark,
		ResponseRawData: input.ResponseRawData,
		OperationStatus: input.OperationStatus,
	}); err != nil {
		glog.Error(ctx, "添加操作记录出错", err)
	}
	return
}

// GetOrderDetail 获取充值操作记录
func (h *sCardRedeemOrder) GetOrderDetail(ctx context.Context, orderNo string) (total int, data []*entity.V1CardRedeemOrderHistory, err error) {
	data = make([]*entity.V1CardRedeemOrderHistory, 0)
	m := dao.V1CardRedeemOrderHistory.Ctx(ctx).DB(config.GetDatabaseV1())
	err = m.Where(dao.V1CardRedeemOrderHistory.Columns().OrderNo, orderNo).
		OrderDesc(dao.V1CardRedeemOrderHistory.Columns().CreatedAt).
		ScanAndCount(&data, &total, false)
	return
}
