package pposRequest

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

//NewGateEnter 获取配置
func (that *PPOSClient) NewGateEnter() *GateEnterRequest {
	return &GateEnterRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/enterAdmin/gate/enter",
		},
	}
}

//GateEnterRequest 业务类别
type GateEnterRequest struct {
	Request
	DeviceId string `json:"device_id"` //设备id
	Code     string `json:"code"`      //类别ID
	CodeType string `json:"type"`      //类别根id
	Reader   string `json:"reader"`    //读头
	IsQuickNew string `json:"is_quick_new"` //快诺优验证标志
}

func (that *GateEnterRequest) SetDeviceID(deviceId string) *GateEnterRequest {
	that.DeviceId = deviceId
	return that
}

func (that *GateEnterRequest) SetCode(code string) *GateEnterRequest {
	that.Code = code
	return that
}

func (that *GateEnterRequest) SetCodeType(codeType string) *GateEnterRequest {
	that.CodeType = codeType
	return that
}

func (that *GateEnterRequest) SetReader(reader string) *GateEnterRequest {
	that.Reader = reader
	return that
}

func (that *GateEnterRequest) SetIsQuickNew(isQuickNew string) *GateEnterRequest {
	that.IsQuickNew = isQuickNew
	return that
}

//Params ...
func (that *GateEnterRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("device_id", utils.ToString(that.DeviceId))
	_params.Set("code", utils.ToString(that.Code))
	_params.Set("type", utils.ToString(that.CodeType))
	_params.Set("reader", utils.ToString(that.Reader))
	if that.IsQuickNew != "" {
		_params.Set("is_quick_new", that.IsQuickNew)
	}
	return _params, nil
}

//Do ...
func (that *GateEnterRequest) Do() (*GateEnterResponse, error) {
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_rs := new(GateEnterResponse)
	_vData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
	}
	if _err := utils.MapToStruct(_vData, _rs); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	return _rs, nil
}
