Exemple #1
0
func TestIn(t *testing.T) {

	var slice = []interface{}{1, 2, 3, 4, 5, 6}

	db := db.NewDB()
	query := db.From("article").In("id", slice)

	fmt.Print(query.ToSql())

}
func UserMap(ids []interface{}) map[int64]entities.User {
	var userMap map[int64]entities.User = make(map[int64]entities.User)
	var users []entities.User
	db := db.NewDB()
	db.From("user").Select("id", "name", "nick", "view_count", "head").In("id", ids).All(&users)

	for _, user := range users {
		userMap[user.Id] = user
	}
	return userMap
}
func TestIn(t *testing.T) {

	var slice = []interface{}{1, 2, 3, 4, 5, 6}

	var articles []entities.Article
	db := db.NewDB()
	db.From("article").In("id", slice).All(&articles)

	for _, value := range articles {
		fmt.Print(value)
	}

}
func AllArticles(userId int64, pagination *db.Pagination) {
	db := db.NewDB()
	var articles []entities.Article
	query := db.From("article")

	if userId > 0 {
		query.Where("user_id", userId)
	}

	query.OrderBy("created_at desc").FillPagination(&articles, pagination)

	articles = setUser(articles)

	pagination.SetData(articles)

}
func ArticlesGyCategory(userId int64, category string, pagination *db.Pagination, isFilterUserId bool) {
	db := db.NewDB()
	var articles []entities.Article
	query := db.From("article")
	if userId > 0 && isFilterUserId {
		query.Where("user_id", userId)
	}

	if len(category) > 0 {
		category = strings.ToLower(category)
		beego.Error("category", category)
		query.Like("categories", "%"+category+"%")
	}

	query.OrderBy("created_at desc").FillPagination(&articles, pagination)

	//设置当前用户点赞标记
	if len(pagination.Data) < 1 || userId <= 0 {
		return
	}

	ids, err := utils.ExtractFieldValues(pagination.Data, "Id")
	if nil != err {
		return
	}
	if userId > 0 {
		signs := ArticleLikeLogs(userId, ids)
		signMap := make(map[int64]bool)
		for _, v := range signs {
			signMap[v] = true
		}
		var newArticles []entities.Article
		for _, v := range articles {
			v.HasLike = signMap[v.Id]
			newArticles = append(newArticles, v)
		}

		pagination.SetData(newArticles)

		newArticles = setUser(newArticles)

		pagination.SetData(newArticles)
	}

}
func AllArticles(userId int64, pagination *db.Pagination) {
	db := db.NewDB()
	var articles []entities.Article
	db.From("article").OrderBy("created_at desc").FillPagination(&articles, pagination)
}
func TopLikeUsers() []entities.User {
	var users []entities.User
	db := db.NewDB()
	db.From("user").Select("id", "name", "nick", "view_count", "head").Great("like_count", 0).OrderBy("like_count desc").Limit(0, 10).All(&users)
	return users
}