|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package implement
-
- import (
- "applet/app/db/dao"
- "applet/app/md"
- "applet/app/utils/cache"
- "applet/app/utils/logx"
- "code.fnuoos.com/zhimeng/model.git/src/super/model"
- "fmt"
- "xorm.io/xorm"
- )
-
- func NewSysCfgDb(engine *xorm.Engine, masterId string) dao.SysCfgDao {
- return &SysCfgDb{
- Db: engine,
- MasterId: masterId,
- }
- }
-
- type SysCfgDb struct {
- Db *xorm.Engine
- MasterId string
- }
-
- func (s SysCfgDb) SysCfgGetAll() (*[]model.SysCfg, error) {
- var cfgList []model.SysCfg
- if err := s.Db.Cols("key,val,memo").Find(&cfgList); err != nil {
- return nil, logx.Error(err)
- }
- return &cfgList, nil
- }
-
- func (s SysCfgDb) SysCfgGetOneNoDataNoErr(key string) (*model.SysCfg, error) {
- var cfgList model.SysCfg
- has, err := s.Db.Where("`key`=?", key).Get(&cfgList)
- if err != nil {
- return nil, logx.Error(err)
- }
- if !has {
- return nil, nil
- }
- return &cfgList, nil
- }
-
- func (s SysCfgDb) SysCfgGetOne(key string) (*model.SysCfg, error) {
- var cfgList model.SysCfg
- if has, err := s.Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
- return nil, logx.Error(err)
- }
- return &cfgList, nil
- }
-
- func (s SysCfgDb) SysCfgInsert(key, val, memo string) bool {
- cfg := model.SysCfg{K: key, V: val, Memo: memo}
- _, err := s.Db.InsertOne(&cfg)
- if err != nil {
- logx.Error(err)
- return false
- }
- return true
- }
-
- func (s SysCfgDb) SysCfgUpdate(key, val string) bool {
- cfg := model.SysCfg{K: key, V: val}
- _, err := s.Db.Where("`key`=?", key).Cols("val").Update(&cfg)
- if err != nil {
- logx.Error(err)
- return false
- }
- s.SysCfgDel(key)
- return true
- }
-
- func (s SysCfgDb) SysCfgGetWithDb(HKey string) string {
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, s.MasterId, HKey[0:1])
- get, err := cache.HGetString(cacheKey, HKey)
- if err != nil || get == "" {
- cfg, err := s.SysCfgGetOne(HKey)
- if err != nil || cfg == nil {
- _ = logx.Error(err)
- return ""
- }
-
- // key是否存在
- cacheKeyExist := false
- if cache.Exists(cacheKey) {
- cacheKeyExist = true
- }
-
- // 设置缓存
- _, err = cache.HSet(cacheKey, HKey, cfg.V)
- if err != nil {
- _ = logx.Error(err)
- return ""
- }
- if !cacheKeyExist { // 如果是首次设置 设置过期时间
- _, err := cache.Expire(cacheKey, md.CfgCacheTime)
- if err != nil {
- _ = logx.Error(err)
- return ""
- }
- }
- return cfg.V
- }
- return get
- }
-
- func (s SysCfgDb) SysCfgDel(HKey string) error {
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, s.MasterId, HKey[0:1])
- _, err := cache.HDel(cacheKey, HKey)
- if err != nil {
- return err
- }
- return nil
- }
-
- func (s SysCfgDb) SysCfgFindWithDb(keys ...string) map[string]string {
- res := map[string]string{}
- for _, v := range keys {
- val := s.SysCfgGetWithDb(v)
- res[v] = val
- }
- return res
- }
|