material.go 2.6 KB

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