package cooper

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

//NewUsrk3LogGetRequest ...账户管理 - 支付账户 - 详情
func (that *CooperClient) NewUsrk3LogGetRequest() *Usrk3LogGetRequest {
	return &Usrk3LogGetRequest{
		Request: Request{
			Client:            that,
			publicParamsSlice: []string{
				//...
			},
			apiAddress: "/usrK3/logOne",
		},
	}
}

//Usrk3LogGetRequest
type Usrk3LogGetRequest struct {
	Request
}

func (that *Usrk3LogGetRequest) SetDeviceId(deviceId string) *Usrk3LogGetRequest {
	that.initParams()
	that.params.Set("dev_id", deviceId) //账户id
	return that
}

//Params ...
func (that *Usrk3LogGetRequest) 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 Usrk3LogGetResponse struct {
	DevId    string `json:"dev_id"`
	DeviceEvent   int64  `json:"device_event"`
	DeviceEventStr    string `json:"device_event_str"`
	DeviceEventTime    string `json:"device_event_time"`
	DeviceEventMsg    string `json:"device_event_msg"`
	CTime    string `json:"c_time"`
}

//Do ...
func (that *Usrk3LogGetRequest) Do() (*Usrk3LogGetResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	//_rsData, _ok := _data.(map[string]interface{})
	//if !_ok {
	//	return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	//}
	//
	_str, _err := json.Marshal(_data)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	//返回结果
	_rs := new(Usrk3LogGetResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
