package sys_user_payment

import (
	"context"

	"kami/internal/dao"
	"kami/internal/model"
	"kami/internal/model/do"
	"kami/internal/model/entity"
	"kami/utility/config"
	"kami/utility/utils"

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

// AddRecord 添加充值流水记录
func (s *sSysUserPayment) AddRecord(ctx context.Context, input model.SysUserPaymentRecordInput, tx gdb.TX) (recordId int64, err error) {
	m := dao.V1SysUserPaymentRecords.Ctx(ctx).DB(config.GetDatabaseV1())
	if tx != nil {
		m = m.TX(tx)
	}
	recordId, err = m.InsertAndGetId(do.V1SysUserPaymentRecords{
		TransactionId:   utils.GenerateRandomUUID(),
		UserId:          input.UserId,
		OrderNo:         input.OrderNo,
		PaymentId:       input.PaymentId,
		Notes:           input.Notes,
		Amount:          input.Amount,
		TransactionType: input.TransactionType,
		Category:        input.Category,
		Status:          "success",
	})
	return
}

func (s *sSysUserPayment) ListRecords(ctx context.Context, input model.SysUserRecordListInput) (total int, list []*entity.V1SysUserPaymentRecords, err error) {
	total = 0
	list = []*entity.V1SysUserPaymentRecords{}
	m := dao.V1SysUserPaymentRecords.Ctx(ctx).DB(config.GetDatabaseV1())
	if input.UserId != "" {
		m = m.Where(dao.V1SysUserPaymentRecords.Columns().UserId, input.UserId)
	}
	if input.TransactionType != "" {
		m = m.Where(dao.V1SysUserPaymentRecords.Columns().TransactionType, input.TransactionType)
	}
	err = m.Page(input.Current, input.PageSize).OrderDesc(dao.V1SysUserPaymentRecords.Columns().CreatedAt).ScanAndCount(&list, &total, true)
	err = utils.HandleNoRowsError(err)
	return
}
