package manage

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

func GetGoodsCouponUserById(id int64) (domain.GoodsCouponUser, error) {
	var goodsCouponUser domain.GoodsCouponUser
	_, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", id).Get(&goodsCouponUser)
	if err != nil {
		return goodsCouponUser, err
	}
	return goodsCouponUser, nil
}

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

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

func SetGoodsCouponUser(goodsCouponUserMap map[string]interface{}, id int64) (domain.GoodsCouponUser, bool, error) {
	var goodsCouponUser domain.GoodsCouponUser
	_, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", id).Update(goodsCouponUserMap)
	if err != nil {
		return goodsCouponUser, false, err
	}
	return goodsCouponUser, true, nil
}

func DeleteGoodsCouponUser(id int64) bool {
	var goodsCouponUser domain.GoodsCouponUser
	i, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", id).Delete(&goodsCouponUser)
	if err != nil {
		return false
	}
	return i > 0
}
