package router

import (
	"demo/data/dao/manage"
	"demo/data/domain"
	"demo/data/domain/vo"
	"demo/share"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mitchellh/mapstructure"
	"github.com/spf13/cast"
	"time"
)

// BackGoodsRouter 废弃
func BackGoodsRouter(engine *gin.RouterGroup) {
	user := engine.Group("/back/goods")
	PushRouter(user, "GET", "/introduction", GetIntroductionByGoodsId)
	PushRouter(user, "POST", "list", GetGoodsAndIntroductionList)
	PushRouter(user, "POST", "", SaveGoods)
	PushRouter(user, "PUT", "", UpdateGoods)
	PushRouter(user, "Delete", "/:id", DeleteGoodsById)
}

func GetIntroductionByGoodsId(c *gin.Context) {
	id := c.Query("id")
	byId, err := manage.GetGoodsById(cast.ToInt64(id))
	if err != nil {
		c.JSON(200, CreateResultError(500, "商品不存在"))
		return
	}
	introductionById, err := manage.GetGoodsIntroductionById(byId.IntroductionId)
	if err != nil {
		c.JSON(200, CreateResultError(500, "商品介绍不存在"))
		return
	}
	c.JSON(200, CreateResultData(introductionById))
}

// GetGoodsAndIntroductionList 根据商品id获取商品及商品相关文章
func GetGoodsAndIntroductionList(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	param, _ := data("param")
	pageNum, _ := data("pageNum")
	pageSize, _ := data("pageSize")
	goods := domain.Goods{}
	err := mapstructure.Decode(param, &goods)
	if err != nil {
		c.JSON(200, CreateResultError(500, "json转换错误"))
		return
	}
	list, err := manage.GetGoodsList(goods, cast.ToInt(pageNum), cast.ToInt(pageSize))
	if err != nil {
		c.JSON(200, CreateResultError(500, "查询错误"))
		return
	}
	intrIds := make([]int64, 0)
	for i := range list.List {
		///类型判断
		item, ok := list.List[i].(domain.Goods)
		if ok {
			intrIds = append(intrIds, item.IntroductionId)
		}
	}
	intrs, err := manage.GetGoodsIntroductionListInId(intrIds)
	if err != nil {
		c.JSON(200, CreateResultError(500, "查询错误"))
		return
	}

	dataMap := make(map[int64]vo.GoodsVo)
	for i := 0; i < len(list.List); i++ {
		dataMap[list.List[i].(domain.Goods).IntroductionId] = vo.GoodsVo{Goods: list.List[i].(domain.Goods)}
	}
	for i := 0; i < len(intrs); i++ {
		voData, ok := dataMap[intrs[i].Id]
		if ok {
			voData.Introduction = intrs[i]
		}
	}
	//取出values
	resArr := make([]any, 0)
	for _, v := range dataMap {
		resArr = append(resArr, v)
	}
	list.List = resArr
	c.JSON(200, CreateResultData(list))
}

func SaveGoods(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	goodsParam, _ := data("goods")
	//typeId, _ := data("typeId")
	//tagIds, _ := data("tagIds")

	goods := domain.Goods{}
	err := mapstructure.Decode(goodsParam, &goods)
	saveGoods, err := manage.SaveGoods(&goods)
	if err != nil {
		c.JSON(200, CreateResultError(500, "商品保存错误"))
		return
	}
	goodsIntroductionParam, _ := data("introduction")
	goodsIntroduction := domain.GoodsIntroduction{}
	err = mapstructure.Decode(goodsIntroductionParam, &goodsIntroduction)
	goodsIntroduction.CreateBy = fmt.Sprint(GetUserIdByToken(c))
	goodsIntroduction.CreateTime = time.Now()
	saveGoodsIntroduction, err := manage.SaveGoodsIntroduction(&goodsIntroduction)
	if err != nil {
		c.JSON(200, CreateResultError(500, "商品介绍保存错误"))
		return
	}
	m := make(map[string]interface{})
	m["goods"] = saveGoods
	m["introduction"] = saveGoodsIntroduction
	c.JSON(200, CreateResultData(m))
}

func UpdateGoods(c *gin.Context) {
	data := share.GetJsonAnyParam(c)
	goodsIntroduction, _ := data("introduction")
	goods, _ := data("goods")
	id, _ := data("id")
	toMap := UtilStructToMap(cast.ToStringMap(goods))
	setGoods, b, err := manage.SetGoods(toMap, cast.ToInt64(id))
	if err != nil || !b {
		c.JSON(200, CreateResultError(500, "修改错误"))
		return
	}

	intrId := setGoods.IntroductionId
	toIntrMap := UtilStructToMap(cast.ToStringMap(goodsIntroduction))
	toIntrMap["create_by"] = "fmt.Sprint(GetUserIdByToken(c))"
	setGoodsIntroduction, b, err := manage.SetGoodsIntroduction(toIntrMap, cast.ToInt64(intrId))
	if err != nil || !b {
		c.JSON(200, CreateResultError(500, "修改错误"))
		return
	}
	m := make(map[string]interface{})
	m["goods"] = setGoods
	m["introduction"] = setGoodsIntroduction
	c.JSON(200, CreateResultData(m))
}
func DeleteGoodsById(c *gin.Context) {
	id := c.Query("id")
	byId, err := manage.GetGoodsById(cast.ToInt64(id))

	if err != nil {
		c.JSON(200, CreateResultError(500, "删除失败"))
		return
	}
	if byId.Id == 0 || byId.Id > 0 {
		c.JSON(200, CreateResultError(500, "商品不存在删除失败"))
		return
	}
	res := manage.DeleteGoods(cast.ToInt64(id))
	if !res {
		c.JSON(200, CreateResultError(500, "删除错误"))
		return
	}
	res = manage.DeleteGoodsIntroduction(cast.ToInt64(byId.IntroductionId))
	if !res {
		c.JSON(200, CreateResultError(500, "文章删除错误"))
		return
	}

	c.JSON(200, CreateResult())
}
