leader.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package servicer
  2. import (
  3. "strings"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/admin"
  7. "zhiyuan/services/aftersale/leader"
  8. "zhiyuan/services/dept"
  9. "zhiyuan/services/role"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func LeaderList(c *gin.Context) {
  13. page := app.HandlePageNum(c)
  14. type LeaderList struct {
  15. ID int `json:"id"`
  16. UserName string `json:"username"`
  17. Phone string `json:"phone"`
  18. RoleIds string `json:"role_ids"`
  19. RoleNames string `json:"role_names"`
  20. DeptID string `json:"dept_id"`
  21. DeptName string `json:"dept_name"`
  22. }
  23. leaderList := make([]*LeaderList, 0)
  24. where := map[string]string{
  25. "where": " deleted_at=0",
  26. "_order_by": "id desc",
  27. }
  28. showAll := utils.ToInt(c.Query("show_all"))
  29. if showAll == 0 {
  30. where["_page_size"] = utils.ToStr(page.PageSize)
  31. where["_page_num"] = utils.ToStr(page.PageNum)
  32. }
  33. param := make(map[string]interface{})
  34. // site_id
  35. if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 {
  36. where["where"] += " AND (site_id={{site_id}} OR site_id=0)"
  37. param["site_id"] = adminInfo.SiteID
  38. }
  39. if _, err := leader.GetList(where, param, &leaderList); err != nil {
  40. app.Error(c, err.Error())
  41. return
  42. }
  43. // get dept name
  44. deptList, _ := dept.GetListByCache()
  45. deptListMap := utils.ParseSliceMap(deptList, "id")
  46. for k, v := range leaderList {
  47. if dept, ok := deptListMap[v.DeptID]; ok {
  48. v.DeptName = utils.ToStr(dept["name"])
  49. }
  50. leaderList[k] = v
  51. }
  52. // get role name
  53. roleListMap := make(map[int]string)
  54. if roleList, err := role.GetList(nil, []string{"id, name"}, app.Page{}, nil); err == nil {
  55. for _, v := range roleList {
  56. roleListMap[v.ID] = v.Name
  57. }
  58. }
  59. for k, v := range leaderList {
  60. roleNames := ""
  61. roleSlice := strings.Split(v.RoleIds, ",")
  62. for _, vv := range roleSlice {
  63. roleNames += "," + roleListMap[utils.ToInt(vv)]
  64. }
  65. v.RoleNames = strings.TrimLeft(roleNames, ",")
  66. leaderList[k] = v
  67. }
  68. app.Success(c, leaderList)
  69. }