广告平台(媒体使用)
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.
 
 
 
 
 
 

101 lines
2.1 KiB

  1. package svc
  2. import (
  3. db "code.fnuoos.com/zhimeng/model.git/src"
  4. "github.com/gin-gonic/gin"
  5. "xorm.io/xorm"
  6. "applet/app/md"
  7. "applet/app/utils/cache"
  8. )
  9. // 单挑记录获取
  10. func SysCfgGet(c *gin.Context, key string) string {
  11. mid := c.GetString("mid")
  12. eg := db.DBs[mid]
  13. return db.SysCfgGetWithDb(eg, mid, key)
  14. }
  15. // 多条记录获取
  16. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  17. var masterId string
  18. if c == nil {
  19. masterId = ""
  20. } else {
  21. masterId = c.GetString("mid")
  22. }
  23. tmp := SysCfgFindComm(masterId, keys...)
  24. return tmp
  25. }
  26. func SysCfgFindComm(masterId string, keys ...string) map[string]string {
  27. var eg *xorm.Engine
  28. if masterId == "" {
  29. eg = db.Db
  30. } else {
  31. eg = db.DBs[masterId]
  32. }
  33. res := map[string]string{}
  34. //TODO::判断keys长度(大于10个直接查数据库)
  35. if len(keys) > 10 {
  36. cfgList, _ := db.SysCfgGetAll(eg)
  37. if cfgList == nil {
  38. return nil
  39. }
  40. for _, v := range *cfgList {
  41. res[v.Key] = v.Val
  42. }
  43. } else {
  44. for _, key := range keys {
  45. res[key] = db.SysCfgGetWithDb(eg, masterId, key)
  46. }
  47. }
  48. return res
  49. }
  50. // 多条记录获取
  51. func EgSysCfgFind(keys ...string) map[string]string {
  52. var e *xorm.Engine
  53. res := map[string]string{}
  54. if len(res) == 0 {
  55. cfgList, _ := db.SysCfgGetAll(e)
  56. if cfgList == nil {
  57. return nil
  58. }
  59. for _, v := range *cfgList {
  60. res[v.Key] = v.Val
  61. }
  62. // 先不设置缓存
  63. // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
  64. }
  65. if len(keys) == 0 {
  66. return res
  67. }
  68. tmp := map[string]string{}
  69. for _, v := range keys {
  70. if val, ok := res[v]; ok {
  71. tmp[v] = val
  72. } else {
  73. tmp[v] = ""
  74. }
  75. }
  76. return tmp
  77. }
  78. // 清理系统配置信息
  79. func SysCfgCleanCache() {
  80. cache.Del(md.KEY_SYS_CFG_CACHE)
  81. }
  82. // 写入系统设置
  83. func SysCfgSet(c *gin.Context, key, val, memo string) bool {
  84. cfg, err := db.SysCfgGetOne(db.DBs[c.GetString("mid")], key)
  85. if err != nil || cfg == nil {
  86. return db.SysCfgInsert(db.DBs[c.GetString("mid")], key, val, memo)
  87. }
  88. if memo != "" && cfg.Memo != memo {
  89. cfg.Memo = memo
  90. }
  91. SysCfgCleanCache()
  92. return db.SysCfgUpdate(db.DBs[c.GetString("mid")], key, val, cfg.Memo)
  93. }