noticeRecordController.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/warn/model"
  6. "iot_manager_service/app/warn/service"
  7. "iot_manager_service/util/common"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var NoticeRecord = new(noticeRecordCtl)
  13. type noticeRecordCtl struct {
  14. }
  15. func (c noticeRecordCtl) List(ctx *gin.Context) {
  16. value, _ := ctx.Get(middleware.Authorization)
  17. claims := value.(*middleware.Claims)
  18. filter := model.RequestNoticeRecordFilter{}
  19. current, _ := strconv.Atoi(ctx.Query("current"))
  20. size, _ := strconv.Atoi(ctx.Query("size"))
  21. if current == 0 {
  22. current = 1
  23. }
  24. if size <= 0 || size > 100 {
  25. size = 10
  26. }
  27. err := ctx.ShouldBind(&filter)
  28. if err != nil {
  29. ctx.JSON(http.StatusBadRequest, err)
  30. return
  31. }
  32. list, total, err := service.NoticeRecordService.List(claims.TenantId, filter)
  33. if err != nil {
  34. ctx.JSON(http.StatusBadRequest, err)
  35. return
  36. }
  37. pages := math.Ceil(float64((total)) / float64(size))
  38. rsp := model.ResposeNoticeRecord{
  39. Current: current,
  40. Size: size,
  41. Total: total,
  42. Pages: int(pages),
  43. Records: list,
  44. }
  45. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  46. }