package user

import (
	"courseSign/server/global"
    "courseSign/server/model/user"
    "courseSign/server/model/common/request"
    userReq "courseSign/server/model/user/request"
    "courseSign/server/model/common/response"
    "courseSign/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type UsersTreeApi struct {
}

var usersTreeService = service.ServiceGroupApp.UserServiceGroup.UsersTreeService


// CreateUsersTree 创建UsersTree
// @Tags UsersTree
// @Summary 创建UsersTree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UsersTree true "创建UsersTree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /usersTree/createUsersTree [post]
func (usersTreeApi *UsersTreeApi) CreateUsersTree(c *gin.Context) {
	var usersTree user.UsersTree
	err := c.ShouldBindJSON(&usersTree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := usersTreeService.CreateUsersTree(usersTree); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteUsersTree 删除UsersTree
// @Tags UsersTree
// @Summary 删除UsersTree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UsersTree true "删除UsersTree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /usersTree/deleteUsersTree [delete]
func (usersTreeApi *UsersTreeApi) DeleteUsersTree(c *gin.Context) {
	var usersTree user.UsersTree
	err := c.ShouldBindJSON(&usersTree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := usersTreeService.DeleteUsersTree(usersTree); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteUsersTreeByIds 批量删除UsersTree
// @Tags UsersTree
// @Summary 批量删除UsersTree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除UsersTree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /usersTree/deleteUsersTreeByIds [delete]
func (usersTreeApi *UsersTreeApi) DeleteUsersTreeByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := usersTreeService.DeleteUsersTreeByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateUsersTree 更新UsersTree
// @Tags UsersTree
// @Summary 更新UsersTree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UsersTree true "更新UsersTree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /usersTree/updateUsersTree [put]
func (usersTreeApi *UsersTreeApi) UpdateUsersTree(c *gin.Context) {
	var usersTree user.UsersTree
	err := c.ShouldBindJSON(&usersTree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := usersTreeService.UpdateUsersTree(usersTree); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindUsersTree 用id查询UsersTree
// @Tags UsersTree
// @Summary 用id查询UsersTree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.UsersTree true "用id查询UsersTree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /usersTree/findUsersTree [get]
func (usersTreeApi *UsersTreeApi) FindUsersTree(c *gin.Context) {
	var usersTree user.UsersTree
	err := c.ShouldBindQuery(&usersTree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reusersTree, err := usersTreeService.GetUsersTree(usersTree.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reusersTree": reusersTree}, c)
	}
}

// GetUsersTreeList 分页获取UsersTree列表
// @Tags UsersTree
// @Summary 分页获取UsersTree列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query userReq.UsersTreeSearch true "分页获取UsersTree列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /usersTree/getUsersTreeList [get]
func (usersTreeApi *UsersTreeApi) GetUsersTreeList(c *gin.Context) {
	var pageInfo userReq.UsersTreeSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := usersTreeService.GetUsersTreeInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
