supplier.go 2.9 KB

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