service.go 1.5 KB

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