package handler import ( "net/http" "strconv" "giter.top/smart/internal/system/service" "github.com/gin-gonic/gin" ) // ParamHandler 系统参数 HTTP 处理器 type ParamHandler struct { service service.ParamService } // NewParamHandler 创建参数处理器实例 func NewParamHandler(svc service.ParamService) *ParamHandler { return &ParamHandler{service: svc} } // CreateParam 创建系统参数 // @Summary 创建系统参数 // @Tags 系统参数 // @Accept json // @Produce json // @Param request body service.CreateParamRequest true "创建参数请求" // @Success 201 {object} entity.SystemParam // @Router /api/v1/system/params [post] func (h *ParamHandler) CreateParam(c *gin.Context) { var req service.CreateParamRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // TODO: 从上下文获取用户 ID(实际项目中从 JWT token 解析) creatorID := "system" param, err := h.service.CreateParam(c.Request.Context(), &req, creatorID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, param) } // UpdateParam 更新系统参数 // @Summary 更新系统参数 // @Tags 系统参数 // @Accept json // @Produce json // @Param id path string true "参数 ID" // @Param request body service.UpdateParamRequest true "更新参数请求" // @Success 200 {object} entity.SystemParam // @Router /api/v1/system/params/{id} [put] func (h *ParamHandler) UpdateParam(c *gin.Context) { id := c.Param("id") if id == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 ID"}) return } var req service.UpdateParamRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // TODO: 从上下文获取用户 ID lastUpdaterID := "system" param, err := h.service.UpdateParam(c.Request.Context(), id, &req, lastUpdaterID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, param) } // DeleteParams 批量删除系统参数 // @Summary 批量删除系统参数 // @Tags 系统参数 // @Accept json // @Produce json // @Param request body []string true "参数 ID 列表" // @Success 204 // @Router /api/v1/system/params/batch [delete] func (h *ParamHandler) DeleteParams(c *gin.Context) { var ids []string if err := c.ShouldBindJSON(&ids); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := h.service.DeleteParams(c.Request.Context(), ids); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.Status(http.StatusNoContent) } // GetParam 获取单个系统参数 // @Summary 获取单个系统参数 // @Tags 系统参数 // @Produce json // @Param id path string true "参数 ID" // @Success 200 {object} entity.SystemParam // @Router /api/v1/system/params/{id} [get] func (h *ParamHandler) GetParam(c *gin.Context) { id := c.Param("id") if id == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "无效的 ID"}) return } param, err := h.service.GetParam(c.Request.Context(), id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, param) } // GetParamByKey 根据键获取系统参数 // @Summary 根据键获取系统参数 // @Tags 系统参数 // @Produce json // @Param key path string true "参数键" // @Success 200 {object} entity.SystemParam // @Router /api/v1/system/params/key/{key} [get] func (h *ParamHandler) GetParamByKey(c *gin.Context) { key := c.Param("key") param, err := h.service.GetParamByKey(c.Request.Context(), key) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, param) } // ListParams 获取系统参数列表 // @Summary 获取系统参数列表 // @Tags 系统参数 // @Produce json // @Param group query string false "分组" // @Param param_key query string false "参数键(模糊搜索)" // @Param page query int false "页码" default(1) // @Param page_size query int false "每页数量" default(10) // @Success 200 {object} service.ParamListResponse // @Router /api/v1/system/params [get] func (h *ParamHandler) ListParams(c *gin.Context) { group := c.Query("group") paramKey := c.Query("param_key") page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10")) response, err := h.service.ListParams(c.Request.Context(), group, paramKey, page, pageSize) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, response) } // GetAllParams 获取所有系统参数 // @Summary 获取所有系统参数 // @Tags 系统参数 // @Produce json // @Success 200 {object} map[string]entity.SystemParam // @Router /api/v1/system/params/all [get] func (h *ParamHandler) GetAllParams(c *gin.Context) { params, err := h.service.GetAllParams(c.Request.Context()) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, params) }