| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | 
							- package sap
 
- import (
 
- 	"bytes"
 
- 	"context"
 
- 	"encoding/json"
 
- 	"fmt"
 
- 	"io/ioutil"
 
- 	"net/http"
 
- 	"time"
 
- 	"tmr-watch/conf/setting"
 
- 	"tmr-watch/http/handle/restful"
 
- 	"tmr-watch/pkg/app"
 
- 	"tmr-watch/pkg/e"
 
- 	"github.com/astaxie/beego/logs"
 
- 	"github.com/gin-gonic/gin"
 
- )
 
- func SyncSapBar(ctx context.Context) {
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	year := time.Now().Year()
 
- 	data := `{
 
- 		"DEST": {
 
- 		  "DEST_ID": "TMRWATCH",
 
- 		  "BUSS_TP": "CO005"
 
- 		},
 
- 		"DATA": {
 
- 		  "DIDAT": "%d0101",
 
- 		  "DITIM": "%d1231",
 
- 		  "WERKS": "M001",
 
- 		  "TCHSNO": [ ],
 
- 		  "TFCWTS": [ ],
 
- 		  "TCHSTY": [ ]
 
- 		}
 
- 	}`
 
- 	url := "http://192.168.61.117/SAPP0/Feed/CO005/QueryCowsheds"
 
- 	data = fmt.Sprintf(data, year, year)
 
- 	respmap := postPush(url, data)
 
- 	if respmap != nil {
 
- 		if _, ok := respmap.(map[string]interface{})["ZTCO_001"]; ok {
 
- 			for _, item := range respmap.(map[string]interface{})["ZTCO_001"].([]interface{}) {
 
- 				barMap := item.(map[string]interface{})
 
- 				_, err := tx.SQL(` insert into bar(pastureid,bcode,bname,sort,sapid,class,classid,scopeherd,scopeherdid)
 
- 				values(?,?,?,(select max(sort)+1  from bar b where b.pastureid = ? ),?,( select name from barclass where pastureid = ? and code = ?),( select id from barclass where pastureid = ? and code = ?),(select name from scopeherd where code = ? ),
 
- 				(select id from scopeherd where code = ? ))
 
- 				ON DUPLICATE KEY UPDATE bname = ? ,bcode = ? `, 1, barMap["CHSTX"], barMap["CHSTX"], 1, barMap["CHSNO"], 1, barMap["CHSTX"], 1, barMap["CHSTX"],
 
- 					barMap["FCWTS"], barMap["FCWTS"], barMap["CHSTX"], barMap["CHSTX"]).Execute()
 
- 				if err != nil {
 
- 					logs.Error(err)
 
- 					return
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- }
 
- type udPastureInfo struct {
 
- 	Token     string `xorm:"token"`
 
- 	Pastureid string `xorm:"pastureid"`
 
- }
 
- func MaterialOutbound(c *gin.Context) {
 
- 	appG := app.Gin{C: c}
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	pastureinfo := new(udPastureInfo)
 
- 	err := tx.SQL(`select column_default  as pastureid   from    information_schema.COLUMNS
 
- 	WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
 
- 	if err != nil {
 
- 		logs.Error(err)
 
- 		return
 
- 	}
 
- 	dataList, err := tx.SQL(`	select d1.fname as MAKTX,d2.fname as CHSTX, round( d1.actualweightminus * (d2.lweight / (select sum(lweight) from  downloadplandtl2  where pid = d2.pid )),2) as MENGE,b.sapid 	
 
- 	,bc.code as FCWTS
 
- 	from  downloadedplan d  
 
-  JOIN downloadplandtl2 d2 on d2.pid in ( select id from downloadedplan where pid = d.pid and mydate = d.mydate) and d2.type = 0    
 
- 	join downloadplandtl1 d1 on  d1.pid = d.id  
 
- 	
 
- -- 	join feed  f on f.feedcode = d1.feedcode and f.pastureid = d.pastureid
 
- join bar  b on b.id = d2.fbarid  and b.pastureid = d.pastureid
 
- left join barclass bc on bc.id = b.classid 
 
- 	where d.mydate = ? and d.lpplantype <> 2 	 and d.pastureid = ?
 
-  order by d.id `, "2022-06-24", pastureinfo.Pastureid).Query().List()
 
- 	if err != nil {
 
- 		logs.Error(err)
 
- 		appG.Response(http.StatusInternalServerError, e.ERROR, false)
 
- 		return
 
- 	}
 
- 	data := `{
 
- 		"DEST": {
 
- 		  "DEST_ID": "DFEED",
 
- 		  "BUSS_TP": "MM016"
 
- 		},
 
- 		"DATA": {
 
- 		  "BUDAT": "%s",
 
- 		  "WERKS": "%s",
 
- 		  "ITEMS": %s
 
- 		}
 
- 	  }`
 
- 	databyte, _ := json.Marshal(dataList)
 
- 	data = fmt.Sprintf(data, time.Now().Format("20060102"), "0001", string(databyte))
 
- 	url := "http://192.168.61.117/SAPP0/Feed/MM016/StockOut"
 
- 	postPush(url, data)
 
- 	appG.Response(http.StatusOK, e.SUCCESS, true)
 
- }
 
- func postPush(url, data string) interface{} {
 
- 	var jsonStr = []byte(data)
 
- 	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
 
- 	if err != nil {
 
- 		logs.Error(err)
 
- 		return nil
 
- 	}
 
- 	req.SetBasicAuth("zinf_TMR", "zinf_TMR@1234")
 
- 	req.Header.Set("Content-Type", "application/json")
 
- 	client := &http.Client{}
 
- 	resp, err := client.Do(req)
 
- 	if err != nil {
 
- 		logs.Error(err)
 
- 		return nil
 
- 	}
 
- 	defer resp.Body.Close()
 
- 	body, _ := ioutil.ReadAll(resp.Body)
 
- 	var respData map[string]interface{}
 
- 	err = json.Unmarshal(body, &respData)
 
- 	if err != nil {
 
- 		logs.Error(err)
 
- 		return nil
 
- 	}
 
- 	if _, ok := respData["DATA"]; ok {
 
- 		return respData["DATA"]
 
- 	} else {
 
- 		return nil
 
- 	}
 
- }
 
- func SyncSapFeed(ctx context.Context) {
 
- 	tx := restful.Engine.NewSession()
 
- 	defer tx.Close()
 
- 	data := `{
 
- 		"DEST": {
 
- 			"DEST_ID": "TMRWATCH",
 
- 			"BUSS_TP": "MM002"
 
- 		},
 
- 		"DATA": {
 
- 			"BUDAT_B": "20220819",
 
- 			"BUDAT_E": "20220819",
 
- 			"TMTART": {
 
- 				"MATNR": "XD01"
 
- 				
 
- 			},
 
- 			"TMATKL": []
 
- 		}
 
- 	}`
 
- 	url := "http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial/"
 
- 	respmap := postPush(url, data)
 
- 	for _, item := range respmap.(map[string]interface{})["TMARA"].([]interface{}) {
 
- 		sapMap := item.(map[string]interface{})
 
- 		fmt.Println(sapMap)
 
- 		_, err := tx.SQL(` replace into  feed_sap(MATNR,MTART,MATKL,MAKTX,MEINS,UMREZ,MEINH,UMREN,ZMINC,ZGUIG,LVORM,LAEDA)values(?,?,?,?,?,?,?,?,?,?,?,?)`,
 
- 			sapMap["MATNR"], sapMap["MTART"], sapMap["MATKL"], sapMap["MAKTX"], sapMap["MEINS"], sapMap["UMREZ"], sapMap["MEINH"],
 
- 			sapMap["UMREN"], sapMap["ZMINC"], sapMap["ZGUIG"], sapMap["LVORM"], sapMap["LAEDA"]).Execute()
 
- 		if err != nil {
 
- 			logs.Error(err)
 
- 			return
 
- 		}
 
- 	}
 
- }
 
 
  |