// this file is auto create by program, don't edit manually

package data

import (
	mhayaError "github.com/mhaya/error"
	mhayaLogger "github.com/mhaya/logger"
)

type serverConfig struct {
	maps map[int]*ServerConfigRow
}

type ServerConfigRow struct {
	ID        int         // 配置ID
	Name      string      // 配置描述信息
	Enable    int         // 是否启用0否1是
	Max       int         // 服务器最大负载
	Expansion int         // 扩容指标（到达扩容指标权重降低到1）
	Weight    WeightInt32 // 权重规则
	Range     int         // 规则标准(均值每100为标准)
}

func (p *serverConfig) Name() string {
	return "ServerConfig"
}

func (p *serverConfig) Init() {
	p.maps = make(map[int]*ServerConfigRow)
}

func (p *serverConfig) OnLoad(maps interface{}, _ bool) (int, error) {
	list, ok := maps.([]interface{})
	if !ok {
		return 0, mhayaError.Error("maps convert to []interface{} error.")
	}

	loadMaps := make(map[int]*ServerConfigRow)
	for index, data := range list {
		loadConfig := &ServerConfigRow{}
		err := DecodeData(data, loadConfig)
		if err != nil {
			mhayaLogger.Warnf("decode error. [row = %d, %v], err = %s", index+1, loadConfig, err)
			continue
		}

		loadMaps[loadConfig.ID] = loadConfig
	}

	p.maps = loadMaps

	return len(list), nil
}

func (p *serverConfig) OnAfterLoad(_ bool) {
}

func (p *serverConfig) Get() (*ServerConfigRow, bool) {
	for _, row := range p.maps {
		if row.Enable == 1 {
			return row, true
		}
	}
	return nil, false
}
