dm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package global
  2. import (
  3. "database/sql/driver"
  4. _ "dm"
  5. dm "dmgorm2"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. "gorm.io/gorm/schema"
  11. "io"
  12. "log"
  13. "os"
  14. "time"
  15. _ "github.com/go-sql-driver/mysql"
  16. )
  17. type LocalTime time.Time
  18. func InitDm() {
  19. dmSqlMap := make(map[string]*gorm.DB)
  20. logWriter := io.MultiWriter(utils.Binlog) //binlog日志,记录到文件中去
  21. if utils.RunMode == `dev` { // 测试环境,默认输出在控制台,不需要的话,可以注释if里面下面的这行
  22. logWriter = io.MultiWriter(utils.Binlog, os.Stdout)
  23. }
  24. newLogger := logger.New(log.New(logWriter, "\r\n", log.LstdFlags), logger.Config{
  25. SlowThreshold: 200 * time.Millisecond, //慢sql :200ms
  26. LogLevel: logger.Info, //记录的日志类型,info代表所有信息都记录
  27. IgnoreRecordNotFoundError: true, //是否忽略找不到数据错误信息(只是日志记录记录成err还是普通的输出的区别,并不影响业务代码中的:找不到数据行error)
  28. Colorful: false, //是否颜色输出
  29. })
  30. connectDm(utils.MYSQL_URL, "default", newLogger, dmSqlMap, true)
  31. connectDm(utils.MYSQL_URL_RDDP, "rddp", newLogger, dmSqlMap, false)
  32. connectDm(utils.MYSQL_URL_EDB, "edb", newLogger, dmSqlMap, false)
  33. connectDm(utils.MYSQL_URL_DATA, "data", newLogger, dmSqlMap, false)
  34. connectDm(utils.MYSQL_URL_GL, "gl", newLogger, dmSqlMap, false)
  35. if utils.MYSQL_AI_URL != "" {
  36. connectDm(utils.MYSQL_AI_URL, "ai", newLogger, dmSqlMap, false)
  37. }
  38. DmSQL = dmSqlMap
  39. }
  40. func connectDm(dsn, aliasName string, newLogger logger.Interface, dmSqlMap map[string]*gorm.DB, isDefault bool) {
  41. if dsn == `` {
  42. return
  43. }
  44. db, err := gorm.Open(dm.Open(dsn), &gorm.Config{
  45. Logger: newLogger,
  46. NamingStrategy: schema.NamingStrategy{
  47. SingularTable: true, // 表示使用单数表名,启用该选项后,GORM 将不会对表名进行复数化处理
  48. },
  49. })
  50. if err != nil {
  51. panic(fmt.Errorf("mysql 启动异常,数据库:%s;Err:%s", aliasName, err))
  52. }
  53. sqlDB, err := db.DB()
  54. if err != nil {
  55. panic(fmt.Errorf("mysql 创建连接池失败,数据库:%s;Err:%s", aliasName, err))
  56. }
  57. dmSqlMap[aliasName] = db
  58. if isDefault {
  59. DEFAULT_DmSQL = db
  60. }
  61. sqlDB.SetMaxIdleConns(50)
  62. sqlDB.SetMaxOpenConns(100)
  63. sqlDB.SetConnMaxLifetime(10 * time.Minute)
  64. }
  65. func (t *LocalTime) MarshalJSON() ([]byte, error) {
  66. tTime := time.Time(*t)
  67. if tTime.IsZero() {
  68. return []byte("\"\""), nil
  69. }
  70. return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02 15:04:05"))), nil
  71. }
  72. func (t LocalTime) Value() (driver.Value, error) {
  73. var zeroTime time.Time
  74. tlt := time.Time(t)
  75. if tlt.UnixNano() == zeroTime.UnixNano() {
  76. return nil, nil
  77. }
  78. return tlt, nil
  79. }
  80. func (t *LocalTime) Scan(v interface{}) error {
  81. if value, ok := v.(time.Time); ok {
  82. *t = LocalTime(value)
  83. return nil
  84. }
  85. return fmt.Errorf("can not convert %v to timestamp", v)
  86. }