package manage

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

func GetShopTopicById(id int64) (domain.ShopTopic, error) {
	var shopTopic domain.ShopTopic
	_, err := configs.Engine.Table("shop_topic").Where("id = ?", id).Get(&shopTopic)
	if err != nil {
		return shopTopic, err
	}
	return shopTopic, nil
}

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

func GetShopTopicList(shopTopic domain.ShopTopic, pageNum, pageSize int) (vo.BaseListVo, error) {
	var arr []domain.ShopTopic
	Count, err := configs.Engine.Table("shop_topic").Limit(pageSize, (pageNum-1)*pageSize).FindAndCount(&arr, &shopTopic)
	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 SaveShopTopic(shopTopic *domain.ShopTopic) (domain.ShopTopic, error) {
	_, err := configs.Engine.Table("shop_topic").Insert(&shopTopic)
	if err != nil {
		return *shopTopic, err
	}
	return *shopTopic, nil
}

func SetShopTopic(shopTopicMap map[string]interface{}, id int64) (domain.ShopTopic, bool, error) {
	var shopTopic domain.ShopTopic
	_, err := configs.Engine.Table("shop_topic").Where("id = ?", id).Update(shopTopicMap)
	if err != nil {
		return shopTopic, false, err
	}
	return shopTopic, true, nil
}

func DeleteShopTopic(id int64) bool {
	var shopTopic domain.ShopTopic
	i, err := configs.Engine.Table("shop_topic").Where("id = ?", id).Delete(&shopTopic)
	if err != nil {
		return false
	}
	return i > 0
}
