func (m *UnwantWord) GetList(page, limit int, needTotal bool) (*Pager, error) { var unwant []models.UnwantWord tableName := "unwant_word" sql := "select * from " + tableName if page == 0 { page = 1 } if limit == 0 { limit = 20 } offset := (page - 1) * limit sql = sql + " limit " + fmt.Sprintf("%d", offset) + ", " + fmt.Sprintf("%d", limit) _, err := m.Db.Select(&unwant, sql) userIds := []string{} unwantCnt := len(unwant) // Map 相关子id for i := 0; i < unwantCnt; i++ { userId := fmt.Sprintf("%d", unwant[i].Creator) if !helpers.StringInSlice(userId, userIds) { userIds = append(userIds, userId) } } total := int64(0) if needTotal { total, _ = m.Db.SelectInt("select count(*) from " + tableName) } // 获取相关用户数据. var userMap map[int64]models.User if len(userIds) > 0 { userModule := NewUser(m.Db) userMap, err = userModule.GetUserMapById(userIds) if err != nil { return nil, err } } // Merge数据. for i := 0; i < unwantCnt; i++ { unwant[i].Author = userMap[unwant[i].Creator].Username } pager := &Pager{ Total: total, List: unwant, } return pager, err }
// 未登录用户只能返回统一标准topic list. func (c *Topic) GetTopicList(page int, order string, limit int, maxid int, needTotal bool) (*Pager, error) { var topics []models.Topic tableName := "topic" sql := "select * from " + tableName + " where is_del=0" if maxid > 0 { sql = sql + " and topic_id > " + strconv.Itoa(maxid) } if page == 0 { page = 1 } if limit == 0 { limit = 20 } offset := (page - 1) * limit switch order { case "recommend": order = "liked_cnt desc" case "nearby": order = "topic_id desc" case "hottest": order = "ommented_cnt desc" default: order = "topic_id desc" } sql = sql + " order by " + order sql = sql + " limit " + fmt.Sprintf("%d", offset) + ", " + fmt.Sprintf("%d", limit) _, err := c.Db.Select(&topics, sql) picIds := []string{} userIds := []string{} topicCnt := len(topics) // Map 相关子id for i := 0; i < topicCnt; i++ { bgId := fmt.Sprintf("%d", topics[i].BgId) userId := fmt.Sprintf("%d", topics[i].UserId) if topics[i].BgId != 0 && !helpers.StringInSlice(bgId, picIds) { picIds = append(picIds, bgId) } if !helpers.StringInSlice(userId, userIds) { userIds = append(userIds, userId) } } total := int64(0) if needTotal { total, _ = c.Db.SelectInt("select count(*) from " + tableName) } // 获取相关图片数据. var urlMap map[int64]models.Attach if len(picIds) > 0 { attachModule := NewAttach(c.Db) urlMap, err = attachModule.GetAttachMapById(picIds) if err != nil { return nil, err } } // 获取相关用户数据. var userMap map[int64]models.User if len(userIds) > 0 { userModule := NewUser(c.Db) userMap, err = userModule.GetUserMapById(userIds) if err != nil { return nil, err } } // Merge数据. for i := 0; i < topicCnt; i++ { topics[i].Background = urlMap[topics[i].BgId].Uri topics[i].Author = userMap[topics[i].UserId].Username } pager := &Pager{ Total: total, List: topics, } return pager, err }