supplier.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kpt.xdmy/apiserver/config"
  6. "time"
  7. "github.com/pkg/errors"
  8. log "github.com/sirupsen/logrus"
  9. _ "github.com/go-sql-driver/mysql"
  10. "kpt.xdmy/apiserver/model"
  11. "kpt.xdmy/apiserver/model/http"
  12. )
  13. var supplierChan chan error
  14. // 定时 从sap拉取供应商数据
  15. // 预留参数;时间、编码,提供给手动调用接口
  16. func (s *Service) SapSupplier(t time.Time, code string, pastureid int) {
  17. supplierChan = make(chan error, 10)
  18. r := new(http.SupplierReq)
  19. rp := new(http.SupplierResp)
  20. var e error
  21. r.Dest.DestID = "EQMAN"
  22. r.Dest.BussTp = "MM004"
  23. //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM004/QuerySupplier/"
  24. r.Dest.Url = fmt.Sprintf("%s/Common/MM004/QuerySupplier/", config.Conf.Http.Routing)
  25. if t.IsZero() {
  26. t = time.Now()
  27. }
  28. r.Data.BudatB = t.Format("20060102")
  29. r.Data.BudatE = t.Format("20060102")
  30. //r.Data.BudatB = "20221004"
  31. //r.Data.BudatE = "20221022"
  32. //r.Data.CompanyCode = "1004"
  33. r.Data.CompanyCode = code
  34. r.Data.Types = []http.ZSORT{{Type: "设备"}, {Type: "服务类"}, {Type: "备品备件类"}, {Type: "工程类"}, {Type: "其他类"}}
  35. rbyte, _ := json.Marshal(r)
  36. fmt.Println(string(rbyte))
  37. if e = s.SyncSap(r, rp, rbyte); e == nil {
  38. if rp.Dest.Status == "S" {
  39. log.Infof("sap supplier success : mlen=%d", len(rp.Data.Master))
  40. } else {
  41. e = errors.Errorf("sap supplier fail: %s", rp.Dest.MessText)
  42. }
  43. } else {
  44. e = errors.Wrap(e, "sap supplier error:")
  45. }
  46. var update error
  47. var count int
  48. for _, v := range rp.Data.Master {
  49. update = nil
  50. s.updateProvider(&v)
  51. update = <-supplierChan
  52. if update != nil {
  53. log.Error(update)
  54. count++
  55. l := model.SapDetailLog{Name: "supplier", Code: v.Code, ErrorText: update.Error()}
  56. if e := s.d.DB.Create(&l); e != nil {
  57. log.Errorf("add sapdetail log: %v", e)
  58. }
  59. }
  60. // break
  61. }
  62. if e != nil {
  63. log.Error(e)
  64. } else if update != nil {
  65. e = errors.Errorf("supplier update fail sum:%d", count)
  66. } else {
  67. log.Infof("supplier update success sum:%d", len(rp.Data.Master)-count)
  68. }
  69. s.AddSapLog(r, e)
  70. return
  71. }
  72. // 更新供应商
  73. func (s *Service) updateProvider(sup *http.Supplier) {
  74. pv := new(model.Provider)
  75. if len(sup.UpDate) == 8 {
  76. sup.UpDate = sup.UpDate[:4] + "-" + sup.UpDate[4:6] + "-" + sup.UpDate[6:]
  77. }
  78. if len(sup.UpTime) == 6 {
  79. sup.UpTime = sup.UpTime[:2] + ":" + sup.UpTime[2:4] + ":" + sup.UpTime[4:]
  80. }
  81. if sup.UpTime != "" {
  82. sup.UpDate = sup.UpDate + " " + sup.UpTime
  83. }
  84. if t, e := time.Parse("2006-01-02 15:04:05", sup.UpDate); e != nil {
  85. supplierChan <- errors.Errorf("time parse : %s, %s", sup.UpDate)
  86. } else {
  87. pv.ModifyTime = t
  88. }
  89. pv.ProviderIntro = sup.Name
  90. pv.ProviderName = sup.ShortName
  91. pv.ProviderNumber = sup.Code
  92. pv.CompanyCode = sup.CompanyCode
  93. pv.SortName = sup.ZSORT
  94. pv.Linkman = sup.Contact
  95. pv.Email = sup.Mail
  96. pv.Telphone = sup.Phone
  97. pv.SapCode = sup.Code
  98. if sup.Dflag == "" {
  99. pv.Enable = 1
  100. }
  101. fmt.Println(pv)
  102. if e := s.d.DB.Where(model.Provider{SapCode: sup.Code}).Assign(pv).FirstOrCreate(&pv).Error; e != nil {
  103. supplierChan <- errors.Wrapf(e, "provider update failed:code =%s", sup.Code)
  104. } else {
  105. log.Infof("provider update succeeded:%s,%s,%s", sup.Code, sup.ShortName, sup.CompanyCode)
  106. supplierChan <- nil
  107. }
  108. }