package model

import (
	"database/sql/driver"
	"encoding/json"
	mapset "github.com/deckarep/golang-set"
	"time"
)

type ListenServer struct {
	Port         int           `yaml:"Port"`         //Server监听端口
	ReadTimeOut  time.Duration `yaml:"ReadTimeOut"`  //Tcp 读超时时间
	WriteTimeOut time.Duration `yaml:"WriteTimeOut"` //Tcp 写超时时间
}

type Redis struct {
	Type           string        `yaml:"Type"`
	Host           string        `yaml:"Host"`
	Port           string        `yaml:"Port"`
	PassWord       string        `yaml:"PassWord"`
	Name           int           `yaml:"Name"`
	MaxIdle        int           `yaml:"MaxIdle"`
	MaxActive      int           `yaml:"MaxActive"`
	ReadTimeout    time.Duration `yaml:"ReadTimeout"`
	WriteTimeout   time.Duration `yaml:"WriteTimeout"`
	ConnectTimeout time.Duration `yaml:"ConnectTimeout"`
	IdleTimeout    time.Duration `yaml:"IdleTimeout"`
}

type Rds struct {
	Host          string `yaml:"Host"`
	Port          int    `yaml:"Port"`
	User          string `yaml:"User"`
	Name          string `yaml:"Name"`
	PassWord      string `yaml:"PassWord"`
	CharSet       string `yaml:"CharSet"`
	MaxIdleConns  int    `yaml:"MaxIdleConns"`
	MaxOpenConns  int    `yaml:"MaxOpenConns"`
	SlowThreshold int    `yaml:"SlowThreshold"`
}

//type RMq struct {
//	Type     string `yaml:"Type"`
//	Host     string `yaml:"Host"`
//	Port     int    `yaml:"Port"`
//	User     string `yaml:"User"`
//	PassWord string `yaml:"PassWord"`
//}

type Arry []int64

func (j *Arry) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), &j)
}

func (j Arry) Value() (driver.Value, error) {
	return json.Marshal(j)
}

func (j Arry) Set() mapset.Set {

	s1 := mapset.NewSet()

	for _, item := range j {
		s1.Add(item)
	}
	return s1
}

func (j Arry) SetToValue(s1 mapset.Set) Arry {

	s2 := Arry{}

	for _, item := range s1.ToSlice() {
		s2 = append(s2, item.(int64))
	}
	return s2
}

func (j Arry) ToInt64() []int64 {
	s1 := []int64{}
	for _, item := range j {
		s1 = append(s1, item)
	}
	return s1
}

func (j Arry) Of(id int64) bool {
	for _, item := range j {
		if item == id {
			return true
		}
	}
	return false
}

type ArryString []string

func (j *ArryString) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), &j)
}

func (j ArryString) Value() (driver.Value, error) {
	return json.Marshal(j)
}

func (j ArryString) Set() mapset.Set {

	s1 := mapset.NewSet()

	for _, item := range j {
		s1.Add(item)
	}
	return s1
}

func (j ArryString) SetToValue(s1 mapset.Set) ArryString {

	s2 := ArryString{}

	for _, item := range s1.ToSlice() {
		s2 = append(s2, item.(string))
	}
	return s2
}

func (j ArryString) ToString() []string {
	s1 := []string{}
	for _, item := range j {
		s1 = append(s1, item)
	}
	return s1
}

func (j ArryString) Of(id string) bool {
	for _, item := range j {
		if item == id {
			return true
		}
	}
	return false
}

func (j *ArryString) Add(id string) *ArryString {
	*j = append(*j, id)
	return j
}

func (j *ArryString) Remove(id string) *ArryString {
	// 如果未找到要移除的元素,直接返回原数组
	return j
}

type JSON map[string]interface{}

func (j *JSON) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), &j)
}

func (j JSON) Value() (driver.Value, error) {
	return json.Marshal(j)
}

type RouterMethod struct {
	Name   string
	Path   string
	Params interface{}
	Method string
	Leaf   bool // 是否叶子节点
	F      interface{}
	Child  []*RouterMethod
}