package data

import (
	"bsm/full/module/base/initial/internal/impl"
	"bsm/full/module/base/initial/internal/models"
	"gorm.io/gorm"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/vars"
)

func GetDatasByCache() ([]*models.InitialDatas, error) {
	var datas []*models.InitialDatas

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

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

	err = impl.DBService.Order("data_type ASC, id ASC").Find(&datas).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, errcode.ErrDB
	}

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