附近小店
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

db_sys_cfg.go 2.7 KiB

2 ay önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. func SysCfgGetOneData(Db *xorm.Engine, key string) string {
  27. var cfgList model.SysCfg
  28. if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
  29. return ""
  30. }
  31. return cfgList.Val
  32. }
  33. // 返回最后插入id
  34. func SysCfgInsert(Db *xorm.Engine, key, val, memo string) bool {
  35. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  36. _, err := Db.InsertOne(&cfg)
  37. if err != nil {
  38. logx.Error(err)
  39. return false
  40. }
  41. return true
  42. }
  43. func SysCfgUpdate(Db *xorm.Engine, key, val, memo string) bool {
  44. cfg := model.SysCfg{Key: key, Val: val, Memo: memo}
  45. _, err := Db.Where("`key`=?", key).Cols("val,memo").Update(&cfg)
  46. if err != nil {
  47. logx.Error(err)
  48. return false
  49. }
  50. return true
  51. }
  52. // 单条记录获取DB
  53. func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
  54. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId) + HKey
  55. get, err := cache.GetString(cacheKey)
  56. if err != nil || get == "" {
  57. cfg, err := SysCfgGetOne(eg, HKey)
  58. if err != nil || cfg == nil {
  59. _ = logx.Error(err)
  60. return ""
  61. }
  62. // key是否存在
  63. cacheKeyExist := false
  64. if cache.Exists(cacheKey) {
  65. cacheKeyExist = true
  66. }
  67. // 设置缓存
  68. _, err = cache.SetEx(cacheKey, cfg.Val, 30)
  69. if err != nil {
  70. _ = logx.Error(err)
  71. return ""
  72. }
  73. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  74. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  75. if err != nil {
  76. _ = logx.Error(err)
  77. return ""
  78. }
  79. }
  80. return cfg.Val
  81. }
  82. return get
  83. }
  84. func SysCfgGetWithStr(eg *xorm.Engine, masterId string, HKey string) string {
  85. cfg, err := SysCfgGetOne(eg, HKey)
  86. if err != nil || cfg == nil {
  87. _ = logx.Error(err)
  88. return ""
  89. }
  90. return cfg.Val
  91. }
  92. // 多条记录获取DB
  93. func SysCfgFindWithDb(eg *xorm.Engine, masterId string, keys ...string) map[string]string {
  94. res := map[string]string{}
  95. //TODO::判断keys长度(大于10个直接查数据库)
  96. if len(keys) > 10 {
  97. cfgList, _ := SysCfgGetAll(eg)
  98. if cfgList == nil {
  99. return nil
  100. }
  101. for _, v := range *cfgList {
  102. res[v.Key] = v.Val
  103. }
  104. } else {
  105. for _, key := range keys {
  106. res[key] = SysCfgGetWithDb(eg, masterId, key)
  107. }
  108. }
  109. return res
  110. }