package cacheApi

import (
	"epur-pay/model"
	"epur-pay/pkg/logger"
	"epur-pay/pkg/rds"
	"epur-pay/pkg/utils"
	"sync"
)

type Version struct {
	Data      sync.Map
	duplicate *Version
}

func (p *Version) Refresh() *Version {

	p.duplicate = &Version{}

	versions := make([]model.Version, 0)

	utils.Error(rds.DB.Table(model.Version{}.TableName()).Where(`status = 0`).Scan(&versions).Error)

	for index, _ := range versions {
		p.duplicate.Data.Store(versions[index].Type, versions[index])
	}

	return p.duplicate
}

func (p *Version) RefreshRow(data interface{}) {

	instance := data.(model.Version)
	p.Data.Store(instance.Type, instance)
}

func (p *Version) Get(key string) *model.Version {

	if row, ok := p.Data.Load(key); ok {
		t1 := row.(model.Version)
		return &t1
	} else {
		logger.AccessLogger.Errorln("缓存不存在了,注意!", key)
		return nil
	}
}