package controller

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

// Device 设备统计
var Device = new(deviceCtl)

type deviceCtl struct{}

func (c deviceCtl) CountDevice(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestDeviceCountFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	records, err := service.DeviceCountService.CountDevice(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

func (c deviceCtl) MonthList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestDeviceCountFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	var records interface{}
	err = nil
	if req.QueryType == "day" {
		records, err = service.DeviceCountService.GetDayList(claims.TenantId, req)
	} else {
		records, err = service.DeviceCountService.GetMonthList(claims.TenantId, req)
	}
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}

func (c deviceCtl) YearList(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	var req model.RequestDeviceCountFilter
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	records, err := service.DeviceCountService.GetYearList(claims.TenantId, req)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
}
