sap.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/pkg/errors"
  7. log "github.com/sirupsen/logrus"
  8. "kpt.xdmy/apiserver/model"
  9. "kpt.xdmy/apiserver/model/http"
  10. "kpt.xdmy/pkg/util"
  11. )
  12. func (s *Service) AddSapLog(p http.SapDest, err error) {
  13. b, e := util.Marshal(p)
  14. if e != nil {
  15. log.Error(e)
  16. return
  17. }
  18. d := p.GetDest()
  19. l := model.SapLog{
  20. Url: d.Url,
  21. Name: d.BussTp,
  22. Status: d.Status,
  23. MsgText: d.MessText,
  24. Param: string(b),
  25. CreatedAt: time.Now(),
  26. }
  27. if err != nil {
  28. l.Flag = 1
  29. }
  30. if e := s.d.DB.Create(&l).Error; e != nil {
  31. log.Error(e)
  32. } else {
  33. log.Infof("create sap log:%s ", p.GetUrl())
  34. }
  35. }
  36. func (s *Service) AddSapApiLog(req http.SapDest, resp interface{}, data interface{}, err error) {
  37. r, e := util.Marshal(req)
  38. if e != nil {
  39. log.Error(e)
  40. return
  41. }
  42. a, e := util.Marshal(resp)
  43. if e != nil {
  44. log.Error(e)
  45. return
  46. }
  47. p, e := util.Marshal(data)
  48. if e != nil {
  49. log.Error(e)
  50. return
  51. }
  52. q := req.GetDest()
  53. respMap := make(map[string]interface{})
  54. respbyte, _ := json.Marshal(resp)
  55. respErr := json.Unmarshal(respbyte, &respMap)
  56. if respErr != nil {
  57. log.Error(respErr)
  58. return
  59. }
  60. sp := respMap["DEST"].(map[string]interface{})
  61. l := model.SapApiLog{
  62. Url: q.Url,
  63. Name: q.BussTp,
  64. Request: string(r),
  65. Response: string(a),
  66. MsgText: sp["MESS_TEXT"].(string),
  67. Status: sp["STATUS_OS"].(string),
  68. Param: string(p),
  69. CreatedAt: time.Now(),
  70. }
  71. if err != nil {
  72. l.Flag = 1
  73. }
  74. if e := s.d.DB.Create(&l).Error; e != nil {
  75. log.Error(e)
  76. } else {
  77. log.Infof("create sap api log:%s ", q.GetUrl())
  78. }
  79. }
  80. func (s *Service) AddSapTransferApiLog(req http.SapDest, resp *http.TransferResp, data interface{}, err error) {
  81. r, e := util.Marshal(req)
  82. if e != nil {
  83. log.Error(e)
  84. return
  85. }
  86. a, e := util.Marshal(resp)
  87. if e != nil {
  88. log.Error(e)
  89. return
  90. }
  91. p, e := util.Marshal(data)
  92. if e != nil {
  93. log.Error(e)
  94. return
  95. }
  96. q := req.GetDest()
  97. sp := resp.Dest
  98. l := model.SapApiLog{
  99. Url: q.Url,
  100. Name: q.BussTp,
  101. Request: string(r),
  102. Response: string(a),
  103. MsgText: sp.MessText,
  104. Status: sp.Status,
  105. Param: string(p),
  106. CreatedAt: time.Now(),
  107. }
  108. if err != nil {
  109. l.Flag = 1
  110. }
  111. //fmt.Println(time.Now())
  112. if e := s.d.DB.Create(&l).Error; e != nil {
  113. log.Error(e)
  114. } else {
  115. log.Infof("create sap api log:%s ", q.GetUrl())
  116. }
  117. }
  118. func (s *Service) SyncSap(r http.SapDest, rp interface{}, data interface{}) (err error) {
  119. req, e := s.http.NewRequest("POST", r.GetUrl(), data)
  120. if e != nil {
  121. return errors.Wrapf(e, "SyncSap")
  122. }
  123. s.http.SetBasicAuth(req)
  124. if err = s.http.Do(req, &rp); err != nil {
  125. err = errors.Wrapf(err, "SyncSap")
  126. }
  127. //if r.GetUrl() != config.Conf.Http.Routing+"/SRM/MM018/PurchaseInfo" {
  128. s.AddSapApiLog(r, rp, data, err)
  129. //}
  130. return
  131. }
  132. func (s *Service) SyncSapTransfer(r http.SapDest, rp interface{}, data interface{}) (err error) {
  133. fmt.Println(r, rp)
  134. req, e := s.http.NewRequest("POST", r.GetUrl(), data)
  135. if e != nil {
  136. return errors.Wrapf(e, "SyncSap")
  137. }
  138. s.http.SetBasicAuth(req)
  139. if err = s.http.Do(req, &rp); err != nil {
  140. err = errors.Wrapf(err, "SyncSap")
  141. }
  142. s.AddSapTransferApiLog(r, rp.(*http.TransferResp), data, e)
  143. return
  144. }
  145. func (s *Service) SyncSrm(r http.SapDest, rp interface{}, data interface{}) (err error) {
  146. req, e := s.http.NewRequest("POST", r.GetUrl(), data)
  147. if e != nil {
  148. return errors.Wrapf(e, "SyncSap")
  149. }
  150. s.http.SetSrmBasicAuth(req)
  151. if err = s.http.Do(req, rp); err != nil {
  152. err = errors.Wrapf(err, "SyncSap")
  153. }
  154. return
  155. }