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

package cooper

import "strconv"

func ToInt(d interface{}) int {
	switch n := d.(type) {
	case int:
		return n
	case string:
		i, err := strconv.Atoi(n)
		if err != nil {
			panic(n + err.Error())
		}
		return i
	case int64:
		return int(n)
	case float32:
		return int(n)
	case float64:
		return int(n)
	case bool:
		if n {
			return 1
		}
		return 0
	case []byte:
		i, err := strconv.Atoi(string(n))
		if err != nil {
			panic(err.Error())
		}
		return i
	}
	return 0
}

//ToInt64 ...
func ToInt64(d interface{}) int64 {
	switch n := d.(type) {
	case int64:
		return n
	case string:
		if n == "" {
			return 0
		}
		i, err := strconv.Atoi(n)
		if err != nil {
			panic(n + err.Error())
		}
		return int64(i)
	case int:
		return int64(n)
	case float32:
		return int64(n)
	case float64:
		return int64(n)
	case bool:
		if n {
			return 1
		}
		return 0
	case []byte:
		i, err := strconv.Atoi(string(n))
		if err != nil {
			panic(err.Error())
		}
		return int64(i)
	}
	return 0
}

//ToString ...
func ToString(d interface{}) string {
	switch n := d.(type) {
	case int64:
		return strconv.FormatInt(n, 10)
	case string:
		return n
	case int:
		return strconv.Itoa(n)
	case float32:
		return strconv.Itoa(int(n))
	case float64:
		return strconv.Itoa(int(n))
	case []byte:
		return string(n)
	case bool:
		if n {
			return "1"
		}
		return "0"
	}
	return "0"
}
