package utils

import (
	"encoding/json"
	"fmt"
	"time"
)

func StringSliceEqualBCE(a, b []int) bool {
	if len(a) != len(b) {
		return false
	}

	if (a == nil) != (b == nil) {
		return false
	}

	b = b[:len(a)]
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}

	return true
}

func IsContain(items []interface{}, item string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

type LocalTime time.Time

// MarshalJSON satify the json marshal interface
func (l LocalTime) MarshalJSON() ([]byte, error) {
	stamp := fmt.Sprintf("\"%s\"", time.Time(l).Format("2006-01-02 15:04:05"))
	return []byte(stamp), nil
}

type LocalDate time.Time

// MarshalJSON satify the json marshal interface
func (l LocalDate) MarshalJSON() ([]byte, error) {
	stamp := fmt.Sprintf("\"%s\"", time.Time(l).Format("2006-01-02"))
	return []byte(stamp), nil
}

func JsonToMap(jsonStr string) (map[string]string, error) {
	m := make(map[string]string)
	err := json.Unmarshal([]byte(jsonStr), &m)
	if err != nil {
		fmt.Printf("Unmarshal with error: %+v\n", err)
		return nil, err
	}

	for k, v := range m {
		fmt.Printf("%v: %v\n", k, v)
	}

	return m, nil
}

func InsertSlice(index int, newstr []string, src []string) (ns []string) {
	ns = append(ns, src[:index]...) // 切片后加..., 相当于拆包成单个元素
	ns = append(ns, newstr...)
	ns = append(ns, src[index:]...)
	return
}

//RankPeriodCheckTime 设置赛季时检查时间范围的合法性
//func RankPeriodCheckTime(rankTypeId int64, beginTime int64, endTime int64, idNot int64) bool {
//	sql := `SELECT count(*) FROM rank_period
//			WHERE rank_type_id = %d
//			AND id <> %d
//			AND
//			(
//			(UNIX_TIMESTAMP(begin_time) BETWEEN %d AND %d)
//			OR
//			(UNIX_TIMESTAMP(end_time) BETWEEN %d AND %d)
//			OR
//			(%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
//			OR
//			(%d BETWEEN UNIX_TIMESTAMP(begin_time) AND UNIX_TIMESTAMP(end_time))
//			)
//			LIMIT 1 `
//	sql = fmt.Sprintf(sql, rankTypeId, idNot, beginTime, endTime, beginTime, endTime, beginTime, endTime)
//	var cnt int
//	err := utils.ExecuteQueryOne(&cnt, sql)
//	if err != nil {
//		log.Error("SQL Execute err:%s", err)
//		return false
//	}
//	if cnt > 0 {
//		return false
//	}
//	return true
//}