/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2021-04-12
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */

package pposRequest

import (
	"PPOSUtils/utils"
	"encoding/json"
	"errors"
	"net/url"
)

//new...
func (that *PPOSClient) NewBusinessList() *BusinessListRequest {
	return &BusinessListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/business/list",
		},
	}
}

//Request...
type BusinessListRequest struct {
	Request
}

func (that *BusinessListRequest) SetKeyword(value string) *BusinessListRequest {
	that.initParams()
	that.params.Set("keyword", value) //关键字
	return that
}

func (that *BusinessListRequest) SetPage(value int64) *BusinessListRequest {
	that.initParams()
	that.params.Set("page", utils.ToString(value)) //页码
	return that
}

func (that *BusinessListRequest) SetSize(value int64) *BusinessListRequest {
	that.initParams()
	that.params.Set("size", utils.ToString(value)) //页码
	return that
}

//Params ...
func (that *BusinessListRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	return _params, nil
}

type BusinessListResponse struct {
	Count int64          `json:"count"` //count
	List  []BusinessList `json:"list"`  //list
	Page  int64          `json:"page"`  //page
	Size  int64          `json:"size"`  //size
}

type BusinessList struct {
	BusinessId   int64  `json:"business_id"`
	BusinessName string `json:"business_name"`
}

//Do ...
func (that *BusinessListRequest) Do() (BusinessListResponse, error) {
	_rs := BusinessListResponse{}
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return _rs, errors.New(that.apiAddress + "请求响应参数不正确2")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
