广告平台(媒体使用)
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

195 righe
4.8 KiB

  1. package db
  2. import (
  3. "errors"
  4. "fmt"
  5. "xorm.io/xorm"
  6. "applet/app/cfg"
  7. "applet/app/db/model"
  8. "applet/app/utils/logx"
  9. )
  10. func MapBaseExists() (bool, error) {
  11. return Db.IsTableExist("db_mapping")
  12. }
  13. func InitMapDbs(c *cfg.DBCfg, prd bool) {
  14. var tables *[]model.DbMapping
  15. exists, err := MapBaseExists()
  16. if !exists || err != nil {
  17. logx.Fatalf("db_mapping not exists : %v", err)
  18. }
  19. // tables := MapAllDatabases(debug)
  20. if prd {
  21. tables = GetAllDatabasePrd() //debug 获取生产
  22. } else {
  23. tables = GetAllDatabaseDev() //debug 获取开发
  24. }
  25. if tables == nil {
  26. logx.Fatal("no database tables data")
  27. }
  28. var e *xorm.Engine
  29. DBs = map[string]*xorm.Engine{}
  30. for _, v := range *tables {
  31. if v.DbName != "" && v.DeletedAt == 0 && v.DbName != c.Name {
  32. dbCfg := cfg.DBCfg{
  33. Name: v.DbName,
  34. ShowLog: c.ShowLog,
  35. MaxLifetime: c.MaxLifetime,
  36. MaxOpenConns: c.MaxOpenConns,
  37. MaxIdleConns: c.MaxIdleConns,
  38. Path: fmt.Sprintf(c.Path, v.DbName),
  39. }
  40. if v.DbHost != "" && v.DbUsername != "" && v.DbPassword != "" {
  41. dbCfg.Host = v.DbHost
  42. dbCfg.User = v.DbUsername
  43. dbCfg.Psw = v.DbPassword
  44. } else {
  45. dbCfg.Host = c.Host
  46. dbCfg.User = c.User
  47. dbCfg.Psw = c.Psw
  48. }
  49. e, err = NewDB(&dbCfg)
  50. if err != nil || e == nil {
  51. logx.Warnf("db engine can't create, please check config, params[host:%s, user:%s, psw: %s, name: %s], err: %v", dbCfg.Host, dbCfg.User, dbCfg.Psw, dbCfg.Name, err)
  52. } else {
  53. DBs[v.DbMasterId] = e
  54. }
  55. }
  56. }
  57. }
  58. func MapAllDatabases(debug bool) *[]model.DbMapping {
  59. sql := "`db_name` != ?"
  60. if debug {
  61. sql = "`db_name` = ?"
  62. }
  63. var m []model.DbMapping
  64. if err := Db.Where(sql, cfg.DB.Name).Find(&m); err != nil || len(m) == 0 {
  65. logx.Warn(err)
  66. return nil
  67. }
  68. return &m
  69. }
  70. // GetAllDatabasePrd is 获取生成库 所有db 除了 deleted_at = 1 的
  71. func GetAllDatabasePrd() *[]model.DbMapping {
  72. var m []model.DbMapping
  73. if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&m); err != nil || len(m) == 0 {
  74. logx.Warn(err)
  75. return nil
  76. }
  77. return &m
  78. }
  79. // GetAllDatabaseDev is 获取开发库 所有db 除了 deleted_at = 1 的
  80. func GetAllDatabaseDev() *[]model.DbMapping {
  81. var m []model.DbMapping
  82. var err error
  83. fmt.Println("cfg.Local is: ", cfg.Local)
  84. if cfg.Local { // 本地调试 加快速度
  85. fmt.Println("notice:LOCAL TEST, only masterId:** 123456 ** available!")
  86. err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123456).Find(&m)
  87. } else {
  88. err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
  89. }
  90. //err := Db.Where("deleted_at != ? AND is_dev = '1' and db_master_id='123456'", 1).Find(&m)
  91. if err != nil || len(m) == 0 {
  92. logx.Warn(err)
  93. return nil
  94. }
  95. return &m
  96. }
  97. //GetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  98. func GetDatabaseByMasterID(Db *xorm.Engine, id string) (*model.DbMapping, error) {
  99. var m model.DbMapping
  100. has, err := Db.Where("db_master_id=?", id).Get(&m)
  101. if !has {
  102. return nil, errors.New("Not Found DB data by " + id)
  103. }
  104. if err != nil {
  105. return nil, err
  106. }
  107. if m.DbHost == "" {
  108. m.DbHost = cfg.DB.Host
  109. m.DbUsername = cfg.DB.User
  110. m.DbPassword = cfg.DB.Psw
  111. }
  112. return &m, nil
  113. }
  114. //SessionGetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  115. func SessionGetDatabaseByMasterID(Db *xorm.Session, id string) (*model.DbMapping, error) {
  116. var m model.DbMapping
  117. has, err := Db.Where("db_master_id=?", id).Get(&m)
  118. if !has {
  119. return nil, errors.New("Not Found DB data by " + id)
  120. }
  121. if err != nil {
  122. return nil, err
  123. }
  124. if m.DbHost == "" {
  125. m.DbHost = cfg.DB.Host
  126. m.DbName = cfg.DB.Name
  127. m.DbUsername = cfg.DB.User
  128. m.DbPassword = cfg.DB.Psw
  129. }
  130. return &m, nil
  131. }
  132. // 获取自动任务队列
  133. func MapCrontabCfg(eg *xorm.Engine) *[]model.SysCfg {
  134. var c []model.SysCfg
  135. // 数据库查询如果有下划线会认为是一个任意字符
  136. if err := eg.Where("`key` LIKE 'mall_cron\\_%' AND val != ''").Cols("`key`,`val`").Find(&c); err != nil || len(c) == 0 {
  137. logx.Warn(err)
  138. return nil
  139. }
  140. return &c
  141. }
  142. // 获取官方域名
  143. func GetOfficialDomainInfoByType(Db *xorm.Engine, masterId, key string) (string, error) {
  144. type SysCfg struct {
  145. K string
  146. V string
  147. Memo string
  148. }
  149. var domainBase SysCfg
  150. has, err := Db.Where("k=?", "domain_base").Get(&domainBase)
  151. if err != nil {
  152. return "", err
  153. }
  154. if has == false {
  155. return "", errors.New("can not find key by : domain_base")
  156. }
  157. if key == "wap" {
  158. return "h5." + masterId + "." + domainBase.V, nil
  159. }
  160. if key == "api" {
  161. var apiDomain SysCfg
  162. has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
  163. if err != nil {
  164. return "", err
  165. }
  166. if has == false {
  167. return "", errors.New("can not find key by : domain_api_base")
  168. }
  169. return apiDomain.V, nil
  170. }
  171. if key == "admin" {
  172. return "admin." + masterId + "." + domainBase.V, nil
  173. }
  174. // 默认返回H5的
  175. return "h5." + masterId + "." + domainBase.V, nil
  176. }