package check

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

// GetConfigCache 获取配置缓存
func GetConfigByCache(app, os string) ([]*models.InitialConfig, error) {
	var configs []*models.InitialConfig

	key := impl.RedisService.BuildKey("config", app, os)

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

	err = impl.DBService.Where("app = ? AND (os = ? OR os = ?)", app, os, "ALL").
		Order("CASE WHEN os = '" + os + "' THEN 0 ELSE 1 END, id DESC").
		Find(&configs).Error
	if err != nil {
		return nil, err
	}

	err = impl.RedisService.Set(key, configs, vars.DefaultTTL)
	return configs, err
}
