supplier.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. log "github.com/sirupsen/logrus"
  6. _ "github.com/go-sql-driver/mysql"
  7. "kpt.xdmy/apiserver/model"
  8. "kpt.xdmy/apiserver/model/http"
  9. )
  10. var supplierChan chan error
  11. func (s *Service) SapSupplier(t time.Time, code string) {
  12. supplierChan = make(chan error, 10)
  13. r := new(http.SupplierReq)
  14. rp := new(http.SupplierResp)
  15. var e error
  16. r.Dest.DestID = "EQMAN"
  17. r.Dest.BussTp = "MM004"
  18. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM004/QuerySupplier/"
  19. if t.IsZero() {
  20. t = time.Now()
  21. }
  22. r.Data.BudatB = t.Format("20060102")
  23. r.Data.BudatE = t.Format("20060102")
  24. r.Data.BudatB = "20220920"
  25. r.Data.BudatE = "20220922"
  26. // r.Data.CompanyCode = "1004"
  27. r.Data.CompanyCode = "9099"
  28. r.Data.Types = []http.ZSORT{{Type: "设备"}}
  29. if e = s.SyncSap(r, rp); e == nil {
  30. if rp.Dest.Status == "S" {
  31. log.Infof("sap supplier success : mlen=%d", len(rp.Data.Master))
  32. } else {
  33. e = errors.Errorf("sap supplier fail: %s", rp.Dest.MessText)
  34. }
  35. } else {
  36. e = errors.Wrap(e, "sap supplier error:")
  37. }
  38. var update error
  39. var count int
  40. for _, v := range rp.Data.Master {
  41. update = nil
  42. go s.updateProvider(&v)
  43. update = <-supplierChan
  44. if update != nil {
  45. log.Error(update)
  46. count++
  47. l := model.SapDetailLog{Name: "supplier", Code: v.Code, ErrorText: update.Error()}
  48. if e := s.d.DB.Create(&l); e != nil {
  49. log.Errorf("add sapdetail log: %v", e)
  50. }
  51. }
  52. break
  53. }
  54. if e != nil {
  55. log.Error(e)
  56. } else if update != nil {
  57. e = errors.Errorf("supplier update fail sum:%d", count)
  58. } else {
  59. log.Infof("supplier update success sum:%d", len(rp.Data.Master)-count)
  60. }
  61. s.AddSapLog(r, e)
  62. return
  63. }
  64. func (s *Service) updateProvider(sup *http.Supplier) {
  65. pv := new(model.Provider)
  66. if len(sup.UpDate) == 8 {
  67. sup.UpDate = sup.UpDate[:4] + "-" + sup.UpDate[4:6] + "-" + sup.UpDate[6:]
  68. }
  69. if len(sup.UpTime) == 6 {
  70. sup.UpTime = sup.UpTime[:2] + ":" + sup.UpTime[2:4] + ":" + sup.UpTime[4:]
  71. }
  72. if sup.UpTime != "" {
  73. sup.UpDate = sup.UpDate + " " + sup.UpTime
  74. }
  75. if t, e := time.Parse("2006-01-02 15:04:05", sup.UpDate); e != nil {
  76. supplierChan <- errors.Errorf("time parse : %s, %s", sup.UpDate)
  77. } else {
  78. pv.ModifyTime = t
  79. }
  80. pv.ProviderIntro = sup.Name
  81. pv.ProviderName = sup.ShortName
  82. pv.ProviderNumber = sup.Code
  83. pv.CompanyCode = sup.CompanyCode
  84. pv.SortName = sup.ZSORT
  85. pv.Linkman = sup.Contact
  86. pv.Email = sup.Mail
  87. pv.Telphone = sup.Phone
  88. if sup.Dflag == "" {
  89. pv.Enable = 1
  90. }
  91. if e := s.d.DB.Where(model.Provider{ProviderNumber: sup.Code}).Assign(pv).FirstOrCreate(&pv).Error; e != nil {
  92. supplierChan <- errors.Wrapf(e, "provider update failed:code =%s", sup.Code)
  93. } else {
  94. log.Infof("provider update succeeded:%s,%s,%s", sup.Code, sup.ShortName, sup.CompanyCode)
  95. supplierChan <- nil
  96. }
  97. }