智盟项目
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.

90 lines
2.4 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils/cache"
  6. "applet/app/utils/logx"
  7. "fmt"
  8. "xorm.io/xorm"
  9. )
  10. type MasterListCfgDb struct {
  11. Db *xorm.Engine `json:"db"`
  12. }
  13. func (masterListCfgDb *MasterListCfgDb) Set() { // set方法
  14. masterListCfgDb.Db = ZhimengDb
  15. }
  16. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetAll() (*[]model.MasterListCfg, error) {
  17. var cfgList []model.MasterListCfg
  18. if err := Db.Cols("k,v,memo").Find(&cfgList); err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. return &cfgList, nil
  22. }
  23. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetCron() (*[]model.MasterListCfg, error) {
  24. var cfgList []model.MasterListCfg
  25. if err := Db.Where("`k` LIKE 'zhimeng_cron\\_%' AND v != ''").Cols("k,v,memo").Find(&cfgList); err != nil {
  26. return nil, logx.Error(err)
  27. }
  28. return &cfgList, nil
  29. }
  30. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneNoDataNoErr(key string) (*model.MasterListCfg, error) {
  31. var cfgList model.MasterListCfg
  32. has, err := Db.Where("`k`=?", key).Get(&cfgList)
  33. if err != nil {
  34. return nil, logx.Error(err)
  35. }
  36. if !has {
  37. return nil, nil
  38. }
  39. return &cfgList, nil
  40. }
  41. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOne(uid, key string) (*model.MasterListCfg, error) {
  42. var cfgList model.MasterListCfg
  43. if has, err := Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList); err != nil || has == false {
  44. return nil, logx.Error(err)
  45. }
  46. return &cfgList, nil
  47. }
  48. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneData(uid, key string) string {
  49. var cfgList model.MasterListCfg
  50. if has, err := Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList); err != nil || has == false {
  51. return ""
  52. }
  53. return cfgList.V
  54. }
  55. func (masterListCfgDb *MasterListCfgDb) MasterListCfgInsert(uid, key, val, memo string) bool {
  56. cfg := model.MasterListCfg{Uid: uid, K: key, V: val, Memo: memo}
  57. _, err := Db.InsertOne(&cfg)
  58. if err != nil {
  59. logx.Error(err)
  60. return false
  61. }
  62. return true
  63. }
  64. func (masterListCfgDb *MasterListCfgDb) MasterListCfgUpdate(uid, key, val string) bool {
  65. cfg := model.MasterListCfg{K: key, V: val}
  66. _, err := Db.Where("`k`=? and uid=?", key, uid).Cols("val").Update(&cfg)
  67. if err != nil {
  68. logx.Error(err)
  69. return false
  70. }
  71. masterListCfgDb.MasterListCfgDel(key)
  72. return true
  73. }
  74. func (masterListCfgDb *MasterListCfgDb) MasterListCfgDel(HKey string) error {
  75. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  76. _, err := cache.HDel(cacheKey, HKey)
  77. if err != nil {
  78. return err
  79. }
  80. return nil
  81. }