package pposRequest

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

//new...
func (that *PPOSClient) NewAccountFreshTokenRequest() *AccountFreshTokenRequest {
	return &AccountFreshTokenRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/account/freshToken",
			publicParamsSlice: []string{
				"mp_id",
				"personnel_id",
			},
		},
	}
}

//Response...
type NewAccountFreshTokenResponse struct {
	Name        string `json:"name"`
	AccountId   int64  `json:"account_id"`
	AccountType string `json:"account_type"`
	IsSuper     int64  `json:"is_super"`
	CompanyId   int64  `json:"company_id"`
	PersonnelId int64  `json:"personnel_id"`
	Token       string `json:"token"`
}

//Request...
type AccountFreshTokenRequest struct {
	Request
}

//Params ...
func (that *AccountFreshTokenRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	return _params, nil
}
func (that *AccountFreshTokenRequest) SetPostMpID(value int64) *AccountFreshTokenRequest {
	that.initParams()
	that.params.Set("post_mp_id", utils.ToString(value)) //非令牌中的mp_id
	return that
}

//Do ...
func (that *AccountFreshTokenRequest) Do() (*NewAccountFreshTokenResponse, 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(NewAccountFreshTokenResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Umarshal failed2!")
	}
	return _rs, nil
}
