附近小店
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

233 Zeilen
5.7 KiB

  1. package db
  2. import (
  3. model2 "applet/app/db/offical/model"
  4. "errors"
  5. "fmt"
  6. "xorm.io/xorm"
  7. "applet/app/cfg"
  8. "applet/app/db/model"
  9. "applet/app/utils/logx"
  10. )
  11. func MapBaseExists() (bool, error) {
  12. return Db.IsTableExist("db_mapping")
  13. }
  14. func InitMapDbs(c *cfg.DBCfg, prd bool) {
  15. var tables *[]model.DbMapping
  16. exists, err := MapBaseExists()
  17. if !exists || err != nil {
  18. logx.Fatalf("db_mapping not exists : %v", err)
  19. }
  20. // tables := MapAllDatabases(debug)
  21. if prd {
  22. tables = GetAllDatabasePrd() //debug 获取生产
  23. } else {
  24. tables = GetAllDatabaseDev() //debug 获取开发
  25. }
  26. if tables == nil {
  27. logx.Fatal("no database tables data")
  28. }
  29. var e *xorm.Engine
  30. DBs = map[string]*xorm.Engine{}
  31. for _, v := range *tables {
  32. if v.DbName != "" && v.DeletedAt == 0 && v.DbName != c.Name {
  33. dbCfg := cfg.DBCfg{
  34. Name: v.DbName,
  35. ShowLog: c.ShowLog,
  36. MaxLifetime: c.MaxLifetime,
  37. MaxOpenConns: c.MaxOpenConns,
  38. MaxIdleConns: c.MaxIdleConns,
  39. Path: fmt.Sprintf(c.Path, v.DbName),
  40. }
  41. if v.DbHost != "" && v.DbUsername != "" && v.DbPassword != "" {
  42. dbCfg.Host = v.DbHost
  43. dbCfg.User = v.DbUsername
  44. dbCfg.Psw = v.DbPassword
  45. } else {
  46. dbCfg.Host = c.Host
  47. dbCfg.User = c.User
  48. dbCfg.Psw = c.Psw
  49. }
  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. 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, 22255132).Find(&m)
  87. //err = Db.Where(" db_master_id=?", 74856566).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 'community_team_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_wap_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 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 masterId + "." + domainBase.V, nil
  177. }
  178. func GetOfficialDomainInfoByTypeToAgent(Db *xorm.Engine, masterId, uuid, key string) (string, error) {
  179. type SysCfg struct {
  180. K string
  181. V string
  182. Memo string
  183. }
  184. var domainBase model2.MasterListCfg
  185. has, err := Db.Where("uid=? and k=?", uuid, "domain_base").Get(&domainBase)
  186. if err != nil {
  187. return "", err
  188. }
  189. if has == false {
  190. return "", errors.New("can not find key by : domain_base")
  191. }
  192. if key == "wap" {
  193. return masterId + ".h5." + domainBase.V, nil
  194. }
  195. if key == "api" {
  196. var apiDomain SysCfg
  197. has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
  198. if err != nil {
  199. return "", err
  200. }
  201. if has == false {
  202. return "", errors.New("can not find key by : domain_api_base")
  203. }
  204. return apiDomain.V, nil
  205. }
  206. if key == "admin" {
  207. return "admin." + masterId + "." + domainBase.V, nil
  208. }
  209. // 默认返回H5的
  210. return masterId + "." + domainBase.V, nil
  211. }