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

db_master_list_cfg.go 2.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 := masterListCfgDb.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 := masterListCfgDb.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 := masterListCfgDb.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. has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
  44. if err != nil {
  45. return nil, logx.Error(err)
  46. }
  47. if has == false {
  48. cfgList = model.MasterListCfg{Uid: uid, K: key}
  49. masterListCfgDb.Db.InsertOne(&cfgList)
  50. }
  51. return &cfgList, nil
  52. }
  53. func (masterListCfgDb *MasterListCfgDb) MasterListCfgGetOneData(uid, key string) string {
  54. var cfgList model.MasterListCfg
  55. has, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
  56. if err != nil {
  57. return ""
  58. }
  59. if has == false {
  60. cfgList = model.MasterListCfg{Uid: uid, K: key}
  61. masterListCfgDb.Db.InsertOne(&cfgList)
  62. }
  63. return cfgList.V
  64. }
  65. func (masterListCfgDb *MasterListCfgDb) MasterListCfgInsert(uid, key, val, memo string) bool {
  66. cfg := model.MasterListCfg{Uid: uid, K: key, V: val, Memo: memo}
  67. _, err := masterListCfgDb.Db.InsertOne(&cfg)
  68. if err != nil {
  69. logx.Error(err)
  70. return false
  71. }
  72. return true
  73. }
  74. func (masterListCfgDb *MasterListCfgDb) MasterListCfgUpdate(uid, key, val string) bool {
  75. masterListCfgDb.MasterListCfgGetOneData(uid, key)
  76. cfg := model.MasterListCfg{K: key, V: val}
  77. _, err := masterListCfgDb.Db.Where("`k`=? and uid=?", key, uid).Cols("v").Update(&cfg)
  78. if err != nil {
  79. logx.Error(err)
  80. return false
  81. }
  82. masterListCfgDb.MasterListCfgDel(key)
  83. return true
  84. }
  85. func (masterListCfgDb *MasterListCfgDb) MasterListCfgDel(HKey string) error {
  86. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, HKey[0:1])
  87. _, err := cache.HDel(cacheKey, HKey)
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }