package common

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
)

// JSONMap JSON映射类型
type JSONMap map[string]interface{}

// Value 实现driver.Valuer接口
func (m JSONMap) Value() (driver.Value, error) {
	if m == nil {
		return nil, nil
	}
	return json.Marshal(m)
}

// Scan 实现sql.Scanner接口
func (m *JSONMap) Scan(value interface{}) error {
	if value == nil {
		*m = make(map[string]interface{})
		return nil
	}
	var err error
	switch v := value.(type) {
	case []byte:
		err = json.Unmarshal(v, m)
	case string:
		err = json.Unmarshal([]byte(v), m)
	default:
		err = errors.New("basetypes.JSONMap.Scan: invalid value type")
	}
	if err != nil {
		return err
	}
	return nil
}

// TreeNode 树节点接口
type TreeNode[T any] interface {
	GetChildren() []T
	SetChildren(children T)
	GetID() int
	GetParentID() int
}

// ClearDB 清理数据库配置
type ClearDB struct {
	TableName    string
	CompareField string
	Interval     string
}
