package controller import ( "net/http" "strconv" "ezops/model" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // ApprovalHandler 审批处理器 type ApprovalHandler struct { db *gorm.DB } // NewApprovalHandler 创建审批处理器 func NewApprovalHandler(db *gorm.DB) *ApprovalHandler { return &ApprovalHandler{db: db} } // ListTemplates 获取审批模板列表 // @Summary 获取审批模板列表 // @Description 获取所有审批模板列表 // @Tags 审批流程 // @Produce json // @Security Bearer // @Param page query int false "页码" // @Param page_size query int false "每页数量" // @Success 200 {object} model.Response{data=model.ListResponse} // @Router /approvals/templates [get] func (h *ApprovalHandler) ListTemplates(c *gin.Context) { var query model.PageQuery c.ShouldBindQuery(&query) db := h.db.Model(&model.ApprovalTemplate{}) if category := c.Query("category"); category != "" { db = db.Where("category = ?", category) } if qType := c.Query("type"); qType != "" { db = db.Where("type = ?", qType) } var total int64 db.Count(&total) var templates []model.ApprovalTemplate db.Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&templates) c.JSON(http.StatusOK, model.Response{ Code: 200, Message: "success", Data: model.ListResponse{ List: templates, Total: total, }, }) } // GetTemplate 获取审批模板详情 // @Summary 获取审批模板详情 // @Description 获取指定审批模板的详细信息 // @Tags 审批流程 // @Produce json // @Security Bearer // @Param id path int true "模板ID" // @Success 200 {object} model.Response{data=model.ApprovalTemplate} // @Router /approvals/templates/{id} [get] func (h *ApprovalHandler) GetTemplate(c *gin.Context) { id, _ := strconv.Atoi(c.Param("id")) var template model.ApprovalTemplate if err := h.db.First(&template, id).Error; err != nil { c.JSON(http.StatusNotFound, model.Response{ Code: 404, Message: "模板不存在", }) return } c.JSON(http.StatusOK, model.Response{ Code: 200, Message: "success", Data: template, }) } // CreateTemplate 创建审批模板 // @Summary 创建审批模板 // @Description 创建新审批模板 // @Tags 审批流程 // @Accept json // @Produce json // @Security Bearer // @Param request body model.CreateApprovalTemplateRequest true "创建请求" // @Success 200 {object} model.Response{data=model.ApprovalTemplate} // @Router /approvals/templates [post] func (h *ApprovalHandler) CreateTemplate(c *gin.Context) { var req model.CreateApprovalTemplateRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Response{ Code: 400, Message: "请求参数错误: " + err.Error(), }) return } template := model.ApprovalTemplate{ Name: req.Name, Code: req.Code, Description: req.Description, Type: req.Type, FormSchema: req.FormSchema, FlowConfig: req.FlowConfig, Category: req.Category, Enabled: true, } if err := h.db.Create(&template).Error; err != nil { c.JSON(http.StatusInternalServerError, model.Response{ Code: 500, Message: "创建失败", }) return } c.JSON(http.StatusOK, model.Response{ Code: 200, Message: "创建成功", Data: template, }) } // UpdateTemplate 更新审批模板 // @Summary 更新审批模板 // @Description 更新指定审批模板 // @Tags 审批流程 // @Accept json // @Produce json // @Security Bearer // @Param id path int true "模板ID" // @Param request body model.CreateApprovalTemplateRequest true "更新请求" // @Success 200 {object} model.Response{data=model.ApprovalTemplate} // @Router /approvals/templates/{id} [put] func (h *ApprovalHandler) UpdateTemplate(c *gin.Context) { id, _ := strconv.Atoi(c.Param("id")) var req model.CreateApprovalTemplateRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Response{ Code: 400, Message: "请求参数错误: " + err.Error(), }) return } var template model.ApprovalTemplate if err := h.db.First(&template, id).Error; err != nil { c.JSON(http.StatusNotFound, model.Response{ Code: 404, Message: "模板不存在", }) return } updates := map[string]interface{}{ "name": req.Name, "description": req.Description, "type": req.Type, "form_schema": req.FormSchema, "flow_config": req.FlowConfig, } if err := h.db.Model(&template).Updates(updates).Error; err != nil { c.JSON(http.StatusInternalServerError, model.Response{ Code: 500, Message: "更新失败", }) return } h.db.First(&template, id) c.JSON(http.StatusOK, model.Response{ Code: 200, Message: "更新成功", Data: template, }) } // DeleteTemplate 删除审批模板 // @Summary 删除审批模板 // @Description 删除指定审批模板 // @Tags 审批流程 // @Produce json // @Security Bearer // @Param id path int true "模板ID" // @Success 200 {object} model.Response // @Router /approvals/templates/{id} [delete] func (h *ApprovalHandler) DeleteTemplate(c *gin.Context) { id, _ := strconv.Atoi(c.Param("id")) if err := h.db.Delete(&model.ApprovalTemplate{}, id).Error; err != nil { c.JSON(http.StatusInternalServerError, model.Response{ Code: 500, Message: "删除失败", }) return } c.JSON(http.StatusOK, model.Response{ Code: 200, Message: "删除成功", }) } // Process 相关 handler 见 approval_process.go