feat: 迁移预警规则管理和历史分类查询

This commit is contained in:
Yvan
2026-11-03 20:19:26 +08:00
parent cd42a1d3f0
commit 7203c34409
31 changed files with 1264 additions and 15 deletions
+2 -1
View File
@@ -26,6 +26,7 @@ func NewGinEngine(runtime *config.Store, access *systemservice.AccessControlServ
func emptyHandlers() *handler.Set {
return &handler.Set{
AlertRule: &handler.AlertRule{},
BusinessPromote: &handler.BusinessPromote{},
Business: &handler.Business{},
Authority: &handler.Authority{}, Menu: &handler.Menu{}, API: &handler.API{},
@@ -99,7 +100,7 @@ func TestGinStartupLogsEveryRegisteredRoute(t *testing.T) {
if got, want := strings.Count(text, `"msg":"router registered"`), len(engine.Routes()); got != want {
t.Fatalf("registered route log count = %d, want %d", got, want)
}
if !strings.Contains(text, `"msg":"router register success"`) || !strings.Contains(text, `"route_count":212`) {
if !strings.Contains(text, `"msg":"router register success"`) || !strings.Contains(text, `"route_count":219`) {
t.Fatalf("startup route summary is missing: %s", text)
}
}
+89
View File
@@ -0,0 +1,89 @@
package handler
import (
"github.com/gin-gonic/gin"
"github.com/go-kratos/kratos/v3/errors"
"kra/internal/service/business"
"kra/internal/service/dto"
"strconv"
)
type AlertRule struct{ service *business.AlertRuleService }
func NewAlertRule(service *business.AlertRuleService) *AlertRule { return &AlertRule{service: service} }
func (h *AlertRule) List(c *gin.Context) {
var req dto.AlertRuleQuery
if err := c.ShouldBindQuery(&req); err != nil {
Fail(c, "查询参数不正确")
return
}
v, err := h.service.List(c.Request.Context(), &req)
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, v)
}
func (h *AlertRule) Types(c *gin.Context) {
v, err := h.service.Types(c.Request.Context())
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, v)
}
func (h *AlertRule) Find(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
Fail(c, "规则 ID 不正确")
return
}
v, err := h.service.Find(c.Request.Context(), id)
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, v)
}
func (h *AlertRule) Save(c *gin.Context) {
var req dto.AlertRuleRequest
if err := c.ShouldBindJSON(&req); err != nil {
Fail(c, "预警规则参数不正确")
return
}
id, err := h.service.Save(c.Request.Context(), &req)
if err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OKWithData(c, gin.H{"id": id})
}
func (h *AlertRule) Delete(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
Fail(c, "规则 ID 不正确")
return
}
if err := h.service.Delete(c.Request.Context(), id); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
func (h *AlertRule) Toggle(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
Fail(c, "规则 ID 不正确")
return
}
enabled, err := strconv.Atoi(c.Query("enabled"))
if err != nil {
Fail(c, "启用状态参数不正确")
return
}
if err := h.service.Toggle(c.Request.Context(), id, enabled); err != nil {
Fail(c, errors.FromError(err).Message)
return
}
OK(c)
}
+1
View File
@@ -8,6 +8,7 @@ var ProviderSet = wire.NewSet(
NewBusiness,
NewBusinessPassword,
NewBusinessPromote,
NewAlertRule,
NewAuthority, NewMenu, NewAPI, NewPermission, NewOrganization,
NewAnnouncement, NewEmail, NewPayment, NewTask, NewMedia, NewAudit,
NewExport, NewVersion, NewDictionary, NewParameter, NewAPIToken,
+1
View File
@@ -1,6 +1,7 @@
package handler
type Set struct {
AlertRule *AlertRule
BusinessPromote *BusinessPromote
Business *Business
BusinessPassword *BusinessPassword
+17
View File
@@ -0,0 +1,17 @@
package router
import (
"github.com/gin-gonic/gin"
"kra/internal/server/handler"
)
func RegisterAlertRule(private *gin.RouterGroup, h *handler.AlertRule) {
g := private.Group("/sw/alertRule")
g.GET("/list", h.List)
g.GET("/ruleTypes", h.Types)
g.GET("/:id", h.Find)
g.POST("/add", h.Save)
g.PUT("/edit", h.Save)
g.DELETE("/:id", h.Delete)
g.PUT("/toggle/:id", h.Toggle)
}
+1
View File
@@ -20,6 +20,7 @@ func (r *Routes) RegisterRoutes(public, private *gin.RouterGroup, engine *gin.En
return
}
registrations := []func(){
func() { RegisterAlertRule(private, r.handlers.AlertRule) },
func() { private.GET("/sw/business/:userId/promotes", r.handlers.BusinessPromote.List) },
func() { RegisterBusiness(private, r.handlers.Business) },
func() { RegisterBusinessPassword(public, r.handlers.BusinessPassword) },