package gome_live_show

import (
	"encoding/json"
	"errors"
	"git.rosy.net.cn/baseapi/utils"
	"net/http"
)

// 获取商品品类信息查询
func (a *API) GetGoodsCategoryList() (goodsCategory *GoodsCategoryListRes, err error) {
	goodsCategoryMap, err := a.AccessAPI(gomeUrl, GoodsCategoryApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(nil)))
	if err != nil {
		return nil, err
	}
	result := &GoodsCategoryListRes{}
	if err := utils.Map2StructByJson(goodsCategoryMap, result, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return result, nil
}

//
////  查询所有的商品规格
//func (a *API) QueryGoodsSizeListAll(param *QueryGoodsSpecSizeReq) {
//	param.PageNumber = 1
//	param.PageSize = 50
//	result, err := a.QueryGoodsSizeList(param)
//	if err != nil {
//		return nil, err
//	}
//
//	for ; pageNo <= result.Data.Total/pageSize+1; pageNo++ {
//		result2, err := getAPI(string(model.VendorGoMei)).QueryGoodsListById(&gomei.QueryGoodsListForStoreReq{
//			Page: gomei.Page{
//				PageNumber: pageNo,
//				PageSize:   pageSize,
//			},
//		})
//		if err != nil {
//			return nil, err
//		}
//
//		for _, v := range result2.Data.Records {
//			if skuName := vendorSku2Jx2(v); skuName != nil {
//				skuNameList = append(skuNameList, skuName)
//			}
//		}
//	}
//
//	return skuNameList, err
//	reslut, err := a.QueryGoodsSizeList(param)
//
//}
//jsonMarshal,err := json.Marshal(gomeResult)
//if err != nil {
//return nil, err
//}
//if err := json.Unmarshal(jsonMarshal,data);err != nil {
//return nil, err
//}
// 查询商品规格
func (a *API) QueryGoodsSizeList(param *QueryGoodsSpecSizeReq) (result *QueryGoodsSpecSizeRes, err error) {
	reqByte, err := json.Marshal(param)
	if err != nil {
		return
	}

	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecListApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(reqByte)))
	if err != nil {
		return nil, err
	}

	data := &QueryGoodsSpecSizeRes{}
	if err := utils.Map2StructByJson(gomeResult, data, false); err != nil {
		return nil, err
	}
	jsonMarshal, err := json.Marshal(gomeResult)
	if err != nil {
		return nil, err
	}
	if err := json.Unmarshal(jsonMarshal, data); err != nil {
		return nil, err
	}

	if data.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return data, err
}

// 查询商品规格详情
func (a *API) QueryGoodsDetails(specCode string) (result *QueryGoodsSpecDetailRes, err error) {
	byteParam, err := json.Marshal(map[string]string{"specCode": specCode})
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecDetailApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(byteParam)))
	if err != nil {
		return nil, err
	}

	detail := &QueryGoodsSpecDetailRes{}
	if err := utils.Map2StructByJson(gomeResult, detail, false); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return detail, err
}

// 新增商品规格
func (a *API) CreateGoodsSpceList(param *CreateSpecDetailParam) (result *SystemParameterRes, err error) {
	paramByte, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecCreateApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(paramByte)))

	resultData := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, resultData, true); err != nil {
		return nil, err
	}
	if result.Code != 200 {
		return nil, errors.New(result.Message)
	}
	return resultData, nil
}

// 修改商品规格
func (a *API) UpdateGoodsSize(param *UpdateGoodsSizeReq) (*SystemParameterRes, error) {
	paramByte, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}

	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecUpdateApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(paramByte)))

	resultData := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, resultData, true); err != nil {
		return nil, err
	}
	if resultData.Code != 200 {
		return nil, errors.New(resultData.Message)
	}
	return resultData, nil
}

// 删除商品规格
func (a *API) DeleteGoodsSize(specCode string) (*SystemParameterRes, error) {
	byteParam, err := json.Marshal(map[string]string{"specCode": specCode})
	if err != nil {
		return nil, err
	}
	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecDeleteApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam([]byte(byteParam))))
	if err != nil {
		return nil, err
	}

	resultData := &SystemParameterRes{}
	if err := utils.Map2StructByJson(gomeResult, resultData, true); err != nil {
		return nil, err
	}
	if resultData.Code != 200 {
		return nil, errors.New(resultData.Message)
	}
	return resultData, nil
}

// 获取不同商品的类型
func (a *API) GetGoodsTypeList(cat3Code string) (*QueryGoodsTypeListRes, error) {
	byteParam, err := json.Marshal(map[string]string{"cat3Code": cat3Code})
	if err != nil {
		return nil, err
	}
	gomeResult, err := a.AccessAPI(gomeUrl, GoodsSpecTypeApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam([]byte(byteParam))))
	if err != nil {
		return nil, err
	}

	resultData := &QueryGoodsTypeListRes{}
	if err := utils.Map2StructByJson(gomeResult, resultData, true); err != nil {
		return nil, err
	}
	if resultData.Code != 200 {
		return nil, errors.New(resultData.Message)
	}
	return resultData, nil
}
