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.
 
 
 

214 lines
5.4 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 cfg.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. var mm []model.SupplyDbMapping
  78. if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&mm); err != nil || len(mm) == 0 {
  79. logx.Warn(err)
  80. return nil
  81. }
  82. for _, v := range mm {
  83. m = append(m, model.DbMapping{
  84. DbMasterId: v.DbMasterId,
  85. DbHost: v.DbHost,
  86. DbUsername: v.DbUsername,
  87. DbPassword: v.DbPassword,
  88. DbName: v.DbName,
  89. ExternalMysql: v.ExternalMysql,
  90. IsDev: v.IsDev,
  91. CreatedAt: v.CreatedAt,
  92. UpdatedAt: v.UpdatedAt,
  93. DeletedAt: v.DeletedAt,
  94. })
  95. }
  96. return &m
  97. }
  98. // GetAllDatabaseDev is 获取开发库 所有db 除了 deleted_at = 1 的
  99. func GetAllDatabaseDev() *[]model.DbMapping {
  100. var m []model.DbMapping
  101. var err error
  102. fmt.Println("cfg.Local is: ", cfg.Local)
  103. if cfg.Local { // 本地调试 加快速度
  104. fmt.Println("notice:LOCAL TEST, only masterId:** 123456 ** available!")
  105. err = Db.Where("deleted_at != ? AND is_dev = '1' AND db_master_id= ?", 1, 123456).
  106. Or("db_master_id = ?", 12293740).Find(&m)
  107. } else {
  108. err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
  109. }
  110. //err := Db.Where("deleted_at != ? AND is_dev = '1' and db_master_id='123456'", 1).Find(&m)
  111. if err != nil || len(m) == 0 {
  112. logx.Warn(err)
  113. return nil
  114. }
  115. return &m
  116. }
  117. //GetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  118. func GetDatabaseByMasterID(Db *xorm.Engine, id string) (*model.DbMapping, error) {
  119. var m model.DbMapping
  120. has, err := Db.Where("db_master_id=?", id).Get(&m)
  121. if !has {
  122. return nil, errors.New("Not Found DB data by " + id)
  123. }
  124. if err != nil {
  125. return nil, err
  126. }
  127. if m.DbHost == "" {
  128. m.DbHost = cfg.DB.Host
  129. m.DbUsername = cfg.DB.User
  130. m.DbPassword = cfg.DB.Psw
  131. }
  132. return &m, nil
  133. }
  134. //SessionGetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  135. func SessionGetDatabaseByMasterID(Db *xorm.Session, id string) (*model.DbMapping, error) {
  136. var m model.DbMapping
  137. has, err := Db.Where("db_master_id=?", id).Get(&m)
  138. if !has {
  139. return nil, errors.New("Not Found DB data by " + id)
  140. }
  141. if err != nil {
  142. return nil, err
  143. }
  144. if m.DbHost == "" {
  145. m.DbHost = cfg.DB.Host
  146. m.DbName = cfg.DB.Name
  147. m.DbUsername = cfg.DB.User
  148. m.DbPassword = cfg.DB.Psw
  149. }
  150. return &m, nil
  151. }
  152. // 获取自动任务队列
  153. func MapCrontabCfg(eg *xorm.Engine) *[]model.SysCfg {
  154. var c []model.SysCfg
  155. // 数据库查询如果有下划线会认为是一个任意字符
  156. if err := eg.Where("`key` LIKE 'mall_cron\\_%' AND val != ''").Cols("`key`,`val`").Find(&c); err != nil || len(c) == 0 {
  157. logx.Warn(err)
  158. return nil
  159. }
  160. return &c
  161. }
  162. // 获取官方域名
  163. func GetOfficialDomainInfoByType(Db *xorm.Engine, masterId, key string) (string, error) {
  164. type SysCfg struct {
  165. K string
  166. V string
  167. Memo string
  168. }
  169. var domainBase SysCfg
  170. has, err := Db.Where("k=?", "domain_base").Get(&domainBase)
  171. if err != nil {
  172. return "", err
  173. }
  174. if has == false {
  175. return "", errors.New("can not find key by : domain_base")
  176. }
  177. if key == "wap" {
  178. return "h5." + masterId + "." + domainBase.V, nil
  179. }
  180. if key == "api" {
  181. var apiDomain SysCfg
  182. has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
  183. if err != nil {
  184. return "", err
  185. }
  186. if has == false {
  187. return "", errors.New("can not find key by : domain_api_base")
  188. }
  189. return apiDomain.V, nil
  190. }
  191. if key == "admin" {
  192. return "admin." + masterId + "." + domainBase.V, nil
  193. }
  194. // 默认返回H5的
  195. return "h5." + masterId + "." + domainBase.V, nil
  196. }