// 获得回复最多的10条帖子(TODO:避免一直显示相同的) func FindHotTopics() []map[string]interface{} { topicExList, err := model.NewTopicEx().Order("reply DESC").Limit("0,10").FindAll() if err != nil { logger.Errorln("topic service FindHotReplies error:", err) return nil } tidMap := make(map[int]int, len(topicExList)) topicExMap := make(map[int]*model.TopicEx, len(topicExList)) for _, topicEx := range topicExList { tidMap[topicEx.Tid] = topicEx.Tid topicExMap[topicEx.Tid] = topicEx } tids := util.MapIntKeys(tidMap) topics := FindTopicsByTids(tids) if topics == nil { return nil } uidMap := make(map[int]int, len(topics)) for _, topic := range topics { uidMap[topic.Uid] = topic.Uid } userMap := getUserInfos(uidMap) result := make([]map[string]interface{}, len(topics)) for i, topic := range topics { oneTopic := make(map[string]interface{}) util.Struct2Map(oneTopic, topic) util.Struct2Map(oneTopic, topicExMap[topic.Tid]) oneTopic["user"] = userMap[topic.Uid] result[i] = oneTopic } return result }
// 获得资源详细信息 func FindResource(id string) (resourceMap map[string]interface{}, comments []map[string]interface{}) { condition := "id=" + id resource := model.NewResource() err := resource.Where(condition).Find() if err != nil { logger.Errorln("resource service FindResource error:", err) return } resourceMap = make(map[string]interface{}) util.Struct2Map(resourceMap, resource) resourceMap["catname"] = GetCategoryName(resource.Catid) // 链接的host if resource.Form == model.LinkForm { urlObj, err := url.Parse(resource.Url) if err == nil { resourceMap["host"] = urlObj.Host } } else { resourceMap["url"] = "/resources/" + strconv.Itoa(resource.Id) } resourceEx := model.NewResourceEx() err = resourceEx.Where(condition).Find() if err != nil { logger.Errorln("resource service FindResource Error:", err) return } util.Struct2Map(resourceMap, resourceEx) // 评论信息 comments, ownerUser, _ := FindObjComments(id, strconv.Itoa(model.TYPE_RESOURCE), resource.Uid, 0) resourceMap["user"] = ownerUser return }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,tid传string类型 func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) { condition := "tid=" + tid // 帖子信息 topic := model.NewTopic() err = topic.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } // 帖子不存在 if topic.Tid == 0 { return } topicMap = make(map[string]interface{}) util.Struct2Map(topicMap, topic) topicEx := model.NewTopicEx() err = topicEx.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } if topicEx.Tid == 0 { return } util.Struct2Map(topicMap, topicEx) // 节点名字 topicMap["node"] = model.GetNodeName(topic.Nid) // 回复信息(评论) replyList, err := model.NewComment().Where("objid=" + tid + " and objtype=" + strconv.Itoa(model.TYPE_TOPIC)).FindAll() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } replyNum := len(replyList) uids := make(map[int]int, replyNum+1) uids[topic.Uid] = topic.Uid for _, reply := range replyList { uids[reply.Uid] = reply.Uid } // 获得用户信息 userMap := getUserInfos(uids) topicMap["user"] = userMap[topic.Uid] // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = userMap[topicMap["lastreplyuid"].(int)].Username } replies = make([]map[string]interface{}, 0, replyNum) for _, reply := range replyList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, reply) tmpMap["user"] = userMap[reply.Uid] replies = append(replies, tmpMap) } return }
// 获得主题详细信息(包括详细回复) // 为了避免转换,tid传string类型 func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) { condition := "tid=" + tid // 主题信息 topic := model.NewTopic() err = topic.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } // 主题不存在 if topic.Tid == 0 { err = errors.New("The topic of tid is not exists") return } topicMap = make(map[string]interface{}) util.Struct2Map(topicMap, topic) // 解析内容中的 @ topicMap["content"] = decodeTopicContent(topic) topicEx := model.NewTopicEx() err = topicEx.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } if topicEx.Tid == 0 { return } util.Struct2Map(topicMap, topicEx) // 节点名字 topicMap["node"] = GetNodeName(topic.Nid) // 回复信息(评论) replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid) topicMap["user"] = owerUser // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = lastReplyUser.Username } if topic.EditorUid != 0 { topicMap["editor_username"] = FindUsernameByUid(topic.EditorUid) } return }
// 获得发给某人的短消息(收件箱) func FindToMsgsByUid(uid string) []map[string]interface{} { messages, err := model.NewMessage().Where("to=" + uid + " AND tdel=" + model.TdelNotDel).Order("ctime DESC").FindAll() if err != nil { logger.Errorln("message service FindToMsgsByUid Error:", err) return nil } uids := make(map[int]int) ids := make([]int, 0, len(messages)) for _, message := range messages { uids[message.From] = message.From if message.Hasread == model.NotRead { ids = append(ids, message.Id) } } // 标记已读 go MarkHasRead(ids, false, util.MustInt(uid)) userMap := getUserInfos(uids) result := make([]map[string]interface{}, len(messages)) for i, message := range messages { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, message) tmpMap["user"] = userMap[message.From] // 为了跟系统消息一致 tmpMap["title"] = "发来了一条消息:" result[i] = tmpMap } return result }
// 某个wiki页面详细信息 func FindWiki(uri string) map[string]interface{} { wiki := model.NewWiki() if err := wiki.Where("uri=" + uri).Find(); err != nil { logger.Errorln("wiki service FindWiki error:", err) return nil } uids := make(map[int]int) uids[wiki.Uid] = wiki.Uid if wiki.Cuid != "" { cuids := strings.Split(wiki.Cuid, ",") for _, cuid := range cuids { tmpUid := util.MustInt(cuid) uids[tmpUid] = tmpUid } } userMap := getUserInfos(uids) result := make(map[string]interface{}) util.Struct2Map(result, wiki) result["user"] = userMap[wiki.Uid] if wiki.Cuid != "" { cuids := strings.Split(wiki.Cuid, ",") cusers := make([]*model.User, len(cuids)) for i, cuid := range cuids { cusers[i] = userMap[util.MustInt(cuid)] } result["cuser"] = cusers } return result }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,tid传string类型 func FindTopicByTid(tid string) (topicMap map[string]interface{}, replies []map[string]interface{}, err error) { condition := "tid=" + tid // 帖子信息 topic := model.NewTopic() err = topic.Where(condition).Find() if err != nil { logger.Errorln("topic service FindTopicByTid Error:", err) return } // 帖子不存在 if topic.Tid == 0 { return } topicMap = make(map[string]interface{}) util.Struct2Map(topicMap, topic) // 节点名字 topicMap["node"] = model.GetNodeName(topic.Nid) // 回复信息(评论) replies, owerUser, lastReplyUser := FindObjComments(tid, strconv.Itoa(model.TYPE_TOPIC), topic.Uid, topic.Lastreplyuid) topicMap["user"] = owerUser // 有人回复 if topic.Lastreplyuid != 0 { topicMap["lastreplyusername"] = lastReplyUser.Username } return }
// 获得某个对象的所有评论 // owner: 被评论对象属主 // TODO:分页暂不做 func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) { commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll() if err != nil { logger.Errorln("comment service FindObjComments Error:", err) return } commentNum := len(commentList) uids := make(map[int]int, commentNum+1) uids[owner] = owner // 避免某些情况下最后回复人没在回复列表中 uids[lastCommentUid] = lastCommentUid for _, comment := range commentList { uids[comment.Uid] = comment.Uid } // 获得用户信息 userMap := getUserInfos(uids) ownerUser = userMap[owner] if lastCommentUid != 0 { lastReplyUser = userMap[lastCommentUid] } comments = make([]map[string]interface{}, 0, commentNum) for _, comment := range commentList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, comment) tmpMap["content"] = decodeCmtContent(comment) tmpMap["user"] = userMap[comment.Uid] comments = append(comments, tmpMap) } return }
// 获得某个对象的所有评论 // owner: 被评论对象属主 // TODO:分页暂不做 func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) { commentList, err := model.NewComment().Where("objid=" + objid + " and objtype=" + objtype).FindAll() if err != nil { logger.Errorln("comment service FindObjComments Error:", err) return } uids := util.Models2Intslice(commentList, "Uid") // 避免某些情况下最后回复人没在回复列表中 uids = append(uids, owner, lastCommentUid) // 获得用户信息 userMap := GetUserInfos(uids) ownerUser = userMap[owner] if lastCommentUid != 0 { lastReplyUser = userMap[lastCommentUid] } comments = make([]map[string]interface{}, 0, len(commentList)) for _, comment := range commentList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, comment) tmpMap["content"] = template.HTML(decodeCmtContent(comment)) tmpMap["user"] = userMap[comment.Uid] comments = append(comments, tmpMap) } return }
// 获得某个分类的资源列表 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 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 } 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) 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 }
// 获取某人发送的消息 func FindFromMsgsByUid(uid string) []map[string]interface{} { messages, err := model.NewMessage().Where("from=" + uid + " AND fdel=" + model.FdelNotDel).Order("ctime DESC").FindAll() if err != nil { logger.Errorln("message service FindFromMsgsByUid Error:", err) return nil } uids := make(map[int]int) for _, message := range messages { uids[message.To] = message.To } userMap := getUserInfos(uids) result := make([]map[string]interface{}, len(messages)) for i, message := range messages { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, message) tmpMap["user"] = userMap[message.To] result[i] = tmpMap } return result }
// 获得最新资源 func FindRecentResources() []map[string]interface{} { resourceList, err := model.NewResource().Limit("0,10").Order("mtime DESC").FindAll() if err != nil { logger.Errorln("resource service FindRecentResources error:", err) return nil } uids := util.Models2Intslice(resourceList, "Uid") userMap := GetUserInfos(uids) count := len(resourceList) resources := make([]map[string]interface{}, count) for i, resource := range resourceList { tmpMap := make(map[string]interface{}) util.Struct2Map(tmpMap, resource) tmpMap["user"] = userMap[resource.Uid] resources[i] = tmpMap } return resources }
// 获得回复最多的10条帖子(TODO:避免一直显示相同的) func FindHotTopics() []map[string]interface{} { topics, err := model.NewTopic().Order("reply DESC").Limit("0,10").FindAll() if err != nil { logger.Errorln("topic service FindHotReplies error:", err) return nil } uidMap := make(map[int]int, len(topics)) for _, topic := range topics { uidMap[topic.Uid] = topic.Uid } userMap := getUserInfos(uidMap) result := make([]map[string]interface{}, len(topics)) for i, topic := range topics { oneTopic := make(map[string]interface{}) util.Struct2Map(oneTopic, topic) oneTopic["user"] = userMap[topic.Uid] result[i] = oneTopic } return result }
// 获得某个分类的资源列表 // 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 }