package sys_payment

import (
	"context"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/duke-git/lancet/v2/slice"
	"github.com/jinzhu/copier"

	"kami/api/sys_payment/v1"
)

func (c *ControllerV1) PaymentSummaryList(ctx context.Context, req *v1.PaymentSummaryListReq) (res *v1.PaymentSummaryListRes, err error) {
	paymentService := service.SysUserPayment()
	total, data, err := paymentService.DailySummary(ctx, model.SysUserPaymentDailySummaryInput{
		PaymentSummaryListReq: req,
	})
	if err != nil {
		err = errHandler.WrapError(ctx, errHandler.CornError, err, "获取用户充值统计失败")
		return
	}
	slice.ForEach(data, func(index int, item *model.SysUserRecord) {
		//消耗减退回是实际消耗
		item.TotalActualConsumeAmount = item.TotalConsumeAmount.Sub(item.TotalReturnAmount)
	})
	res = &v1.PaymentSummaryListRes{}
	res.Total = total
	_ = copier.CopyWithOption(&res.List, &data, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	return
}
