广告涉及的mq都放这里
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.

svc_sys_cfg_get.go 3.6 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package svc
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "xorm.io/xorm"
  5. "applet/app/cfg"
  6. "applet/app/db"
  7. "applet/app/md"
  8. "applet/app/utils"
  9. "applet/app/utils/cache"
  10. )
  11. // 单挑记录获取
  12. func SysCfgGet(c *gin.Context, key string) string {
  13. mid := c.GetString("mid")
  14. eg := db.DBs[mid]
  15. return db.SysCfgGetWithDb(eg, mid, key)
  16. }
  17. // 多条记录获取
  18. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  19. var masterId string
  20. if c == nil {
  21. masterId = ""
  22. } else {
  23. masterId = c.GetString("mid")
  24. }
  25. tmp := SysCfgFindComm(masterId, keys...)
  26. return tmp
  27. }
  28. // SysCfgGetByMasterId get one config by master id
  29. func SysCfgGetByMasterId(masterId, key string) string {
  30. res := SysCfgFindComm(masterId, key)
  31. if _, ok := res[key]; !ok {
  32. return ""
  33. }
  34. return res[key]
  35. }
  36. // SysCfgFindComm get cfg by master id
  37. func SysCfgFindComm(masterId string, keys ...string) map[string]string {
  38. var eg *xorm.Engine
  39. if masterId == "" {
  40. eg = db.Db
  41. } else {
  42. eg = db.DBs[masterId]
  43. }
  44. res := map[string]string{}
  45. //TODO::判断keys长度(大于10个直接查数据库)
  46. if len(keys) > 10 {
  47. cfgList, _ := db.SysCfgGetAll(eg)
  48. if cfgList == nil {
  49. return nil
  50. }
  51. for _, v := range *cfgList {
  52. res[v.Key] = v.Val
  53. }
  54. } else {
  55. for _, key := range keys {
  56. res[key] = db.SysCfgGetWithDb(eg, masterId, key)
  57. }
  58. }
  59. return res
  60. }
  61. // 多条记录获取
  62. func EgSysCfgFind(keys ...string) map[string]string {
  63. var e *xorm.Engine
  64. res := map[string]string{}
  65. if len(res) == 0 {
  66. cfgList, _ := db.SysCfgGetAll(e)
  67. if cfgList == nil {
  68. return nil
  69. }
  70. for _, v := range *cfgList {
  71. res[v.Key] = v.Val
  72. }
  73. // 先不设置缓存
  74. // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
  75. }
  76. if len(keys) == 0 {
  77. return res
  78. }
  79. tmp := map[string]string{}
  80. for _, v := range keys {
  81. if val, ok := res[v]; ok {
  82. tmp[v] = val
  83. } else {
  84. tmp[v] = ""
  85. }
  86. }
  87. return tmp
  88. }
  89. // 清理系统配置信息
  90. func SysCfgCleanCache() {
  91. cache.Del(md.KEY_SYS_CFG_CACHE)
  92. }
  93. // 写入系统设置
  94. func SysCfgSet(c *gin.Context, key, val, memo string) bool {
  95. cfg, err := db.SysCfgGetOne(db.DBs[c.GetString("mid")], key)
  96. if err != nil || cfg == nil {
  97. return db.SysCfgInsert(db.DBs[c.GetString("mid")], key, val, memo)
  98. }
  99. if memo != "" && cfg.Memo != memo {
  100. cfg.Memo = memo
  101. }
  102. SysCfgCleanCache()
  103. return db.SysCfgUpdate(db.DBs[c.GetString("mid")], key, val, cfg.Memo)
  104. }
  105. // 多条记录获取
  106. func SysCfgFindByIds(eg *xorm.Engine, keys ...string) map[string]string {
  107. key := utils.Md5(eg.DataSourceName() + md.KEY_SYS_CFG_CACHE)
  108. res := map[string]string{}
  109. c, ok := cfg.MemCache.Get(key).(map[string]string)
  110. if !ok || len(c) == 0 {
  111. cfgList, _ := db.DbsSysCfgGetAll(eg)
  112. if cfgList == nil {
  113. return nil
  114. }
  115. for _, v := range *cfgList {
  116. res[v.Key] = v.Val
  117. }
  118. cfg.MemCache.Put(key, res, 10)
  119. } else {
  120. res = c
  121. }
  122. if len(keys) == 0 {
  123. return res
  124. }
  125. tmp := map[string]string{}
  126. for _, v := range keys {
  127. if val, ok := res[v]; ok {
  128. tmp[v] = val
  129. } else {
  130. tmp[v] = ""
  131. }
  132. }
  133. return tmp
  134. }
  135. // 多条记录获取
  136. func SysCfgFindByIdsToCache(eg *xorm.Engine, dbName string, keys ...string) map[string]string {
  137. key := utils.Md5(dbName + md.KEY_SYS_CFG_CACHE)
  138. res := map[string]string{}
  139. c, ok := cfg.MemCache.Get(key).(map[string]string)
  140. if !ok || len(c) == 0 {
  141. cfgList, _ := db.DbsSysCfgGetAll(eg)
  142. if cfgList == nil {
  143. return nil
  144. }
  145. for _, v := range *cfgList {
  146. res[v.Key] = v.Val
  147. }
  148. cfg.MemCache.Put(key, res, 1800)
  149. } else {
  150. res = c
  151. }
  152. if len(keys) == 0 {
  153. return res
  154. }
  155. tmp := map[string]string{}
  156. for _, v := range keys {
  157. if val, ok := res[v]; ok {
  158. tmp[v] = val
  159. } else {
  160. tmp[v] = ""
  161. }
  162. }
  163. return tmp
  164. }