package card_info_c_trip

import (
	"context"
	"kami/api/commonApi"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/model/entity"
	"kami/internal/service"

	v1 "kami/api/card_info_c_trip/v1"

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

func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	res = &v1.ListRes{}
	total, list, err := service.CardRedeemOrder().List(ctx, &model.RedeemCardListInput{
		CommonPageReq: commonApi.CommonPageReq{
			Current:  req.Current,
			PageSize: req.PageSize,
		},
		GiftCardPwd:     req.GiftCardPwd,
		MerchantId:      req.MerchantId,
		Attach:          req.Attach,
		DateRange:       req.DateRange,
		AccountNickName: req.AccountNickName,
		AccountCk:       req.AccountCk,
		UserId:          userInfo.Id,
		Category:        consts.CardRedeemAccountCategoryCTrip,
	})
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "获取订单列表失败")
		return
	}

	res = &v1.ListRes{}
	res.Total = total
	res.List = slice.Map(list, func(index int, item *model.RedeemCardOrderOutput) *entity.V1CardRedeemOrderInfo {
		return item.V1CardRedeemOrderInfo
	})
	return
}
