package sysUser

import (
	"context"
	v1 "kami/api/sysUser/v1"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"

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

func (c *ControllerV1) UserEdit(ctx context.Context, req *v1.UserEditReq) (res *v1.UserEditRes, err error) {
	_, err = service.SysAuth().LoginOnlyIFrame(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeNotAuthorized, err, "权限不足")
		return
	}
	sysUserService := service.SysUser()
	output, err := sysUserService.GetUserById(ctx, req.ID)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "獲取用户失败")
		return
	}
	if output.Id == "" {
		err = errHandler.WrapError(ctx, gcode.CodeOK, err, "当前用户不存在")
		return
	}
	err = sysUserService.Edit(ctx, &model.UserEditInput{
		UserEditReq: *req,
	})
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "更新用户失败")
		return
	}
	//for _, channelType := range req.ChannelList {
	//	if err2 := service.SysUserChannelConfig().AddOrUpdateOne(ctx, output.Id, channelType.ChannelName, channelType.Num); err2 != nil {
	//		glog.Error(ctx, "添加用户渠道配置失败", err2)
	//	}
	//}
	return
}
