dao.go 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package upcrm
  2. import (
  3. "context"
  4. "go-common/app/job/main/up/conf"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. //Dao upcrm dao
  10. type Dao struct {
  11. // config
  12. conf *conf.Config
  13. // db
  14. crmdb *gorm.DB
  15. httpClient *bm.Client
  16. }
  17. //New new dao
  18. func New(c *conf.Config) *Dao {
  19. var d = &Dao{
  20. conf: c,
  21. }
  22. crmdb, err := gorm.Open("mysql", c.Upcrm.DSN)
  23. if crmdb == nil {
  24. log.Error("connect to db fail, err=%v", err)
  25. return nil
  26. }
  27. crmdb.SingularTable(true)
  28. d.crmdb = crmdb
  29. d.crmdb.LogMode(c.IsTest)
  30. return d
  31. }
  32. //SetHTTPClient set http client
  33. func (d *Dao) SetHTTPClient(client *bm.Client) {
  34. d.httpClient = client
  35. }
  36. //GetDb get current gorm db
  37. func (d *Dao) GetDb() *gorm.DB {
  38. return d.crmdb
  39. }
  40. // Close close resource.
  41. func (d *Dao) Close() {
  42. d.crmdb.Close()
  43. }
  44. // Ping ping success.
  45. func (d *Dao) Ping(c context.Context) (err error) {
  46. return
  47. }