package db import ( "applet/app/db/station" "applet/app/utils/logx" "github.com/gin-gonic/gin" "xorm.io/xorm" ) // 单条记录获取DB func SysCfgGet(c *gin.Context, key string) string { res := SysCfgFind(c, key) if _, ok := res[key]; !ok { return "" } return res[key] } // 多条记录获取 func SysCfgFind(c *gin.Context, keys ...string) map[string]string { eg := DBs[c.GetString("master_id")] masterId := c.GetString("master_id") res := map[string]string{} //TODO::判断keys长度(大于10个直接查数据库) if len(keys) > 10 { cfgList, _ := SysCfgGetAll(eg) if cfgList == nil { return nil } for _, v := range *cfgList { res[v.Key] = v.Val } } else { for _, key := range keys { res[key] = SysCfgGetWithDb(eg, masterId, key) } } return res } func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) { var cfgList []model.SysCfg if err := Db.Cols("key,val,memo").Find(&cfgList); err != nil { return nil, logx.Error(err) } return &cfgList, nil } func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) { var cfgList model.SysCfg if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false { return nil, logx.Error(err) } return &cfgList, nil } // 单条记录获取DB func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string { cfg, err := SysCfgGetOne(eg, HKey) if err != nil || cfg == nil { _ = logx.Error(err) return "" } return cfg.Val }