|
- package svc
-
- import (
- db "code.fnuoos.com/zhimeng/model.git/src"
- "github.com/gin-gonic/gin"
- "xorm.io/xorm"
-
- "applet/app/md"
- "applet/app/utils/cache"
- )
-
- // 单挑记录获取
- func SysCfgGet(c *gin.Context, key string) string {
- mid := c.GetString("mid")
- eg := db.Db
- return db.SysCfgGetWithDb(eg, mid, key)
- }
-
- // 多条记录获取
- func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
- var masterId string
- if c == nil {
- masterId = ""
- } else {
- masterId = c.GetString("mid")
- }
- tmp := SysCfgFindComm(masterId, keys...)
- return tmp
- }
- func SysCfgFindComm(masterId string, keys ...string) map[string]string {
- var eg *xorm.Engine
- if masterId == "" {
- eg = db.Db
- } else {
- eg = db.Db
- }
- res := map[string]string{}
- //TODO::判断keys长度(大于10个直接查数据库)
- if len(keys) > 10 {
- cfgList, _ := db.SysCfgGetAll(eg)
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- } else {
- for _, key := range keys {
- res[key] = db.SysCfgGetWithDb(eg, masterId, key)
- }
- }
- return res
- }
-
- // 多条记录获取
- func EgSysCfgFind(keys ...string) map[string]string {
- var e *xorm.Engine
- res := map[string]string{}
- if len(res) == 0 {
- cfgList, _ := db.SysCfgGetAll(e)
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- // 先不设置缓存
- // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // 清理系统配置信息
- func SysCfgCleanCache() {
- cache.Del(md.KEY_SYS_CFG_CACHE)
- }
-
- // 写入系统设置
- func SysCfgSet(c *gin.Context, key, val, memo string) bool {
- cfg, err := db.SysCfgGetOne(db.Db, key)
- if err != nil || cfg == nil {
- return db.SysCfgInsert(db.Db, key, val, memo)
- }
- if memo != "" && cfg.Memo != memo {
- cfg.Memo = memo
- }
- SysCfgCleanCache()
- return db.SysCfgUpdate(db.Db, key, val, cfg.Memo)
- }
|