210 lines
5.2 KiB
Go
210 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"ezops/internal/models"
|
|
|
|
"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} models.Response{data=models.ListResponse}
|
|
// @Router /approvals/templates [get]
|
|
func (h *ApprovalHandler) ListTemplates(c *gin.Context) {
|
|
var query models.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
var total int64
|
|
h.db.Model(&models.ApprovalTemplate{}).Count(&total)
|
|
|
|
var templates []models.ApprovalTemplate
|
|
h.db.Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&templates)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: models.ListResponse{
|
|
List: templates,
|
|
Total: total,
|
|
},
|
|
})
|
|
}
|
|
|
|
// GetTemplate 获取审批模板详情
|
|
// @Summary 获取审批模板详情
|
|
// @Description 获取指定审批模板的详细信息
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "模板ID"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates/{id} [get]
|
|
func (h *ApprovalHandler) GetTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var template models.ApprovalTemplate
|
|
if err := h.db.First(&template, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.Response{
|
|
Code: 404,
|
|
Message: "模板不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: template,
|
|
})
|
|
}
|
|
|
|
// CreateTemplate 创建审批模板
|
|
// @Summary 创建审批模板
|
|
// @Description 创建新审批模板
|
|
// @Tags 审批流程
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body models.CreateApprovalTemplateRequest true "创建请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates [post]
|
|
func (h *ApprovalHandler) CreateTemplate(c *gin.Context) {
|
|
var req models.CreateApprovalTemplateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
template := models.ApprovalTemplate{
|
|
Name: req.Name,
|
|
Code: req.Code,
|
|
Description: req.Description,
|
|
Type: req.Type,
|
|
FormSchema: req.FormSchema,
|
|
FlowConfig: req.FlowConfig,
|
|
Enabled: true,
|
|
}
|
|
|
|
if err := h.db.Create(&template).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.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 models.CreateApprovalTemplateRequest true "更新请求"
|
|
// @Success 200 {object} models.Response{data=models.ApprovalTemplate}
|
|
// @Router /approvals/templates/{id} [put]
|
|
func (h *ApprovalHandler) UpdateTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
var req models.CreateApprovalTemplateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, models.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var template models.ApprovalTemplate
|
|
if err := h.db.First(&template, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, models.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, models.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
h.db.First(&template, id)
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: template,
|
|
})
|
|
}
|
|
|
|
// DeleteTemplate 删除审批模板
|
|
// @Summary 删除审批模板
|
|
// @Description 删除指定审批模板
|
|
// @Tags 审批流程
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "模板ID"
|
|
// @Success 200 {object} models.Response
|
|
// @Router /approvals/templates/{id} [delete]
|
|
func (h *ApprovalHandler) DeleteTemplate(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&models.ApprovalTemplate{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, models.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, models.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|
|
// Process 相关 handler 见 approval_process.go
|