golang 的 rabbitmq 消费项目
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.
 
 
 

196 lines
4.9 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. dbCfg.Host = "zhios123.rwlb.rds.aliyuncs.com:3306"
  50. e, err = NewDB(&dbCfg)
  51. if err != nil || e == nil {
  52. 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)
  53. } else {
  54. DBs[v.DbMasterId] = e
  55. }
  56. }
  57. }
  58. }
  59. func MapAllDatabases(debug bool) *[]model.DbMapping {
  60. sql := "`db_name` != ?"
  61. if debug {
  62. sql = "`db_name` = ?"
  63. }
  64. var m []model.DbMapping
  65. if err := Db.Where(sql, cfg.DB.Name).Find(&m); err != nil || len(m) == 0 {
  66. logx.Warn(err)
  67. return nil
  68. }
  69. return &m
  70. }
  71. // GetAllDatabasePrd is 获取生成库 所有db 除了 deleted_at = 1 的
  72. func GetAllDatabasePrd() *[]model.DbMapping {
  73. var m []model.DbMapping
  74. if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&m); err != nil || len(m) == 0 {
  75. logx.Warn(err)
  76. return nil
  77. }
  78. return &m
  79. }
  80. // GetAllDatabaseDev is 获取开发库 所有db 除了 deleted_at = 1 的
  81. func GetAllDatabaseDev() *[]model.DbMapping {
  82. var m []model.DbMapping
  83. var err error
  84. fmt.Println("cfg.Local is: ", cfg.Local)
  85. if cfg.Local { // 本地调试 加快速度
  86. fmt.Println("notice:LOCAL TEST, only masterId:** 123456 ** available!")
  87. err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id=?", 1, 123456).Find(&m)
  88. } else {
  89. err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
  90. }
  91. //err := Db.Where("deleted_at != ? AND is_dev = '1' and db_master_id='123456'", 1).Find(&m)
  92. if err != nil || len(m) == 0 {
  93. logx.Warn(err)
  94. return nil
  95. }
  96. return &m
  97. }
  98. //GetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  99. func GetDatabaseByMasterID(Db *xorm.Engine, id string) (*model.DbMapping, error) {
  100. var m model.DbMapping
  101. has, err := Db.Where("db_master_id=?", id).Get(&m)
  102. if !has {
  103. return nil, errors.New("Not Found DB data by " + id)
  104. }
  105. if err != nil {
  106. return nil, err
  107. }
  108. if m.DbHost == "" {
  109. m.DbHost = cfg.DB.Host
  110. m.DbUsername = cfg.DB.User
  111. m.DbPassword = cfg.DB.Psw
  112. }
  113. return &m, nil
  114. }
  115. //SessionGetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  116. func SessionGetDatabaseByMasterID(Db *xorm.Session, id string) (*model.DbMapping, error) {
  117. var m model.DbMapping
  118. has, err := Db.Where("db_master_id=?", id).Get(&m)
  119. if !has {
  120. return nil, errors.New("Not Found DB data by " + id)
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. if m.DbHost == "" {
  126. m.DbHost = cfg.DB.Host
  127. m.DbName = cfg.DB.Name
  128. m.DbUsername = cfg.DB.User
  129. m.DbPassword = cfg.DB.Psw
  130. }
  131. return &m, nil
  132. }
  133. // 获取自动任务队列
  134. func MapCrontabCfg(eg *xorm.Engine) *[]model.SysCfg {
  135. var c []model.SysCfg
  136. // 数据库查询如果有下划线会认为是一个任意字符
  137. if err := eg.Where("`key` LIKE 'mall_cron\\_%' AND val != ''").Cols("`key`,`val`").Find(&c); err != nil || len(c) == 0 {
  138. logx.Warn(err)
  139. return nil
  140. }
  141. return &c
  142. }
  143. // 获取官方域名
  144. func GetOfficialDomainInfoByType(Db *xorm.Engine, masterId, key string) (string, error) {
  145. type SysCfg struct {
  146. K string
  147. V string
  148. Memo string
  149. }
  150. var domainBase SysCfg
  151. has, err := Db.Where("k=?", "domain_base").Get(&domainBase)
  152. if err != nil {
  153. return "", err
  154. }
  155. if has == false {
  156. return "", errors.New("can not find key by : domain_base")
  157. }
  158. if key == "wap" {
  159. return "h5." + masterId + "." + domainBase.V, nil
  160. }
  161. if key == "api" {
  162. var apiDomain SysCfg
  163. has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
  164. if err != nil {
  165. return "", err
  166. }
  167. if has == false {
  168. return "", errors.New("can not find key by : domain_api_base")
  169. }
  170. return apiDomain.V, nil
  171. }
  172. if key == "admin" {
  173. return "admin." + masterId + "." + domainBase.V, nil
  174. }
  175. // 默认返回H5的
  176. return "h5." + masterId + "." + domainBase.V, nil
  177. }