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" ) var supplierChan chan error // 定时 从sap拉取供应商数据 // 预留参数;时间、编码,提供给手动调用接口 func (s *Service) SapSupplier(t time.Time, code string) { supplierChan = make(chan error, 10) r := new(http.SupplierReq) rp := new(http.SupplierResp) var e error r.Dest.DestID = "EQMAN" r.Dest.BussTp = "MM004" r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM004/QuerySupplier/" 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.CompanyCode = "1004" r.Data.CompanyCode = "9099" r.Data.Types = []http.ZSORT{{Type: "设备"}} if e = s.SyncSap(r, rp); e == nil { if rp.Dest.Status == "S" { log.Infof("sap supplier success : mlen=%d", len(rp.Data.Master)) } else { e = errors.Errorf("sap supplier fail: %s", rp.Dest.MessText) } } else { e = errors.Wrap(e, "sap supplier error:") } var update error var count int for _, v := range rp.Data.Master { update = nil go s.updateProvider(&v) update = <-supplierChan if update != nil { log.Error(update) count++ l := model.SapDetailLog{Name: "supplier", Code: v.Code, ErrorText: update.Error()} if e := s.d.DB.Create(&l); e != nil { log.Errorf("add sapdetail log: %v", e) } } break } if e != nil { log.Error(e) } else if update != nil { e = errors.Errorf("supplier update fail sum:%d", count) } else { log.Infof("supplier update success sum:%d", len(rp.Data.Master)-count) } s.AddSapLog(r, e) return } // 更新供应商 func (s *Service) updateProvider(sup *http.Supplier) { pv := new(model.Provider) if len(sup.UpDate) == 8 { sup.UpDate = sup.UpDate[:4] + "-" + sup.UpDate[4:6] + "-" + sup.UpDate[6:] } if len(sup.UpTime) == 6 { sup.UpTime = sup.UpTime[:2] + ":" + sup.UpTime[2:4] + ":" + sup.UpTime[4:] } if sup.UpTime != "" { sup.UpDate = sup.UpDate + " " + sup.UpTime } if t, e := time.Parse("2006-01-02 15:04:05", sup.UpDate); e != nil { supplierChan <- errors.Errorf("time parse : %s, %s", sup.UpDate) } else { pv.ModifyTime = t } pv.ProviderIntro = sup.Name pv.ProviderName = sup.ShortName pv.ProviderNumber = sup.Code pv.CompanyCode = sup.CompanyCode pv.SortName = sup.ZSORT pv.Linkman = sup.Contact pv.Email = sup.Mail pv.Telphone = sup.Phone if sup.Dflag == "" { pv.Enable = 1 } if e := s.d.DB.Where(model.Provider{ProviderNumber: sup.Code}).Assign(pv).FirstOrCreate(&pv).Error; e != nil { supplierChan <- errors.Wrapf(e, "provider update failed:code =%s", sup.Code) } else { log.Infof("provider update succeeded:%s,%s,%s", sup.Code, sup.ShortName, sup.CompanyCode) supplierChan <- nil } }