package servicer import ( "strings" "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/aftersale/leader" "zhiyuan/services/dept" "zhiyuan/services/role" "github.com/gin-gonic/gin" ) func LeaderList(c *gin.Context) { page := app.HandlePageNum(c) type LeaderList struct { ID int `json:"id"` UserName string `json:"username"` Phone string `json:"phone"` RoleIds string `json:"role_ids"` RoleNames string `json:"role_names"` DeptID string `json:"dept_id"` DeptName string `json:"dept_name"` } leaderList := make([]*LeaderList, 0) where := map[string]string{ "where": " deleted_at=0", "_order_by": "id desc", } showAll := utils.ToInt(c.Query("show_all")) if showAll == 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } param := make(map[string]interface{}) // site_id if adminInfo := admin.GetAdminCache(c.GetInt("adminID")); adminInfo.SiteID > 0 { where["where"] += " AND (site_id={{site_id}} OR site_id=0)" param["site_id"] = adminInfo.SiteID } if _, err := leader.GetList(where, param, &leaderList); err != nil { app.Error(c, err.Error()) return } // get dept name deptList, _ := dept.GetListByCache() deptListMap := utils.ParseSliceMap(deptList, "id") for k, v := range leaderList { if dept, ok := deptListMap[v.DeptID]; ok { v.DeptName = utils.ToStr(dept["name"]) } leaderList[k] = v } // get role name roleListMap := make(map[int]string) if roleList, err := role.GetList(nil, []string{"id, name"}, app.Page{}, nil); err == nil { for _, v := range roleList { roleListMap[v.ID] = v.Name } } for k, v := range leaderList { roleNames := "" roleSlice := strings.Split(v.RoleIds, ",") for _, vv := range roleSlice { roleNames += "," + roleListMap[utils.ToInt(vv)] } v.RoleNames = strings.TrimLeft(roleNames, ",") leaderList[k] = v } app.Success(c, leaderList) }