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

type RankConfigRow struct {
	ID        int          // #排行榜ID
	Enable    int          // 是否开启(0否1是)
	Type      int          // 榜单类型(1日榜单2周榜单)
	Source    int          // 来源(1积分2邀请3抽奖)
	ShowTotal int          // 总展示多少名
	Reward    []ItemReward // 榜单总奖池
	IsBot     int          // 机器人是否参加(0否1是)
	StartTime string       // 开始时间
	EndTime   string       // 结束时间
	ShowTime  string       // 展示时间
}

func (p *rankConfig) Name() string {
	return "rankConfig"
}

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

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

func (p *rankConfig) GetAll() map[int]*RankConfigRow {
	var ret = make(map[int]*RankConfigRow)
	for _, v := range p.maps {
		if v.Enable == 1 {
			ret[v.ID] = v
		}
	}
	return ret
}

func (p *rankConfig) Get(pk int) (*RankConfigRow, bool) {
	i, found := p.maps[pk]
	return i, found
}

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