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.

156 lines
4.1 KiB

4 years ago
4 years ago
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/model"
  7. "gin-vue-admin/model/request"
  8. "gin-vue-admin/source"
  9. "github.com/spf13/viper"
  10. "gorm.io/driver/mysql"
  11. "gorm.io/gorm"
  12. )
  13. //@author: [songzhibin97](https://github.com/songzhibin97)
  14. //@function: writeConfig
  15. //@description: 回写配置
  16. //@param:
  17. //@return: error
  18. func writeConfig(viper *viper.Viper, conf map[string]interface{}) error {
  19. for k, v := range conf {
  20. viper.Set(k, v)
  21. }
  22. return viper.WriteConfig()
  23. }
  24. //@author: [songzhibin97](https://github.com/songzhibin97)
  25. //@function: createTable
  26. //@description: 创建数据库(mysql)
  27. //@param: dsn string, driver string, createSql
  28. //@return: error
  29. func createTable(dsn string, driver string, createSql string) error {
  30. db, err := sql.Open(driver, dsn)
  31. if err != nil {
  32. return err
  33. }
  34. defer db.Close()
  35. if err = db.Ping(); err != nil {
  36. return err
  37. }
  38. _, err = db.Exec(createSql)
  39. return err
  40. }
  41. func initDB(InitDBFunctions ...model.InitDBFunc) (err error) {
  42. for _, v := range InitDBFunctions {
  43. err = v.Init()
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. return nil
  49. }
  50. //@author: [songzhibin97](https://github.com/songzhibin97)
  51. //@function: InitDB
  52. //@description: 创建数据库并初始化
  53. //@param: authorityId string
  54. //@return: err error, treeMap map[string][]model.SysMenu
  55. func InitDB(conf request.InitDB) error {
  56. if conf.Host == "" {
  57. conf.Host = "127.0.0.1"
  58. }
  59. if conf.Port == "" {
  60. conf.Port = "3306"
  61. }
  62. dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", conf.UserName, conf.Password, conf.Host, conf.Port)
  63. fmt.Println(dsn)
  64. createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName)
  65. if err := createTable(dsn, "mysql", createSql); err != nil {
  66. return err
  67. }
  68. setting := map[string]interface{}{
  69. "mysql.path": fmt.Sprintf("%s:%s", conf.Host, conf.Port),
  70. "mysql.db-name": conf.DBName,
  71. "mysql.username": conf.UserName,
  72. "mysql.password": conf.Password,
  73. "mysql.config": "charset=utf8mb4&parseTime=True&loc=Local",
  74. }
  75. if err := writeConfig(global.GVA_VP, setting); err != nil {
  76. return err
  77. }
  78. m := global.GVA_CONFIG.Mysql
  79. if m.Dbname == "" {
  80. return nil
  81. }
  82. linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
  83. mysqlConfig := mysql.Config{
  84. DSN: linkDns, // DSN data source name
  85. DefaultStringSize: 191, // string 类型字段的默认长度
  86. DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
  87. DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
  88. DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
  89. SkipInitializeWithVersion: false, // 根据版本自动配置
  90. }
  91. if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
  92. //global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
  93. //os.Exit(0)
  94. //return nil
  95. return nil
  96. } else {
  97. sqlDB, _ := db.DB()
  98. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  99. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  100. global.GVA_DB = db
  101. }
  102. err := global.GVA_DB.AutoMigrate(
  103. model.SysUser{},
  104. model.SysAuthority{},
  105. model.SysApi{},
  106. model.SysBaseMenu{},
  107. model.SysBaseMenuParameter{},
  108. model.JwtBlacklist{},
  109. model.SysDictionary{},
  110. model.SysDictionaryDetail{},
  111. model.ExaFileUploadAndDownload{},
  112. model.ExaFile{},
  113. model.ExaFileChunk{},
  114. model.ExaSimpleUploader{},
  115. model.ExaCustomer{},
  116. model.SysOperationRecord{},
  117. model.WorkflowProcess{},
  118. model.WorkflowNode{},
  119. model.WorkflowEdge{},
  120. model.WorkflowStartPoint{},
  121. model.WorkflowEndPoint{},
  122. model.WorkflowMove{},
  123. model.ExaWfLeave{},
  124. )
  125. if err != nil {
  126. return err
  127. }
  128. err = initDB(
  129. source.Admin,
  130. source.Api,
  131. source.AuthorityMenu,
  132. source.Authority,
  133. source.AuthoritiesMenus,
  134. source.Casbin,
  135. source.DataAuthorities,
  136. source.Dictionary,
  137. source.DictionaryDetail,
  138. source.File,
  139. source.BaseMenu,
  140. source.Workflow)
  141. if err != nil {
  142. return err
  143. }
  144. return nil
  145. }