kptcwq911 vor 2 Jahren
Ursprung
Commit
a7a2afceee

+ 2 - 0
apiserver/model/buydetail.go

@@ -23,6 +23,8 @@ type BigBuyDetail struct {
 	SapStatus    int       `gorm:"column:sapStatus"`
 	SrmStatus    int       `gorm:"column:srmStatus"`
 	SrmNumber    string    `gorm:"column:srmNumber"`
+	SapError     string    `gorm:"column:sapError"`
+	SrmError     string    `gorm:"column:srmError"`
 }
 type BuyDetail struct {
 	// 采购订单明细

+ 0 - 34
apiserver/routers/job.go

@@ -1,34 +0,0 @@
-package routers
-
-import (
-	// "github.com/robfig/cron"
-
-	"time"
-
-	"github.com/robfig/cron/v3"
-	"kpt.xdmy/apiserver/service"
-	"kpt.xdmy/pkg/log"
-)
-
-func SapJobInit(s *service.Service) {
-	svc = s
-	c := cron.New()
-	// 02:00
-	if _, e := c.AddFunc("00 00 02 * * *", jobMaterial); e != nil {
-		log.Error("jobMaterial", e)
-	}
-	if _, e := c.AddFunc("00 00 02 * * *", jobTransfer); e != nil {
-		log.Error("transfer", e)
-	}
-	c.Start()
-
-}
-
-func jobMaterial() {
-	t := time.Now()
-	var code string
-	svc.SapMaterial(t, code)
-}
-func jobTransfer() {
-	
-}

+ 1 - 0
apiserver/routers/router.go

@@ -63,6 +63,7 @@ func InitRouter() *gin.Engine {
 
 	{
 
+		apiV1.POST("sap/transfer/send", ManaulSapOrder)
 		apiV1.POST("sap/transfer/send", ManualTransfer)
 		apiV1.POST("part/eas/batch", EasBatch)
 		apiV1.POST("/sap/proof/off/list", ProofOffList)

+ 9 - 0
apiserver/routers/sap.go

@@ -38,3 +38,12 @@ func ManualTransfer(c *gin.Context) {
 	count, e := service.ManualTransfer(code)
 	c.JSON(200, map[string]interface{}{"count": count, "error": e})
 }
+
+func ManaulSapOrder(c *gin.Context) {
+	code := c.PostForm("code")
+	if e := service.ManaulSapOrderJob(code); e != nil {
+		c.JSON(200, "success")
+	} else {
+		c.JSON(200, "fail")
+	}
+}

+ 20 - 12
apiserver/service/job.go

@@ -1,26 +1,34 @@
 package service
 
 import (
+	"fmt"
 	"time"
-
-	"github.com/robfig/cron/v3"
-	log "github.com/sirupsen/logrus"
 )
 
 func SapJobInit() {
-	c := cron.New()
+	fmt.Println("================================================================")
+	// test()
+	// c := cron.New()
 	// 02:00
-	if _, e := c.AddFunc("5 5 5 * * ", jobMaterial); e != nil {
-		log.Error("jobMaterial", e)
-	}
-	if _, e := c.AddFunc("1 * * * *", jobTransfer); e != nil {
-		// if _, e := c.AddFunc("@every 1m", jobTransfer); e != nil {
-		log.Error("transfer ", e)
-	}
-	c.Start()
+	// if _, e := c.AddFunc("5 5 5 * * ", jobMaterial); e != nil {
+	// 	log.Error("jobMaterial", e)
+	// }
+	// if _, e := c.AddFunc("1 * * * *", jobTransfer); e != nil {
+	// 	// if _, e := c.AddFunc("@every 1m", jobTransfer); e != nil {
+	// 	log.Error("transfer ", e)
+	// }
+
+	// if _, e := c.AddFunc("@every 1m", test); e != nil {
+	// 	logrus.Error(e)
+	// }
+
+	// c.Start()
 
 }
 
+func test() {
+	fmt.Println("================================================================")
+}
 func jobMaterial() {
 	t := time.Now()
 	var code string

+ 0 - 0
apiserver/service/part.go → apiserver/service/loadData.go


+ 73 - 27
apiserver/service/order.go

@@ -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) {

+ 1 - 3
client/main.go

@@ -1,14 +1,12 @@
 package main
 
 import (
-	"time"
-
 	"kpt.xdmy/test"
 )
 
 func main() {
 	test.Init()
 	test.Test()
-	time.Sleep(time.Minute * 3)
+	// time.Sleep(time.Minute * 3)
 	// test.EasBatch()
 }

+ 8 - 1
doc/sql/a.sql

@@ -29,7 +29,14 @@ alter table BigPartUse
 add column ProofCode  varchar(50) not null default '' 
 alter table BigPartrefund
 add column ProofCode  varchar(50) not null default '' 
-alter  table  BigBuyDetail
+alter  table  BigBuyDetail  add column srmStatus integer  default  0;
+alter  table  BigBuyDetail  add column srmNumber varchar(50)  default  '';
+alter  table  BigBuyDetail  add column sapError varchar(100)  default  '';
+alter  table  BigBuyDetail  add column srmError varchar(100)  default  '';
+srmStatus
+srmNumber
+sapError
+srmError
 add column SapStatus  int not null default 0
 add column sapNumber  varchar(50) not null default ''
 

+ 1 - 0
doc/sql/s.sql

@@ -24,6 +24,7 @@ or sqlname ='updatePartRepertoryTK'  --(还需要前端 修改参数)
 select sqlname,sqlstr,params from apisql where  sqlname ='getPartsListLY';
 select sqlname,sqlstr,params from apisql where  sqlname ='getPartsListLY_m'
 
+select sqlname,sqlstr,params from apisql where  sqlname ="insertBigPartTransfer";
 select sqlname,sqlstr,params from apisql where  sqlname ="getWarehouselistDB";
 select sqlname,sqlstr,params from apisql where  sqlname ="getBigpartapplyList";
 select sqlname,sqlstr,params from apisql where  sqlname ="updatePartapplyuseAmount_m";

+ 1 - 1
doc/sql/test.md

@@ -15,7 +15,7 @@
 insertBigPartTransfer;
 BigPartTransfer
 5. 申购
-新增采购组(未修改)
+新增采购组(未修改)
 6. 合同新增备件
 增加税码
 后端:insertContractV2,taxcode

+ 2 - 1
test/test.go

@@ -4,6 +4,7 @@ import (
 	// "flag"
 
 	"flag"
+	"fmt"
 
 	"github.com/gin-gonic/gin"
 	log "github.com/sirupsen/logrus"
@@ -30,6 +31,7 @@ func Test() {
 	// Xorm()
 }
 func TestJob() {
+	fmt.Println("================================================================")
 	service.SapJobInit()
 }
 func Init() {
@@ -45,7 +47,6 @@ func Init() {
 	config.Init(path)
 	s = service.New(config.Conf)
 
-	// routers.SapJobInit(svc)
 	// RouterInit()
 	// api.ApiInit(svc)
 	// routers.Init(svc)