蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_sys_cfg.go 1.8 KiB

3 days ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils/cache"
  6. "applet/app/utils/logx"
  7. "fmt"
  8. "xorm.io/xorm"
  9. )
  10. // 系统配置get
  11. func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) {
  12. var cfgList []model.SysCfg
  13. if err := Db.Cols("key,val,memo").Find(&cfgList); err != nil {
  14. return nil, logx.Error(err)
  15. }
  16. return &cfgList, nil
  17. }
  18. // 获取一条记录
  19. func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) {
  20. var cfgList model.SysCfg
  21. if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
  22. return nil, logx.Error(err)
  23. }
  24. return &cfgList, nil
  25. }
  26. // 返回最后插入id
  27. func SysCfgInsert(Db *xorm.Engine, key, val, memo string) bool {
  28. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  29. _, err := Db.InsertOne(&cfg)
  30. if err != nil {
  31. logx.Error(err)
  32. return false
  33. }
  34. return true
  35. }
  36. func SysCfgUpdate(Db *xorm.Engine, key, val, memo string) bool {
  37. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  38. _, err := Db.Where("`key`=?", key).Cols("val,memo").Update(&cfg)
  39. if err != nil {
  40. logx.Error(err)
  41. return false
  42. }
  43. return true
  44. }
  45. func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
  46. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId) + HKey
  47. get, err := cache.GetString(cacheKey)
  48. if err != nil || get == "" {
  49. cfg, err := SysCfgGetOne(eg, HKey)
  50. if err != nil || cfg == nil {
  51. _ = logx.Error(err)
  52. return ""
  53. }
  54. // key是否存在
  55. cacheKeyExist := false
  56. if cache.Exists(cacheKey) {
  57. cacheKeyExist = true
  58. }
  59. // 设置缓存
  60. _, err = cache.SetEx(cacheKey, cfg.Val, 30)
  61. if err != nil {
  62. _ = logx.Error(err)
  63. return ""
  64. }
  65. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  66. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  67. if err != nil {
  68. _ = logx.Error(err)
  69. return ""
  70. }
  71. }
  72. return cfg.Val
  73. }
  74. return get
  75. }