package gome_live_show

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

// 查询店铺列表
func (a *API) QueryStoreList(storeList *GetStoreListReq) (result *GetStoreListRes, err error) {
	if storeList.PageSize == ConstNumberOne {
		storeList.PageSize = PageSize
	}
	if storeList.PageNumber == ConstNumberOne {
		storeList.PageNumber = PageNumber
	}
	storeByte, err := json.Marshal(storeList)
	if err != nil {
		return nil, err
	}

	gomeStoreList, err := a.AccessAPI(gomeUrl, GetStoreListApi, http.MethodPost, utils.Struct2FlatMap(a.MakeRequestParam(storeByte)))
	if err != nil {
		return nil, err
	}

	resultData := &GetStoreListRes{}
	if err := utils.Map2StructByJson(gomeStoreList, resultData, false); err != nil {
		return nil, err
	}
	if resultData.Code != 200 {
		return nil, errors.New(resultData.Message)
	}
	return resultData, nil
}

// 店铺商品分类列表查询
func (a *API) QueryStoreCategoryList(param *QueryStoreGoodsCategoryListReq) (*QueryStoreGoodsCategoryListRes, error) {
	byteParam, err := json.Marshal(param)
	if err != nil {
		return nil, err
	}

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

	result := &QueryStoreGoodsCategoryListRes{}
	if err := utils.Map2StructByJson(gomeResult, result, false); err != nil {
		return nil, err
	}
	return result, nil
}

// 店铺商品分类新增
func (a *API) AddStoreCategory(req *StoreAddGoodsCategory) (*SystemParameterRes, error) {
	byteParam, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

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

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

// 店铺商品分类编辑
func (a *API) ModifyStoreGoodsCategory(req *StoreModifyGoodsCategory) (*SystemParameterRes, error) {
	byteParam, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

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

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

// 店铺商品分类删除
func (a *API) DeleteStoreGoodsCategory(storeCode, categoryCode string) (*SystemParameterRes, error) {
	byteParam, err := json.Marshal(map[string]string{"storeCode": storeCode, "categoryCode": categoryCode})
	if err != nil {
		return nil, err
	}

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

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