package manage

import (
	"demo/configs"
	"demo/data/domain"
	"demo/data/domain/vo"
)

func GetArticleTagById(id int64) (domain.ArticleTag, error) {
	var articleTag domain.ArticleTag
	_, err := configs.Engine.Table("article_tag").Where("id = ?", id).Get(&articleTag)
	if err != nil {
		return articleTag, err
	}
	return articleTag, nil
}

func GetArticleTagListInId(ids []int64) ([]domain.ArticleTag, error) {
	rows, err := configs.Engine.Table("article_tag").In("id", ids).Rows(&domain.ArticleTag{})
	if err != nil {
		return make([]domain.ArticleTag, 0), err
	}
	defer rows.Close()
	var list []domain.ArticleTag
	for rows.Next() {
		var u domain.ArticleTag
		rows.Scan(&u)
		list = append(list, u)
	}
	return list, nil
}

func GetArticleTagList(articleTag domain.ArticleTag, pageNum, pageSize int) (vo.BaseListVo, error) {
	var arr []domain.ArticleTag
	Count, err := configs.Engine.Table("article_tag").Limit(pageSize, (pageNum-1)*pageSize).FindAndCount(&arr, &articleTag)
	var vo vo.BaseListVo
	if err != nil {
		return vo, err
	}
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, len(arr))
	for i, v := range arr {
		vo.List[i] = v
	}
	vo.Total = int(Count)
	return vo, nil
}

func SaveArticleTag(articleTag *domain.ArticleTag) (domain.ArticleTag, error) {
	_, err := configs.Engine.Table("article_tag").Insert(&articleTag)
	if err != nil {
		return *articleTag, err
	}
	return *articleTag, nil
}

func SetArticleTag(articleTagMap map[string]interface{}, id int64) (domain.ArticleTag, bool, error) {
	var articleTag domain.ArticleTag
	_, err := configs.Engine.Table("article_tag").Where("id = ?", id).Update(articleTagMap)
	if err != nil {
		return articleTag, false, err
	}
	return articleTag, true, nil
}

func DeleteArticleTag(id int64) bool {
	var articleTag domain.ArticleTag
	i, err := configs.Engine.Table("article_tag").Where("id = ?", id).Delete(&articleTag)
	if err != nil {
		return false
	}
	return i > 0
}
