func FindUsers() (map[int]*model.User, error) { userList, err := model.NewUser().FindAll() if err != nil { logger.Errorln("user service FindUsers Error:", err) return nil, err } userCount := len(userList) userMap := make(map[int]*model.User, userCount) uids := make([]int, userCount) for i, user := range userList { userMap[user.Uid] = user uids[i] = user.Uid } // 获得角色信息 userRoleList, err := model.NewUserRole().Where("uid in (" + util.Join(uids, ",") + ")").FindAll() if err != nil { logger.Errorln("user service FindUsers Error:", err) return nil, err } for _, userRole := range userRoleList { user := userMap[userRole.Uid] if len(user.Roleids) == 0 { user.Roleids = []int{userRole.Roleid} user.Rolenames = []string{model.AllRole[userRole.Roleid].Name} } else { user.Roleids = append(user.Roleids, userRole.Roleid) user.Rolenames = append(user.Rolenames, model.AllRole[userRole.Roleid].Name) } } return userMap, nil }
// 获取多个帖子详细信息 func FindTopicsByTids(tids []int) []*model.Topic { inTids := util.Join(tids, ",") topics, err := model.NewTopic().Where("tid in(" + inTids + ")").FindAll() if err != nil { logger.Errorln("topic service FindRecentReplies error:", err) return nil } return topics }
// 获取多个wiki页面详细信息 func FindWikisByIds(ids []int) []*model.Wiki { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") wikis, err := model.NewWiki().Where("id in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("wiki service FindWikisByIds error:", err) return nil } return wikis }
// 获取多个文章详细信息 func FindArticlesByIds(ids []int) []*model.Article { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") articles, err := model.NewArticle().Where("id in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("article service FindArticlesByIds error:", err) return nil } return articles }
// 获取多个资源详细信息 func FindResourcesByIds(ids []int) []*model.Resource { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") resources, err := model.NewResource().Where("id in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("resource service FindResourcesByIds error:", err) return nil } return resources }
// 获取多个项目详细信息 func FindProjectsByIds(ids []int) []*model.OpenProject { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") projects, err := model.NewOpenProject().Where("id in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("project service FindProjectsByIds error:", err) return nil } return projects }
// 获取多个评论信息 func FindCommentsByIds(cids []int) []*model.Comment { if len(cids) == 0 { return nil } inCids := util.Join(cids, ",") comments, err := model.NewComment().Where("cid in(" + inCids + ")").FindAll() if err != nil { logger.Errorln("comment service FindCommentsByIds error:", err) return nil } return comments }
// 获取多个主题详细信息 func FindTopicsByIds(ids []int) []*model.Topic { if len(ids) == 0 { return nil } inIds := util.Join(ids, ",") topics, err := model.NewTopic().Where("tid in(" + inIds + ")").FindAll() if err != nil { logger.Errorln("topic service FindTopicsByIds error:", err) return nil } return topics }
// 获取用户信息 func getUserInfos(uids map[int]int) map[int]*model.User { // 获取用户信息 inUids := util.Join(util.MapIntKeys(uids), ",") users, err := model.NewUser().Where("uid in(" + inUids + ")").FindAll() if err != nil { logger.Errorln("user service getUserInfos Error:", err) return map[int]*model.User{} } userMap := make(map[int]*model.User, len(users)) for _, user := range users { userMap[user.Uid] = user } return userMap }
// 获得某个分类的资源列表 func FindResourcesByCatid(catid string) []map[string]interface{} { resourceList, err := model.NewResource().Where("catid=" + catid).Order("mtime DESC").FindAll() if err != nil { logger.Errorln("resource service FindResourcesByCatid error:", err) return nil } count := len(resourceList) ids := make([]int, count) uids := make(map[int]int) for i, resource := range resourceList { ids[i] = resource.Id uids[resource.Uid] = resource.Uid } // 获取扩展信息(计数) resourceExList, err := model.NewResourceEx().Where("id in(" + util.Join(ids, ",") + ")").FindAll() if err != nil { logger.Errorln("resource service FindResourcesByCatid Error:", err) return nil } resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList)) for _, resourceEx := range resourceExList { resourceExMap[resourceEx.Id] = resourceEx } userMap := getUserInfos(uids) resources := make([]map[string]interface{}, count) for i, resource := range resourceList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, resource) util.Struct2Map(tmpMap, resourceExMap[resource.Id]) tmpMap["user"] = userMap[resource.Uid] // 链接的host if resource.Form == model.LinkForm { urlObj, err := url.Parse(resource.Url) if err == nil { tmpMap["host"] = urlObj.Host } } else { tmpMap["url"] = "/resources/" + strconv.Itoa(resource.Id) } resources[i] = tmpMap } return resources }
// 标记消息已读 func MarkHasRead(ids []int, isSysMsg bool, uid int) bool { if len(ids) == 0 { return true } condition := "id=" + strconv.Itoa(ids[0]) if len(ids) > 1 { condition = "id in(" + util.Join(ids, ",") + ")" } var err error if isSysMsg { err = model.NewSystemMessage().Set("hasread=" + model.HasRead).Where(condition).Update() } else { err = model.NewMessage().Set("hasread=" + model.HasRead).Where(condition).Update() } if err != nil { logger.Errorln("message service MarkHasRead Error:", err) return false } return true }
func FindTopicsByWhere(where, order, limit string) (topics []map[string]interface{}, total int) { topicObj := model.NewTopic() if where != "" { topicObj.Where(where) } if order != "" { topicObj.Order(order) } if limit != "" { topicObj.Limit(limit) } topicList, err := topicObj.FindAll() if err != nil { logger.Errorln("topic service topicObj.FindAll Error:", err) return } // 获得总帖子数 total, err = topicObj.Count() if err != nil { logger.Errorln("topic service topicObj.Count Error:", err) return } count := len(topicList) tids := make([]int, count) uids := make(map[int]int) nids := make([]int, count) for i, topic := range topicList { tids[i] = topic.Tid uids[topic.Uid] = topic.Uid if topic.Lastreplyuid != 0 { uids[topic.Lastreplyuid] = topic.Lastreplyuid } nids[i] = topic.Nid } // 获取扩展信息(计数) topicExList, err := model.NewTopicEx().Where("tid in(" + util.Join(tids, ",") + ")").FindAll() if err != nil { logger.Errorln("topic service NewTopicEx FindAll Error:", err) return } topicExMap := make(map[int]*model.TopicEx, len(topicExList)) for _, topicEx := range topicExList { topicExMap[topicEx.Tid] = topicEx } userMap := getUserInfos(uids) // 获取节点信息 nodes := model.GetNodesName(nids) topics = make([]map[string]interface{}, count) for i, topic := range topicList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, topic) util.Struct2Map(tmpMap, topicExMap[topic.Tid]) tmpMap["user"] = userMap[topic.Uid] // 有人回复 if tmpMap["lastreplyuid"].(int) != 0 { tmpMap["lastreplyusername"] = userMap[tmpMap["lastreplyuid"].(int)].Username } tmpMap["node"] = nodes[tmpMap["nid"].(int)] topics[i] = tmpMap } return }
// 获得某个分类的资源列表 // page 当前第几页 func FindResourcesByCatid(catid string, page int) (resources []map[string]interface{}, total int) { var offset = 0 if page > 1 { offset = (page - 1) * PAGE_NUM } resourceObj := model.NewResource() limit := strconv.Itoa(offset) + "," + strconv.Itoa(PAGE_NUM) resourceList, err := resourceObj.Where("catid=?", catid).Order("mtime DESC").Limit(limit).FindAll() if err != nil { logger.Errorln("resource service FindResourcesByCatid error:", err) return } // 获得该类别总资源数 total, err = resourceObj.Count() if err != nil { logger.Errorln("resource service resourceObj.Count Error:", err) return } count := len(resourceList) ids := make([]int, count) uids := make([]int, count) for i, resource := range resourceList { ids[i] = resource.Id uids[i] = resource.Uid } // 获取扩展信息(计数) resourceExList, err := model.NewResourceEx().Where("id in(" + util.Join(ids, ",") + ")").FindAll() if err != nil { logger.Errorln("resource service FindResourcesByCatid Error:", err) return } resourceExMap := make(map[int]*model.ResourceEx, len(resourceExList)) for _, resourceEx := range resourceExList { resourceExMap[resourceEx.Id] = resourceEx } userMap := GetUserInfos(uids) resources = make([]map[string]interface{}, count) for i, resource := range resourceList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, resource) util.Struct2Map(tmpMap, resourceExMap[resource.Id]) tmpMap["user"] = userMap[resource.Uid] // 链接的host if resource.Form == model.LinkForm { urlObj, err := url.Parse(resource.Url) if err == nil { tmpMap["host"] = urlObj.Host } } else { tmpMap["url"] = "/resources/" + strconv.Itoa(resource.Id) } resources[i] = tmpMap } return }