package service import ( "time" "github.com/pkg/errors" log "github.com/sirupsen/logrus" _ "github.com/go-sql-driver/mysql" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" // "kpt.xdmy/pkg/log" ) var partChan chan error func (s *Service) SapMaterial(t time.Time, code string) { partChan = make(chan error, 10) r := new(http.MaterialReq) rp := new(http.MaterialResp) var e error r.Dest.DestID = "EQMAN" r.Dest.BussTp = "MM002" r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM002/QueryMaterial/" if t.IsZero() { t = time.Now() } r.Data.BudatB = t.Format("20060102") r.Data.BudatE = t.Format("20060102") r.Data.BudatB = "20220920" r.Data.BudatE = "20220922" r.Data.Type = http.MTART{MTART: "XD08"} // r.Data.Group = []http.MATKL{{MATKL: "14010101"}} // r.Data.Group = []http.MATKL{{MATKL: "14"}} if code != "" { // r.Data.Codes = []http.MATNR{{Code: "14.01.06.02.000216"}} } if e = s.SyncSap(r, rp); e == nil { if rp.Dest.Status == "S" { log.Infof("sap material success:len=%d", len(rp.Data.Master)) } else { e = errors.Errorf("sap material fail", rp.Dest.MessText) } } else { e = errors.Wrapf(e, "sap material error") } var update error var count int for _, v := range rp.Data.Master { update = nil go s.UpdatePart(&v) update = <-partChan if update != nil { log.Error(update) count++ l := model.SapDetailLog{Name: "part", Code: v.Code, ErrorText: update.Error()} if e := s.d.DB.Create(&l); e != nil { log.Errorf("add sapdetail log: %v", e) } } } if e != nil { log.Error(e) } else if update != nil { e = errors.Errorf("material update fail sum:%d", count) } else { log.Infof("material update success sum:%d", len(rp.Data.Master)-count) } s.AddSapLog(r, e) return } func (s *Service) UpdatePart(p *http.MaterialDetail) { part := new(model.Parts) if len(p.ModifyTime) >= 8 { p.ModifyTime = p.ModifyTime[:4] + "-" + p.ModifyTime[4:6] + "-" + p.ModifyTime[6:8] } else { partChan <- errors.New("ModifyTime is invalid:" + p.ModifyTime) } if t, e := time.ParseInLocation("2006-01-02", p.ModifyTime, time.Local); e != nil { partChan <- errors.Wrapf(e, "time:%s", p.ModifyTime) } else { part.ModifyTime = t } if p.Dflag == "" { part.Enable = 1 part.Statue = 1 } part.PartCode = p.Code part.Note = p.Description part.Category = p.Type part.Unit = p.Unit part.Name = p.Name part.Specification = p.Model if e := s.d.DB.Where(model.Parts{PartCode: p.Code}).Assign(*part).FirstOrCreate(part).Error; e != nil { partChan <- errors.Wrapf(e, "code:%s", p.Code) } else { log.Infof("sap part update success:%s", p.Code) partChan <- nil } }