package utils

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"github.com/shopspring/decimal"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/log"
	"strconv"
	"strings"
)

// Intersect 返回两个数组的交集
func Intersect(nums1 []int64, nums2 []int64) []int64 {
	//if len(nums1) > len(nums2) {
	//	return Intersect(nums2, nums1)
	//}
	//m := map[int64]int64{}
	//for _, num := range nums1 {
	//	m[num]++
	//}
	//
	//var intersection []int64
	//for _, num := range nums2 {
	//	if m[num] > 0 {
	//		intersection = append(intersection, num)
	//		m[num]--
	//	}
	//}
	//return intersection
	m := make(map[int64]int)
	nn := make([]int64, 0)
	for _, v := range nums1 {
		m[v]++
	}
	for _, v := range nums2 {
		times, _ := m[v]
		if times > 0 {
			nn = append(nn, v)
		}
	}
	return nn

}

// Intersect32 返回两个数组的交集
func Intersect32(nums1 []int32, nums2 []int32) []int32 {
	if len(nums1) > len(nums2) {
		return Intersect32(nums2, nums1)
	}
	m := map[int32]int32{}
	for _, num := range nums1 {
		m[num]++
	}

	var intersection []int32
	for _, num := range nums2 {
		if m[num] > 0 {
			intersection = append(intersection, num)
			m[num]--
		}
	}
	return intersection
}

// Difference 求差集 slice1-并集
func Difference(slice1, slice2 []int64) []int64 {
	//m := make(map[int64]int)
	//nn := make([]int64, 0)
	//inter := Intersect(slice1, slice2)
	//for _, v := range inter {
	//	m[v]++
	//}
	//
	//for _, value := range slice1 {
	//	times, _ := m[value]
	//	if times == 0 {
	//		nn = append(nn, value)
	//	}
	//}
	//return nn
	m := make(map[int64]int)
	nn := make([]int64, 0)
	inter := Intersect(slice1, slice2)
	for _, v := range inter {
		m[v]++
	}
	for _, v := range slice1 {
		times, _ := m[v]
		if times == 0 {
			nn = append(nn, v)
		}
	}
	return nn

}

// SliceAtoi 字符创数组转数字数组
func SliceAtoi(sa []string) ([]int64, error) {
	si := make([]int64, 0, len(sa))
	for _, a := range sa {
		i, err := strconv.ParseInt(a, 10, 64)
		if err != nil {
			return si, err
		}
		si = append(si, i)
	}
	return si, nil
}

// SliceItoa 数字数组转字符创数组
func SliceItoa(sa []int64) []string {
	si := make([]string, 0, len(sa))
	for _, a := range sa {
		s := strconv.Itoa(int(a))
		si = append(si, s)
	}
	return si
}

func Round(value float64, places int32) float64 {
	quantity := decimal.NewFromFloat(value)
	d := quantity.Round(places)
	rsp, _ := d.Float64()
	return rsp
}

// NumberToCNNumber 数字转中文数字
func NumberToCNNumber(num int) string {
	chineseMap := []string{"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"}
	chineseNum := []string{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
	var listNum []int
	for ; num > 0; num = num / 10 {
		listNum = append(listNum, num%10)
	}
	n := len(listNum)
	chinese := ""
	//注意这里是倒序的
	for i := n - 1; i >= 0; i-- {
		chinese = fmt.Sprintf("%s%s%s", chinese, chineseNum[listNum[i]], chineseMap[i])
	}
	//注意替换顺序
	for {
		copyChinese := chinese
		copyChinese = strings.Replace(copyChinese, "零万", "万", 1)
		copyChinese = strings.Replace(copyChinese, "零亿", "亿", 1)
		copyChinese = strings.Replace(copyChinese, "零十", "零", 1)
		copyChinese = strings.Replace(copyChinese, "零百", "零", 1)
		copyChinese = strings.Replace(copyChinese, "零千", "零", 1)
		copyChinese = strings.Replace(copyChinese, "零零", "", 1)
		//copyChinese = strings.Replace(copyChinese, "零圆", "", 1)
		//copyChinese = strings.Replace(copyChinese, "零", "", 1)

		if copyChinese == chinese {
			break
		} else {
			chinese = copyChinese
		}
	}
	return "第" + chinese + "阶段"
}

// RemoveDuplication 数组去重
func RemoveDuplication(arr []string) []string {
	set := make(map[string]struct{}, len(arr))
	j := 0
	for _, v := range arr {
		_, ok := set[v]
		if ok {
			continue
		}
		set[v] = struct{}{}
		arr[j] = v
		j++
	}
	return arr[:j]
}

func RemoveDuplicationInt64(arr []int64) []int64 {
	set := make(map[int64]struct{}, len(arr))
	j := 0
	for _, v := range arr {
		_, ok := set[v]
		if ok {
			continue
		}
		set[v] = struct{}{}
		arr[j] = v
		j++
	}
	return arr[:j]
}

func RemoveDuplicationString(arr []string) []string {
	set := make(map[string]struct{}, len(arr))
	j := 0
	for _, v := range arr {
		_, ok := set[v]
		if ok {
			continue
		}
		set[v] = struct{}{}
		arr[j] = v
		j++
	}
	return arr[:j]
}

func RemoveDuplicationInt32(arr []int32) []int32 {
	set := make(map[int32]struct{}, len(arr))
	j := 0
	for _, v := range arr {
		_, ok := set[v]
		if ok {
			continue
		}
		set[v] = struct{}{}
		arr[j] = v
		j++
	}
	return arr[:j]
}

// IsContain 判断int32数组是否包含
func IsContain(items []int32, item int32) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

// IsContain64 判断int64数组是否包含
func IsContain64(items []int64, item int64) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

func AnyToHex(any interface{}) []byte {
	buff := new(bytes.Buffer)
	//数据写入buff
	err := binary.Write(buff, binary.BigEndian, any)
	if err != nil {
		log.Logger.Error(err.Error())
	}
	return buff.Bytes()
}