package manage

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

func GetShopTopicSkuById(id int64) (domain.ShopTopicSku, error) {
	var shopTopicSku domain.ShopTopicSku
	_, err := configs.Engine.Table("shop_topic_sku").Where("id = ?", id).Get(&shopTopicSku)
	if err != nil {
		return shopTopicSku, err
	}
	return shopTopicSku, nil
}

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

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

func SetShopTopicSku(shopTopicSkuMap map[string]interface{}, id int64) (domain.ShopTopicSku, bool, error) {
	var shopTopicSku domain.ShopTopicSku
	_, err := configs.Engine.Table("shop_topic_sku").Where("id = ?", id).Update(shopTopicSkuMap)
	if err != nil {
		return shopTopicSku, false, err
	}
	return shopTopicSku, true, nil
}

func DeleteShopTopicSku(id int64) bool {
	var shopTopicSku domain.ShopTopicSku
	i, err := configs.Engine.Table("shop_topic_sku").Where("id = ?", id).Delete(&shopTopicSku)
	if err != nil {
		return false
	}
	return i > 0
}
