// 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 ruleConfig struct {
	maps map[int]*RuleConfigRow
}

type RuleConfigRow struct {
	ID      int // ID
	Enable  int // 是否开启规则 0 否 1是
	Num     int // 注册人数
	MaxTime int // 过期时间(按天)
}

func (p *ruleConfig) Name() string {
	return "RuleConfig"
}

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

func (p *ruleConfig) 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]*RuleConfigRow)
	for index, data := range list {
		loadConfig := &RuleConfigRow{}
		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 *ruleConfig) OnAfterLoad(_ bool) {
}

func (p *ruleConfig) Get(pk int) (*RuleConfigRow, bool) {
	i, found := p.maps[pk]
	if found {
		if i.Enable == 0 {
			return nil, false
		}
	}
	return i, found
}

func (p *ruleConfig) Contain(pk int) bool {
	_, found := p.Get(pk)
	return found
}
