package sysUser

import (
	"context"

	"kami/api/commonApi"
	v1 "kami/api/sysUser/v1"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"
	"kami/utility/token"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/jinzhu/copier"
)

func (c *ControllerV1) UserSearch(ctx context.Context, req *v1.UserSearchReq) (res *v1.UserSearchRes, err error) {
	if !token.IsFromIframe(ctx) {
		err = gerror.NewCode(gcode.CodeNotAuthorized)
		return
	}
	userService := service.SysUser()
	total, list, err := userService.ListWithPayment(ctx, &model.UserSearchInput{
		UserSearchReq: *req,
	})
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeDbOperationError, err, "获取用户列表失败")
	}
	res = &v1.UserSearchRes{
		CommonPageRes: commonApi.CommonPageRes[*v1.SysUserRoleDeptRes]{
			Total: total,
			CommonDataRes: commonApi.CommonDataRes[*v1.SysUserRoleDeptRes]{
				List: make([]*v1.SysUserRoleDeptRes, 0),
			},
		},
	}
	err = copier.CopyWithOption(&res.List, &list, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	return
}
