面包店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_sys_cfg_get.go 2.8 KiB

8 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/utils/cache"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // 单挑记录获取
  10. func SysCfgGet(c *gin.Context, key string) string {
  11. sysCfgDb := db.SysCfgDb{}
  12. sysCfgDb.Set()
  13. return sysCfgDb.SysCfgGetWithDb(key)
  14. }
  15. func SysCfgDel(c *gin.Context, key string) bool {
  16. sysCfgDb := db.SysCfgDb{}
  17. sysCfgDb.Set()
  18. err := sysCfgDb.SysCfgDel(key)
  19. if err != nil {
  20. return false
  21. }
  22. return true
  23. }
  24. // 多条记录获取
  25. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  26. var masterId string
  27. if c == nil {
  28. masterId = ""
  29. } else {
  30. masterId = c.GetString("mid")
  31. }
  32. tmp := SysCfgFindComm(masterId, keys...)
  33. return tmp
  34. }
  35. // SysCfgGetByMasterId get one config by master id
  36. func SysCfgGetByMasterId(masterId, key string) string {
  37. res := SysCfgFindComm(masterId, key)
  38. if _, ok := res[key]; !ok {
  39. return ""
  40. }
  41. return res[key]
  42. }
  43. // SysCfgFindComm get cfg by master id
  44. func SysCfgFindComm(masterId string, keys ...string) map[string]string {
  45. sysCfgDb := db.SysCfgDb{}
  46. sysCfgDb.Set()
  47. res := map[string]string{}
  48. cfgKey := fmt.Sprintf("%s:cfg_cache", masterId)
  49. err := cache.GetJson(cfgKey, &res)
  50. if err != nil || len(res) == 0 {
  51. cfgList, _ := sysCfgDb.SysCfgGetAll()
  52. if cfgList == nil {
  53. return nil
  54. }
  55. for _, v := range *cfgList {
  56. res[v.Key] = v.Val
  57. }
  58. cache.SetJson(cfgKey, res, 1800)
  59. }
  60. if len(keys) == 0 {
  61. return res
  62. }
  63. tmp := map[string]string{}
  64. for _, v := range keys {
  65. if val, ok := res[v]; ok {
  66. tmp[v] = val
  67. } else {
  68. tmp[v] = ""
  69. }
  70. }
  71. return tmp
  72. }
  73. // 多条记录获取
  74. func EgSysCfgFind(keys ...string) map[string]string {
  75. sysCfgDb := db.SysCfgDb{}
  76. sysCfgDb.Set()
  77. res := map[string]string{}
  78. if len(res) == 0 {
  79. cfgList, _ := sysCfgDb.SysCfgGetAll()
  80. if cfgList == nil {
  81. return nil
  82. }
  83. for _, v := range *cfgList {
  84. res[v.Key] = v.Val
  85. }
  86. // 先不设置缓存
  87. // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
  88. }
  89. if len(keys) == 0 {
  90. return res
  91. }
  92. tmp := map[string]string{}
  93. for _, v := range keys {
  94. if val, ok := res[v]; ok {
  95. tmp[v] = val
  96. } else {
  97. tmp[v] = ""
  98. }
  99. }
  100. return tmp
  101. }
  102. // 清理系统配置信息
  103. func SysCfgCleanCache(c *gin.Context) {
  104. var tmp = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  105. for _, v := range tmp {
  106. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, v)
  107. cache.Del(cacheKey)
  108. }
  109. }
  110. // 写入系统设置
  111. func SysCfgSet(c *gin.Context, key, val, memo string) bool {
  112. sysCfgDb := db.SysCfgDb{}
  113. sysCfgDb.Set()
  114. cfg, err := sysCfgDb.SysCfgGetOne(key)
  115. if err != nil || cfg == nil {
  116. return sysCfgDb.SysCfgInsert(key, val, memo)
  117. }
  118. if memo != "" && cfg.Memo != memo {
  119. cfg.Memo = memo
  120. }
  121. SysCfgCleanCache(c)
  122. return sysCfgDb.SysCfgUpdate(key, val)
  123. }