/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2020-08-21
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */

package pposRequest

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

//new...
func (that *PPOSClient) NewLoginMp() *LoginMpRequest {
	return &LoginMpRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/account/loginMp",
		},
	}
}

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

//Request...
type LoginMpRequest struct {
	Request
}

func (that *LoginMpRequest) SetAccount(value string) *LoginMpRequest {
	that.initParams()
	that.params.Set("account", value) //登录账号
	return that
}
func (that *LoginMpRequest) SetPassword(value string) *LoginMpRequest {
	that.initParams()
	that.params.Set("password", value) //登录密码
	return that
}
func (that *LoginMpRequest) SetDomain(value string) *LoginMpRequest {
	that.initParams()
	that.params.Set("domain", value) //使用域名
	return that
}
func (that *LoginMpRequest) SetPostMpID(value int64) *LoginMpRequest {
	that.initParams()
	that.params.Set("post_mp_id", utils.ToString(value)) //非令牌中的mp_id
	return that
}

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

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