service.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package service
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "time"
  6. "kpt.xdmy/apiserver/config"
  7. "kpt.xdmy/apiserver/dao"
  8. "kpt.xdmy/apiserver/model"
  9. "kpt.xdmy/pkg/http"
  10. "kpt.xdmy/pkg/log"
  11. )
  12. // var s *Service
  13. var nch chan error
  14. type Service struct {
  15. c *config.Config
  16. d *dao.Dao
  17. http *http.Client
  18. sqlserver *dao.SqlServer
  19. }
  20. func New(conf *config.Config) *Service {
  21. c := http.NewClient(conf)
  22. d := dao.New(conf)
  23. sqlserver := dao.NewSqlServer(conf)
  24. model.ModelInit(d)
  25. // SapJobInit()
  26. SyncTable(d.DB)
  27. s = &Service{
  28. d: d,
  29. http: c,
  30. sqlserver: sqlserver,
  31. }
  32. return s
  33. }
  34. func (s *Service) queryDB(tname string, c interface{}, r interface{}) (err error) {
  35. if err := s.d.DB.Table(tname).Where(c).Find(r).Error; err != nil {
  36. err = log.Error("queryDB", err, c)
  37. }
  38. return
  39. }
  40. func (s *Service) insertDB(tname string, p interface{}) (err error) {
  41. if err = s.d.DB.Table(tname).Create(p).Error; err != nil {
  42. err = log.Error("insertDB "+tname, err, p)
  43. }
  44. return
  45. }
  46. func (s *Service) updateDB(tname string, c interface{}, r interface{}) (err error) {
  47. if err = s.d.DB.Table(tname).Where(c).Updates(r).Error; err != nil {
  48. err = log.Error("updateDB", err, r)
  49. }
  50. return
  51. }
  52. func (s *Service) RawDB(sql string, dest interface{}, c ...interface{}) (err error) {
  53. err = s.d.DB.Raw(sql, c).Scan(&dest).Error
  54. if err != nil {
  55. err = log.Error("service RawDB", err, sql, c)
  56. }
  57. return
  58. }
  59. func (s *Service) Xorm() {
  60. st := time.Now()
  61. if r, e := s.d.DB.Raw("SELECT * FROM parts limit 3000").Rows(); e != nil {
  62. fmt.Print(e)
  63. } else {
  64. r.Close()
  65. }
  66. log.Printf("xorm:%d", time.Now().Sub(st).Milliseconds())
  67. }
  68. func SyncTable(tx *gorm.DB) {
  69. tx.AutoMigrate(&model.FeeQuery{})
  70. }