77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-kratos/kratos/v3/errors"
|
|
businessbiz "kra/internal/biz/business"
|
|
"kra/internal/service/business"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type AlertRecord struct{ service *business.AlertRecordService }
|
|
|
|
func NewAlertRecord(service *business.AlertRecordService) *AlertRecord {
|
|
return &AlertRecord{service: service}
|
|
}
|
|
|
|
func (h *AlertRecord) List(c *gin.Context) {
|
|
var req dto.AlertRecordQuery
|
|
if c.ShouldBindQuery(&req) != nil {
|
|
Fail(c, "查询参数不正确")
|
|
return
|
|
}
|
|
page, err := h.service.List(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, page)
|
|
}
|
|
|
|
func (h *AlertRecord) Detail(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
Fail(c, "预警记录 ID 不正确")
|
|
return
|
|
}
|
|
detail, err := h.service.Detail(c.Request.Context(), id)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, detail)
|
|
}
|
|
|
|
func (h *AlertRecord) Process(action businessbiz.AlertAction) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<20)
|
|
var req dto.AlertProcessRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
Fail(c, "预警记录参数不正确")
|
|
return
|
|
}
|
|
if err := h.service.Process(c.Request.Context(), &req, action); err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OK(c)
|
|
}
|
|
}
|
|
|
|
func (h *AlertRecord) AddNote(c *gin.Context) {
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<20)
|
|
var req dto.AlertNoteRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
Fail(c, "预警记录参数不正确")
|
|
return
|
|
}
|
|
if err := h.service.AddNote(c.Request.Context(), &req); err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OK(c)
|
|
}
|