package manage

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

func GetGoodsOrderById(id int64) (domain.GoodsOrder, error) {
	var goodsOrder domain.GoodsOrder
	_, err := configs.Engine.Table("goods_order").Where("id = ?", id).Get(&goodsOrder)
	if err != nil {
		return goodsOrder, err
	}
	return goodsOrder, nil
}

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

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

func SetGoodsOrder(goodsOrderMap map[string]interface{}, id int64) (domain.GoodsOrder, bool, error) {
	var goodsOrder domain.GoodsOrder
	_, err := configs.Engine.Table("goods_order").Where("id = ?", id).Update(goodsOrderMap)
	if err != nil {
		return goodsOrder, false, err
	}
	return goodsOrder, true, nil
}

func DeleteGoodsOrder(id int64) bool {
	var goodsOrder domain.GoodsOrder
	i, err := configs.Engine.Table("goods_order").Where("id = ?", id).Delete(&goodsOrder)
	if err != nil {
		return false
	}
	return i > 0
}
