|
@@ -1,11 +1,12 @@
|
|
|
package service
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
|
|
- "github.com/k0kubun/pp/v3"
|
|
|
+ "github.com/pkg/errors"
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
|
@@ -51,11 +52,53 @@ func (s *Service) PurchaseOrder(p interface{}) {
|
|
|
}
|
|
|
}
|
|
|
for _, v := range bdetails {
|
|
|
- s.SapOrder(&v)
|
|
|
+ SapOrder(&v)
|
|
|
+ SrmOrder(&v)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (s *Service) SapOrder(p *model.BigBuyDetail) {
|
|
|
+func AutoSapOrderJob() {
|
|
|
+ orders := []model.BigBuyDetail{}
|
|
|
+ if e := s.d.DB.Where("sapStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{}).Error; e != nil {
|
|
|
+ log.Errorf("AutoSapOrderJob error: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range orders {
|
|
|
+ e, sapNumber := SapOrder(&v)
|
|
|
+ up := model.BigBuyDetail{SapNumber: sapNumber}
|
|
|
+ if e != nil {
|
|
|
+ up.SapStatus = 2
|
|
|
+ up.SapError = e.Error()
|
|
|
+ } else {
|
|
|
+ up.SapStatus = 1
|
|
|
+ }
|
|
|
+ if e := s.d.DB.Model(&v).Where("buyeCode =?", v.BuyeCode).Updates(up); e != nil {
|
|
|
+ log.Errorf("BigBuyDetail update error: %v", e)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+func ManaulSapOrderJob(buyeCode string) (err error) {
|
|
|
+ order := new(model.BigBuyDetail)
|
|
|
+ if e := s.d.DB.First(order, model.BigBuyDetail{SapStatus: 2, BuyeCode: buyeCode}).Error; e != nil {
|
|
|
+ err = errors.Wrapf(e, "ManaulSapOrderJob BigBuyDetail query ")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e, sapNumber := SapOrder(order)
|
|
|
+ up := model.BigBuyDetail{SapNumber: sapNumber}
|
|
|
+ if e != nil {
|
|
|
+ up.SapStatus = 2
|
|
|
+ up.SapError = e.Error()
|
|
|
+ err = errors.Wrapf(e, "ManaulSapOrder sap ")
|
|
|
+ } else {
|
|
|
+ up.SapStatus = 1
|
|
|
+ }
|
|
|
+ if e := s.d.DB.Model(order).Where("buyeCode=?", buyeCode).Updates(up); e != nil {
|
|
|
+ log.Errorf("BigBuyDetail update error: %v", e)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func SapOrder(p *model.BigBuyDetail) (error, string) {
|
|
|
r := new(http.SapOrderReq)
|
|
|
rp := new(http.OrderResp)
|
|
|
parts := []model.BuyDetail{}
|
|
@@ -68,16 +111,13 @@ func (s *Service) SapOrder(p *model.BigBuyDetail) {
|
|
|
r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
|
|
|
|
|
|
if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
|
|
|
- log.Error(e)
|
|
|
- return
|
|
|
+ return errors.Wrapf(e, "provider query:id=%d", p.ProviderID), ""
|
|
|
}
|
|
|
if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
|
|
|
- log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
|
|
|
- return
|
|
|
+ return errors.Wrapf(e, "pasture query:id=%d", p.PastureID), ""
|
|
|
}
|
|
|
if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
|
|
|
- log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
|
|
|
- return
|
|
|
+ return errors.Wrapf(e, "buydetail query:bigid=%d", p.ID), ""
|
|
|
}
|
|
|
r.Data.IsCancle = ""
|
|
|
r.Data.PayCondition = ""
|
|
@@ -97,12 +137,10 @@ func (s *Service) SapOrder(p *model.BigBuyDetail) {
|
|
|
bc := new(model.BigContract)
|
|
|
|
|
|
if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
|
|
|
- log.Errorf("order detail error:%v", e)
|
|
|
- return
|
|
|
+ return errors.Wrapf(e, "contract query :id=%d", v.ContractID), ""
|
|
|
}
|
|
|
if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
|
|
|
- log.Errorf("order detail error:%v", e)
|
|
|
- return
|
|
|
+ return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID), ""
|
|
|
}
|
|
|
|
|
|
detail.RowNumber = fmt.Sprintf("%d", i+1)
|
|
@@ -123,36 +161,44 @@ func (s *Service) SapOrder(p *model.BigBuyDetail) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if e := s.SyncSap(r, rp); e != nil {
|
|
|
- log.Errorf("sap buy order error: %v", e)
|
|
|
- p.SapStatus = 3
|
|
|
+ if e := s.SyncSap(r, rp); e != nil && rp.Dest.Status == "S" {
|
|
|
+ log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
|
|
|
+ return nil, rp.Data.SapNumber
|
|
|
} else {
|
|
|
- if rp.Dest.Status == "S" {
|
|
|
- log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
|
|
|
- p.SapStatus = 1
|
|
|
- p.SapNumber = rp.Data.SapNumber
|
|
|
+ saplog := new(model.SapLog)
|
|
|
+ if param, e := json.Marshal(r); e != nil {
|
|
|
+ log.Errorf("sap order param marshal:%v", e)
|
|
|
} else {
|
|
|
- log.Warnf("buy order fail :msg=%s,r=%v", rp.Data.MsgText, r)
|
|
|
- p.SapStatus = 2
|
|
|
- pp.Print(r)
|
|
|
+ saplog.Param = string(param)
|
|
|
+ saplog.Name = "sap-order"
|
|
|
+ s.d.DB.Create(saplog)
|
|
|
}
|
|
|
- }
|
|
|
- if p.SapStatus == 1 {
|
|
|
- if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SapStatus: p.SapStatus, SapNumber: p.SapNumber}).Error; e != nil {
|
|
|
- log.Errorf("update big buy detail error: %v", e)
|
|
|
+ if e == nil {
|
|
|
+ return errors.Errorf("sap order fail:%s", rp.Data.MsgText), ""
|
|
|
+ } else {
|
|
|
+ return errors.Wrapf(e, "sap order error:%v", e), ""
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
func AutoSrmOrderJob() {
|
|
|
orders := []model.BigBuyDetail{}
|
|
|
if e := s.d.DB.Where("srmStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
|
|
|
log.Errorf("AutoSrmOrderJob error: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range orders {
|
|
|
+ SrmOrder(&v)
|
|
|
}
|
|
|
}
|
|
|
func ManaulSrmOrderJob() {
|
|
|
orders := []model.BigBuyDetail{}
|
|
|
if e := s.d.DB.Where("srmStatus=2").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
|
|
|
log.Errorf("ManaulSrmOrderJob error: %v", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range orders {
|
|
|
+ SrmOrder(&v)
|
|
|
}
|
|
|
}
|
|
|
func SrmOrder(p *model.BigBuyDetail) {
|