sys.go 915 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package config
  2. import (
  3. "gadmin/internal/gorm/model"
  4. "github.com/spf13/cast"
  5. )
  6. func GetInt64(key string) int64 {
  7. val := Get(key)
  8. if val == nil {
  9. return 0
  10. }
  11. return cast.ToInt64(val)
  12. }
  13. // Get 获取指定配置
  14. func Get(key string) interface{} {
  15. var models *model.Config
  16. DB.Select("id", "value").Where("`key` = ?", key).First(&models)
  17. if models == nil {
  18. return nil
  19. }
  20. return models.Value
  21. }
  22. func FirstOrCreate(key string, groupName string) int64 {
  23. var models *model.Config
  24. DB.Select("id", "value").Where("`key` = ?", key).First(&models)
  25. if models == nil || models.ID == 0 {
  26. ni := model.Config{Key: key, Value: "0", DefaultValue: "0", Type: "int", Group_: groupName, Sort: 0}
  27. DB.Create(&ni)
  28. return 0
  29. }
  30. return cast.ToInt64(models.Value)
  31. }
  32. // Save 更新指定配置值
  33. func Save(key string, value interface{}) {
  34. DB.Model(&model.Config{}).Where("`key` = ?", key).Update("value", value)
  35. }