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"
)

// NoticeRecord 发送记录
var NoticeRecord = new(noticeRecordCtl)

type noticeRecordCtl struct {
}

func (c noticeRecordCtl) List(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	filter := model.RequestNoticeRecordFilter{}
	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
	}
	list, total, err := service.NoticeRecordService.List(claims.TenantId, filter)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, err)
		return
	}
	pages := math.Ceil(float64((total)) / float64(size))
	rsp := model.ResposeNoticeRecord{
		Current: current,
		Size:    size,
		Total:   total,
		Pages:   int(pages),
		Records: list,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
}
