123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package service
- import (
- "encoding/json"
- "fmt"
- "time"
- "github.com/pkg/errors"
- log "github.com/sirupsen/logrus"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- "kpt.xdmy/pkg/util"
- )
- func (s *Service) AddSapLog(p http.SapDest, err error) {
- b, e := util.Marshal(p)
- if e != nil {
- log.Error(e)
- return
- }
- d := p.GetDest()
- l := model.SapLog{
- Url: d.Url,
- Name: d.BussTp,
- Status: d.Status,
- MsgText: d.MessText,
- Param: string(b),
- CreatedAt: time.Now(),
- }
- if err != nil {
- l.Flag = 1
- }
- if e := s.d.DB.Create(&l).Error; e != nil {
- log.Error(e)
- } else {
- log.Infof("create sap log:%s ", p.GetUrl())
- }
- }
- func (s *Service) AddSapApiLog(req http.SapDest, resp interface{}, data interface{}, err error) {
- r, e := util.Marshal(req)
- if e != nil {
- log.Error(e)
- return
- }
- a, e := util.Marshal(resp)
- if e != nil {
- log.Error(e)
- return
- }
- p, e := util.Marshal(data)
- if e != nil {
- log.Error(e)
- return
- }
- q := req.GetDest()
- respMap := make(map[string]interface{})
- respbyte, _ := json.Marshal(resp)
- respErr := json.Unmarshal(respbyte, &respMap)
- if respErr != nil {
- log.Error(respErr)
- return
- }
- sp := respMap["DEST"].(map[string]interface{})
- l := model.SapApiLog{
- Url: q.Url,
- Name: q.BussTp,
- Request: string(r),
- Response: string(a),
- MsgText: sp["MESS_TEXT"].(string),
- Status: sp["STATUS_OS"].(string),
- Param: string(p),
- CreatedAt: time.Now(),
- }
- if err != nil {
- l.Flag = 1
- }
- if e := s.d.DB.Create(&l).Error; e != nil {
- log.Error(e)
- } else {
- log.Infof("create sap api log:%s ", q.GetUrl())
- }
- }
- func (s *Service) AddSapTransferApiLog(req http.SapDest, resp *http.TransferResp, data interface{}, err error) {
- r, e := util.Marshal(req)
- if e != nil {
- log.Error(e)
- return
- }
- a, e := util.Marshal(resp)
- if e != nil {
- log.Error(e)
- return
- }
- p, e := util.Marshal(data)
- if e != nil {
- log.Error(e)
- return
- }
- q := req.GetDest()
- sp := resp.Dest
- l := model.SapApiLog{
- Url: q.Url,
- Name: q.BussTp,
- Request: string(r),
- Response: string(a),
- MsgText: sp.MessText,
- Status: sp.Status,
- Param: string(p),
- CreatedAt: time.Now(),
- }
- if err != nil {
- l.Flag = 1
- }
- //fmt.Println(time.Now())
- if e := s.d.DB.Create(&l).Error; e != nil {
- log.Error(e)
- } else {
- log.Infof("create sap api log:%s ", q.GetUrl())
- }
- }
- func (s *Service) SyncSap(r http.SapDest, rp interface{}, data interface{}) (err error) {
- req, e := s.http.NewRequest("POST", r.GetUrl(), data)
- if e != nil {
- return errors.Wrapf(e, "SyncSap")
- }
- s.http.SetBasicAuth(req)
- if err = s.http.Do(req, &rp); err != nil {
- err = errors.Wrapf(err, "SyncSap")
- }
- //if r.GetUrl() != config.Conf.Http.Routing+"/SRM/MM018/PurchaseInfo" {
- s.AddSapApiLog(r, rp, data, err)
- //}
- return
- }
- func (s *Service) SyncSapTransfer(r http.SapDest, rp interface{}, data interface{}) (err error) {
- fmt.Println(r, rp)
- req, e := s.http.NewRequest("POST", r.GetUrl(), data)
- if e != nil {
- return errors.Wrapf(e, "SyncSap")
- }
- s.http.SetBasicAuth(req)
- if err = s.http.Do(req, &rp); err != nil {
- err = errors.Wrapf(err, "SyncSap")
- }
- s.AddSapTransferApiLog(r, rp.(*http.TransferResp), data, e)
- return
- }
- func (s *Service) SyncSrm(r http.SapDest, rp interface{}, data interface{}) (err error) {
- req, e := s.http.NewRequest("POST", r.GetUrl(), data)
- if e != nil {
- return errors.Wrapf(e, "SyncSap")
- }
- s.http.SetSrmBasicAuth(req)
- if err = s.http.Do(req, rp); err != nil {
- err = errors.Wrapf(err, "SyncSap")
- }
- return
- }
|