package card_info_original_jd

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

	"github.com/jinzhu/copier"

	"kami/api/card_info_original_jd/v1"

	"github.com/gogf/gf/v2/errors/gcode"
)

func (c *ControllerV1) OriginalJDAccountList(ctx context.Context, req *v1.OriginalJDAccountListReq) (res *v1.OriginalJDAccountListRes, err error) {
	userInfo, err := service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	accountService := service.CardRedeemAccount()
	total, list, err := accountService.List(ctx, &model.RedeemAccountListInput{
		//JDAccountListReq: req,
		CommonPageReq: commonApi.CommonPageReq{
			Current:  req.Current,
			PageSize: req.PageSize,
		},
		Name:     req.Name,
		NickName: req.NickName,
		Cookie:   req.Cookie,
		Category: consts.CardRedeemAccountCategoryOriginalJD,
		UserId:   userInfo.Id,
	})
	res = &v1.OriginalJDAccountListRes{
		CommonPageRes: commonApi.CommonPageRes[v1.OriginalJDAccountListRecord]{
			Total: total,
			CommonDataRes: commonApi.CommonDataRes[v1.OriginalJDAccountListRecord]{
				List: make([]v1.OriginalJDAccountListRecord, 0),
			},
		},
	}
	_ = copier.CopyWithOption(&res.List, &list, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	err = errHandler.WrapError(ctx, gcode.CodeInternalError, err)
	return
}
