89 lines
2.9 KiB
Go
89 lines
2.9 KiB
Go
// errorcode_handler.go Day 56:错误码维护端点(system_error_code 增改停用)。
|
|
// - GET /system/error-codes 分页查询(code 前缀/name 模糊/status;登录即可)
|
|
// - POST /system/error-codes 新增(system:write——admin 直通,错误码变更
|
|
// 影响拦截/重发/入审策略,收紧至系统管理员)
|
|
// - PUT /system/error-codes/:id 修改+启停(status=0 停用,历史记录保留)
|
|
//
|
|
// 消费者归一化仍走 report.Normalize 纯函数词表(三态判定与本表解耦)。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/response"
|
|
"go-sms/internal/service/report"
|
|
)
|
|
|
|
type errorCodeHandler struct{ svc *report.ErrorCodeService }
|
|
|
|
func registerErrorCodeRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker, svc *report.ErrorCodeService) {
|
|
h := &errorCodeHandler{svc: svc}
|
|
gate := func(obj, act string) gin.HandlerFunc {
|
|
return middleware.RequirePermission(perm, obj, act)
|
|
}
|
|
protected.GET("/system/error-codes", h.list)
|
|
protected.POST("/system/error-codes", gate("system", "write"), h.create)
|
|
protected.PUT("/system/error-codes/:id", gate("system", "write"), h.update)
|
|
}
|
|
|
|
// list GET /system/error-codes?page=&page_size=&code=&name=&status=
|
|
func (h *errorCodeHandler) list(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
ps, _ := strconv.Atoi(c.DefaultQuery("page_size", "50"))
|
|
q := report.ErrorCodeQuery{Page: page, PageSize: ps, Code: c.Query("code"), Name: c.Query("name")}
|
|
if s := c.Query("status"); s != "" {
|
|
v, err := strconv.ParseInt(s, 10, 8)
|
|
if err == nil {
|
|
sv := int8(v)
|
|
q.Status = &sv
|
|
}
|
|
}
|
|
rows, total, err := h.svc.List(c.Request.Context(), q)
|
|
if err != nil {
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"list": rows, "total": total})
|
|
}
|
|
|
|
type errorCodeReq struct {
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
RawValue string `json:"raw_value"`
|
|
Action int8 `json:"action"` // 0 失败 1 重发 2 入审
|
|
Status int8 `json:"status"` // 1 启用 0 停用(仅 PUT)
|
|
}
|
|
|
|
func (h *errorCodeHandler) create(c *gin.Context) {
|
|
var req errorCodeReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
row, err := h.svc.Create(c.Request.Context(), req.Code, req.Name, req.RawValue, req.Action)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, row)
|
|
}
|
|
|
|
func (h *errorCodeHandler) update(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req errorCodeReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
row, err := h.svc.Update(c.Request.Context(), id, req.Name, req.RawValue, req.Action, req.Status)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, row)
|
|
}
|