func LastArticle() (entities.Article, error) {
	var err error
	var article entities.Article
	db := db.NewDB()
	err = db.QueryTable("article").OrderBy("-id").One(&article, "id", "user_id", "title", "tag", "content", "created_at", "updated_at")
	return article, err
}
func Articles(page int) ([]entities.Article, error) {
	var err error
	var articles []entities.Article
	db := db.NewDB()
	_, err = db.QueryTable("article").All(&articles, "id", "user_id", "title", "tag", "content", "created_at", "updated_at")
	return articles, err
}
func ArticleByIdAndUserId(articleId int64, userId int64) (*entities.Article, error) {
	var err error
	var article entities.Article
	db := db.NewDB()
	err = db.QueryTable("article").Filter("user_id", userId).Filter("id", articleId).One(&article)
	return &article, err
}
func LastArticle() (entities.Article, error) {
	var err error
	var article entities.Article
	db := db.NewDB()
	err = db.QueryTable("article").OrderBy("-id").One(&article)
	return article, err
}
func ValidSystemMail() (entities.SystemMail, error) {

	var mail entities.SystemMail

	db := db.NewDB()

	err := db.QueryTable("system_mail").Filter("valid", entities.SYSTEM_MAIL_VALID_YES).One(&mail)

	return mail, err
}
func Categories() ([]entities.Category, error) {
	var err error
	var categories []entities.Category
	var categoriesKey = constants.CATEGORY_KEY
	err = redis_util.Get(categoriesKey, &categories)
	if err == nil {
		return categories, nil;
	}
	db := db.NewDB()
	_, err = db.QueryTable("category").OrderBy("order").All(&categories, "id", "name", "order", "created_at", "updated_at")
	return categories, err
}
func UserCategories(userId int64) ([]entities.Category, error) {
	var err error
	var categories []entities.Category
	db := orm.NewOrm()
	querySetter := db.QueryTable("category")

	if userId > 0 {
		querySetter = querySetter.Filter("user_id", userId)
	}
	_, err = querySetter.OrderBy("order").All(&categories, "id", "user_id", "name", "order", "created_at", "updated_at")
	return categories, err
}
func TestRel(t *testing.T) {
	db := db.NewDB()
	var articles []entities.Article
	db.QueryTable("article").RelatedSel().Filter("user_id", 1).All(&articles)
}