package manage

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

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

func GetListArticleTopic(articleTopic domain.ArticleTopic, pageNum, pageSize int) (vo.BaseListVo, error) {
	rows, err := configs.Engine.Limit(pageSize, (pageNum-1)*pageSize).Desc("id").Rows(&articleTopic)
	var vo vo.BaseListVo
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, 0)
	for rows.Next() {
		var u domain.ArticleTopic
		rows.Scan(&u)
		vo.List = append(vo.List, u)
	}
	//查询数量
	count, err := configs.Engine.Count(&articleTopic)
	vo.Total = int(count)
	return vo, nil
}

func SaveArticleTopic(articleTopic *domain.ArticleTopic) (domain.ArticleTopic, error) {
	_, err := configs.Engine.Insert(&articleTopic)
	if err != nil {
		return *articleTopic, err
	}
	return *articleTopic, nil
}

func SetArticleTopic(articleTopicMap map[string]interface{}, id int) (domain.ArticleTopic, bool, error) {
	var articleTopic domain.ArticleTopic
	_, err := configs.Engine.Table("article_topic").Where("id = ?", id).Update(articleTopicMap)
	if err != nil {
		return articleTopic, false, err
	}
	return articleTopic, true, nil
}
