package system

import (
	"sundynix-go/global"
	"sundynix-go/model/commom/request"
	"sundynix-go/model/commom/response"
	"sundynix-go/model/system"
	systemReq "sundynix-go/model/system/request"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ClientApi struct {
}

// SaveClient
// @Tags      客户端管理
// @Summary   创建client
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      system.Client  true  "client"
// @Success   200   {object}  response.Response{msg=string}  "创建client"
// @Router    /client/save [post]
func (s *ClientApi) SaveClient(c *gin.Context) {
	var client system.Client
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	err = clientService.SaveClient(client)
	if err != nil {
		global.Logger.Error("保存客户端失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("保存客户端成功!", c)
}

// UpdateClient
// @Tags      客户端管理
// @Summary   更新client
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      system.Client  true  "client"
// @Success   200   {object}  response.Response{msg=string}  "更新client"
// @Router    /client/update [post]
func (s *ClientApi) UpdateClient(c *gin.Context) {
	var client system.Client
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	err = clientService.UpdateClient(client)
	if err != nil {
		global.Logger.Error("更新客户端失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("更新客户端成功!", c)
}

// GetClientList
// @Tags      客户端管理
// @Summary   获取client列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      systemReq.GetClientList  true  "client"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "获取client列表"
// @Router    /client/getClientList [post]
func (s *ClientApi) GetClientList(c *gin.Context) {
	var pageInfo systemReq.GetClientList
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	list, total, err := clientService.GetClientList(pageInfo)
	if err != nil {
		global.Logger.Error("获取客户端列表失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Current,
		PageSize: pageInfo.PageSize,
	}, c)
}

// Delete
// @Tags      客户端管理
// @Summary   删除client
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.IdsReq  true  "ids"
// @Success   200   {object}  response.Response{msg=string}  "删除client"
// @Router    /client/delete [post]
func (s *ClientApi) Delete(c *gin.Context) {
	var ids request.IdsReq
	err := c.ShouldBindJSON(&ids)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	err = clientService.DeleteClientByIds(ids)
	if err != nil {
		global.Logger.Error("删除客户端失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithMsg("删除客户端成功!", c)
}

// Detail
// @Tags      客户端管理
// @Summary   获取client详情
// @Description  id获取详情
// @Security  ApiKeyAuth
// @Produce   application/json
// @Param     id  query  string  true  "id"
// @Success   200   {object}  response.Response{data=system.Client,msg=string}  "获取client详情"
// @Router    /client/detail [get]
func (s *ClientApi) Detail(c *gin.Context) {
	id := c.Query("id")
	client, err := clientService.GetClientById(id)
	if err != nil {
		global.Logger.Error("获取客户端详情失败!", zap.Error(err))
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.OkWithData(client, c)

}
