package service import ( "encoding/json" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" log "github.com/sirupsen/logrus" "kpt.xdmy/apiserver/config" "strconv" "time" "github.com/pkg/errors" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" ) // 定时每分钟读取已完成 未同步的的调拨单,同步到sap // 每个调拨单定时同步只进行一次 func autoTransfer() { bp := []model.BigPartTransfer{} if e := s.d.DB.Raw(`select b.* from bigparttransfer b join pasture p on b.pastureId = p.id where CreatDate >='2022-11-01' and p.vendor = 1 and b.sapStatus =0 and b.statue = 1`).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(bigPartTransferid float64, tx *sqlx.Tx) error { bp := model.BigPartTransfer{} //re := make(map[string]interface{}) //r := s.d.DB.Where("CreatDate >'2022-10-28' ") //if code != "" { // r = r.Where("applycode", code) //} //r = r.Find(&bp, model.BigPartTransfer{Statue: 1}) //if r.Error != nil { // log.Errorf("ManualTransfers find :%v", r.Error) // re["find"] = r.Error // return 0, re //} bigpartlaidByte, err := MapStr(tx, `select * from bigparttransfer where id = ? `, bigPartTransferid) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if bigpartlaidByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartlaidByte, &data) if err != nil { log.Error(err) return errors.New("入库详情转码出错!!!") } bp.ID, _ = strconv.Atoi(data["id"].(string)) t, err := time.Parse("2006-01-02T15:04:05", data["creatDate"].(string)[:len("2006-01-02 15:04:05")]) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err)) } bp.CreatDate = t t1, err := time.Parse("2006-01-02T15:04:05", data["applyDate"].(string)[:len("2006-01-02 15:04:05")]) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err)) } bp.ApplyDate = t1 bp.InPastureID, _ = strconv.Atoi(data["inPastureId"].(string)) bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string)) bp.ApplyCode = data["applyCode"].(string) bp.LaidCode = data["laidCode"].(string) } err = SapTransfer(&bp, tx) if err != nil { return err } return nil } // 调拨接口日志 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, tx *sqlx.Tx) error { // 寄售(零库存)无调拨 r := new(http.TransferReq) parts := []model.PartTransferStr{} 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) //} partsByte, err := GetDataList(tx, `select * from parttransfer where bigid = ? `, []interface{}{bp.ID}) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if partsByte != nil { err := json.Unmarshal(partsByte, &parts) if err != nil { log.Error(err) return errors.New("入库信息转码出错!!!") } } //fmt.Println(bp.LaidCode) //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) //} bigpartlaidByte, err := MapStr(tx, `select * from bigpartlaid where laidCode = ? `, bp.LaidCode) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if bigpartlaidByte != nil { data := make(map[string]interface{}) err := json.Unmarshal(bigpartlaidByte, &data) if err != nil { log.Error(err) return errors.New("入库详情转码出错!!!") } blaid.ID, _ = strconv.Atoi(data["id"].(string)) t, err := time.Parse("2006-01-02T15:04:05", data["storageTime"].(string)[:len("2006-01-02 15:04:05")]) if err != nil { log.Error(err) return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err)) } blaid.StorageTime = t } 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 = "http://192.168.61.117/SAPP0/Common/MM012/TransferStock" r.Dest.Url = fmt.Sprintf("%s/Common/MM012/TransferStock", config.Conf.Http.Routing) r.Data.OrderDate = bp.CreatDate.Format("20060102") r.Data.PostDate = bp.ApplyDate.Format("20060102") r.Data.TransferCode = bp.ApplyCode r.Data.Group = inpa.PurchasingGroup // r.Data.Group = inpa.PurchasingGroup //"B11" for i, v := range parts { detail := new(http.TransferDetail) outStock := new(model.PartRepertory) pslaid := new(model.PartLaidStr) //inw := new(model.Warehouse) c := new(model.Contract) bc := new(model.BigContract) if e := s.d.DB.Where(" id = ?", v.PartRepID).First(outStock).Error; e != nil { return errors.Wrapf(e, "partrepertory query :id=%d", v.PartRepID) } bigpartlaidByte, err := MapStr(tx, `select * from partlaid where bigid = ? and partid = ? `, blaid.ID, v.PartID) if err != nil { log.Error(err) return errors.New("无入库信息!!!") } if bigpartlaidByte != nil { err := json.Unmarshal(bigpartlaidByte, &pslaid) if err != nil { log.Error(err) return errors.New("入库详情转码出错!!!") } } //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("寄售无调拨") } } if v.RowNumber != nil { if v.RowNumber.(float64) > 0 { detail.RowNumber = fmt.Sprintf("%v", v.RowNumber) } else { detail.RowNumber = fmt.Sprintf("%d", (i + 1)) } } else { detail.RowNumber = fmt.Sprintf("%d", (i + 1)) } detail.MaterialCode = v.PartCode.(string) detail.Quantity = v.Amount.(string) detail.Unit = v.Unit.(string) detail.Per = "1" detail.DeliverDate = blaid.StorageTime.Format("20060102") detail.OutFactory = outpa.FactoryCode detail.InFactory = inpa.FactoryCode detail.NetPrice = outStock.Price detail.OutLocation = "1004" detail.InLocation = "1004" r.Data.Detail = append(r.Data.Detail, *detail) _, err = tx.Exec(` update parttransfer set rowNumber = ? where id = ? `, detail.RowNumber, v.ID) if err != nil { log.Error(err) return errors.New("修改调拨行号出错!!!") } } rp := new(http.TransferResp) rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e := s.SyncSapTransfer(r, rp, rbyte); e != nil { log.Info(r) s.AddSapLog(r, e) return errors.Wrapf(e, "transfer error:%v") } else { s.AddSapLog(r, e) if rp.Dest.Status == "S" { log.Printf("transfer success:%s,%s", rp.Data.Proof.EqTransferCode, rp.Data.Proof.ProofCode) _, err = tx.Exec(` update bigparttransfer set ProofCode = ?,sapStatus = 1 where id = ? `, rp.Data.Proof.ProofCode, bp.ID) if err != nil { log.Error(err) //return errors.New("修改调拨状态出错!!!") } return nil } else { return errors.New(fmt.Sprintf("tranfer fail: %s", rp.Dest.MessText)) } } }