123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package sap
- import (
- "bytes"
- "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/Anderson-Lu/gofasion/gofasion"
- "github.com/astaxie/beego/logs"
- "github.com/gin-gonic/gin"
- "github.com/xormplus/xorm"
- )
- func SyncMaterialOutbound() error {
- 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 err
- }
- materialOutbound(pastureinfo.Pastureid, time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
- return nil
- }
- func SyncSapBar(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- start := fsion.Get("startTime").ValueStr()
- end := fsion.Get("endTime").ValueStr()
- pastureId := fsion.Get("pastureId").ValueStr()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- pastureinfo := new(udPastureInfo)
- err := tx.SQL(`select * from pasture where pastureid = ?`, pastureId).GetFirst(pastureinfo).Error
- if err != nil {
- logs.Error(err)
- return
- }
- // year := time.Now().Year()
- data := `{
- "DATA": {
- "DIDAT": "%s",
- "DITIM": "%s",
- "WERKS": "%s"
- },
- "DEST": {
- "DEST_ID": "TMRWATCH",
- "BUSS_TP": "CO005"
- }
- }`
- startTime, _ := time.ParseInLocation("2006-01-02", start, time.Local)
- endTime, _ := time.ParseInLocation("2006-01-02", end, time.Local)
- url := "http://192.168.61.117/SAPP0/Feed/CO005/QueryCowsheds"
- data = fmt.Sprintf(data, startTime.Format("20060102"), endTime.Format("20060102"), pastureinfo.Werks)
- respmap, _ := postPush(url, data, 3, tx, pastureId)
- 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,class,classcode,cattle,cattlecode,sapcode)
- values(?,?,?,(select max(sort)+1 from bar b where b.pastureid = ? ),
- (select distName from dist where distCode = ? and distType = '牛舍类型' ),?,
- (select distName from dist where distCode = ? and distType = '牛群类别'),?,?)
- ON DUPLICATE KEY UPDATE bname = ? ,
- class = (select distName from dist where distCode = ? and distType = '牛舍类型' ) ,classcode = ?,
- cattle = (select distName from dist where distCode = ? and distType = '牛群类别' ) ,cattlecode = ?
- `,
- pastureId, barMap["CHSNO"], fmt.Sprintf("%v_sap", barMap["CHSTX"]), pastureId, barMap["CHSTY"], barMap["CHSTY"],
- barMap["FCWTS"], barMap["FCWTS"], barMap["CHSNO"],
- fmt.Sprintf("%v_sap", barMap["CHSTX"]), barMap["CHSTY"], barMap["CHSTY"], barMap["FCWTS"], barMap["FCWTS"]).Execute()
- if err != nil {
- logs.Error(err)
- return
- }
- }
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- type udPastureInfo struct {
- Werks string `xorm:"werks"`
- Pastureid string `xorm:"pastureid"`
- }
- func MaterialOutbound(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- pastureId := fsion.Get("pastureid").ValueStr()
- date := fsion.Get("date").ValueStr()
- materialOutbound(pastureId, date)
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func materialOutbound(pastureId, date string) error {
- tx := restful.Engine.NewSession()
- defer tx.Close()
- pastureinfo := new(udPastureInfo)
- err := tx.SQL(`select * from pasture where pastureid = ?`, pastureId).GetFirst(pastureinfo).Error
- if err != nil {
- logs.Error(err)
- return err
- }
- dataList, err := tx.SQL(` SELECT
- tem.sort ZEILE,
- tem.feedname MAKTX,
- ifnull(ROUND(if(tem.lweight<0,0,tem.lweight),(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid =? )),0)lweight,
- ifnull(ROUND(tem.actualweightminus,(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid = ?)),0) MENGE
- ,tem.bname CHSTX,tem.sapcode CHSNO,tem.cattle FCWTS,tem.MATNR,tem.sapGoods LGORT
- FROM (
-
-
-
- (SELECT
- d2.type,
- d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
- SUM(
- d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid and ftd2.date= ftp.date ))
- )lweight,
- d1.sort sort,
- SUM(
- if ( (SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date) > 0 ,
- d2.actualweightminus/(SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid and ftd2.date= ftp.date )),
-
- d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetail ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid ))
- )
- )actualweightminus,b.sort as d2sort,b.bname,b.sapcode, b.cattlecode ,f.sapCode MATNR,f.sapGoods ,(select cattle from feedtemplet where id = d.tempid) cattle
-
- FROM
- downloadedplan d
-
- JOIN downloadplandtl2 d2
- ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
- JOIN downloadplandtl1 d1
- ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
- LEFT JOIN ftdetaildate ftd ON ftd.pastureid =d1.pastureid AND ftd.ftid = d1.fid AND d1.feedcode = '-1' AND ftd.date = d.mydate AND ftd.version = d.tversion
- left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
- left join feed f on f.id = d1.fid
- left join feedtempletdate ftp on ftp.id = d.tempid AND ftp.date = ftd.date
-
- WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode != -1 and b.sapCode is not null and f.sapCode is not null
- GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
- HAVING d2.type = 0 order by b.sort,d2.fbarid)
-
- UNION
-
- (SELECT
- d2.type,
- d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
- SUM(
- d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid and ftd2.date= ftp.date ))
- )lweight,
- d1.sort sort,
- SUM(
- if ( (SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date) > 0 ,
- d2.actualweightminus/(SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid and ftd2.date= ftp.date )),
-
- d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
- d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetail ftd2 WHERE ftd2.pastureid =d1.pastureid AND ftd2.ftid = d1.fid ))
- )
- )actualweightminus,b.sort as d2sort,b.bname,b.sapcode, b.cattlecode ,f.sapCode MATNR,f.sapGoods ,(select cattle from feedtemplet where id = d.tempid) cattle
-
- FROM
- downloadedplan d
- JOIN downloadplandtl2 d2
- ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
- JOIN downloadplandtl1 d1
- ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
- -- LEFT JOIN ftdetaildate ftd ON ftd.pastureid =d1.pastureid AND ftd.ftid = d1.fid AND d1.feedcode = '-1' AND ftd.date = d.mydate AND ftd.version = d.tversion
- left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
-
- left join feedtempletdate ftp on ftp.id = d1.fid and ftp.date = ?
- left join ftdetaildate ftd on ftd.ftid = ftp.id and ftd.date = ?
- left join feed f on f.id = ftd.fid
- WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode = -1 and b.sapCode is not null and f.sapCode is not null
- GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
- HAVING d2.type = 0 order by b.sort,d2.fbarid)
-
- ) tem
-
- ORDER BY tem.d2sort,tem.feedname `, pastureId, pastureId, pastureId, date, date, date, pastureId, date).Query().List()
- if err != nil {
- logs.Error(err)
- return err
- }
- data := `{
- "DEST": {
- "DEST_ID": "DFEED",
- "BUSS_TP": "MM016"
- },
- "DATA": {
- "BUDAT": "%s",
- "WERKS": "%s",
- "ITEMS": %s
- }
- }`
- databyte, _ := json.Marshal(dataList)
- now, _ := time.ParseInLocation("2006-01-02", date, time.Local)
- data = fmt.Sprintf(data, now.Format("20060102"), pastureinfo.Werks, string(databyte))
- url := "http://192.168.61.117/SAPP0/Feed/MM016/StockOut"
- postPush(url, data, 0, tx, pastureId)
- return nil
- // appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- func postPush(url, data string, msgtype int, tx *xorm.Session, pastureId string) (interface{}, bool) {
- 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.SetBasicAuth("zinf_TMR", "$C2Cj$\\Uv#")
- req.Header.Set("Content-Type", "application/json")
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- logs.Error(err)
- // return nil
- }
- fmt.Println(resp.Body)
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- var respData map[string]interface{}
- tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
- values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, url)
- err = json.Unmarshal(body, &respData)
- if err != nil {
- logs.Error(err)
- // return nil
- }
- if _, ok := respData["DATA"]; ok {
- if msgtype == 3 {
- if _, ok := respData["ZTCO_001"]; !ok {
- return respData["DATA"], false
- }
- return respData["DATA"], true
- }
- if _, ok := respData["DATA"]; !ok {
- return respData["DATA"], false
- }
- return respData["DATA"], true
- } else {
- return respData["DATA"], false
- }
- }
- func SyncSapFeed(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- start := fsion.Get("startTime").ValueStr()
- end := fsion.Get("endTime").ValueStr()
- mtart := fsion.Get("mtart").ValueStr()
- pastureId := fsion.Get("pastureId").ValueStr()
- tx := restful.Engine.NewSession()
- defer tx.Close()
- data := `{
- "DEST": {
- "DEST_ID": "TMRWATCH",
- "BUSS_TP": "MM002"
- },
- "DATA": {
- "BUDAT_B": "%s",
- "BUDAT_E": "%s",
- "TMTART": {
- "MTART": "%s"
-
- },
- "TMATKL": []
- }
- }`
- startTime, _ := time.ParseInLocation("2006-01-02", start, time.Local)
- endTime, _ := time.ParseInLocation("2006-01-02", end, time.Local)
- data = fmt.Sprintf(data, startTime.Format("20060102"), endTime.Format("20060102"), mtart)
- // "https://app.modernfarming.cn:7443/sap/Common/MM002/QueryMaterial/"
- // http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial
- url := "http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial"
- respmap, success := postPush(url, data, 2, tx, pastureId)
- // var a interface{} = nil
- fmt.Println(respmap, success)
- if success == false {
- appG.Response(http.StatusOK, e.SUCCESS, true)
- } else {
- for _, item := range respmap.(map[string]interface{})["TMARA"].([]interface{}) {
- sapMap := item.(map[string]interface{})
- // fmt.Println(item)
- _, 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
- }
- }
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- // return
- }
|