package sys_user_payment

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

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

// AddDeductionRecord 添加预扣款记录
func (s *sSysUserPayment) AddDeductionRecord(ctx context.Context, input *model.AddDeductionRecordInput, tx gdb.TX) (err error) {
	m := dao.V1SysUserDeductions.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	_, err = m.Insert(do.V1SysUserDeductions{
		UserId:              input.UserId,
		OrderNo:             input.OrderNo,
		OrderCategory:       input.OrderCategory,
		UserPaymentId:       input.UserPaymentId,
		Balance:             input.Balance,
		UserPaymentRecordId: input.UserPaymentRecordId,
		OperationStatus:     input.OperationStatus, // 开始预扣款
	})
	return
}

// DeductionSuccess 扣款(交易)成功
func (s *sSysUserPayment) DeductionSuccess(ctx context.Context, input *model.AddDeductionRecordInput, tx gdb.TX) (err error) {
	err = s.AddDeductionRecord(ctx, &model.AddDeductionRecordInput{
		OrderNo:         input.OrderNo,
		OrderCategory:   input.OrderCategory,
		UserId:          input.UserId,
		Balance:         input.Balance,
		OperationStatus: consts.DeductionStatusSuccess,
	}, tx)
	if err != nil {
		return
	}
	return
}

// DeductionReturn 扣款(交易)失败
func (s *sSysUserPayment) DeductionReturn(ctx context.Context, input *model.AddDeductionRecordInput, tx gdb.TX) (err error) {
	err = s.AddDeductionRecord(ctx, &model.AddDeductionRecordInput{
		OrderNo:         input.OrderNo,
		OrderCategory:   input.OrderCategory,
		UserId:          input.UserId,
		Balance:         input.Balance,
		OperationStatus: consts.DeductionStatusReturn,
	}, tx)
	if err != nil {
		return
	}
	_, err = s.Recharge(ctx, model.SysUserPaymentRechargeOrConsumeInput{
		UserId:          input.UserId,
		OrderNo:         input.OrderNo,
		Amount:          decimal.NewFromFloat(input.Balance),
		Remark:          input.Remark,
		TransactionType: consts.UserPaymentRecordStatusDeductionReturn,
		Category:        input.OrderCategory,
	}, tx)
	return
}

// Deduction 用户钱包预扣款
func (s *sSysUserPayment) Deduction(ctx context.Context, input *model.DeductionInput, tx gdb.TX) (err error) {
	if tx == nil {
		tx, _ = config.GetDatabaseV1().Ctx(ctx).Begin(ctx)
		defer func() {
			if err != nil {
				_ = tx.Rollback()
				return
			}
			_ = tx.Commit()
		}()
	}
	err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		// 添加预扣款记录
		err2 := s.AddDeductionRecord(ctx, &model.AddDeductionRecordInput{
			OrderNo:         input.OrderNo,
			OrderCategory:   input.OrderCategory,
			UserId:          input.UserId,
			Balance:         input.Balance,
			OperationStatus: consts.DeductionStatusStart,
		}, tx)
		return err2
	})
	if err != nil {
		_ = s.AddDeductionRecord(ctx, &model.AddDeductionRecordInput{
			OrderNo:         input.OrderNo,
			OrderCategory:   input.OrderCategory,
			UserId:          input.UserId,
			Balance:         input.Balance,
			OperationStatus: consts.DeductionStatusFail,
		}, tx)
	}
	return
}
