package dao

import (
	"demo/configs"
	"demo/data/dao/manage"
	"demo/data/domain"
	"demo/data/domain/vo"
	"fmt"
	"strconv"
	"strings"
)

func GetAdviceData() (vo.ShopAdviceVo, error) {
	var carousels []domain.ShopAdviceCarousel
	err := configs.Engine.Table("shop_advice_carousel").
		Where("state = 1 and show_type = 'carousel'").
		Asc("sort").
		Limit(10).Find(&carousels)
	if err != nil {
		return vo.ShopAdviceVo{}, err
	}
	sku := make([]vo.AdviceSku, 0)
	err = configs.Engine.Table("shop_advice_carousel").
		Where("state = 1 and show_type = 'package'").
		Join("INNER", "goods_sku", "shop_advice_carousel.to_id = goods_sku.id").
		Limit(6).Find(&sku)
	if err != nil {
		return vo.ShopAdviceVo{}, err
	}
	vo := vo.ShopAdviceVo{
		Carousels:   carousels,
		PackageList: sku,
	}
	return vo, err
}

func GetTopicList() ([]vo.ShopHomeTopicVo, error) {
	var topics []domain.ShopTopic
	err := configs.Engine.Table("shop_topic").Asc("sort").Find(&topics)
	if err != nil {
		fmt.Println(err)
		return nil, nil
	}
	var vos = make([]vo.ShopHomeTopicVo, 0)
	for i := range topics {
		vo := vo.ShopHomeTopicVo{Topic: topics[i]}
		vos = append(vos, vo)
		skus := make([]domain.GoodsSku, 0)
		err := configs.Engine.Table("shop_topic_sku").Select("goods_sku.id,goods_sku.sku_image,goods_sku.sku_name,goods_sku.price,goods_sku.historical_prices,goods_sku.inventory_number,goods_sku.commodity_area_id,goods_sku.goods_id,goods_sku.create_by,goods_sku.create_time").
			Join("INNER", "goods_sku", "shop_topic_sku.sku_id = goods_sku.id").
			Where("shop_topic_sku.topic_id = ?", topics[i].Id).Find(&skus)
		if err != nil {
			fmt.Println(err)
		}
		vo.Skus = skus
	}
	for i := range vos {
		if len(vos[i].Skus) == 0 {
			//查询替换
			//vo, err := manage.GetGoodsSkuList(domain.GoodsSku{}, 1, 20)
			var skus []domain.GoodsSku
			err = configs.Engine.Select("goods_sku.id,goods_sku.sku_image,goods_sku.sku_name,goods_sku.price,goods_sku.historical_prices,goods_sku.inventory_number,goods_sku.commodity_area_id,goods_sku.goods_id,goods_sku.create_by,goods_sku.create_time").
				Table("goods_sku").Join("INNER", "goods", "goods_sku.goods_id = goods.id").
				Join("INNER", "goods_type", "goods.type_id = goods_type.id").Limit(20).
				Where("find_in_set(goods_type.id,?) ", vos[i].Topic.TypeIds).Find(&skus)
			if err != nil {
				fmt.Println(err)
			}
			vos[i].Skus = skus
		}
	}
	return vos, nil
}
func GetTopicPageDataListById(topicId int64) (vo.TopicPageData, error) {
	topic := domain.ShopTopic{}
	var data vo.TopicPageData
	topicRes, err := configs.Engine.Table("shop_topic").Where("id = ?", topicId).Get(&topic)
	if err != nil {
		fmt.Println(err)
		return data, err
	}
	if !topicRes {
		return data, err
	}
	//获取商品类型
	typeIds := make([]int64, 0)
	split := strings.Split(topic.TypeIds, ",")
	for i := range split {
		parseInt, err := strconv.ParseInt(split[i], 10, 64)
		if err != nil {
			fmt.Println(err)
			return data, err
		}
		typeIds = append(typeIds, parseInt)
	}
	types, err := manage.GetGoodsTypeListInId(typeIds)
	if err != nil {
		fmt.Println(err)
		return data, err
	}
	typeData := make([]vo.TopicPageTypeData, 0)
	for i := range types {
		typeVo := vo.TopicPageTypeData{
			Type: types[i],
		}

		//根据typeId查询每个商品中的第一个sku
		skus := make([]domain.GoodsSku, 0)

		//获取商品
		goodsId := make([]int64, 0)
		err := configs.Engine.Select("id").Table("goods").Where("type_id = ?", types[i].Id).Find(&goodsId)
		if err != nil {
			fmt.Println(err)
			return data, err
		}
		for i := range goodsId {
			sku := domain.GoodsSku{}
			skuRes, err := configs.Engine.Table("goods_sku").Where("goods_id = ?", goodsId[i]).Get(&sku)
			if err != nil {
				fmt.Println(err)
				return data, err
			}
			if !skuRes {
				continue
			}
			skus = append(skus, sku)
		}
		typeVo.SkuList = skus

		typeData = append(typeData, typeVo)
	}
	data.Topic = topic
	data.Types = typeData
	return data, nil
}
