package goods

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"pychr/global"
	"pychr/model/common/response"
	"pychr/model/goods"
	goodsReq "pychr/model/goods/request"
	"pychr/service"
)

type GoodsShowApi struct {
}

var goodsShowService = service.ServiceGroupApp.GoodsServiceGroup.GoodsShowService

// CreateGoodsShow 创建商品
// @Tags GoodsShow
// @Summary 创建商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body goods.GoodsShow true "创建商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /goodsShow/createGoodsShow [post]
func (goodsShowApi *GoodsShowApi) CreateGoodsShow(c *gin.Context) {
	var goodsShow goods.GoodsShow
	err := c.ShouldBindJSON(&goodsShow)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := goodsShowService.CreateGoodsShow(&goodsShow); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteGoodsShow 删除商品
// @Tags GoodsShow
// @Summary 删除商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body goods.GoodsShow true "删除商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /goodsShow/deleteGoodsShow [delete]
func (goodsShowApi *GoodsShowApi) DeleteGoodsShow(c *gin.Context) {
	ID := c.Query("ID")
	if err := goodsShowService.DeleteGoodsShow(ID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteGoodsShowByIds 批量删除商品
// @Tags GoodsShow
// @Summary 批量删除商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /goodsShow/deleteGoodsShowByIds [delete]
func (goodsShowApi *GoodsShowApi) DeleteGoodsShowByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := goodsShowService.DeleteGoodsShowByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateGoodsShow 更新商品
// @Tags GoodsShow
// @Summary 更新商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body goods.GoodsShow true "更新商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /goodsShow/updateGoodsShow [put]
func (goodsShowApi *GoodsShowApi) UpdateGoodsShow(c *gin.Context) {
	var goodsShow goods.GoodsShow
	err := c.ShouldBindJSON(&goodsShow)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := goodsShowService.UpdateGoodsShow(goodsShow); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindGoodsShow 用id查询商品
// @Tags GoodsShow
// @Summary 用id查询商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query goods.GoodsShow true "用id查询商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /goodsShow/findGoodsShow [get]
func (goodsShowApi *GoodsShowApi) FindGoodsShow(c *gin.Context) {
	ID := c.Query("ID")
	if regoodsShow, err := goodsShowService.GetGoodsShow(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"regoodsShow": regoodsShow}, c)
	}
}

// GetGoodsShowList 分页获取商品列表
// @Tags GoodsShow
// @Summary 分页获取商品列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query goodsReq.GoodsShowSearch true "分页获取商品列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /goodsShow/getGoodsShowList [get]
func (goodsShowApi *GoodsShowApi) GetGoodsShowList(c *gin.Context) {
	var pageInfo goodsReq.GoodsShowSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := goodsShowService.GetGoodsShowInfoList(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)
	}
}
