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

package cooper

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

//NewUidGet ...
func (that *CooperClient) NewUid() *UidRequest {
	return &UidRequest{
		Client:     that,
		apiAddress: "/uid/get",
	}
}

type UidReturn struct {
	Uid []int64
}

//UidRequest 唯一id生成器
type UidRequest struct {
	Client     *CooperClient
	Name       string `json:"name"`       //名称
	PreDomain  string `json:"pre_domain"` //前置域
	StartId    int64  `json:"start_id"`   //初始化开始值
	Count      int64  `json:"count"`      //数量
	apiAddress string
}

func (that *UidRequest) SetName(name string, startID ...int64) *UidRequest {
	that.Name = name
	if len(startID) > 0 {
		that.StartId = startID[0]
	}
	return that
}
func (that *UidRequest) SetPreDomain(value string) *UidRequest {
	that.PreDomain = value //例如：20211230000000 + ....
	return that
}

func (that *UidRequest) SetCount(conut int64) *UidRequest {
	that.Count = conut
	that.apiAddress = "/uid/getSlice"
	return that
}

//Params ...
func (that *UidRequest) Params() (url.Values, error) {
	var m = make(url.Values)
	m.Set("name", ToString(that.Name))
	m.Set("start_id", ToString(that.StartId))
	m.Set("count", ToString(that.Count))
	if that.PreDomain != "" {
		m.Set("pre_domain", that.PreDomain)
	}
	return m, nil
}

//Do ...
func (that *UidRequest) Do() (*UidReturn, error) {
	//
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_intSlice := []int64{}
	switch _v := _data.(type) {
	case float64:
		_intSlice = append(_intSlice, ToInt64(_v))
	case string:
		_intSlice = append(_intSlice, ToInt64(_v))
	case int64:
		_intSlice = append(_intSlice, ToInt64(_v))
	case int:
		_intSlice = append(_intSlice, ToInt64(_v))
	case []int64:
		_intSlice = append(_intSlice, _v...)
	case []interface{}:
		for _, _val := range _v {
			_intSlice = append(_intSlice, ToInt64(_val))
		}
	default:
		return nil, errors.New("uid return data type is:" + utils.TypeOf(_data))
	}
	return &UidReturn{_intSlice}, nil
}
