func SaveArticle(article *entities.Article) error {

	var err error
	db := db.NewDB()
	db.Begin()

	bBuffer := bytes.NewBufferString("insert into article (user_id,title, tags,categories, content, created_at) ")
	bBuffer.WriteString("values(?,?,?,?,?,now())")

	_, err = db.Raw(bBuffer.String(), []interface{}{article.UserId, article.Title, article.Tags, article.Categories, article.Content}).Exec()

	if nil == err {
		var categories []entities.Category
		if len(article.Categories) > 0 {
			categoryNames := strings.Split(article.Categories, ",")
			categories = entities.NewCategories(article.UserId, categoryNames)
		}
		BatchSaveOrUpdateCategory(db, categories)
	}

	if nil == err {
		db.Commit()
	} else {
		db.Rollback()
	}

	return err
}
func UpdateArticle(article *entities.Article) error {

	var err error
	db := db.NewDB()
	db.Begin()

	sql := "update article set title = ? ,tags=?,categories=?, content=?, updated_at=now() where user_id = ? and  id = ? "

	_, err = db.Raw(sql, []interface{}{article.Title, article.Tags, article.Categories, article.Content, article.UserId, article.Id}).Exec()

	if nil == err {
		var categories []entities.Category
		if len(article.Categories) > 0 {
			categoryNames := strings.Split(article.Categories, ",")
			categories = entities.NewCategories(article.UserId, categoryNames)
		}
		BatchSaveOrUpdateCategory(db, categories)
	}

	if nil == err {
		db.Commit()
	} else {
		db.Rollback()
	}

	return err
}
func ArticleLikeLogs(userId int64, articleIds interface{}) []int64 {
	var signs []int64
	articleIdStr := utils.SliceToString(articleIds, ",")
	sql := "select article_id from article_like where user_id = ? and valid=1 and article_id in (" + articleIdStr + ") "
	db := db.NewDB()
	db.Raw(sql, userId).QueryRows(&signs)
	return signs
}
func TopLikeArticles() []entities.Article {

	var articles []entities.Article
	sql := "select id,user_id,title from article where like_count >0 and  created_at > date_sub(now(),interval ? DAY) order by like_count desc limit 10"

	dayRange := ParameterIntValue("like_article_day_range")

	db := db.NewDB()
	db.Raw(sql, []interface{}{dayRange}).QueryRows(&articles)
	return articles
}