service.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Conf *http.Client
  20. }
  21. func New(conf *config.Config) *Service {
  22. c := http.NewClient(conf)
  23. d := dao.New(conf)
  24. sqlserver := dao.NewSqlServer(conf)
  25. model.ModelInit(d)
  26. // SapJobInit()
  27. SyncTable(d.DB)
  28. s = &Service{
  29. d: d,
  30. http: c,
  31. sqlserver: sqlserver,
  32. Conf: c,
  33. }
  34. return s
  35. }
  36. func (s *Service) queryDB(tname string, c interface{}, r interface{}) (err error) {
  37. if err := s.d.DB.Table(tname).Where(c).Find(r).Error; err != nil {
  38. err = log.Error("queryDB", err, c)
  39. }
  40. return
  41. }
  42. func (s *Service) insertDB(tname string, p interface{}) (err error) {
  43. if err = s.d.DB.Table(tname).Create(p).Error; err != nil {
  44. err = log.Error("insertDB "+tname, err, p)
  45. }
  46. return
  47. }
  48. func (s *Service) updateDB(tname string, c interface{}, r interface{}) (err error) {
  49. if err = s.d.DB.Table(tname).Where(c).Updates(r).Error; err != nil {
  50. err = log.Error("updateDB", err, r)
  51. }
  52. return
  53. }
  54. func (s *Service) RawDB(sql string, dest interface{}, c ...interface{}) (err error) {
  55. err = s.d.DB.Raw(sql, c).Scan(&dest).Error
  56. if err != nil {
  57. err = log.Error("service RawDB", err, sql, c)
  58. }
  59. return
  60. }
  61. func (s *Service) Xorm() {
  62. st := time.Now()
  63. if r, e := s.d.DB.Raw("SELECT * FROM parts limit 3000").Rows(); e != nil {
  64. fmt.Print(e)
  65. } else {
  66. r.Close()
  67. }
  68. log.Printf("xorm:%d", time.Now().Sub(st).Milliseconds())
  69. }
  70. func SyncTable(tx *gorm.DB) {
  71. tx.AutoMigrate(&model.FeeQuery{})
  72. }