123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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)
- }
|