/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		logan <lgq092@163.com>
 * @date		2021-02-28
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */
package cooper

import (
	"errors"
	"net/url"
)

//NewSmsNewRequest ...短信发送
func (that *CooperClient) NewSmsNewRequest() *SmsNewRequest {
	return &SmsNewRequest{
		Request: Request{
			Client:            that,
			publicParamsSlice: []string{
				//...
			},
			apiAddress: "/sms/push",
		},
	}
}

//SmsNewRequest
type SmsNewRequest struct {
	Request
}

//SetTempId 设置papa_sms_temp表的消息模板id
func (that *SmsNewRequest) SetTempId(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("temp_id", value)
	return that
}

//SetSmsType 设置消息发送方式：1短信，2后台站内消息，3微信推送
func (that *SmsNewRequest) SetSmsType(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sms_type", value)
	return that
}

//SetSmsParams 设置消息模板参数：多个参数值，用逗号连接
func (that *SmsNewRequest) SetSmsParams(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sms_params", value)
	return that
}

//SetSmsMobile 设置手机号，多个用逗号分隔
func (that *SmsNewRequest) SetSmsMobile(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sms_mobile", value)
	return that
}

//SetSmsCallback 设置消息发送成功后，回调地址：带域名的完整地址
func (that *SmsNewRequest) SetSmsCallback(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sms_callback", value)
	return that
}

//SetSmsSendAsync 设置消息发送模式：0同步（默认），1异步
func (that *SmsNewRequest) SetSmsSendAsync(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sms_send_async", value)
	return that
}

//SetSignName 设置签名
func (that *SmsNewRequest) SetSignName(value string) *SmsNewRequest {
	that.initParams()
	that.params.Set("sign_name", value)
	return that
}

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

type SmsPushResponse struct {
	RecordId  string `json:"record_id"`  //消息发送凭证
	SmsStatus string `json:"sms_status"` //消息状态(成功：1)
	Desc      string `json:"desc"`       //消息描述
}

//Do ...
func (that *SmsNewRequest) Do() (*SmsPushResponse, error) {
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	_rs := new(SmsPushResponse)
	if _err := that.ParseResponeMp(_rs, _sliceData, false); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	return _rs, nil
}
