package handler import ( "net/http" "github.com/gin-gonic/gin" "myschools.me/heritage/heritage-api/service" ) type OrgCreateRequest struct { ParentID string `json:"parentId"` Name string `json:"name"` Enabled *bool `json:"enabled"` Sort *int `json:"sort"` Remark string `json:"remark"` } type OrgUpdateRequest struct { ParentID *string `json:"parentId"` Name *string `json:"name"` Enabled *bool `json:"enabled"` Sort *int `json:"sort"` Remark *string `json:"remark"` } func OrgCreate(c *gin.Context) { var req OrgCreateRequest if err := c.ShouldBindJSON(&req); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"}) return } enabled := true if req.Enabled != nil { enabled = *req.Enabled } sort := 0 if req.Sort != nil { sort = *req.Sort } o, err := service.OrgCreateOrg(req.ParentID, req.Name, enabled, sort, req.Remark) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"org": o}) } func OrgUpdate(c *gin.Context) { id := c.Param("id") var req OrgUpdateRequest if err := c.ShouldBindJSON(&req); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"}) return } o, err := service.OrgUpdateOrg(id, req.ParentID, req.Name, req.Enabled, req.Sort, req.Remark) if err != nil { if err == service.ErrNotFound { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"}) return } c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"org": o}) } func OrgDelete(c *gin.Context) { id := c.Param("id") if err := service.OrgDeleteOrg(id); err != nil { if err == service.ErrNotFound { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"}) return } c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "删除失败"}) return } c.JSON(http.StatusOK, gin.H{"data": "ok"}) } func OrgGet(c *gin.Context) { id := c.Param("id") o, err := service.OrgGetOrg(id) if err != nil { if err == service.ErrNotFound { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"}) return } c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"}) return } c.JSON(http.StatusOK, gin.H{"org": o}) } func OrgList(c *gin.Context) { page, size := pageAndSize(c) parentID := c.Query("parentId") var pid *string if parentID != "" { pid = &parentID } items, total, err := service.OrgListOrgs(pid, page, size) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"}) return } c.JSON(http.StatusOK, gin.H{ "items": items, "total": total, "page": page, "size": size, }) }