package service import ( "fmt" "gorm.io/gorm" "time" "kpt.xdmy/apiserver/config" "kpt.xdmy/apiserver/dao" "kpt.xdmy/apiserver/model" "kpt.xdmy/pkg/http" "kpt.xdmy/pkg/log" ) // var s *Service var nch chan error type Service struct { c *config.Config d *dao.Dao http *http.Client sqlserver *dao.SqlServer Conf *http.Client } func New(conf *config.Config) *Service { c := http.NewClient(conf) d := dao.New(conf) sqlserver := dao.NewSqlServer(conf) model.ModelInit(d) // SapJobInit() SyncTable(d.DB) s = &Service{ d: d, http: c, sqlserver: sqlserver, Conf: c, } return s } func (s *Service) queryDB(tname string, c interface{}, r interface{}) (err error) { if err := s.d.DB.Table(tname).Where(c).Find(r).Error; err != nil { err = log.Error("queryDB", err, c) } return } func (s *Service) insertDB(tname string, p interface{}) (err error) { if err = s.d.DB.Table(tname).Create(p).Error; err != nil { err = log.Error("insertDB "+tname, err, p) } return } func (s *Service) updateDB(tname string, c interface{}, r interface{}) (err error) { if err = s.d.DB.Table(tname).Where(c).Updates(r).Error; err != nil { err = log.Error("updateDB", err, r) } return } func (s *Service) RawDB(sql string, dest interface{}, c ...interface{}) (err error) { err = s.d.DB.Raw(sql, c).Scan(&dest).Error if err != nil { err = log.Error("service RawDB", err, sql, c) } return } func (s *Service) Xorm() { st := time.Now() if r, e := s.d.DB.Raw("SELECT * FROM parts limit 3000").Rows(); e != nil { fmt.Print(e) } else { r.Close() } log.Printf("xorm:%d", time.Now().Sub(st).Milliseconds()) } func SyncTable(tx *gorm.DB) { tx.AutoMigrate(&model.FeeQuery{}) }