package manage

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

func GetShopAdviceCarouselById(id int64) (domain.ShopAdviceCarousel, error) {
	var shopAdviceCarousel domain.ShopAdviceCarousel
	_, err := configs.Engine.Table("shop_advice_carousel").Where("id = ?", id).Get(&shopAdviceCarousel)
	if err != nil {
		return shopAdviceCarousel, err
	}
	return shopAdviceCarousel, nil
}

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

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

func SetShopAdviceCarousel(shopAdviceCarouselMap map[string]interface{}, id int64) (domain.ShopAdviceCarousel, bool, error) {
	var shopAdviceCarousel domain.ShopAdviceCarousel
	_, err := configs.Engine.Table("shop_advice_carousel").Where("id = ?", id).Update(shopAdviceCarouselMap)
	if err != nil {
		return shopAdviceCarousel, false, err
	}
	return shopAdviceCarousel, true, nil
}

func DeleteShopAdviceCarousel(id int64) bool {
	var shopAdviceCarousel domain.ShopAdviceCarousel
	i, err := configs.Engine.Table("shop_advice_carousel").Where("id = ?", id).Delete(&shopAdviceCarousel)
	if err != nil {
		return false
	}
	return i > 0
}
