material.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // "kpt.xdmy/pkg/log"
  10. )
  11. var partChan chan error
  12. // 定时从sap拉取物料(备件等)数据
  13. // 预留参数时间、备件编码,给手动调用接口
  14. func (s *Service) SapMaterial(t time.Time, code string) {
  15. partChan = make(chan error, 10)
  16. r := new(http.MaterialReq)
  17. rp := new(http.MaterialResp)
  18. var e error
  19. r.Dest.DestID = "EQMAN"
  20. r.Dest.BussTp = "MM002"
  21. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM002/QueryMaterial/"
  22. if t.IsZero() {
  23. t = time.Now()
  24. }
  25. r.Data.BudatB = t.Format("20060102")
  26. r.Data.BudatE = t.Format("20060102")
  27. r.Data.BudatB = "20220920"
  28. r.Data.BudatE = "20220922"
  29. r.Data.Type = http.MTART{MTART: "XD08"}
  30. // r.Data.Group = []http.MATKL{{MATKL: "14010101"}}
  31. // r.Data.Group = []http.MATKL{{MATKL: "14"}}
  32. if code != "" {
  33. // r.Data.Codes = []http.MATNR{{Code: "14.01.06.02.000216"}}
  34. }
  35. if e = s.SyncSap(r, rp); e == nil {
  36. if rp.Dest.Status == "S" {
  37. log.Infof("sap material success:len=%d", len(rp.Data.Master))
  38. } else {
  39. e = errors.Errorf("sap material fail", rp.Dest.MessText)
  40. }
  41. } else {
  42. e = errors.Wrapf(e, "sap material error")
  43. }
  44. var update error
  45. var count int
  46. for _, v := range rp.Data.Master {
  47. update = nil
  48. go s.UpdatePart(&v)
  49. update = <-partChan
  50. if update != nil {
  51. log.Error(update)
  52. count++
  53. l := model.SapDetailLog{Name: "part", Code: v.Code, ErrorText: update.Error()}
  54. if e := s.d.DB.Create(&l); e != nil {
  55. log.Errorf("add sapdetail log: %v", e)
  56. }
  57. }
  58. }
  59. if e != nil {
  60. log.Error(e)
  61. } else if update != nil {
  62. e = errors.Errorf("material update fail sum:%d", count)
  63. } else {
  64. log.Infof("material update success sum:%d", len(rp.Data.Master)-count)
  65. }
  66. s.AddSapLog(r, e)
  67. return
  68. }
  69. // 物料数据更新
  70. func (s *Service) UpdatePart(p *http.MaterialDetail) {
  71. part := new(model.Parts)
  72. if len(p.ModifyTime) >= 8 {
  73. p.ModifyTime = p.ModifyTime[:4] + "-" + p.ModifyTime[4:6] + "-" + p.ModifyTime[6:8]
  74. } else {
  75. partChan <- errors.New("ModifyTime is invalid:" + p.ModifyTime)
  76. }
  77. if t, e := time.ParseInLocation("2006-01-02", p.ModifyTime, time.Local); e != nil {
  78. partChan <- errors.Wrapf(e, "time:%s", p.ModifyTime)
  79. } else {
  80. part.ModifyTime = t
  81. }
  82. if p.Dflag == "" {
  83. part.Enable = 1
  84. part.Statue = 1
  85. }
  86. part.PartCode = p.Code
  87. part.Note = p.Description
  88. part.Category = p.Type
  89. part.Unit = p.Unit
  90. part.Name = p.Name
  91. part.Specification = p.Model
  92. if e := s.d.DB.Where(model.Parts{PartCode: p.Code}).Assign(*part).FirstOrCreate(part).Error; e != nil {
  93. partChan <- errors.Wrapf(e, "code:%s", p.Code)
  94. } else {
  95. log.Infof("sap part update success:%s", p.Code)
  96. partChan <- nil
  97. }
  98. }