package pposRequest

import (
	"errors"
	"net/url"
)

//获取营业点商品三级分类
func (that *PPOSClient) NewGoodsSaleCateListRequest() *GoodsSaleCateListRequest {
	return &GoodsSaleCateListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsSale/cateList",
			publicParamsSlice: []string{
				"sales_id",
			},
		}}
}

type GoodsSaleCateListRequest struct {
	Request
}

//Params ...
func (that *GoodsSaleCateListRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil

}

type ProductResponse struct {
	ProductCateId   int64  `json:"product_cate_id"`
	ProductCateName string `json:"product_cate_name"`
}

//Do ...
func (that *GoodsSaleCateListRequest) Do() ([]*ProductResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_itemData, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_rs := make([]*ProductResponse, 0)
	for _, _v := range _itemData {
		_item := new(ProductResponse)
		if _err := that.ParseResponeMp(_item, _v); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _item)
	}
	//返回结果
	return _rs, nil
}
