package cooper

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

//获取灯控设备日志
func (that *CooperClient) NewUsrK3LogListRequest() *UsrK3LogListRequest {
	return &UsrK3LogListRequest{
		Request: Request{
			Client:     that,
			publicParamsSlice: []string{
				"page",
				"size",
			},
			apiAddress: "/usrK3/logList",
		},
	}
}

//UsrK3LogListRequest ...
type UsrK3LogListRequest struct {
	Request
}

func (that *UsrK3LogListRequest) SetPage(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("page", value) //页码
	return that
}

func (that *UsrK3LogListRequest) SetSize(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("size", value)
	return that
}

func (that *UsrK3LogListRequest) SetCompanyId(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("company_id", value)
	return that
}

//设备IDs
func (that *UsrK3LogListRequest) SetDevIds(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("dev_ids", value)
	return that
}

func (that *UsrK3LogListRequest) SetStartDate(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("start_date", value)
	return that
}

func (that *UsrK3LogListRequest) SetEndDate(value string) *UsrK3LogListRequest {
	that.initParams()
	that.params.Set("end_date", value)
	return that
}

//Params ...
func (that *UsrK3LogListRequest) 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 UsrK3LogListResponse struct {
	Count int64         `json:"count"` //count
	List  []UsrK3LogListDataResponse `json:"list"`  //list
	Page  int64         `json:"page"`  //page
	Size  int64         `json:"size"`  //size
}

type UsrK3LogListDataResponse 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 *UsrK3LogListRequest) Do() (*UsrK3LogListResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Umarshal failed!" + _err.Error())
	}
	//返回结果
	_rs := new(UsrK3LogListResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed2!" + _err.Error())
	}
	return _rs, nil
}
