sap.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package sap
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. "tmr-watch/conf/setting"
  10. "tmr-watch/http/handle/restful"
  11. "tmr-watch/pkg/app"
  12. "tmr-watch/pkg/e"
  13. "github.com/Anderson-Lu/gofasion/gofasion"
  14. "github.com/astaxie/beego/logs"
  15. "github.com/gin-gonic/gin"
  16. "github.com/xormplus/xorm"
  17. )
  18. func SyncMaterialOutbound() error {
  19. tx := restful.Engine.NewSession()
  20. defer tx.Close()
  21. pastureinfo := new(udPastureInfo)
  22. err := tx.SQL(`select column_default as pastureid from information_schema.COLUMNS
  23. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  24. if err != nil {
  25. logs.Error(err)
  26. return err
  27. }
  28. materialOutbound(pastureinfo.Pastureid, time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
  29. surplusOutbound(pastureinfo.Pastureid, time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
  30. return nil
  31. }
  32. func SyncSapBar(c *gin.Context) {
  33. appG := app.Gin{C: c}
  34. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  35. fsion := gofasion.NewFasion(string(dataByte))
  36. start := fsion.Get("startTime").ValueStr()
  37. end := fsion.Get("endTime").ValueStr()
  38. pastureId := fsion.Get("pastureId").ValueStr()
  39. tx := restful.Engine.NewSession()
  40. defer tx.Close()
  41. pastureinfo := new(udPastureInfo)
  42. err := tx.SQL(`select * from pasture where pastureid = ?`, pastureId).GetFirst(pastureinfo).Error
  43. if err != nil {
  44. logs.Error(err)
  45. return
  46. }
  47. // year := time.Now().Year()
  48. data := `{
  49. "DATA": {
  50. "DIDAT": "%s",
  51. "DITIM": "%s",
  52. "WERKS": "%s"
  53. },
  54. "DEST": {
  55. "DEST_ID": "TMRWATCH",
  56. "BUSS_TP": "CO005"
  57. }
  58. }`
  59. startTime, _ := time.ParseInLocation("2006-01-02", start, time.Local)
  60. endTime, _ := time.ParseInLocation("2006-01-02", end, time.Local)
  61. url := "http://192.168.61.117/SAPP0/Feed/CO005/QueryCowsheds"
  62. data = fmt.Sprintf(data, startTime.Format("20060102"), endTime.Format("20060102"), pastureinfo.Werks)
  63. respmap, _ := postPush(url, data, 3, tx, pastureId)
  64. if respmap != nil {
  65. if _, ok := respmap.(map[string]interface{})["ZTCO_001"]; ok {
  66. for _, item := range respmap.(map[string]interface{})["ZTCO_001"].([]interface{}) {
  67. barMap := item.(map[string]interface{})
  68. _, err := tx.SQL(` insert into bar(pastureid,bcode,bname,sort,class,classcode,cattle,cattlecode,sapcode)
  69. values(?,?,?,(select max(sort)+1 from bar b where b.pastureid = ? ),
  70. (select distName from dist where distCode = ? and distType = '牛舍类型' ),?,
  71. (select distName from dist where distCode = ? and distType = '牛群类别'),?,?)
  72. ON DUPLICATE KEY UPDATE bname = ? ,
  73. class = (select distName from dist where distCode = ? and distType = '牛舍类型' ) ,classcode = ?,
  74. cattle = (select distName from dist where distCode = ? and distType = '牛群类别' ) ,cattlecode = ?
  75. `,
  76. pastureId, barMap["CHSNO"], fmt.Sprintf("%v_sap", barMap["CHSTX"]), pastureId, barMap["CHSTY"], barMap["CHSTY"],
  77. barMap["FCWTS"], barMap["FCWTS"], barMap["CHSNO"],
  78. fmt.Sprintf("%v_sap", barMap["CHSTX"]), barMap["CHSTY"], barMap["CHSTY"], barMap["FCWTS"], barMap["FCWTS"]).Execute()
  79. if err != nil {
  80. logs.Error(err)
  81. return
  82. }
  83. }
  84. }
  85. }
  86. appG.Response(http.StatusOK, e.SUCCESS, true)
  87. }
  88. type udPastureInfo struct {
  89. Werks string `xorm:"werks"`
  90. Pastureid string `xorm:"pastureid"`
  91. }
  92. func MaterialOutbound(c *gin.Context) {
  93. appG := app.Gin{C: c}
  94. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  95. fsion := gofasion.NewFasion(string(dataByte))
  96. pastureId := fsion.Get("pastureid").ValueStr()
  97. date := fsion.Get("date").ValueStr()
  98. materialOutbound(pastureId, date)
  99. appG.Response(http.StatusOK, e.SUCCESS, true)
  100. }
  101. func SurplusOutbound(c *gin.Context) {
  102. appG := app.Gin{C: c}
  103. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  104. fsion := gofasion.NewFasion(string(dataByte))
  105. pastureId := fsion.Get("pastureid").ValueStr()
  106. date := fsion.Get("date").ValueStr()
  107. surplusOutbound(pastureId, date)
  108. appG.Response(http.StatusOK, e.SUCCESS, true)
  109. }
  110. func materialOutbound(pastureId, date string) error {
  111. tx := restful.Engine.NewSession()
  112. defer tx.Close()
  113. pastureinfo := new(udPastureInfo)
  114. err := tx.SQL(`select * from pasture where pastureid = ?`, pastureId).GetFirst(pastureinfo).Error
  115. if err != nil {
  116. logs.Error(err)
  117. return err
  118. }
  119. dataList, err := tx.SQL(` SELECT
  120. tem.sort ZEILE,
  121. tem.feedname MAKTX,
  122. ifnull(ROUND(if(tem.lweight<0,0,tem.lweight),(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid =? )),0)lweight,
  123. ifnull(ROUND(tem.actualweightminus,(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid = ?)),0) MENGE
  124. ,tem.bname CHSTX,tem.sapcode CHSNO,tem.cattle FCWTS,tem.MATNR,tem.sapGoods LGORT
  125. FROM (
  126. (SELECT
  127. d2.type,
  128. d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
  129. SUM(
  130. 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)*
  131. 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 ))
  132. )lweight,
  133. d1.sort sort,
  134. SUM(
  135. 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 ,
  136. 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)*
  137. 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 )),
  138. 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)*
  139. 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 ))
  140. )
  141. )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
  142. FROM
  143. downloadedplan d
  144. JOIN downloadplandtl2 d2
  145. ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
  146. JOIN downloadplandtl1 d1
  147. ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
  148. 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
  149. left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
  150. left join feed f on f.id = d1.fid
  151. left join feedtempletdate ftp on ftp.id = d.tempid AND ftp.date = ftd.date
  152. WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode != -1 and b.sapCode is not null and f.sapCode is not null
  153. GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
  154. HAVING d2.type = 0 order by b.sort,d2.fbarid)
  155. UNION
  156. (SELECT
  157. d2.type,
  158. d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
  159. SUM(
  160. 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)*
  161. 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= ftd1.date ))
  162. )lweight,
  163. d1.sort sort,
  164. SUM(
  165. 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 ,
  166. 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)*
  167. 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 )),
  168. 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)*
  169. 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 = ftd1.version and ftd1.id = ftd2.ftid ))
  170. )
  171. )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
  172. FROM
  173. downloadedplan d
  174. JOIN downloadplandtl2 d2
  175. ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
  176. JOIN downloadplandtl1 d1
  177. ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
  178. left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
  179. left join feedtempletdate ftd1 on d1.feedcode = '-1' and ftd1.date = d1.date and ftd1.id = d1.fid
  180. LEFT JOIN ftdetaildate ftd ON ftd.pastureid =d1.pastureid AND ftd.ftid = ftd1.id AND d1.feedcode = '-1' AND ftd.date =d1.date AND ftd.version = ftd1.version
  181. left join feed f on f.id = ftd.fid
  182. WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode = -1 and b.sapCode is not null and f.sapCode is not null
  183. GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
  184. HAVING d2.type = 0 order by b.sort,d2.fbarid)
  185. ) tem
  186. ORDER BY tem.d2sort,tem.feedname `, pastureId, pastureId, pastureId, date, pastureId, date).Query().List()
  187. if err != nil {
  188. logs.Error(err)
  189. return err
  190. }
  191. data := `{
  192. "DEST": {
  193. "DEST_ID": "DFEED",
  194. "BUSS_TP": "MM016"
  195. },
  196. "DATA": {
  197. "BUDAT": "%s",
  198. "WERKS": "%s",
  199. "ITEMS": %s
  200. }
  201. }`
  202. databyte, _ := json.Marshal(dataList)
  203. now, _ := time.ParseInLocation("2006-01-02", date, time.Local)
  204. data = fmt.Sprintf(data, now.Format("20060102"), pastureinfo.Werks, string(databyte))
  205. url := "http://192.168.61.117/SAPP0/Feed/MM016/StockOut"
  206. postPush(url, data, 0, tx, pastureId)
  207. return nil
  208. }
  209. func postPush(url, data string, msgtype int, tx *xorm.Session, pastureId string) (interface{}, bool) {
  210. var jsonStr = []byte(data)
  211. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  212. if err != nil {
  213. logs.Error(err)
  214. // return nil
  215. }
  216. // req.SetBasicAuth("zinf_TMR", "zinf_TMR@1234") 测试线密码
  217. req.SetBasicAuth("zinf_TMR", "$C2Cj$\\Uv#")
  218. req.Header.Set("Content-Type", "application/json")
  219. client := &http.Client{}
  220. resp, err := client.Do(req)
  221. if err != nil {
  222. logs.Error(err)
  223. // return nil
  224. }
  225. fmt.Println(resp.Body)
  226. defer resp.Body.Close()
  227. body, _ := ioutil.ReadAll(resp.Body)
  228. var respData map[string]interface{}
  229. tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
  230. values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, url)
  231. err = json.Unmarshal(body, &respData)
  232. if err != nil {
  233. logs.Error(err)
  234. // return nil
  235. }
  236. if _, ok := respData["DATA"]; ok {
  237. if msgtype == 3 {
  238. if _, ok := respData["ZTCO_001"]; !ok {
  239. return respData["DATA"], false
  240. }
  241. return respData["DATA"], true
  242. }
  243. if _, ok := respData["DATA"]; !ok {
  244. return respData["DATA"], false
  245. }
  246. return respData["DATA"], true
  247. } else {
  248. return respData["DATA"], false
  249. }
  250. }
  251. func SyncSapFeed(c *gin.Context) {
  252. appG := app.Gin{C: c}
  253. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  254. fsion := gofasion.NewFasion(string(dataByte))
  255. start := fsion.Get("startTime").ValueStr()
  256. end := fsion.Get("endTime").ValueStr()
  257. mtart := fsion.Get("mtart").ValueStr()
  258. pastureId := fsion.Get("pastureId").ValueStr()
  259. tx := restful.Engine.NewSession()
  260. defer tx.Close()
  261. data := `{
  262. "DEST": {
  263. "DEST_ID": "TMRWATCH",
  264. "BUSS_TP": "MM002"
  265. },
  266. "DATA": {
  267. "BUDAT_B": "%s",
  268. "BUDAT_E": "%s",
  269. "TMTART": {
  270. "MTART": "%s"
  271. },
  272. "TMATKL": []
  273. }
  274. }`
  275. startTime, _ := time.ParseInLocation("2006-01-02", start, time.Local)
  276. endTime, _ := time.ParseInLocation("2006-01-02", end, time.Local)
  277. data = fmt.Sprintf(data, startTime.Format("20060102"), endTime.Format("20060102"), mtart)
  278. // "https://app.modernfarming.cn:7443/sap/Common/MM002/QueryMaterial/"
  279. // http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial
  280. url := "http://192.168.61.117/SAPP0/Common/MM002/QueryMaterial"
  281. respmap, success := postPush(url, data, 2, tx, pastureId)
  282. // var a interface{} = nil
  283. fmt.Println(respmap, success)
  284. if success == false {
  285. appG.Response(http.StatusOK, e.SUCCESS, true)
  286. } else {
  287. for _, item := range respmap.(map[string]interface{})["TMARA"].([]interface{}) {
  288. sapMap := item.(map[string]interface{})
  289. // fmt.Println(item)
  290. _, err := tx.SQL(` replace into feed_sap(MATNR,MTART,MATKL,MAKTX,MEINS,UMREZ,MEINH,UMREN,ZMINC,ZGUIG,LVORM,LAEDA)values(?,?,?,?,?,?,?,?,?,?,?,?)`,
  291. sapMap["MATNR"], sapMap["MTART"], sapMap["MATKL"], sapMap["MAKTX"], sapMap["MEINS"], sapMap["UMREZ"], sapMap["MEINH"],
  292. sapMap["UMREN"], sapMap["ZMINC"], sapMap["ZGUIG"], sapMap["LVORM"], sapMap["LAEDA"]).Execute()
  293. if err != nil {
  294. logs.Error(err)
  295. return
  296. }
  297. }
  298. appG.Response(http.StatusOK, e.SUCCESS, true)
  299. }
  300. // return
  301. }
  302. func surplusOutbound(pastureId, date string) error {
  303. tx := restful.Engine.NewSession()
  304. defer tx.Close()
  305. pastureinfo := new(udPastureInfo)
  306. err := tx.SQL(`select * from pasture where pastureid = ?`, pastureId).GetFirst(pastureinfo).Error
  307. if err != nil {
  308. logs.Error(err)
  309. return err
  310. }
  311. dataList, err := tx.SQL(`SELECT
  312. tem.sort ZEILE,
  313. tem.feedname MAKTX,
  314. ifnull(ROUND(if(tem.lweight<0,0,tem.lweight),(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid =? )),0)lweight,
  315. ifnull(ROUND(tem.actualweightminus,(select inforvalue from sysopt where inforname="decimalPlaces" and pastureid = ?)),0) MENGE
  316. ,tem.bname CHSTX,tem.sapcode CHSNO,tem.cattle FCWTS,(select inforvalue from sysopt where inforname="excess" and pastureid = ?) MATNR,tem.sapGoods LGORT
  317. FROM (
  318. (SELECT
  319. d2.type,
  320. d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
  321. SUM(
  322. 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)*
  323. 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 ))
  324. )lweight,
  325. d1.sort sort,
  326. SUM(
  327. 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 ,
  328. 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)*
  329. 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 )),
  330. 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)*
  331. 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 ))
  332. )
  333. )actualweightminus,b.sort as d2sort,b.bname,b.sapcode, b.cattlecode ,f.sapGoods ,(select cattle from feedtemplet where id = d.tempid) cattle
  334. FROM
  335. downloadedplan d
  336. JOIN downloadplandtl2 d2
  337. ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
  338. JOIN downloadplandtl1 d1
  339. ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
  340. 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
  341. left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
  342. left join feed f on f.id = d1.fid
  343. left join feedtempletdate ftp on ftp.id = d.tempid AND ftp.date = ftd.date
  344. WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode != -1 and b.sapCode is not null and d1.fname like concat('%','_剩料','%')
  345. GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
  346. HAVING d2.type = 0 order by b.sort,d2.fbarid)
  347. UNION
  348. (SELECT
  349. d2.type,
  350. d2.fname,IFNULL(ftd.fname,d1.fname ) feedname,
  351. SUM(
  352. 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)*
  353. 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= ftd1.date ))
  354. )lweight,
  355. d1.sort sort,
  356. SUM(
  357. 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 ,
  358. 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)*
  359. 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 )),
  360. 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)*
  361. 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 = ftd1.version and ftd1.id = ftd2.ftid ))
  362. )
  363. )actualweightminus,b.sort as d2sort,b.bname,b.sapcode, b.cattlecode ,f.sapGoods ,(select cattle from feedtemplet where id = d.tempid) cattle
  364. FROM
  365. downloadedplan d
  366. JOIN downloadplandtl2 d2
  367. ON d.pastureid = d2.pastureid AND d2.pid = d.id and d2.type = 0
  368. JOIN downloadplandtl1 d1
  369. ON d.pastureid = d1.pastureid AND d1.flpid = d2.flpid AND d1.type = 0 AND d2.date = d1.date
  370. left join bar b on b.pastureid =d1.pastureid and b.id = d2.fbarid
  371. left join feedtempletdate ftd1 on d1.feedcode = '-1' and ftd1.date = d1.date and ftd1.id = d1.fid
  372. LEFT JOIN ftdetaildate ftd ON ftd.pastureid =d1.pastureid AND ftd.ftid = ftd1.id AND d1.feedcode = '-1' AND ftd.date =d1.date AND ftd.version = ftd1.version
  373. left join feed f on f.id = ftd.fid
  374. WHERE d.pastureid = ? AND d.mydate = ? and d1.feedcode = -1 and b.sapCode is not null and d1.fname like concat('%','_剩料','%')
  375. GROUP BY d2.fbarid,IFNULL(ftd.fname,d1.fname )
  376. HAVING d2.type = 0 order by b.sort,d2.fbarid)
  377. ) tem
  378. ORDER BY tem.d2sort,tem.feedname `, pastureId, pastureId, pastureId, pastureId, date, pastureId, date).Query().List()
  379. if err != nil {
  380. logs.Error(err)
  381. return err
  382. }
  383. data := `{
  384. "DEST": {
  385. "DEST_ID": "DFEED",
  386. "BUSS_TP": "MM016"
  387. },
  388. "DATA": {
  389. "BUDAT": "%s",
  390. "WERKS": "%s",
  391. "ITEMS": %s
  392. }
  393. }`
  394. databyte, _ := json.Marshal(dataList)
  395. now, _ := time.ParseInLocation("2006-01-02", date, time.Local)
  396. data = fmt.Sprintf(data, now.Format("20060102"), pastureinfo.Werks, string(databyte))
  397. url := "https://app.modernfarming.cn:7443/sap/QAS/Feed/MM032/LeftoverEntry"
  398. postPush(url, data, 0, tx, pastureId)
  399. return nil
  400. }