package data

import (
	"bsm/full/module/base/initial/internal/impl"
	"bsm/full/module/base/initial/internal/models"
	"git.apinb.com/bsm-sdk/core/vars"
)

func GetCountryByCache() ([]*models.InitialCountry, error) {
	var country []*models.InitialCountry

	cacheKey := impl.RedisService.BuildKey("country")

	err := impl.RedisService.Get(cacheKey, &country)
	if err == nil {
		return country, nil
	}

	err = impl.DBService.Where("enabled = ?", true).
		Order("sort_order ASC, name ASC").
		Find(&country).Error

	err = impl.RedisService.Set(cacheKey, country, vars.DefaultTTL)
	return country, err
}
