package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/device/model"
	"iot_manager_service/app/device/service"
	"iot_manager_service/app/middleware"
	"iot_manager_service/util/common"
	"net/http"
)

// 工作台管理对象
var Workbench = new(workbenchCtl)

type workbenchCtl struct{}

func (c *workbenchCtl) CountDevice(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	devices, err := service.WorkbenchService.CountDevice(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, devices))
}

func (c *workbenchCtl) CountAlarm(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	alarms, err := service.WorkbenchService.CountAlarm(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, alarms))
}

func (c *workbenchCtl) Aqi(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	alarms, err := service.WorkbenchService.Aqi(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, alarms))
}

func (c *workbenchCtl) CountJobTodo(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	alarms, err := service.WorkbenchService.CountJobTodo(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, alarms))
}

func (c *workbenchCtl) Notification(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	alarms, err := service.WorkbenchService.Notification(claims.TenantId)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, alarms))
}

func (c *workbenchCtl) LightRate(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.ReqLightRates
	if err := ctx.ShouldBindQuery(&req); err != nil {
		ctx.JSON(http.StatusServiceUnavailable, err)
	}
	//queryType=month&startDate=2022-09-01&endDate=2022-10-04
	alarms, err := service.WorkbenchService.LightRate(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, alarms))
}
