package admin import ( "net/http" "zhiyuan/models" "zhiyuan/pkg/app" "zhiyuan/pkg/errcode" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/dept" "zhiyuan/services/form" "github.com/gin-gonic/gin" ) func DeptInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "department id must be a number", nil) return } dept, err := dept.GetInfoByID(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, dept) } func DeptList(c *gin.Context) { isTree := utils.ToInt(c.Query("tree")) if isTree == 1 { DeptListTree(c) return } page := app.HandlePageNums(c) where := make(map[string]interface{}) total, err := dept.Count(where) if err != nil { app.Error(c, err.Error()) return } depts, err := dept.GetList(where, []string{"id", "name", "pid", "attribute"}, page, nil) if err != nil { app.Error(c, err.Error()) return } data := gin.H{ "list": depts, "total": total, "limit": page.PageSize, } app.Success(c, data) } func DeptListTree(c *gin.Context) { depts, err := dept.GetList(nil, []string{"id", "name", "pid", "attribute"}, app.Page{}, nil) if err != nil { app.Error(c, err.Error()) return } app.Success(c, utils.GenTree(depts, "id", "pid", "children", 0)) } func DeptAdd(c *gin.Context) { var form form.DeptAdd if app.Bind(c, &form) != nil { return } id, err := dept.Add(form) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func DeptEdit(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "department id must be a number", nil) return } var form form.DeptAdd if app.Bind(c, &form) != nil { return } err := dept.EditByID(form, id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func DeptDel(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "department id must be a number", nil) return } err := dept.DeleteByID(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func DeptMyList(c *gin.Context) { type AdminInfo struct { ID int `json:"id"` DeptId int `json:"dept_id"` } var adminInfo *AdminInfo admin.GetInfoByID(c.GetInt("adminID"), []string{"id", "username"}, &adminInfo) if adminInfo == nil { app.Response(c, http.StatusUnauthorized, errcode.TokenInvalid, nil) return } depts := dept.GetSubDepts(adminInfo.DeptId) if depts == nil { depts = []*models.Dept{} } data := gin.H{ "list": depts, "count": len(depts), } app.Success(c, data) }