package sysUser

import (
	"context"

	"kami/api/commonApi"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

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

	"kami/api/sysUser/v1"
)

func (c *ControllerV1) UserForbiddenById(ctx context.Context, req *v1.UserForbiddenByIdReq) (res *v1.UserForbiddenByIdRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	userService := service.SysUser()
	for _, id := range req.Ids {
		user, err2 := userService.GetUserInfoById(ctx, id)
		if err2 != nil {
			err = errHandler.WrapError(ctx, gcode.CodeInternalError, err2, "获取用户信息失败")
			return
		}
		if user.UserStatus == consts.SysUserStatusNormal {
			err = userService.ChangeUserStatus(ctx, &model.UserStatusInput{
				UserStatusReq: v1.UserStatusReq{
					CommonStrId: commonApi.CommonStrId{
						ID: id,
					},
					UserStatus: consts.SysUserStatusForbidden,
				},
			})
		} else {
			err = userService.ChangeUserStatus(ctx, &model.UserStatusInput{
				UserStatusReq: v1.UserStatusReq{
					CommonStrId: commonApi.CommonStrId{
						ID: id,
					},
					UserStatus: consts.SysUserStatusNormal,
				},
			})
		}
	}
	return
}
