package card_apple_order

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

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

// ModifyOrderStatus 修改充值订单状态
func (h *sAppleOrder) ModifyOrderStatus(ctx context.Context, orderNo string, status consts.AppleRechargeOrderStatus, remark string, tx gdb.TX) (err error) {
	m := dao.V1CardAppleRechargeInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	d := do.V1CardAppleRechargeInfo{
		Status: status,
		Remark: remark,
	}
	if status == consts.AppleRechargeOrderWaiting {
		d.AccountId = ""
		d.AccountName = ""
		d.CreatedUserId = ""
	}
	_, err = m.Where(dao.V1CardAppleRechargeInfo.Columns().OrderNo, orderNo).Update(d)
	return
}

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

// ModifyCallBackStatus 修改回调状态
func (h *sAppleOrder) ModifyCallBackStatus(ctx context.Context, orderNo string, callbackStatus bool, tx gdb.TX) (err error) {
	m := dao.V1CardAppleRechargeInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	status := consts.CardAppleNotifyStatusFail
	//operation := consts.AppleRechargeOperationCallBackOverLimit
	if callbackStatus {
		//operation = consts.AppleRechargeOperationCallBackSuccess
		status = consts.CardAppleNotifyStatusSuccess
	}
	_, err = m.Where(dao.V1CardAppleRechargeInfo.Columns().OrderNo, orderNo).Update(do.V1CardAppleRechargeInfo{NotifyStatus: status})
	//_ = h.AddHistory(ctx, &model.AppleCardRechargeHistoryInput{
	//	RechargeId:  0,
	//	OrderNo:     orderNo,
	//	AccountID:   "",
	//	AccountName: "",
	//	Operation:   operation,
	//	Remark:      "",
	//}, nil)
	return
}

// UpdateRemark 更新状态
func (h *sAppleOrder) UpdateRemark(ctx context.Context, orderNo string, remark string, tx gdb.TX) (err error) {
	m := dao.V1CardAppleRechargeInfo.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Where(dao.V1CardAppleRechargeInfo.Columns().OrderNo, orderNo).Update(do.V1CardAppleRechargeInfo{Remark: remark})
	return
}
