package otherutils

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
)

// 判断一个int类型的值是否在一个数组或者切片中
func ArrayExistInt(value int, array []int) bool {
	for _, v := range array {
		if value == v {
			return true
		}
	}
	return false
}

// 判断一个int32类型的值是否在一个数组或者切片中
func ArrayExistInt32(value int32, array []int32) bool {
	for _, v := range array {
		if value == v {
			return true
		}
	}
	return false
}

// 判断一个int64类型的值是否在一个数组或者切片中
func ArrayExistInt64(value int64, array []int64) bool {
	for _, v := range array {
		if value == v {
			return true
		}
	}
	return false
}

// 判断一个string类型的值是否在一个数组或者切片中
func ArrayExistString(value string, array []string) bool {
	for _, v := range array {
		if value == v {
			return true
		}
	}
	return false
}

// 返回分割后转换为string
func StringSplitString(s, sep string) ([]string, error) {
	ret := make([]string, 0)
	str := strings.Split(s, sep)
	for _, v := range str {
		if v != "" {
			ret = append(ret, v)
		}
	}
	return ret, nil
}

// 返回分割后转换为int
func StringSplitInt(s, sep string) ([]int, error) {
	ret := make([]int, 0)
	str := strings.Split(s, sep)
	for _, v := range str {
		if v != "" {
			value, err := strconv.Atoi(v)
			if err != nil {
				return ret, err
			}
			ret = append(ret, value)
		}
	}
	return ret, nil
}

// 返回分割后转换为int64
func StringSplitInt64(s, sep string) ([]int64, error) {
	ret := make([]int64, 0)
	str := strings.Split(s, sep)
	for _, v := range str {
		if v != "" {
			value, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				return ret, err
			}
			ret = append(ret, value)
		}
	}
	return ret, nil
}

// []string后转换为[]int64
func ArrStringConvertArrInt64(s []string) ([]int64, error) {
	ret := make([]int64, 0)
	for _, v := range s {
		if v != "" {
			value, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				return ret, err
			}
			ret = append(ret, value)
		}
	}
	return ret, nil
}

// []Int64后转换为[]string
func ArrInt64ConvertArrString(s []int64) []string {
	ret := make([]string, 0)
	for _, v := range s {
		value := strconv.FormatInt(v, 10)
		ret = append(ret, value)
	}
	return ret
}

// 获取key
func GetKey(a int, b int) string {
	return fmt.Sprintf("%v:%v", a, b)
}

// 获取keys
func GetKeys(a int, b int, c int) string {
	return fmt.Sprintf("%v:%v:%v", a, b, c)
}

func DumpToJSON(s interface{}) string {
	marshal, err := json.Marshal(s)
	if err != nil {
		return err.Error()
	}
	return string(marshal)
}
