package db import ( "applet/app/db/model" "applet/app/md" "applet/app/utils/cache" "applet/app/utils/logx" "fmt" "xorm.io/xorm" ) type MasterListCfgDb struct { Db *xorm.Engine `json:"db"` } func (masterListCfgDb *MasterListCfgDb) Set() { // set方法 masterListCfgDb.Db = ZhimengDb } func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetAll() (*[]model.MasterListCfg, error) { var cfgList []model.MasterListCfg if err := masterListCfgDb.Db.Cols("k,v,memo").Find(&cfgList); err != nil { return nil, logx.Error(err) } return &cfgList, nil } func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetCron() (*[]model.MasterListCfg, error) { var cfgList []model.MasterListCfg if err := masterListCfgDb.Db.Where("`k` LIKE 'zhimeng_cron\\_%' AND v != ''").Cols("k,v,memo").Find(&cfgList); err != nil { return nil, logx.Error(err) } return &cfgList, nil } func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneNoDataNoErr(key string) (*model.MasterListCfg, error) { var cfgList model.MasterListCfg has, err := masterListCfgDb.Db.Where("`k`=?", key).Get(&cfgList) if err != nil { return nil, logx.Error(err) } if !has { return nil, nil } return &cfgList, nil } func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOne(uid, key string) (*model.MasterListCfg, error) { var cfgList model.MasterListCfg if has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList); err != nil || has == false { return nil, logx.Error(err) } return &cfgList, nil } func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneData(uid, key string) string { var cfgList model.MasterListCfg if has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList); err != nil || has == false { return "" } return cfgList.V } func (masterListCfgDb *MasterListCfgDb) MasterListCfgInsert(uid, key, val, memo string) bool { cfg := model.MasterListCfg{Uid: uid, K: key, V: val, Memo: memo} _, err := masterListCfgDb.Db.InsertOne(&cfg) if err != nil { logx.Error(err) return false } return true } func (masterListCfgDb *MasterListCfgDb) MasterListCfgUpdate(uid, key, val string) bool { cfg := model.MasterListCfg{K: key, V: val} _, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Cols("val").Update(&cfg) if err != nil { logx.Error(err) return false } masterListCfgDb.MasterListCfgDel(key) return true } func (masterListCfgDb *MasterListCfgDb) MasterListCfgDel(HKey string) error { cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1]) _, err := cache.HDel(cacheKey, HKey) if err != nil { return err } return nil }