智盟项目
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_sys_mod_format_img.go 1.4 KiB

5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package db
  2. import (
  3. "applet/app/db/station"
  4. "applet/app/utils/logx"
  5. "github.com/gin-gonic/gin"
  6. "xorm.io/xorm"
  7. )
  8. // 单条记录获取DB
  9. func SysCfgGet(c *gin.Context, key string) string {
  10. res := SysCfgFind(c, key)
  11. if _, ok := res[key]; !ok {
  12. return ""
  13. }
  14. return res[key]
  15. }
  16. // 多条记录获取
  17. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  18. eg := DBs[c.GetString("master_id")]
  19. masterId := c.GetString("master_id")
  20. res := map[string]string{}
  21. //TODO::判断keys长度(大于10个直接查数据库)
  22. if len(keys) > 10 {
  23. cfgList, _ := SysCfgGetAll(eg)
  24. if cfgList == nil {
  25. return nil
  26. }
  27. for _, v := range *cfgList {
  28. res[v.Key] = v.Val
  29. }
  30. } else {
  31. for _, key := range keys {
  32. res[key] = SysCfgGetWithDb(eg, masterId, key)
  33. }
  34. }
  35. return res
  36. }
  37. func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) {
  38. var cfgList []model.SysCfg
  39. if err := Db.Cols("key,val,memo").Find(&cfgList); err != nil {
  40. return nil, logx.Error(err)
  41. }
  42. return &cfgList, nil
  43. }
  44. func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) {
  45. var cfgList model.SysCfg
  46. if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
  47. return nil, logx.Error(err)
  48. }
  49. return &cfgList, nil
  50. }
  51. // 单条记录获取DB
  52. func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
  53. cfg, err := SysCfgGetOne(eg, HKey)
  54. if err != nil || cfg == nil {
  55. _ = logx.Error(err)
  56. return ""
  57. }
  58. return cfg.Val
  59. }