package typeManage

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/request"
	"log-server/model/common/response"
	typeManageModel "log-server/model/typeManage"
	typeManageRes "log-server/model/typeManage/response"
	"log-server/utils"
)

type GameChannelApi struct {}

//添加游戏厂商
func (g *GameChannelApi) CreateGameChannel(c *gin.Context)  {
	var channel typeManageModel.GameChannel
	_ = c.ShouldBindJSON(&channel)
	if err := utils.Verify(channel, utils.GameChannelVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := gameService.CreateGameChannel(channel);err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}

}

//单个删除厂商信息
func (g *GameChannelApi) DeleteGameChannel(c *gin.Context) {
	var channel typeManageModel.GameChannel
	_ = c.ShouldBindJSON(&channel)
	if err := utils.Verify(channel, utils.GameChannelVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := gameService.DeleteGameChannel(channel); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

//批量删除厂商
func (g *GameChannelApi) DeleteGameChannelsByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := gameService.DeleteGameChannelsByIds(ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}





//更新厂商信息
func (g *GameChannelApi) UpdateGameChannel(c *gin.Context)  {
	var channel typeManageModel.GameChannel
	_ = c.ShouldBindJSON(&channel)
	if err := utils.Verify(channel, utils.GameChannelVerify); err != nil{
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := gameService.UpdateGameChannel(channel); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage("修改失败", c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}



//通过id获取单条数据
func (g *GameChannelApi) GetGameChannelById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	//verify数据校验
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	channel, err := gameService.GetGameChannelById(idInfo.ID);
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(typeManageRes.GameChannelResponse{GameChannel: channel}, "获取成功", c)
	}
}

//获取所有游戏厂商列表
func (g *GameChannelApi) GameChannelList(c *gin.Context)  {
	var pageInfo typeManageModel.SearchGameChannelParams
	_ = c.ShouldBindJSON(&pageInfo)
	if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := gameService.GetGameChannelInfoList(pageInfo.GameChannel, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); 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)
	}
}

