package manage

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

func GetGoodsSkuById(id int64) (domain.GoodsSku, error) {
	var goodsSku domain.GoodsSku
	_, err := configs.Engine.Table("goods_sku").Where("id = ?", id).Get(&goodsSku)
	if err != nil {
		return goodsSku, err
	}
	return goodsSku, nil
}

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

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

func SetGoodsSku(goodsSkuMap map[string]interface{}, id int64) (domain.GoodsSku, bool, error) {
	var goodsSku domain.GoodsSku
	_, err := configs.Engine.Table("goods_sku").Where("id = ?", id).Update(goodsSkuMap)
	if err != nil {
		return goodsSku, false, err
	}
	return goodsSku, true, nil
}

func DeleteGoodsSku(id int64) bool {
	var goodsSku domain.GoodsSku
	i, err := configs.Engine.Table("goods_sku").Where("id = ?", id).Delete(&goodsSku)
	if err != nil {
		return false
	}
	return i > 0
}
