package dao

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

// SelectGoodsDetailById 获取相同的商品详情
func SelectGoodsDetailById(skuId int) (vo.DetailVo, error) {
	detailVo := vo.DetailVo{}
	var caId = -1
	//================查询sku========================
	var sku domain.GoodsSku
	//查询 sku
	rows, err := configs.Engine.Table("goods_sku").
		Where("goods_id = (select a.goods_id as goods_id from goods_sku as a where a.id = ? )", skuId).
		Desc("create_time").Rows(&sku)
	if err != nil {
		println("查询sku失败", err)
		return vo.DetailVo{}, err
	}
	var skus []domain.GoodsSku
	var commodityAreaIds = make([]int, 0)
	commodityAreaIdsString := ""
A:
	for rows.Next() {
		rows.Scan(&sku)
		skus = append(skus, sku)
		if sku.Id == skuId {
			caId = sku.CommodityAreaId
		}
		//去重
		for i := range commodityAreaIds {
			if sku.CommodityAreaId == commodityAreaIds[i] {
				continue A
			}
		}

		commodityAreaIds = append(commodityAreaIds, sku.CommodityAreaId)
		commodityAreaIdsString = fmt.Sprint(commodityAreaIdsString, ",", sku.CommodityAreaId)
	}
	commodityAreaIdsString = commodityAreaIdsString[1:]
	fmt.Println("=============================>", commodityAreaIdsString)
	//================查询goods========================
	var goods domain.Goods
	_, err = configs.Engine.Table("goods").Where("id = ?", sku.GoodsId).Get(&goods)
	if err != nil {
		println("查询goods失败", err)
		return vo.DetailVo{}, err
	}
	//=================查询goods_tag========================
	var goodsTag domain.GoodsTag
	rows, err = configs.Engine.Table("goods_tag").
		Where(" FIND_IN_SET(goods_tag.id,?)", goods.TagIds).
		Rows(&goodsTag)
	if err != nil {
		println("查询goods_tag失败", err)
		return vo.DetailVo{}, err
	}
	var goodsTags []domain.GoodsTag
	for rows.Next() {
		rows.Scan(&goodsTag)
		goodsTags = append(goodsTags, goodsTag)
	}
	//=================查询goods_type========================
	var goodsType domain.GoodsType
	_, err = configs.Engine.Table("goods_type").Where("id = ?", goods.TypeId).Get(&goodsType)
	if err != nil {
		println("查询goods_type失败", err)
		return vo.DetailVo{}, err
	}
	//=================查询goods_CommodityArea========================
	var goodsCommodityArea domain.GoodsCommodityArea
	rows, err = configs.Engine.Table("goods_commodity_area").
		Where(" FIND_IN_SET(goods_commodity_area.id,?) and goods_id = ?", commodityAreaIdsString, goods.Id).
		Rows(&goodsCommodityArea)
	if err != nil {
		println("查询goods_CommodityArea失败", err)
		return vo.DetailVo{}, err
	}
	var goodsCommodityAreas []domain.GoodsCommodityArea

	var introductionIds = make([]int, 0)
	var introductionIdString = ""
B:
	for rows.Next() {
		rows.Scan(&goodsCommodityArea)
		goodsCommodityAreas = append(goodsCommodityAreas, goodsCommodityArea)
		for i := range introductionIds {
			if goodsCommodityArea.DetailIntroductionId == introductionIds[i] {
				continue B
			}
		}
		introductionIds = append(introductionIds, goodsCommodityArea.DetailIntroductionId)
		introductionIdString = fmt.Sprint(introductionIdString, ",", goodsCommodityArea.DetailIntroductionId)
	}
	introductionIdString = introductionIdString[1:]
	fmt.Println("=============================>", introductionIdString)
	//=================查询goods_introduction========================
	var goodsIntroduction domain.GoodsIntroduction
	rows, err = configs.Engine.Table("goods_introduction").
		Where("FIND_IN_SET(id,?)", introductionIdString).
		Rows(&goodsIntroduction)
	if err != nil {
		println("查询goods_introduction失败", err)
		return vo.DetailVo{}, err
	}

	var goodsIntroductions []domain.GoodsIntroduction
	for rows.Next() {
		rows.Scan(&goodsIntroduction)
		goodsIntroductions = append(goodsIntroductions, goodsIntroduction)
	}

	detailVo.DetailGoodsVo = vo.DetailGoodsVo{
		Tags:        goodsTags,
		GoodsId:     goods.Id,
		Name:        goods.GoodsName,
		SalesVolume: goods.SalesVolume,
		Type:        goodsType,
	}

	detailVo.DetailSkuVo = vo.DetailSkuVo{
		SkuList:  skus,
		NowSkuId: skuId,
	}

	detailVo.DetailCommodityAreaVo = vo.DetailCommodityAreaVo{
		CommodityAreaList:  goodsCommodityAreas,
		NowCommodityAreaId: caId,
	}

	//=================相关推荐========================
	//复制sku 8个数据
	if len(skus) >= 8 {
		detailVo.RecommendedGoods = skus[0:8]
	} else {
		detailVo.RecommendedGoods = skus
	}
	detailVo.Introduction = goodsIntroductions

	return detailVo, nil
}
