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

type ChannelConfigRow struct {
	ID     int    // 渠道ID
	Name   string // 名称
	Enable int    //是否开启
}

func (p *channelConfig) Name() string {
	return "ChannelConfig"
}

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

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

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

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

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