package card_redeem_order

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

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

func (h *sCardRedeemOrder) UpdateStatus(ctx context.Context, orderNo string, status consts.RedeemOrderStatus, remark string, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardRedeemOrderInfo.Columns().OrderNo, orderNo)
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Update(do.V1CardRedeemOrderInfo{
		Status: status,
		Remark: remark,
	})
	return
}

// SetCallBackCount 重置调用次数
func (h *sCardRedeemOrder) SetCallBackCount(ctx context.Context, orderNo string, callbackCount int, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardRedeemOrderInfo.Columns().OrderNo, orderNo)
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Update(do.V1CardRedeemOrderInfo{
		CallbackCount: callbackCount,
	})
	return
}

// UpdateOrderStatus 更新订单原本状态
func (h *sCardRedeemOrder) UpdateOrderStatus(ctx context.Context, orderNo string, status consts.RedeemOrderStatus, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderInfo.Ctx(ctx).DB(config.GetDatabaseV1()).
		Where(dao.V1CardRedeemOrderInfo.Columns().OrderNo, orderNo)
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Update(do.V1CardRedeemOrderInfo{
		OrderStatus: status,
	})
	return
}

// ModifyCallBackStatus 修改回调状态
func (h *sCardRedeemOrder) ModifyCallBackStatus(ctx context.Context, orderNo string, callbackStatus consts.RedeemOrderCallbackStatus, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Where(dao.V1CardRedeemOrderInfo.Columns().OrderNo, orderNo).Update(do.V1CardRedeemOrderInfo{NotifyStatus: callbackStatus})
	return
}

// UpdateCardNoAndCardType 更新卡号和卡种
func (h *sCardRedeemOrder) UpdateCardNoAndCardType(ctx context.Context, orderNo string, cardNo string, cardTypeName string, tx gdb.TX) (err error) {
	m := dao.V1CardRedeemOrderInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Where(dao.V1CardRedeemOrderInfo.Columns().OrderNo, orderNo).Update(do.V1CardRedeemOrderInfo{
		CardNo:       cardNo,
		CardTypeName: cardTypeName,
	})
	return
}
