智慧食堂-队列消费项目
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.
 
 
 

191 lines
4.0 KiB

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