// 获取总记录数 func CountObjects(qs orm.QuerySeter) (int64, error) { cnt, err := qs.Count() if err != nil { beego.Error("models.CountObjects ", err) return 0, err } return cnt, err }
func (this *TeamController) Teams() { query := strings.TrimSpace(this.GetString("query", "")) if utils.HasDangerousCharacters(query) { this.ServeErrJson("query is invalid") return } per := this.MustGetInt("per", 10) me := this.Ctx.Input.GetData("CurrentUser").(*User) var teams orm.QuerySeter if me.Role == ROOT_ADMIN_ROLE { teams = QueryAllTeams(query) } else { var err error teams, err = QueryMineTeams(query, me.Id) if err != nil { this.ServeErrJson("occur error " + err.Error()) return } } total, err := teams.Count() if err != nil { this.ServeErrJson("occur error " + err.Error()) return } pager := this.SetPaginator(per, total) teams = teams.Limit(per, pager.Offset()) var ts []Team _, err = teams.All(&ts) if err != nil { this.ServeErrJson("occur error " + err.Error()) return } nteams := make([]map[string]interface{}, 0) for _, v := range ts { cu := ReadUserById(v.Creator) t := make(map[string]interface{}) t["Id"] = v.Id t["Name"] = v.Name t["Resume"] = v.Resume t["CreatorCnname"] = cu.Cnname t["CreatorName"] = cu.Name t["IsAdmin"] = (v.IsAdmin(me.Id) || me.Role == ROOT_ADMIN_ROLE) nteams = append(nteams, t) } this.Data["Teams"] = nteams this.Data["Query"] = query this.Data["Me"] = me this.Data["IamRoot"] = me.Role == ROOT_ADMIN_ROLE this.TplName = "team/list.html" }
// query objects and set to template func (this *ModelAdminRouter) SetObjects(qs orm.QuerySeter, objects interface{}) error { cnt, err := qs.Count() if err != nil { return err } // create paginator p := this.SetPaginator(20, cnt) if cnt, err := qs.Limit(p.PerPageNums, p.Offset()).RelatedSel().All(objects); err != nil { return err } else { this.Data["Objects"] = objects this.Data["ObjectsCnt"] = cnt } return nil }
func (this *UserRouter) getFollows(user *models.User, following bool) []map[string]interface{} { limit := 20 var qs orm.QuerySeter if following { qs = user.FollowingUsers() } else { qs = user.FollowerUsers() } nums, _ := qs.Count() pager := this.SetPaginator(limit, nums) qs = qs.Limit(limit, pager.Offset()) var follows []*models.Follow if following { qs.RelatedSel("FollowUser").All(&follows, "FollowUser") } else { qs.RelatedSel("User").All(&follows, "User") } if len(follows) == 0 { return nil } ids := make([]int, 0, len(follows)) for _, follow := range follows { if following { ids = append(ids, follow.FollowUser.Id) } else { ids = append(ids, follow.User.Id) } } var eids orm.ParamsList this.User.FollowingUsers().Filter("FollowUser__in", ids).ValuesFlat(&eids, "FollowUser__Id") var fids map[int]bool if len(eids) > 0 { fids = make(map[int]bool) for _, id := range eids { tid, _ := utils.StrTo(utils.ToStr(id)).Int() if tid > 0 { fids[tid] = true } } } users := make([]map[string]interface{}, 0, len(follows)) for _, follow := range follows { IsFollowed := false var u *models.User if following { u = follow.FollowUser } else { u = follow.User } if fids != nil { IsFollowed = fids[u.Id] } users = append(users, map[string]interface{}{ "User": u, "IsFollowed": IsFollowed, }) } return users }