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

package pposRequest

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

//new...
func (that *PPOSClient) NewSyncGateMachine() *SyncGateMachineRequest {
	return &SyncGateMachineRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/platform/openData/gateEnterRecord",
		},
	}
}

//Request...
type SyncGateMachineRequest struct {
	Request
}

func (that *SyncGateMachineRequest) SetPage(value int64) *SyncGateMachineRequest {
	that.initParams()
	that.params.Set("page", utils.ToString(value)) //页码
	return that
}
func (that *SyncGateMachineRequest) SetSize(value int64) *SyncGateMachineRequest {
	that.initParams()
	that.params.Set("size", utils.ToString(value)) //每页条数
	return that
}
func (that *SyncGateMachineRequest) SetStartTime(value int64) *SyncGateMachineRequest {
	that.initParams()
	that.params.Set("start_time", utils.ToString(value)) //采集时间
	return that
}

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

type SyncGateMachineResponse struct {
	Count int64      `json:"count"` //count
	List  []utils.Mp `json:"list"`  //list
	Page  string     `json:"page"`  //page
	Size  string     `json:"size"`  //size
}

//Do ...
func (that *SyncGateMachineRequest) Do() (SyncGateMachineResponse, error) {
	_rs := SyncGateMachineResponse{}
	_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
}
