package order

import (
	"context"
	"kami/api/commonApi"
	"kami/api/order/v1"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"
	"math"

	"github.com/duke-git/lancet/v2/slice"
	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) OrderSummaryGetList(ctx context.Context, req *v1.OrderSummaryGetListReq) (res *v1.OrderSummaryGetListRes, err error) {
	loginInfo, err := service.SysAuth().LoginWithEverything(ctx)
	if err != nil {
		return
	}
	orderService := service.OrderSummary()
	total, list, err := orderService.GetList(ctx, model.OrderSummaryListInput{
		OrderSummaryGetListReq: *req,
		UserId:                 loginInfo.UserId,
		LoginType:              loginInfo.LoginType,
	})
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeDbOperationError, err, "获取订单汇总列表失败")
		return
	}
	returnList := make([]v1.OrderSummaryRecord, len(list))
	slice.ForEach(list, func(index int, item *model.OrderSummaryListOutput) {
		returnList[index] = v1.OrderSummaryRecord{
			SucceedShowAmount: item.SucceedShowAmount,
			TotalCount:        item.TotalCount,
			TotalFactAmount:   item.TotalFactAmount,
			MerchantUid:       item.MerchantUid,
			MerchantName:      item.MerchantName,
			TotalShowAmount:   item.TotalShowAmount,
			SucceedCount:      item.SucceedCount,
			Date:              item.Date,
			SucceedFactAmount: item.SucceedFactAmount,
			Rate:              math.Round(item.Rate*100) / 100,
			WaitedCount:       item.WaitedCount,
			FailedCount:       item.FailedCount,
		}

	})
	res = &v1.OrderSummaryGetListRes{
		CommonPageRes: commonApi.CommonPageRes[v1.OrderSummaryRecord]{
			CommonDataRes: commonApi.CommonDataRes[v1.OrderSummaryRecord]{
				List: returnList,
			},
			Total: total,
		},
	}
	return
}
