sap.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package sap
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  10. "tmr-watch/conf/setting"
  11. "tmr-watch/http/handle/restful"
  12. "tmr-watch/pkg/app"
  13. "tmr-watch/pkg/e"
  14. "github.com/astaxie/beego/logs"
  15. "github.com/gin-gonic/gin"
  16. )
  17. func SyncSapBar(ctx context.Context) {
  18. tx := restful.Engine.NewSession()
  19. defer tx.Close()
  20. year := time.Now().Year()
  21. data := `{
  22. "DEST": {
  23. "DEST_ID": "TMRWATCH",
  24. "BUSS_TP": "CO005"
  25. },
  26. "DATA": {
  27. "DIDAT": "%d0101",
  28. "DITIM": "%d1231",
  29. "WERKS": "M001",
  30. "TCHSNO": [ ],
  31. "TFCWTS": [ ],
  32. "TCHSTY": [ ]
  33. }
  34. }`
  35. url := "http://192.168.61.117/SAPP0/Feed/CO005/QueryCowsheds"
  36. data = fmt.Sprintf(data, year, year)
  37. respmap := postPush(url, data)
  38. if respmap != nil {
  39. if _, ok := respmap.(map[string]interface{})["ZTCO_001"]; ok {
  40. for _, item := range respmap.(map[string]interface{})["ZTCO_001"].([]interface{}) {
  41. barMap := item.(map[string]interface{})
  42. _, err := tx.SQL(` insert into bar(pastureid,bcode,bname,sort,sapid,class,classid,scopeherd,scopeherdid)
  43. 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 = ? ),
  44. (select id from scopeherd where code = ? ))
  45. ON DUPLICATE KEY UPDATE bname = ? ,bcode = ? `, 1, barMap["CHSTX"], barMap["CHSTX"], 1, barMap["CHSNO"], 1, barMap["CHSTX"], 1, barMap["CHSTX"],
  46. barMap["FCWTS"], barMap["FCWTS"], barMap["CHSTX"], barMap["CHSTX"]).Execute()
  47. if err != nil {
  48. logs.Error(err)
  49. return
  50. }
  51. }
  52. }
  53. }
  54. }
  55. type udPastureInfo struct {
  56. Token string `xorm:"token"`
  57. Pastureid string `xorm:"pastureid"`
  58. }
  59. func MaterialOutbound(c *gin.Context) {
  60. appG := app.Gin{C: c}
  61. tx := restful.Engine.NewSession()
  62. defer tx.Close()
  63. pastureinfo := new(udPastureInfo)
  64. err := tx.SQL(`select column_default as pastureid from information_schema.COLUMNS
  65. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  66. if err != nil {
  67. logs.Error(err)
  68. return
  69. }
  70. 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
  71. ,bc.code as FCWTS
  72. from downloadedplan d
  73. JOIN downloadplandtl2 d2 on d2.pid in ( select id from downloadedplan where pid = d.pid and mydate = d.mydate) and d2.type = 0
  74. join downloadplandtl1 d1 on d1.pid = d.id
  75. -- join feed f on f.feedcode = d1.feedcode and f.pastureid = d.pastureid
  76. join bar b on b.id = d2.fbarid and b.pastureid = d.pastureid
  77. left join barclass bc on bc.id = b.classid
  78. where d.mydate = ? and d.lpplantype <> 2 and d.pastureid = ?
  79. order by d.id `, "2022-06-24", pastureinfo.Pastureid).Query().List()
  80. if err != nil {
  81. logs.Error(err)
  82. appG.Response(http.StatusInternalServerError, e.ERROR, false)
  83. return
  84. }
  85. data := `{
  86. "DEST": {
  87. "DEST_ID": "DFEED",
  88. "BUSS_TP": "MM016"
  89. },
  90. "DATA": {
  91. "BUDAT": "%s",
  92. "WERKS": "%s",
  93. "ITEMS": %s
  94. }
  95. }`
  96. databyte, _ := json.Marshal(dataList)
  97. data = fmt.Sprintf(data, time.Now().Format("20060102"), "0001", string(databyte))
  98. url := "http://192.168.61.117/SAPP0/Feed/MM016/StockOut"
  99. postPush(url, data)
  100. appG.Response(http.StatusOK, e.SUCCESS, true)
  101. }
  102. func postPush(url, data string) interface{} {
  103. var jsonStr = []byte(data)
  104. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  105. if err != nil {
  106. logs.Error(err)
  107. return nil
  108. }
  109. req.SetBasicAuth("zinf_TMR", "zinf_TMR@1234")
  110. req.Header.Set("Content-Type", "application/json")
  111. client := &http.Client{}
  112. resp, err := client.Do(req)
  113. if err != nil {
  114. logs.Error(err)
  115. return nil
  116. }
  117. defer resp.Body.Close()
  118. body, _ := ioutil.ReadAll(resp.Body)
  119. var respData map[string]interface{}
  120. err = json.Unmarshal(body, &respData)
  121. if err != nil {
  122. logs.Error(err)
  123. return nil
  124. }
  125. if _, ok := respData["DATA"]; ok {
  126. return respData["DATA"]
  127. } else {
  128. return nil
  129. }
  130. }
  131. func SyncSapFeed(ctx context.Context) {
  132. tx := restful.Engine.NewSession()
  133. defer tx.Close()
  134. data := `{
  135. "DEST": {
  136. "DEST_ID": "TMRWATCH",
  137. "BUSS_TP": "MM002"
  138. },
  139. "DATA": {
  140. "BUDAT_B": "20220819",
  141. "BUDAT_E": "20220819",
  142. "TMTART": {
  143. "MATNR": "XD01"
  144. },
  145. "TMATKL": []
  146. }
  147. }`
  148. url := "http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial/"
  149. respmap := postPush(url, data)
  150. for _, item := range respmap.(map[string]interface{})["TMARA"].([]interface{}) {
  151. sapMap := item.(map[string]interface{})
  152. fmt.Println(sapMap)
  153. _, err := tx.SQL(` replace into feed_sap(MATNR,MTART,MATKL,MAKTX,MEINS,UMREZ,MEINH,UMREN,ZMINC,ZGUIG,LVORM,LAEDA)values(?,?,?,?,?,?,?,?,?,?,?,?)`,
  154. sapMap["MATNR"], sapMap["MTART"], sapMap["MATKL"], sapMap["MAKTX"], sapMap["MEINS"], sapMap["UMREZ"], sapMap["MEINH"],
  155. sapMap["UMREN"], sapMap["ZMINC"], sapMap["ZGUIG"], sapMap["LVORM"], sapMap["LAEDA"]).Execute()
  156. if err != nil {
  157. logs.Error(err)
  158. return
  159. }
  160. }
  161. }