dept.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package admin
  2. import (
  3. "net/http"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/errcode"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/admin"
  9. "zhiyuan/services/dept"
  10. "zhiyuan/services/form"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func DeptInfo(c *gin.Context) {
  14. id := utils.StrTo(c.Param("id")).MustInt()
  15. if id <= 0 {
  16. app.ErrorMsg(c, "department id must be a number", nil)
  17. return
  18. }
  19. dept, err := dept.GetInfoByID(id)
  20. if err != nil {
  21. app.Error(c, err.Error())
  22. return
  23. }
  24. app.Success(c, dept)
  25. }
  26. func DeptList(c *gin.Context) {
  27. isTree := utils.ToInt(c.Query("tree"))
  28. if isTree == 1 {
  29. DeptListTree(c)
  30. return
  31. }
  32. page := app.HandlePageNums(c)
  33. where := make(map[string]interface{})
  34. total, err := dept.Count(where)
  35. if err != nil {
  36. app.Error(c, err.Error())
  37. return
  38. }
  39. depts, err := dept.GetList(where, []string{"id", "name", "pid", "attribute"}, page, nil)
  40. if err != nil {
  41. app.Error(c, err.Error())
  42. return
  43. }
  44. data := gin.H{
  45. "list": depts,
  46. "total": total,
  47. "limit": page.PageSize,
  48. }
  49. app.Success(c, data)
  50. }
  51. func DeptListTree(c *gin.Context) {
  52. depts, err := dept.GetList(nil, []string{"id", "name", "pid", "attribute"}, app.Page{}, nil)
  53. if err != nil {
  54. app.Error(c, err.Error())
  55. return
  56. }
  57. app.Success(c, utils.GenTree(depts, "id", "pid", "children", 0))
  58. }
  59. func DeptAdd(c *gin.Context) {
  60. var form form.DeptAdd
  61. if app.Bind(c, &form) != nil {
  62. return
  63. }
  64. id, err := dept.Add(form)
  65. if err != nil {
  66. app.Error(c, err.Error())
  67. return
  68. }
  69. app.Success(c, gin.H{"id": id})
  70. }
  71. func DeptEdit(c *gin.Context) {
  72. id := utils.StrTo(c.Param("id")).MustInt()
  73. if id <= 0 {
  74. app.ErrorMsg(c, "department id must be a number", nil)
  75. return
  76. }
  77. var form form.DeptAdd
  78. if app.Bind(c, &form) != nil {
  79. return
  80. }
  81. err := dept.EditByID(form, id)
  82. if err != nil {
  83. app.Error(c, err.Error())
  84. return
  85. }
  86. app.Success(c, nil)
  87. }
  88. func DeptDel(c *gin.Context) {
  89. id := utils.StrTo(c.Param("id")).MustInt()
  90. if id <= 0 {
  91. app.ErrorMsg(c, "department id must be a number", nil)
  92. return
  93. }
  94. err := dept.DeleteByID(id)
  95. if err != nil {
  96. app.Error(c, err.Error())
  97. return
  98. }
  99. app.Success(c, nil)
  100. }
  101. func DeptMyList(c *gin.Context) {
  102. type AdminInfo struct {
  103. ID int `json:"id"`
  104. DeptId int `json:"dept_id"`
  105. }
  106. var adminInfo *AdminInfo
  107. admin.GetInfoByID(c.GetInt("adminID"), []string{"id", "username"}, &adminInfo)
  108. if adminInfo == nil {
  109. app.Response(c, http.StatusUnauthorized, errcode.TokenInvalid, nil)
  110. return
  111. }
  112. depts := dept.GetSubDepts(adminInfo.DeptId)
  113. if depts == nil {
  114. depts = []*models.Dept{}
  115. }
  116. data := gin.H{
  117. "list": depts,
  118. "count": len(depts),
  119. }
  120. app.Success(c, data)
  121. }