package dao

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

// SelectGoodsDetailById 获取相同的商品详情
func SelectGoodsDetailById(skuId int64) (vo.DetailVo, error) {
	detailVo := vo.DetailVo{}
	var caId int64 = -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([]int64, 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)
	}
	if len(skus) == 0 {
		return detailVo, nil
	}

	if len(commodityAreaIdsString) > 1 {
		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,?)", commodityAreaIdsString).
		Rows(&goodsCommodityArea)
	if err != nil {
		println("查询goods_CommodityArea失败", err)
		return vo.DetailVo{}, err
	}
	var goodsCommodityAreas []domain.GoodsCommodityArea

	var introductionIds = make([]int64, 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)
	}
	if len(introductionIdString) > 1 {
		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
}
func GetSkuByKey(key string, pageNum, pageSize int) (vo.BaseListVo, string, error) {
	var sqlKey = "%" + key + "%"
	var vo = vo.BaseListVo{}
	var sku domain.GoodsSku
	//查询tag
	var tag domain.GoodsTag
	var goods domain.Goods
	r1, err := configs.Engine.Cols("id").Where("name like ?", sqlKey).Or("tag like ?", sqlKey).
		//去重
		Distinct("id").Rows(&tag)
	var inTags = make([]int64, 0)
	for r1.Next() {
		r1.Scan(&tag)
		inTags = append(inTags, tag.Id)
	}

	sess := configs.Engine.Cols("id").Distinct("id").Where("goods_name like ?", sqlKey)
	for i := range inTags {
		sess.Or("FIND_IN_SET(?,tag_ids)", inTags[i])
	}
	r2, err := sess.Rows(&goods)
	if err != nil {
		return vo, "商品查询错误", err
	}
	var goodsInTags = make([]int64, 0)
	for r2.Next() {
		r2.Scan(&goods)
		goodsInTags = append(goodsInTags, goods.Id)
	}

	sess = configs.Engine.Where("sku_name like ?", sqlKey)
	for i := range goodsInTags {
		sess.Or("goods_id = ?", goodsInTags[i])
	}
	r3, err := sess.Desc("id").Limit(pageSize, (pageNum-1)*pageSize).Rows(&sku)

	if err != nil {
		return vo, "商品sku查询错误", err
	}

	var skus = make([]any, 0)
	for r3.Next() {
		r3.Scan(&sku)
		skus = append(skus, sku)
	}
	cs := configs.Engine.Where("sku_name like ?", sqlKey)
	for i := range goodsInTags {
		cs.Or("goods_id = ?", goodsInTags[i])
	}
	count, err := cs.Distinct("id").Count("id")

	vo.Total = int(count)
	vo.List = skus
	vo.PageNum = pageNum
	vo.PageSize = pageSize
	return vo, "", nil
}

func SelectSkuInfoBySkuId(skuId int64) (vo.DetailInfoVo, error) {
	//根据skuid 获取GoodsId
	var sku domain.GoodsSku
	var infoVo vo.DetailInfoVo
	_, err := configs.Engine.Table("goods_sku").Where("id = ?", skuId).Get(&sku)
	if err != nil || sku.GoodsId == 0 {
		return infoVo, err
	}
	var goods domain.Goods
	_, err = configs.Engine.Table("goods").Where("id = ?", sku.GoodsId).Get(&goods)
	//查询属性
	var areas = make([]domain.GoodsCommodityArea, 0)
	err = configs.Engine.Table("goods_commodity_area").
		Where("goods_id = ?", goods.Id).Find(&areas)
	if err != nil {
		return infoVo, err
	}
	//查询文章下的所有
	intrIds := make([]int64, 0)

	intrIds = append(intrIds, goods.IntroductionId)

	for i := range areas {
		if areas[i].DetailIntroductionId != 0 {
			intrIds = append(intrIds, areas[i].DetailIntroductionId)
		}
	}

	var iis = ""
	for i := range intrIds {
		iis += fmt.Sprint(",", intrIds[i])
	}
	iis = iis[1:]

	var intrs = make([]domain.GoodsIntroduction, 0)
	err = configs.Engine.Table("goods_introduction").
		Where("FIND_IN_SET(id,?)", iis).Find(&intrs)
	if err != nil {
		return infoVo, err
	}
	var skus = make([]domain.GoodsSku, 0)
	err = configs.Engine.Table("goods_sku").Where("goods_id = ?", goods.Id).Find(&skus)

	infoVo.Sku = sku
	infoVo.Goods = goods
	infoVo.Introductions = intrs
	infoVo.Skus = skus
	infoVo.Areas = areas

	if len(skus) > 8 {
		infoVo.RecommendedGoods = skus[0:8]
	} else {
		infoVo.RecommendedGoods = skus
	}

	return infoVo, nil
}
