// 获得帖子详细信息(包括详细回复) // 为了避免转换,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 }
// 获得帖子详细信息(包括详细回复) // 为了避免转换,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 }
// 获得某个用户最近的帖子 func FindRecentTopics(uid int) []*model.Topic { topics, err := model.NewTopic().Where("uid=" + strconv.Itoa(uid)).Order("ctime DESC").Limit("0, 5").FindAll() if err != nil { logger.Errorln("topic service FindRecentTopics error:", err) return nil } for _, topic := range topics { topic.Node = model.GetNodeName(topic.Nid) } return topics }
// 获得热门节点 func FindHotNodes() []map[string]interface{} { strSql := "SELECT nid, COUNT(1) AS topicnum FROM topics GROUP BY nid ORDER BY topicnum DESC LIMIT 10" rows, err := model.NewTopic().DoSql(strSql) if err != nil { logger.Errorln("topic service FindHotNodes error:", err) return nil } nodes := make([]map[string]interface{}, 0, 10) for rows.Next() { var nid, topicnum int err = rows.Scan(&nid, &topicnum) if err != nil { logger.Errorln("rows.Scan error:", err) continue } name := model.GetNodeName(nid) node := map[string]interface{}{ "name": name, "nid": nid, } nodes = append(nodes, node) } return nodes }