udservice.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package gm
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "tmr-watch/conf/setting"
  11. "tmr-watch/http/handle/restful"
  12. "tmr-watch/pkg/logging"
  13. "github.com/astaxie/beego/logs"
  14. "github.com/xormplus/xorm"
  15. )
  16. // "../../../pkg/setting"
  17. // "../../../routers/restful"
  18. // http://172.16.86.95:8080/copartner_downloads?equipmentId=48ffa4de-7870-4b18-a052-9f8d3a46089a&farmId=ef275fb06fd847d88422517773e5a616&method=downloadpen
  19. func GmUdSync(pastureId, farmId string) {
  20. UdMaterialIssuePush(pastureId, farmId, "")
  21. UdFeedtempletPush(pastureId, farmId, "")
  22. UdUtirPush(pastureId, farmId, "")
  23. UdBarFeedRemainPush(pastureId, farmId, "")
  24. UdBarSync(pastureId, farmId)
  25. UdFeedSync(pastureId, farmId)
  26. }
  27. func UdBarSync(pastureId, farmId string) {
  28. url := fmt.Sprintf("http://172.16.86.95:8080/copartner_downloads?equipmentId=48ffa4de-7870-4b18-a052-9f8d3a46089a&farmId=%s&method=downloadpen", farmId)
  29. barByte := getPull(url)
  30. fmt.Println(string(barByte))
  31. barMap := make(map[string]interface{})
  32. json.Unmarshal(barByte, &barMap)
  33. tx := restful.Engine.NewSession()
  34. defer tx.Close()
  35. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  36. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, "", string(barByte), "", "", 3, url)
  37. if len(barMap) > 0 {
  38. if barMap["status"].(string) != "ok" {
  39. return
  40. }
  41. for _, item := range barMap["msg"].([]interface{}) {
  42. bar := item.(map[string]interface{})
  43. barname := bar["barname"].(string)
  44. count, _ := tx.Table("bar").Where("pastureId = ? ", pastureId).Count()
  45. barExist, _ := tx.SQL(` select count(1) from bar where bcode = ? `, bar["barcode"]).Exist()
  46. _, err := tx.Exec(`insert into bar(pastureid,bcode,bname,sort)values(?,?,?,?) ON DUPLICATE KEY UPDATE bname = ?,udname = ? `, pastureId, bar["barcode"], barname, count+1, barname, barname)
  47. _, err = tx.Exec(`insert into feedp(pastureid,barid,barname,softccount,ccount)values(?,(select id from bar where bcode= concat(?) and pastureId = ? ),?,?,?)
  48. ON DUPLICATE KEY UPDATE softccount = ? `,
  49. // tx.Exec(`update bar `, pastureId, bar["barcode"], barname, count+1, barname, barname)
  50. pastureId, bar["barcode"], pastureId, barname, bar["cowcount"], bar["cowcount"], bar["cowcount"])
  51. fmt.Println(bar["barcode"], barname, bar["cowcount"], pastureId, err)
  52. if !barExist {
  53. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  54. pastureId, bar["barcode"], 1, barname)
  55. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  56. pastureId, bar["barcode"], 2, barname)
  57. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  58. pastureId, bar["barcode"], 3, barname)
  59. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  60. pastureId, bar["barcode"], 4, barname)
  61. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  62. pastureId, bar["barcode"], 5, barname)
  63. tx.Exec(`insert into fpdetail(pastureId,barid,weight,ptsrate,times,barname) values(?,(select id from bar where bcode= ? and pastureId = ? ),0,0,?,?)`,
  64. pastureId, bar["barcode"], 6, barname)
  65. }
  66. }
  67. }
  68. }
  69. func UdFeedSync(pastureId, farmId string) {
  70. url := fmt.Sprintf("http://172.16.86.95:8080/copartner_downloads?equipmentId=48ffa4de-7870-4b18-a052-9f8d3a46089a&farmId=%s&method=downloadmaterial", farmId)
  71. feedByte := getPull(url)
  72. fmt.Println(string(feedByte))
  73. tx := restful.Engine.NewSession()
  74. defer tx.Close()
  75. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  76. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, "", string(feedByte), "", "", 3, url)
  77. feedMap := make(map[string]interface{})
  78. json.Unmarshal(feedByte, &feedMap)
  79. fmt.Println(string(feedByte))
  80. if len(feedMap) > 0 {
  81. if feedMap["status"].(string) != "ok" {
  82. return
  83. }
  84. for _, item := range feedMap["msg"].([]interface{}) {
  85. fmt.Println(item)
  86. feed := item.(map[string]interface{})
  87. feedname := feed["feedname"].(string)
  88. feedClassExist, err := tx.Table("feedclass").Where("fccode = ? ", feed["feedclass"]).Where(" pastureId = ? ", pastureId).Exist()
  89. fmt.Println(err)
  90. if !feedClassExist {
  91. count, _ := tx.Table("feedclass").Where("pastureId = ? ", pastureId).Count()
  92. ids, err := setting.SnowIds.NextId()
  93. if err != nil {
  94. ids = time.Now().UnixNano()
  95. logging.Info("create SnowIds err", err)
  96. }
  97. _, err = tx.Exec(`insert into feedclass(id,pastureId,fcname,fccode,bigfeedclassname,sort,bigfeedclassid) values(?,?,?,?,?,?,?)`,
  98. ids, pastureId, feed["feedclass"], feed["feedclass"], feed["feedclass"], count+1, ids)
  99. fmt.Println(err)
  100. }
  101. ids, err := setting.SnowIds.NextId()
  102. if err != nil {
  103. ids = time.Now().UnixNano()
  104. logging.Info("create SnowIds err", err)
  105. }
  106. _, err = tx.Exec(`insert into feed(id,pastureId,feedcode,udname,fclassid,fclass,fname) values(?,?,?,?,(select id from feedclass where fccode = ? and pastureId = ? ),?,if(char_length(?) > 7, left(?,7),?) )
  107. ON DUPLICATE KEY UPDATE udname = ? ,fclassid = (select id from feedclass where fccode = ? and pastureId = ? ) ,fclass = ?`,
  108. ids, pastureId, feed["feedcode"], feedname, feed["feedclass"], pastureId, feed["feedclass"], feedname, feedname, feedname, feedname, feed["feedclass"], pastureId, feed["feedclass"])
  109. fmt.Println(err)
  110. }
  111. }
  112. }
  113. func getPull(url string) []byte {
  114. forwardingReq := new(ForwardingReq)
  115. forwardingReq.Url = url
  116. forwardingReq.Method = "GET"
  117. jsonStr, _ := json.Marshal(forwardingReq)
  118. req, err := http.NewRequest("POST", setting.ServerSetting.UDForwardingSvc+":8080/forwarding", bytes.NewBuffer(jsonStr))
  119. if err != nil {
  120. logs.Error(err)
  121. // return nil
  122. }
  123. req.Header.Set("Content-Type", "application/json")
  124. client := &http.Client{}
  125. resp, err := client.Do(req)
  126. if err != nil {
  127. logs.Error(err)
  128. return nil
  129. }
  130. defer resp.Body.Close()
  131. body, _ := ioutil.ReadAll(resp.Body)
  132. if strings.Index(resp.Status, "200") > -1 {
  133. bodyData := make(map[string]string)
  134. json.Unmarshal(body, &bodyData)
  135. if _, ok := bodyData["data"]; ok {
  136. return []byte(bodyData["data"])
  137. }
  138. }
  139. return nil
  140. }
  141. //搅拌
  142. func UdUtirPush(pastureId, farmId, date string) error {
  143. tx := restful.Engine.NewSession()
  144. defer tx.Close()
  145. var now string
  146. if date != "" {
  147. now = date
  148. } else {
  149. now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  150. }
  151. dataList, err := tx.SQL(`select date_format(d1.date,'%Y-%m-%d') dropDate,d.times loadShift,d.datacaptureno tmrNo,ft.tcode recipeId,ft.ccname recipeName,if(d1.feedcode = -1 ,d1.fid,f.feedcode) ingId,ifnull(f.fname,ftd.fname) ingName,
  152. ifnull(ifnull(fc.fcname,(select fcname from feedclass fc1 join feed f1 on f1.fclassid = fc1.id where f1.id =ftd.fid )),'饲料') ingType,
  153. ifnull(f.dry * d1.actualweightminus,0) dmPct,d1.sort mixNo,ifnull(d1.feedallowratio,0) allowableError , if(f.fname is not null, ifnull(d1.lweight,''), ifnull(ftd.fweight / (select sum(fweight) from ftdetaildate where date = d1.date and ftid = d1.fid ) * d1.lweight,"") ) expWeight,
  154. if(f.fname is not null, ifnull(d1.actualweightminus,''), ifnull(ftd.fweight / (select sum(fweight) from ftdetaildate where date = d1.date and ftid = d1.fid ) * d1.actualweightminus,"") ) actualWeight,
  155. DATE_FORMAT(IFNULL((SELECT MAX(intime) FROM downloadplandtl1 d2
  156. WHERE d1.pid=d2.pid AND d1.pastureid = d2.pastureid AND d2.intime<d1.intime)
  157. , (SELECT MAX(intime) FROM downloadedplan d2
  158. WHERE d1.date=d2.mydate AND d1.pastureid = d2.pastureid AND d2.id=d1.pid)
  159. ), '%Y-%m-%d %H:%i:%s')
  160. startTime ,
  161. date_format(d1.intime,'%Y-%m-%d %H:%i:%s') endTime, driver.drivername tmrName ,d1.feedallowratio allowable_error
  162. from downloadplandtl1 d1 join downloadedplan d on d.id = d1.pid
  163. left join feedtempletdate ft on ft.id = d.tempid and ft.date = d1.date
  164. left join feed f on f.feedcode = d1.feedcode
  165. left join feedclass fc on fc.id = f.fclassid
  166. left join ftdetaildate ftd on ftd.date = d1.date and ftd.ftid = d1.fid
  167. join tmr t on t.id = d.tmrid
  168. left driver on driver.drivercode = t.eqcode
  169. where d1.date = DATE_FORMAT(subdate(now(),1) ,'%Y-%m-%d' ) and d.havebutton = 1 and d1.type = 0 `, now).Query().List()
  170. if err != nil {
  171. logging.Error(err)
  172. return err
  173. }
  174. dataByte, _ := json.Marshal(&dataList)
  175. reqJson := `{
  176. "reqMethod": "PARTY",
  177. "param": {
  178. "code": "1",
  179. "method": "uploadadddata",
  180. "resultData": %s,
  181. "farmId": "%s",
  182. "rowCount": %d,
  183. "errMessage": ""
  184. },
  185. "regCode": "cpt180511",
  186. "command": "3d6e4752",
  187. "apiId": "getCptData"
  188. }`
  189. reqJson = fmt.Sprintf(reqJson, string(dataByte), farmId, len(dataList))
  190. postPush(reqJson, 0, tx, pastureId)
  191. fmt.Println(reqJson)
  192. return nil
  193. }
  194. func UdMaterialIssuePush(pastureId, farmId, date string) error {
  195. tx := restful.Engine.NewSession()
  196. defer tx.Close()
  197. var now string
  198. if date != "" {
  199. now = date
  200. } else {
  201. now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  202. }
  203. dataList, err := tx.SQL(`SELECT d1.sort feedSort, ifnull(f.feedcode,d1.fid) feedId,ifnull(ifnull(f.udname,f.fname),d1.fname) feedName,
  204. d.id,IFNULL(ft.tcode,d2.feedtempletid ) recipeId,IFNULL(ft.tcode,d2.feedtempletid ) feedtempletId,ifnull(ft.tname,d.templetName) feedtempletName ,
  205. IFNULL(d.templetName,d2.feedtempletName ) recipeName,
  206. date_format(d.mydate,'%Y-%m-%d') as dropDate,ifnull((select sort from downloadedplan where pid=d.pid and lpplanType !=d.lpplanType and mydate=d.mydate),d.sort) as tmrNo,
  207. d.Times as dropShift,ifnull(b.bcode,b.id) as penId,b.bname as penName,fp.CCOUNT as cowCount, d2.SORT as feedingNo,
  208. ifnull(ROUND(d2.lweight * (d1.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = d1.pid and type = 0) ),2) ,0)as expWeight,
  209. ifnull(d2.ActualWeightMinus,0) as actualWeight, ifnull(date_format(timestamp(d2.InTime,CONCAT('-',d2.processTime)),'%Y-%m-%d %H:%i:%s'),d.mydate) as startTime,
  210. ifnull(date_format(d2.InTime,'%Y-%m-%d %H:%i:%s'),d.mydate) as endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  211. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  212. ORDER BY dr.operatetime DESC LIMIT 1),"") as tmrName ,d.tmrtname equipmentId ,
  213. d1.fname,
  214. ifnull(TRIM(if(d1.feedcode != '-1', 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 ,
  215. 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)*
  216. d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid )),
  217. 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)*
  218. d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid ))
  219. ),'')),0) feedWeight
  220. ,ifnull(ftd.sort,"") premixFeedSort ,
  221. ifnull((select feedcode from feed where id = ftd.fid),"") premixFeedId,ifnull(ftd.fname,"") premixFeedName,
  222. ifnull(if(d1.feedcode = '-1', 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 ,
  223. 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)*
  224. d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid )),
  225. 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)*
  226. d1.actualweightminus*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid ))
  227. ),''),'0')
  228. premixFeedWeight
  229. from downloadedplan d
  230. LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID
  231. LEFT JOIN (select dd.pid as lppid,dd1.* from downloadedplan dd join downloadplandtl1 dd1 on dd1.pid = dd.id and dd1.type = 0
  232. where dd.mydate= date_format(?,'%Y-%m-%d') and dd.lpplanType in (0,1) ) d1 on d1.lppid = d.pid
  233. LEFT JOIN feedp fp on d2.FBarID=fp.barid
  234. left join bar b on fp.barid = b.id and d.pastureid = b.pastureid
  235. left join feed f on f.feedcode = d1.feedcode
  236. left JOIN feedtempletdate ft on d.tempid=ft.id and ft.date = d1.date and d.tversion = ft.version
  237. left join feedtempletdate ft1 on d1.feedcode = '-1' and ft1.date = d1.date and ft1.id = d1.fid
  238. left join ftdetaildate ftd on ftd.date = d1.date and ftd.ftid = d1.fid AND d1.feedcode = '-1' and ftd.version = ft1.version
  239. where d.mydate= date_format(?,'%Y-%m-%d') and d2.type = 0 and d.havebutton = 1 and d.pastureid = ? and d1.fname is not null order by tmrno `, now, now, pastureId).Query().List()
  240. if err != nil {
  241. logging.Error(err)
  242. return err
  243. }
  244. dataByte, _ := json.Marshal(&dataList)
  245. reqJson := `{
  246. "reqMethod": "PARTY",
  247. "param": {
  248. "code": "1",
  249. "method": "uploaddiliverdata",
  250. "resultData": %s,
  251. "farmId": "%s",
  252. "rowCount": %d,
  253. "errMessage": ""
  254. },
  255. "regCode": "cpt180511",
  256. "command": "3d6e4752",
  257. "apiId": "getCptData"
  258. }`
  259. reqJson = fmt.Sprintf(reqJson, string(dataByte), farmId, len(dataList))
  260. // '0饲喂 1剩料 2原料 3栏舍 4配方 5栏舍牛头数 6库存',
  261. postPush(reqJson, 0, tx, pastureId)
  262. fmt.Println(reqJson)
  263. return nil
  264. }
  265. func UdBarFeedRemainPush(pastureId, farmId, date string) error {
  266. tx := restful.Engine.NewSession()
  267. defer tx.Close()
  268. var now string
  269. if date != "" {
  270. now = date
  271. } else {
  272. now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  273. }
  274. dataList, err := tx.SQL(`SELECT
  275. '' startTime,
  276. '' endTime,
  277. br.RemainDate AS overplusDate,
  278. ifnull(b.bcode,b.id) AS penId,
  279. b.BNAME AS penName,
  280. br.Remain AS actualWeight
  281. FROM
  282. barfeedremain br
  283. LEFT JOIN feedp fp ON br.barid = fp.id
  284. left join bar b on b.id = fp.barid
  285. WHERE
  286. date_format( RemainDate, '%Y-%m-%d' ) =? `, now).Query().List()
  287. if err != nil {
  288. logging.Error(err)
  289. return err
  290. }
  291. dataByte, _ := json.Marshal(&dataList)
  292. reqJson := `{
  293. "reqMethod": "PARTY",
  294. "param": {
  295. "code": "1",
  296. "method": "uploadoverplusdata",
  297. "resultData": %s,
  298. "farmId":"%s",
  299. "rowCount": %d,
  300. "errMessage": ""
  301. },
  302. "regCode": "cpt180511",
  303. "command": "3d6e4752",
  304. "apiId": "getCptData"
  305. }`
  306. reqJson = fmt.Sprintf(reqJson, string(dataByte), farmId, len(dataList))
  307. // '0饲喂 1剩料 2原料 3栏舍 4配方 5栏舍牛头数 6库存',
  308. fmt.Println(reqJson)
  309. postPush(reqJson, 1, tx, pastureId)
  310. return nil
  311. }
  312. // method:
  313. // 配方 getfeedtempletinfo
  314. // 搅拌数据 uploadadddata
  315. // 撒料数据 uploaddiliverdata
  316. // 剩料 uploadoverplusdata
  317. func UdFeedtempletPush(pastureId, farmId, date string) error {
  318. tx := restful.Engine.NewSession()
  319. defer tx.Close()
  320. var now string
  321. if date != "" {
  322. now = date
  323. } else {
  324. now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  325. }
  326. dataList, err := tx.SQL(`SELECT
  327. ft.tcode AS recipeId,
  328. ft.TNAME AS recipeName,
  329. f.feedcode AS ingId,
  330. ifnull(f.udname,f.fname) AS ingName,
  331. round( f.dry * ftd.FWEIGHT, 2 ) AS dmQty,
  332. ftd.FWEIGHT AS afQty,
  333. ftd.SORT AS mixNo,
  334. round( f.Uprice * ftd.FWEIGHT, 2 ) AS recipeCost,
  335. fc.fcname AS ingType,
  336. f.AllowRatio AS allowableError ,fc.fcname ingType
  337. FROM
  338. feedtempletdate ft
  339. JOIN ftdetaildate ftd ON ft.id = ftd.FTID and ft.date = ftd.date
  340. JOIN feed f ON ftd.FID = f.id
  341. JOIN feedclass fc ON f.fclassid = fc.id
  342. WHERE
  343. ft.ENABLE = 1
  344. AND ft.isDelete != 1 and ft.date = ? `, now).Query().List()
  345. if err != nil {
  346. logging.Error(err)
  347. return err
  348. }
  349. dataByte, _ := json.Marshal(&dataList)
  350. reqJson := `{
  351. "reqMethod": "PARTY",
  352. "param": {
  353. "code": "1",
  354. "method": "getfeedtempletinfo",
  355. "resultData": %s,
  356. "farmId": "%s",
  357. "rowCount": %d,
  358. "errMessage": ""
  359. },
  360. "regCode": "cpt180511",
  361. "command": "3d6e4752",
  362. "apiId": "getCptData"
  363. }`
  364. reqJson = fmt.Sprintf(reqJson, string(dataByte), farmId, len(dataList))
  365. fmt.Println(reqJson)
  366. postPush(reqJson, 4, tx, pastureId)
  367. return nil
  368. }
  369. type ForwardingReq struct {
  370. Body string `json:"body"`
  371. Headers []*Headers `json:"headers"`
  372. Url string `json:"url"`
  373. Method string `json:"method"`
  374. }
  375. type Headers struct {
  376. Key string `json:"key"`
  377. Value string `json:"value"`
  378. }
  379. func postPush(data string, msgtype int, tx *xorm.Session, pastureId string) {
  380. forwardingReq := new(ForwardingReq)
  381. forwardingReq.Url = "http://172.16.86.95:8080/copartner_uploads/"
  382. forwardingReq.Method = "POST"
  383. forwardingReq.Body = data
  384. jsonStr, _ := json.Marshal(forwardingReq)
  385. fmt.Println(setting.ServerSetting.UDForwardingSvc)
  386. req, err := http.NewRequest("POST", setting.ServerSetting.UDForwardingSvc+":8080/forwarding", bytes.NewBuffer(jsonStr))
  387. if err != nil {
  388. logs.Error(err)
  389. // return nil
  390. }
  391. req.Header.Set("Content-Type", "application/json")
  392. client := &http.Client{}
  393. resp, err := client.Do(req)
  394. if err != nil {
  395. logs.Error(err)
  396. return
  397. }
  398. defer resp.Body.Close()
  399. body, _ := ioutil.ReadAll(resp.Body)
  400. fmt.Println(string(body))
  401. if strings.Index(resp.Status, "200") > -1 {
  402. bodyData := make(map[string]string)
  403. json.Unmarshal(body, &bodyData)
  404. if _, ok := bodyData["data"]; ok {
  405. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  406. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, bodyData["data"], "", "", msgtype, forwardingReq.Url)
  407. } else {
  408. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  409. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, forwardingReq.Url)
  410. }
  411. } else {
  412. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  413. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, forwardingReq.Url)
  414. }
  415. }