dm_database.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package init_serve
  2. import (
  3. _ "dm"
  4. dm "dmgorm2"
  5. "eta/eta_bridge/global"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "gorm.io/gorm/logger"
  9. "io"
  10. "log"
  11. "os"
  12. "time"
  13. )
  14. type Product struct {
  15. gorm.Model
  16. Code string
  17. Price uint
  18. }
  19. func DMDataBase() {
  20. mysqlConf := global.CONFIG.Mysql
  21. if len(mysqlConf.List) <= 0 {
  22. global.LOG.Error("mysql链接未配置")
  23. panic(fmt.Errorf("mysql链接未配置"))
  24. }
  25. //开启日志
  26. logWriter := io.MultiWriter(global.MYSQL_LOG) //binlog日志,记录到文件中去
  27. if global.CONFIG.Mysql.Stdout {
  28. logWriter = io.MultiWriter(global.MYSQL_LOG, os.Stdout)
  29. }
  30. newLogger := logger.New(log.New(logWriter, "\r\n", log.LstdFlags), logger.Config{
  31. SlowThreshold: 200 * time.Millisecond, //慢sql :200ms
  32. LogLevel: logger.Info, //记录的日志类型,info代表所有信息都记录
  33. IgnoreRecordNotFoundError: true, //是否忽略找不到数据错误信息(只是日志记录记录成err还是普通的输出的区别,并不影响业务代码中的:找不到数据行error)
  34. Colorful: true, //是否颜色输出
  35. })
  36. htConfig := global.CONFIG.HTFutures
  37. db, err := gorm.Open(dm.Open(htConfig.OaDBDns), &gorm.Config{
  38. Logger: newLogger,
  39. })
  40. if err != nil {
  41. global.LOG.Errorf("海通OA数据库 启动异常,数据库:", ";Err:", err)
  42. panic(fmt.Errorf("海通OA数据库 启动异常,数据库:", "Err:%s", err))
  43. }
  44. global.OaDbHT = db
  45. fmt.Println("ht oa database init end")
  46. }