package merchant

import (
	"context"

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

	v1 "kami/api/merchant/v1"
	"kami/internal/errHandler"
	"kami/internal/model/entity"
	"kami/internal/service"
)

func (c *ControllerV1) MerchantSampleAllList(ctx context.Context, req *v1.MerchantSampleAllListReq) (res *v1.MerchantSampleAllListRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	res = &v1.MerchantSampleAllListRes{}
	list, total, err := service.Merchant().All(ctx)
	if err != nil {
		return
	}
	slice.ForEach(list, func(index int, item *entity.V1MerchantInfo) {
		res.List = append(res.List, &v1.SampleAllListRes{
			Id:           item.MerchantUid,
			MerchantName: item.MerchantName,
		})
	})
	res.Total = total
	return
}
