package service import ( "fmt" _ "github.com/go-sql-driver/mysql" log "github.com/sirupsen/logrus" "github.com/pkg/errors" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" ) // 定时每分钟读取已完成 未同步的的调拨单,同步到sap // 每个调拨单定时同步只进行一次 func autoTransfer() { bp := []model.BigPartTransfer{} if e := s.d.DB.Where(" CreatDate > '2022-09-28' and sapStatus =0 ").Find(&bp, model.BigPartTransfer{Statue: 1}).Error; e != nil { log.Errorf("autoTransfer find :%v", e) return } log.Infof("sap transfer len=%d", len(bp)) for _, v := range bp { err := SapTransfer(&v) TransferLog("auto", v.ApplyCode, err) } } // 手动同步调拨单到sap func ManualTransfer(code string) (int, map[string]interface{}) { bp := []model.BigPartTransfer{} re := make(map[string]interface{}) r := s.d.DB.Where("CreatDate >'2022-09-28' ") if code != "" { r = r.Where("applycode", code) } r = r.Find(&bp, model.BigPartTransfer{Statue: 1, SapStatus: 2}) if r.Error != nil { log.Errorf("ManualTransfers find :%v", r.Error) re["find"] = r.Error return 0, re } var count int for _, v := range bp { err := SapTransfer(&v) TransferLog("manual", v.ApplyCode, err) if err == nil { count++ } else { re[v.ApplyCode] = err.Error() } } return count, re } // 调拨接口日志 func TransferLog(c, code string, err error) { m := make(map[string]interface{}) if err != nil { err = errors.Wrap(err, code) m["sapText"] = err.Error() if c == "auto" { m["sapStatus"] = 2 } } else { m["sapStatus"] = 1 } if e := s.d.DB.Model(&model.BigPartTransfer{}).Where("applycode =?", code).Updates(m).Error; e != nil { log.Errorf("BigPartTransfer update :%v", e) } } func SapTransfer(bp *model.BigPartTransfer) error { // 寄售(零库存)无调拨 r := new(http.TransferReq) parts := []model.PartTransfer{} blaid := new(model.BigPartLaid) inpa := new(model.Pasture) outpa := new(model.Pasture) dp := new(model.Department) if e := s.d.DB.Find(&parts, model.PartTransfer{BigID: bp.ID}).Error; e != nil || len(parts) == 0 { return errors.Wrapf(e, "parttarnsfer query bigid: %d", bp.ID) } if e := s.d.DB.First(blaid, model.BigPartLaid{LaidCode: bp.LaidCode}).Error; e != nil { return errors.Wrapf(e, "bigpartlaid query laidcode: %s", bp.LaidCode) } if e := s.d.DB.First(inpa, model.Pasture{ID: bp.InPastureID}).Error; e != nil { return errors.Wrapf(e, "pasture :id=%d", bp.InPastureID) } if e := s.d.DB.First(outpa, model.Pasture{ID: bp.PastureID}).Error; e != nil { return errors.Wrapf(e, "pasture :id=%d", bp.PastureID) } if e := s.d.DB.First(dp, model.Department{ID: bp.DeptId}).Error; e != nil { return errors.Wrapf(e, "pasture :id=%d", bp.DeptId) } r.Dest.DestID = "EQMAN" r.Dest.BussTp = "MM012" r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM012/TransferStock" r.Data.OrderDate = bp.CreatDate.Format("20060102") r.Data.PostDate = bp.ApplyDate.Format("20060102") r.Data.TransferCode = bp.ApplyCode r.Data.Group = "B11" for i, v := range parts { detail := new(http.TransferDetail) outStock := new(model.PartRepertory) pslaid := new(model.PartLaid) inw := new(model.Warehouse) c := new(model.Contract) bc := new(model.BigContract) if e := s.d.DB.First(outStock, model.PartRepertory{ID: v.PartRepID}).Error; e != nil { return errors.Wrapf(e, "partrepertory query :id=%d", v.PartRepID) } if e := s.d.DB.First(pslaid, model.PartLaid{BigID: blaid.ID, PartID: v.PartID}).Error; e != nil { return errors.Wrapf(e, "partlaid query :bigid=%d", blaid.ID) } if e := s.d.DB.First(inw, model.Warehouse{ID: pslaid.LocationID}).Error; e != nil { return errors.Wrapf(e, "warehouse query :id=%d", pslaid.LocationID) } if outStock.ContractID > 0 { // if e := s.d.DB.Table("BigContract").Select("BigContract.IsZeroStock ").Joins("join Contract c on c.bigid =BigContract.id"). // Where("c.id =?", outStock.ContractID).First(bc).Error; e != nil { // return errors.Wrapf(e, "BigContract IsZeroStock :id=%d", outStock.ContractID) // } if e := s.d.DB.First(c, model.Contract{ID: outStock.ContractID}).Error; e != nil { } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID) } if bc.IsZeroStock == 1 { return errors.New("寄售无调拨") } } detail.RowNumber = fmt.Sprintf("%d", i+1) detail.MaterialCode = v.PartCode detail.Quantity = v.Amount detail.Unit = v.Unit detail.Per = "1" detail.DeliverDate = blaid.StorageTime.Format("20060102") detail.OutFactory = outpa.FactoryCode detail.InFactory = inpa.FactoryCode detail.OutLocation = inw.WarehoseCode detail.InLocation = outStock.Location r.Data.Detail = append(r.Data.Detail, *detail) } rp := new(http.TransferResp) if e := s.SyncSap(r, rp); e != nil { log.Info(r) return errors.Wrapf(e, "transfer error:%v") } else { if rp.Dest.Status == "S" { log.Printf("transfer success:%s,%s", rp.Data.Proof.EqTransferCode, rp.Data.Proof.ProofCode) return nil } else { return errors.Wrapf(e, "tranfer fail: %s", rp.Dest.MessText) } } }