package controller

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

// PlatformAlarm 运维告警记录
var PlatformAlarm = new(platformAlarmCrl)

type platformAlarmCrl struct{}

func (c platformAlarmCrl) List(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	filter := model.RequestPlatFormAlartFilter{}
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	err := ctx.ShouldBind(&filter)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	filter.ArmSource = 1 //运维
	list, total, err := service.PlatformAlarmService.GetList(claims.TenantId, filter)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	pages := math.Ceil(float64((total)) / float64(size))
	rsp := model.ResposePlatFormAlart{
		Current: current,
		Size:    size,
		Total:   total,
		Pages:   int(pages),
		Records: list,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

func (c platformAlarmCrl) List2(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	filter := model.RequestPlatFormAlartFilter{}
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	err := ctx.ShouldBind(&filter)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	filter.ArmSource = 2 //运维
	list, total, err := service.PlatformAlarmService.GetList(claims.TenantId, filter)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	pages := math.Ceil(float64((total)) / float64(size))
	rsp := model.ResposePlatFormAlart{
		Current: current,
		Size:    size,
		Total:   total,
		Pages:   int(pages),
		Records: list,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}

func (c platformAlarmCrl) Refresh(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	tenantId := claims.TenantId
	alarmService := service.PlatformAlarmService
	go func() {
		alarmService.Refresh(tenantId)
	}()
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

func (c platformAlarmCrl) HandleAlert(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	id, _ := strconv.Atoi(ctx.Query("id"))
	armHandle, _ := strconv.Atoi(ctx.Query("armHandle"))
	remark := ctx.Query("remark")

	err := service.PlatformAlarmService.HandleAlarm(claims.TenantId, id, armHandle, remark)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}

func (c platformAlarmCrl) Detail(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}
