package card_info_walmart

import (
	"context"

	v1 "kami/api/card_info_walmart/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

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

func (c *ControllerV1) GroupStat(ctx context.Context, req *v1.GroupStatReq) (res *v1.GroupStatRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	if req.UserId != "" && userInfo.Id == "" {
		userInfo.Id = req.UserId
	}
	total, output, err := service.CardRedeemAccount().GroupStats(ctx, &model.RedeemAccountGroupStatsInput{
		Category:      consts.CardRedeemAccountCategoryWalMart,
		CreatedUserId: userInfo.Id,
		GroupStatReq:  req,
	}, nil)
	if err != nil {
		return nil, err
	}
	res = &v1.GroupStatRes{}
	res.Total = total
	res.List = slice.Map(output, func(index int, item *model.RedeemAccountGroupStatsOutput) *v1.V1CardRedeemAccountGroupEntity {
		return item.V1CardRedeemAccountGroupEntity
	})
	return
}
