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

132 lines
3.6 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 := 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. }
  93. func MasterListCfgGetOneData(uid, key string) string {
  94. var cfgList model.MasterListCfg
  95. has, err := ZhimengDb.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
  96. if err != nil {
  97. return ""
  98. }
  99. if has == false {
  100. cfgList = model.MasterListCfg{Uid: uid, K: key}
  101. ZhimengDb.InsertOne(&cfgList)
  102. }
  103. return cfgList.V
  104. }
  105. func MasterListCfgGetKeyAll(key string) *[]model.MasterListCfg {
  106. var cfgList []model.MasterListCfg
  107. err := ZhimengDb.Where("`k`=? ", key).Find(&cfgList)
  108. if err != nil {
  109. return nil
  110. }
  111. return &cfgList
  112. }
  113. func MasterListCfgSave(uid, key, val string) {
  114. var cfgList model.MasterListCfg
  115. has, err := ZhimengDb.Where("`k`=? and uid=?", key, uid).Get(&cfgList)
  116. if err != nil || has == false {
  117. return
  118. }
  119. cfgList.V = val
  120. ZhimengDb.Where("`k`=? and uid=?", key, uid).Update(&cfgList)
  121. return
  122. }