package sysUser

import (
	"context"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

	"github.com/duke-git/lancet/v2/slice"
	"github.com/jinzhu/copier"

	v1 "kami/api/sysUser/v1"

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

func (c *ControllerV1) UserGetAllUser(ctx context.Context, req *v1.UserGetAllUserReq) (res *v1.UserGetAllUserRes, err error) {
	userInfo, err := service.SysAuth().LoginWithIFrameAndLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	userService := service.SysUser()
	data, err := userService.GetAllSimpleUser(ctx)

	if userInfo.Id != "" {
		data = slice.Filter(data, func(index int, item model.UserSimpleUsersOutput) bool {
			return item.Id == userInfo.Id
		})
	}
	err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "获取用户列表失败")
	if err != nil {
		return
	}
	res = &v1.UserGetAllUserRes{}
	err = copier.Copy(&res.List, data)
	err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "复制数据失败")
	return
}
