package dao

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

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

	return sku, nil
}

func CreateOrder(order *domain.GoodsOrder) (*domain.GoodsOrder, error) {
	id, err := configs.Engine.Table("goods_order").Insert(&order)
	fmt.Println("is id? ", id)
	if err != nil {
		return order, err
	}
	//configs.Engine.Table("goods_order").Where("create_by = ?", order.CreateBy).Desc("create_time").Get(&order)
	fmt.Println("order is ", order)
	return order, err
}

// OrderPaySuccess 订单支付成功
func OrderPaySuccess(orderId int) {
	configs.Engine.ID(orderId).Cols("state").Update(map[string]any{"state": 1})
}

// 获取订单集合
func GetOrderByUserId(userId int) ([]domain.GoodsOrder, error) {
	order := domain.GoodsOrder{}
	rows, err := configs.Engine.Table("goods_order").Where("create_by = ?", userId).Desc("create_time").Rows(&order)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var orders []domain.GoodsOrder
	for rows.Next() {
		var order domain.GoodsOrder
		err := rows.Scan(&order)
		if err != nil {
			return nil, err
		}
		orders = append(orders, order)
	}
	return orders, nil
}

// GetUserWalletByUserId 获取用户的钱包信息
func GetUserWalletByUserId(userId int) (domain.UserWallet, error) {
	var wallet domain.UserWallet
	_, err := configs.Engine.Where("user_id = ?", userId).Get(&wallet)
	if err != nil {
		return wallet, err
	}
	return wallet, nil

}

// GetUserCouponByUserId  获取用户的优惠券信息
func GetUserCouponByUserId(userId int) ([]domain.CouponUserJDM, error) {

	var couponUserJDMs []domain.CouponUserJDM
	err := configs.Engine.Table("goods_coupon_user").
		Join("INNER", "goods_coupon", "goods_coupon_user.coupon_id = goods_coupon.id").
		Where("goods_coupon_user.user_id = ?", userId).
		Find(&couponUserJDMs)
	if err != nil {
		return couponUserJDMs, err
	}

	return couponUserJDMs, nil
}

func GetListGoodsOrder(state string, userId, pageNum, pageSize int) (vo.BaseListVo, error) {
	var goodsOrder domain.GoodsOrder
	Session := configs.Engine.Table("goods_order")
	if state != "" {
		Session.Where("state = ?", state)
	}
	rows, err := Session.Where("create_by = ?", userId).Limit(pageSize, (pageNum-1)*pageSize).Desc("id").Rows(&goodsOrder)
	var vo vo.BaseListVo
	if err != nil {
		return vo, err
	}
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	vo.List = make([]any, 0)
	for rows.Next() {
		var u domain.GoodsOrder
		rows.Scan(&u)
		vo.List = append(vo.List, u)
	}
	fmt.Println("996-------", vo.List)
	CountSess := configs.Engine.Table("goods_order")
	if state != "" {
		CountSess.Where("state = ?", state)
	}
	//查询数量
	count, err := CountSess.Where("create_by = ?", userId).Count(&goodsOrder)
	vo.Total = int(count)
	return vo, nil
}

// 领取优惠券
//func pickUpTheCoupon(couponId int, userId int) (bool, error) {
//	gcu := domain.GoodsCouponUser{
//		UserId:         userId,
//		CouponId:       couponId,
//		CollectionTime: time.Now(),
//		State:          "1",
//	}
//	//判断优惠券数量是否充足
//	coupon, err := manage.GetByIdGoodsCoupon(couponId)
//	if err != nil {
//		return false, err
//	}
//	//判断条件
//	if coupon.Count > 0 {
//		if coupon.Count == 1 {
//			//优惠券数量为1时，直接领取
//			_, err := manage.PostGoodsCouponUser(&gcu)
//			if err != nil {
//				return false, err
//			}
//			manage.PutGoodsCoupon(map[string]any{"count": coupon.Count - 1}, couponId)
//			return true, nil
//		} else {
//			//优惠券数量大于1时，判断是否满足领取条件
//			if coupon.Condition == 1 {
//				//满足领取条件
//				_, err := manage.PostGoodsCouponUser(&gcu)
//				if err != nil {
//				}
//			}
//		}
//
//	}
//
//}
