package admin

import (
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/model/common/response"
	"server/utils/logger"
	"strconv"
)

type DeviceGenreApi struct{}

func (dga *DeviceGenreApi) QueryAllDeviceGenres(c *gin.Context) {
	deviceGenres, err := deviceGenreService.QueryAllDeviceGenres()
	if err != nil {
		logger.Get().Error("QueryAllDeviceGenres ---- " + err.Error())
		response.FailWithMessage("获取设备类型失败", c)
		return
	}
	response.OkWithData(deviceGenres, c)
}

func (dga *DeviceGenreApi) CreateDeviceGenre(c *gin.Context) {
	var deviceGenre dao.DeviceGenre
	err := c.ShouldBindJSON(&deviceGenre)
	if err != nil {
		logger.Get().Error("CreateDeviceGenre ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = deviceGenreService.CreateDeviceGenre(deviceGenre)
	if err != nil {
		logger.Get().Error("CreateDeviceGenre ---- " + err.Error())
		response.FailWithMessage("创建设备类型失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (dga *DeviceGenreApi) UpdateDeviceGenre(c *gin.Context) {
	var deviceGenre dao.DeviceGenre
	err := c.ShouldBindJSON(&deviceGenre)
	if err != nil {
		logger.Get().Error("UpdateDeviceGenre ---- " + err.Error())
		response.FailWithMessage("参数解析失败", c)
		return
	}
	err = deviceGenreService.UpdateDeviceGenre(deviceGenre)
	if err != nil {
		logger.Get().Error("UpdateDeviceGenre ---- " + err.Error())
		response.FailWithMessage("更新设备类型失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (dga *DeviceGenreApi) DeleteDeviceGenre(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		logger.Get().Error("DeleteDeviceGenre ---- " + err.Error())
		response.FailWithMessage("参数错误", c)
		return
	}
	err = deviceGenreService.DeleteDeviceGenre(id)
	if err != nil {
		logger.Get().Error("DeleteDeviceGenre ---- " + err.Error())
		response.FailWithMessage("删除设备类型失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}
