package pposRequest

import (
	"errors"
	"fmt"
	"log"
	"net/url"
	"time"
)

//发送消息
func (that *PPOSClient) NewPubMessagePublishRequest() *PubMessagePublishRequest {
	return &PubMessagePublishRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/pub/message/publishMessage",
		},
		bT: time.Now(),
	}
}

//
type PubMessagePublishRequest struct {
	Request
	publicParamsSlice []string  //必传参数名
	bT                time.Time //耗时时间
}

func (that *PubMessagePublishRequest) SetAccount(value string) *PubMessagePublishRequest {
	that.initParams()
	that.params.Set("account", value) //接收人员工id
	return that
}
func (that *PubMessagePublishRequest) SetContent(value string) *PubMessagePublishRequest {
	that.initParams()
	that.params.Set("content", value) //消息内容
	return that
}

func (that *PubMessagePublishRequest) SetCfgName(value string) *PubMessagePublishRequest {
	that.initParams()
	that.params.Set("cfg_name", value) //消息类型名称
	return that
}

func (that *PubMessagePublishRequest) SetCfgTempId(value string) *PubMessagePublishRequest {
	that.initParams()
	that.params.Set("cfg_temp_id", value) //业务消息类型id
	return that
}
func (that *PubMessagePublishRequest) SetExtendField(value string) *PubMessagePublishRequest {
	that.initParams()
	that.params.Set("extend_field", value) //消息扩展信息
	return that
}

//Params ...
func (that *PubMessagePublishRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	//
	for _, _v := range that.publicParamsSlice {
		if _params.Get(_v) == "" {
			return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
		}
	}
	return _params, nil
}

//Do ...
func (that *PubMessagePublishRequest) Do() (bool, error) {
	//返回data
	_rs, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return false, _err
	}
	fmt.Println("+++++++++++", _rs)
	return true, nil
}
