package pposRequest

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

//new...
func (that *PPOSClient) NewCompanyGetByDomainRequest() *CompanyGetByDomainRequest {
	return &CompanyGetByDomainRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/company/getByDomain",
			publicParamsSlice: []string{
				"domain",
			},
		},
	}
}

//Response...
type CompanyGetByDomainResponse struct {
	CompanyId   int64  `json:"company_id"`
	CompanyName string `json:"company_name"`
	Domain      string `json:"domain"`
}

//Request...
type CompanyGetByDomainRequest struct {
	Request
}

func (that *CompanyGetByDomainRequest) SetDomain(value string) *CompanyGetByDomainRequest {
	that.initParams()
	that.params.Set("domain", value) //登录域名
	return that
}

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

//Do ...
func (that *CompanyGetByDomainRequest) Do() (*CompanyGetByDomainResponse, error) {
	//
	_data, _err := that.Client.GetData(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!")
	}
	_rs := new(CompanyGetByDomainResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Umarshal failed2!")
	}
	return _rs, nil
}
