udsync.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "strconv"
  10. "time"
  11. "tmr-watch/conf/setting"
  12. "tmr-watch/http/handle/restful"
  13. "tmr-watch/pkg/logging"
  14. "github.com/astaxie/beego/logs"
  15. "github.com/robfig/cron"
  16. )
  17. func UDSync() {
  18. tx := restful.Engine.NewSession()
  19. defer tx.Close()
  20. pastureinfo := new(udPastureInfo)
  21. err := tx.SQL(`select token,pastureid,werks from pasture p join ( select column_default from information_schema.COLUMNS
  22. WHERE table_name = 'recweight' AND table_schema = 'tmrwatch2' AND column_name = 'pastureid') pas
  23. where p.pastureid = pas.column_default`).GetFirst(pastureinfo).Error
  24. if err != nil {
  25. logs.Error(err)
  26. return
  27. }
  28. log.Println("Starting Cron...")
  29. // UDFeedtempletinfo(pastureinfo.Token, pastureinfo.Pastureid)
  30. defer func() {
  31. if err := recover(); err != nil {
  32. fmt.Printf("CronTest pnic err%+v \n", err)
  33. logging.Error("panic recover err ", err)
  34. }
  35. }()
  36. log.Println("Starting Cron...")
  37. c := cron.New()
  38. // UDuploadnewdiliverdata(pastureinfo.Token, pastureinfo.Pastureid)
  39. err = c.AddFunc("30 21 * * *", func() {
  40. log.Println("UDSync", time.Now())
  41. UDFeedtempletinfo(pastureinfo.Token, pastureinfo.Pastureid)
  42. UDUploadadddata(pastureinfo.Token, pastureinfo.Pastureid)
  43. UDuploadnewdiliverdata(pastureinfo.Token, pastureinfo.Pastureid)
  44. UDUploadoverplusdata(pastureinfo.Token, pastureinfo.Pastureid)
  45. // // 同步栏舍
  46. // resFeedp := httpGet(pastureinfo.Token, "downloadpen")
  47. // syncFeedp(pastureinfo.Pastureid, resFeedp)
  48. // 同步饲料
  49. resFeed := httpGet(pastureinfo.Token, "downloadmaterial")
  50. syncFeed(pastureinfo.Pastureid, resFeed)
  51. })
  52. if err != nil {
  53. println("cron4 err", err.Error())
  54. }
  55. c.Start()
  56. }
  57. //同步饲料数据
  58. func syncFeed(pastureid string, feedList []interface{}) error {
  59. tx := restful.Engine.NewSession()
  60. defer tx.Close()
  61. err := tx.Begin()
  62. if err != nil {
  63. tx.Rollback()
  64. logs.Error("syncFeed-error-1:", err)
  65. return err
  66. }
  67. for _, f := range feedList {
  68. feed := f.(map[string]interface{})
  69. var feedcode, fname, fclass, fclassid, dry interface{}
  70. if _, ok := feed["feedcode"]; ok {
  71. feedcode = feed["feedcode"]
  72. }
  73. if _, ok := feed["feedname"]; ok {
  74. fname = feed["feedname"]
  75. }
  76. if _, ok := feed["feedclass"]; ok {
  77. fclass = feed["feedclass"]
  78. }
  79. if _, ok := feed["drymatter"]; ok {
  80. dry = feed["drymatter"]
  81. }
  82. fclassExist, err := tx.SQL(" select id from feedclass where pastureid = ? and fcname = ?", pastureid, fclass).Exist()
  83. if err != nil {
  84. tx.Rollback()
  85. logs.Error("syncFeed-error-2:", err)
  86. return err
  87. }
  88. if fclassExist {
  89. fclassDataList, err := tx.SQL(" select id from feedclass where pastureid = ? and fcname = ?", pastureid, fclass).QueryString()
  90. if err != nil {
  91. tx.Rollback()
  92. logs.Error("syncFeed-error-3:", err)
  93. return err
  94. }
  95. for _, fclassData := range fclassDataList {
  96. fclassid = fclassData["id"]
  97. }
  98. } else {
  99. ids, err := setting.SnowIds.NextId()
  100. if err != nil {
  101. ids = time.Now().UnixNano()
  102. logging.Info("create SnowIds err", err)
  103. }
  104. _, err = tx.SQL("insert into feedclass(id,pastureid,fccode,fcname,bigfeedclassname,bigfeedclassid,sort)VALUES(?,?,?,?,?,?,(select ifnull(max(f.sort),0) +1 from feedclass f where f.pastureid =? ))",
  105. ids, pastureid, fclass, fclass, fclass, ids, pastureid).Execute()
  106. if err != nil {
  107. tx.Rollback()
  108. logs.Error("syncFeed-error-4:", err)
  109. return err
  110. }
  111. fclassid = ids
  112. }
  113. ids, err := setting.SnowIds.NextId()
  114. if err != nil {
  115. ids = time.Now().UnixNano()
  116. logging.Info("create SnowIds err", err)
  117. }
  118. insertSql := `insert into feed(id,pastureid,feedcode,fname,fclassid,fclass,dry)VALUES(?,?,?,?,?,?,?)
  119. ON DUPLICATE KEY UPDATE fname = ? ,dry = ? `
  120. _, err = tx.SQL(insertSql, ids, pastureid, feedcode, fname, fclassid, fclass, dry, fname, dry).Execute()
  121. if err != nil {
  122. tx.Rollback()
  123. logs.Error("syncFeed-error-5:", err)
  124. return err
  125. }
  126. }
  127. err = tx.Commit()
  128. if err != nil {
  129. tx.Rollback()
  130. logs.Error("syncFeed-error-6:", err)
  131. return err
  132. }
  133. return nil
  134. }
  135. // 同步栏舍数据
  136. func syncFeedp(pastureid string, feedpList []interface{}) error {
  137. tx := restful.Engine.NewSession()
  138. defer tx.Close()
  139. err := tx.Begin()
  140. if err != nil {
  141. logs.Error("syncFeedp-error-1:", err)
  142. return err
  143. }
  144. for _, f := range feedpList {
  145. feedp := f.(map[string]interface{})
  146. var barName, barCode, barId interface{}
  147. // var ftId, tname interface{}
  148. var cowCount interface{}
  149. if _, ok := feedp["barname"]; ok {
  150. barName = feedp["barname"]
  151. }
  152. if _, ok := feedp["barcode"]; ok {
  153. barCode = feedp["barcode"]
  154. }
  155. if _, ok := feedp["cowcount"]; ok {
  156. cowCount = feedp["cowcount"]
  157. }
  158. // if _, ok := feedp["feedtempletcode"]; ok {
  159. // feedtempletCode = feedp["feedtempletCode"]
  160. // }
  161. barCount, err := tx.SQL(" select count(1) from bar where pastureid = ? and bcode = ? ", pastureid, barCode).Count()
  162. if err != nil {
  163. tx.Rollback()
  164. logs.Error("syncFeedp-error-2:", err)
  165. return err
  166. }
  167. if barCount > 0 {
  168. barDataList, err := tx.SQL(" select id from bar where pastureid = ? and bcode = ?", pastureid, barCode).QueryString()
  169. if err != nil {
  170. tx.Rollback()
  171. logs.Error("syncFeedp-error-3:", err)
  172. return err
  173. }
  174. for _, barData := range barDataList {
  175. barId = barData["id"]
  176. }
  177. } else {
  178. barReq, err := tx.SQL("insert into bar(pastureid,bname,bcode)VALUES(?,?,?)", pastureid, barName, barCode).Execute()
  179. if err != nil {
  180. tx.Rollback()
  181. logs.Error("syncFeedp-error-4:", err)
  182. return err
  183. }
  184. id, err := barReq.LastInsertId()
  185. if err != nil {
  186. tx.Rollback()
  187. logs.Error("syncFeedp-error-5:", err)
  188. return err
  189. }
  190. barId = strconv.FormatInt(id, 10)
  191. }
  192. // if feedtempletCode != "" {
  193. // feedtempletDataList, err := tx.SQL(" select id,tname from feedtemplet where pastureid = ? and tcode = ?", pastureid, feedtempletCode).QueryString()
  194. // if err != nil {
  195. // tx.Rollback()
  196. // logs.Error("syncFeedp-error-6:", err)
  197. // return err
  198. // }
  199. // for _, feedtemplet := range feedtempletDataList {
  200. // ftId = feedtemplet
  201. // tname = feedtemplet
  202. // }
  203. // }
  204. insertSql := `insert into feedp(pastureid,barname,barid,softccount)VALUES(?,?,?,?)
  205. ON DUPLICATE KEY UPDATE softccount = ? `
  206. _, err = tx.SQL(insertSql, pastureid, barName, barId, cowCount, cowCount).Execute()
  207. if err != nil {
  208. tx.Rollback()
  209. logs.Error("syncFeedp-error-7:", err)
  210. return err
  211. }
  212. }
  213. err = tx.Commit()
  214. if err != nil {
  215. tx.Rollback()
  216. logs.Error("syncFeedp-error-8:", err)
  217. return err
  218. }
  219. return nil
  220. }
  221. func syncFeedtemplet(pastureid string, feedtempletList []map[string]interface{}) error {
  222. tx := restful.Engine.NewSession()
  223. defer tx.Close()
  224. err := tx.Begin()
  225. if err != nil {
  226. logs.Error("syncFeedtemplet-error-1:", err)
  227. return err
  228. }
  229. // feedtempletName 配方名称 String
  230. // feedtempletCode 配方编码 String
  231. // cowClass 牲畜类别 String
  232. // creater 创建人 String
  233. //少一个配方类型
  234. for _, feedtemplet := range feedtempletList {
  235. var feedtempletName, feedtempletCode, ccid, cowClass, fttype, fttypeid string
  236. if _, ok := feedtemplet["feedtempletName"]; ok {
  237. feedtempletName = feedtemplet["feedtempletName"].(string)
  238. }
  239. if _, ok := feedtemplet["feedtempletCode"]; ok {
  240. feedtempletCode = feedtemplet["feedtempletCode"].(string)
  241. }
  242. if _, ok := feedtemplet["cowClass"]; ok {
  243. cowClass = feedtemplet["cowClass"].(string)
  244. }
  245. err = tx.SQL("select id from cowclass where classname = ? and pastureid = ? ", cowClass, pastureid).GetFirst(&ccid).Error
  246. if err != nil {
  247. tx.Rollback()
  248. logs.Error("syncFeedtemplet-error-2:", err)
  249. return err
  250. }
  251. insertSQl := `insert into feedtemplet(pastureid,tcode,tname,ccid,ccname,fttype,fttypeid,enable,sort)
  252. values(?,?,?,?,?,?,?,?,(select max(f.sort)+1 from feedtemplet f where f.pastureid = ? ))
  253. ON DUPLICATE KEY UPDATE tname = ?,ccid = ?,ccname = ?,fttype = ?,fttypeid = ?`
  254. a, err := tx.SQL(insertSQl, pastureid, feedtempletCode, feedtempletName, ccid, cowClass, fttype, fttypeid, 1, pastureid, feedtempletCode, feedtempletName, ccid, cowClass, fttype, fttypeid).Execute()
  255. if err != nil {
  256. tx.Rollback()
  257. logs.Error("syncFeedtemplet-error-3:", err)
  258. return err
  259. }
  260. ftid, err := a.LastInsertId()
  261. if err != nil {
  262. tx.Rollback()
  263. logs.Error("syncFeedtemplet-error-4:", err)
  264. return err
  265. }
  266. if _, ok := feedtemplet["feeds"]; ok {
  267. for _, feed := range feedtemplet["feeds"].([]map[string]interface{}) {
  268. var feedCode, feedid, sort, weight string
  269. if _, ok := feed["feedCode"]; ok {
  270. feedCode = feed["feedCode"].(string)
  271. }
  272. if _, ok := feed["sort"]; ok {
  273. sort = feed["sort"].(string)
  274. }
  275. if _, ok := feed["weight"]; ok {
  276. weight = feed["weight"].(string)
  277. }
  278. err = tx.SQL("select id from feed where feedcode = ? and pastureid = ? ", feedCode, pastureid).GetFirst(&feedid).Error
  279. if err != nil {
  280. tx.Rollback()
  281. logs.Error("syncFeedtemplet-error-5:", err)
  282. return err
  283. }
  284. ftdetailInsertSQl := `insert into ftdetail(pastureid,ftid,fid,fname,fweight,sort)
  285. values(?,?,?,?,?,?)
  286. ON DUPLICATE KEY UPDATE fweight = ?,sort = ?`
  287. _, err := tx.SQL(ftdetailInsertSQl, pastureid, ftid, feedtempletName, weight, sort, weight, sort).Execute()
  288. if err != nil {
  289. tx.Rollback()
  290. logs.Error("syncFeedtemplet-error-3:", err)
  291. return err
  292. }
  293. ftdetailDateInsertSQl := `insert into ftdetaildate(pastureid,ftid,fid,fname,fweight,sort,date)
  294. values(?,?,?,?,?,?,now())
  295. ON DUPLICATE KEY UPDATE fweight = ?,sort = ?`
  296. _, err = tx.SQL(ftdetailDateInsertSQl, pastureid, ftid, feedtempletName, weight, sort, weight, sort).Execute()
  297. if err != nil {
  298. tx.Rollback()
  299. logs.Error("syncFeedtemplet-error-3:", err)
  300. return err
  301. }
  302. }
  303. }
  304. }
  305. err = tx.Commit()
  306. if err != nil {
  307. tx.Rollback()
  308. logs.Error("syncBar-error-5:", err)
  309. return err
  310. }
  311. return nil
  312. }
  313. type udPastureInfo struct {
  314. Token string `xorm:"token"`
  315. Pastureid string `xorm:"pastureid"`
  316. Werks string `xorm:"werks"`
  317. }
  318. func httpGet(farmId, method string) []interface{} {
  319. url := fmt.Sprintf("https://wdc.unidairy.cn/copartner_downloads/?farmId=%s&method=%s", farmId, method)
  320. res, err := http.Get(url)
  321. if err != nil {
  322. return nil
  323. }
  324. robots, err := ioutil.ReadAll(res.Body)
  325. res.Body.Close()
  326. if err != nil {
  327. return nil
  328. }
  329. var data map[string][]interface{}
  330. json.Unmarshal(robots, &data)
  331. return data["msg"]
  332. }
  333. func UDFeedtempletinfo(token, pastureid string) {
  334. tx := restful.Engine.NewSession()
  335. defer tx.Close()
  336. sqlstr := `SELECT
  337. ft.ID AS recipeId,
  338. ft.TNAME AS recipeName,
  339. f.feedcode AS ingId,
  340. f.FNAME AS ingName,
  341. round( f.dry * ftd.FWEIGHT, 2 ) AS dmQty,
  342. ftd.FWEIGHT AS afQty,
  343. ftd.SORT AS mixNo,
  344. ifnull(round( f.Uprice * ftd.FWEIGHT, 2 ),"") AS recipeCost,
  345. ifnull(fc.FCNAME,"") AS ingType,
  346. f.AllowRatio AS allowableError
  347. FROM
  348. feedtemplet ft
  349. LEFT JOIN ftdetail ftd ON ft.id = ftd.FTID
  350. LEFT JOIN feed f ON ftd.FID = f.id and ft.pastureid = f.pastureid
  351. LEFT JOIN feedclass fc ON f.FCLASS = fc.id where ft.pastureid = ? `
  352. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  353. if err != nil {
  354. logs.Error("syncBar-error-5:", err)
  355. return
  356. }
  357. req := make(map[string]interface{})
  358. req["farmId"] = token
  359. req["rowCount"] = len(data)
  360. req["method"] = "getfeedtempletinfo"
  361. req["resultData"] = data
  362. res := new(UDUploadoverplusdataReq)
  363. res.ApiId = "getKPTData"
  364. // res.ReqMethod = "PARTY"
  365. // res.RegCode = "cpt180511"
  366. // res.Command = "202cb962ac590"
  367. res.Param = req
  368. UDPostPush(res, "application/json")
  369. }
  370. func UDUploadadddata(token, pastureid string) {
  371. logging.Info("UDUploadadddata-pastureid", pastureid)
  372. tx := restful.Engine.NewSession()
  373. defer tx.Close()
  374. sqlstr := `select date_format(d.mydate,'%Y-%m-%d') loadDate,d.SORT tmrNo,d.Times loadShift,
  375. IFNULL(ft.id,((SELECT feedtempletid FROM downloadplandtl2 d2 WHERE d2.pid=d.id LIMIT 1))) recipeId,
  376. IFNULL(d.templetName ,((SELECT feedtempletName FROM downloadplandtl2 d2 WHERE d2.pid=d.id LIMIT 1))) recipeName,
  377. ifnull(f.feedcode,"") as ingId,de.Fname as ingName,ifnull(fc.FCNAME,"") ingType,
  378. ifnull(f.dry,"") dmPct,de.SORT mixNo,de.feedallowratio allowableError, de.LWEIGHT expWeight,
  379. de.ActualWeightMinus actualWeight,ifnull(date_format(timestamp(de.InTime,CONCAT('-',(select processTime from downloadplandtl1_exec where pid = de.pid and sort = de.sort and pastureid = de.pastureid))),'%Y-%m-%d %H:%i:%s'),"") startTime,
  380. date_format(de.InTime,'%Y-%m-%d %H:%i:%s') endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  381. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  382. ORDER BY dr.operatetime DESC LIMIT 1),"") tmrName ,d.tmrtname equipmentId
  383. from downloadedplan d
  384. left join downloadplandtl1 de on de.pid = d.id and de.type = 0
  385. LEFT JOIN feed f on de.Fid=f.id and f.pastureid = de.pastureid LEFT JOIN feedtemplet ft on d.templetName=ft.TNAME
  386. LEFT JOIN feedclass fc on f.FCLASS=fc.id
  387. where d.mydate=date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType=0 and d.pastureid = ?
  388. union all
  389. select date_format(d.mydate,'%Y-%m-%d') loadDate,d.SORT tmrNo,d.Times loadShift,
  390. IFNULL(ft.id,((SELECT feedtempletid FROM downloadplandtl2 d2 WHERE d2.flpid=d.id LIMIT 1))) recipeId,
  391. IFNULL(d.templetName ,((SELECT feedtempletName FROM downloadplandtl2 d2 WHERE d2.flpid=d.id LIMIT 1))) recipeName,
  392. ifnull(f.feedcode,"") as ingId,de.Fname as ingName,ifnull(fc.FCNAME,"") ingType,
  393. ifnull(f.dry,"") dmPct,de.SORT mixNo,de.feedallowratio allowableError, de.LWEIGHT expWeight,
  394. de.ActualWeightMinus actualWeight,ifnull(date_format(timestamp(de.InTime,CONCAT('-',(select processTime from downloadplandtl1_exec where pid = de.pid and sort = de.sort and pastureid = de.pastureid))),'%Y-%m-%d %H:%i:%s'),"") startTime,
  395. date_format(de.InTime,'%Y-%m-%d %H:%i:%s') endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  396. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  397. ORDER BY dr.operatetime DESC LIMIT 1),"") tmrName ,d.tmrtname equipmentId
  398. from downloadedplan d
  399. left join downloadplandtl1 de on de.pid = d.id and de.type = 0
  400. LEFT JOIN feed f on de.Fid=f.id and f.pastureid = de.pastureid LEFT JOIN feedtemplet ft on d.templetName=ft.TNAME
  401. LEFT JOIN feedclass fc on f.FCLASS=fc.id
  402. where d.mydate=date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType=1 and d.pastureid = ?
  403. ORDER BY tmrno`
  404. data, err := tx.SQL(sqlstr, pastureid, pastureid).Query().List()
  405. if err != nil {
  406. logs.Error("UDUploadadddata-error-5:", err)
  407. return
  408. }
  409. req := make(map[string]interface{})
  410. req["farmId"] = token
  411. req["rowCount"] = len(data)
  412. req["method"] = "uploadadddata"
  413. req["resultData"] = data
  414. res := new(UDUploadoverplusdataReq)
  415. res.ApiId = "getKPTData"
  416. res.Param = req
  417. UDPostPush(res, "application/json")
  418. }
  419. func UDuploadnewdiliverdata(token, pastureid string) {
  420. tx := restful.Engine.NewSession()
  421. defer tx.Close()
  422. sqlstr := `SELECT
  423. d.id,IFNULL(ft.id,d2.feedtempletid ) recipeId,
  424. IFNULL(d.templetName,d2.feedtempletName ) recipeName,
  425. 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,
  426. d.Times as dropShift,b.bcode as penId,d2.Fname as penName,fp.CCOUNT as cowCount, d2.SORT as feedingNo,
  427. ROUND(d2.lweight * (dd.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = dd.pid and type = 0) ),2) as expWeight,
  428. d2.ActualWeightMinus as actualWeight, ifnull(date_format(timestamp(d2.InTime,CONCAT('-',d2.processTime)),'%Y-%m-%d %H:%i:%s'),"") as startTime,
  429. date_format(d2.InTime,'%Y-%m-%d %H:%i:%s') as endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  430. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  431. ORDER BY dr.operatetime DESC LIMIT 1),"") as tmrName ,d.tmrtname equipmentId ,
  432. dd.fname,
  433. ROUND(d2.actualweightminus * (dd.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = dd.pid and type = 0) ),2) feedWeight
  434. from downloadedplan d
  435. LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID
  436. LEFT JOIN (select dd.pid as lppid,dd1.* from downloadedplan dd join downloadplandtl1 dd1 on dd1.pid = dd.id and dd1.type = 0
  437. where dd.mydate= date_format(now(),'%Y-%m-%d') and dd.IsCompleted=1 and dd.lpplanType =1 ) dd on dd.lppid = d.pid
  438. LEFT JOIN feedp fp on d2.FBarID=fp.id
  439. left join bar b on fp.barid = b.id and d.pastureid = b.pastureid
  440. left JOIN feedtemplet ft on d.templetName=ft.TNAME
  441. where d.mydate= date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType!=1 and d.pastureid = ? order by tmrno `
  442. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  443. if err != nil {
  444. logs.Error("UDuploadnewdiliverdata-error-1:", err)
  445. return
  446. }
  447. req := make(map[string]interface{})
  448. req["farmId"] = token
  449. req["rowCount"] = len(data)
  450. req["method"] = "uploadnewdiliverdata"
  451. // req["method"] = "uploaddiliverdata"
  452. req["resultData"] = data
  453. res := new(UDUploadoverplusdataReq)
  454. res.ApiId = "getKPTData"
  455. res.Param = req
  456. UDPostPush(res, "application/json")
  457. }
  458. type UDUploadoverplusdataReq struct {
  459. ApiId string `json:"apiId"`
  460. Param interface{} `json:"param"`
  461. ReqMethod string `json:"reqMethod"`
  462. RegCode string `json:"regCode"`
  463. Command string `json:"command"`
  464. }
  465. func UDUploadoverplusdata(token, pastureid string) {
  466. tx := restful.Engine.NewSession()
  467. defer tx.Close()
  468. sqlstr := `SELECT
  469. '' startTime,
  470. '' endTime,
  471. ifnull(br.RemainDate,"") AS overplusDate,
  472. b.bcode AS penId,
  473. fp.barname AS penName,
  474. ifnull(br.Remain,"") AS actualWeight
  475. FROM
  476. barfeedremain br
  477. LEFT JOIN feedp fp ON br.barid = fp.id
  478. left join bar b on fp.barid = b.id
  479. WHERE
  480. RemainDate = date_format(NOW(),'%Y-%m-%d') and br.pastureid = ? `
  481. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  482. if err != nil {
  483. logs.Error("syncBar-error-5:", err)
  484. return
  485. }
  486. req := make(map[string]interface{})
  487. req["farmId"] = token
  488. req["rowCount"] = len(data)
  489. req["method"] = "uploadoverplusdata"
  490. req["code"] = "1"
  491. req["errMessage"] = ""
  492. req["resultData"] = data
  493. res := new(UDUploadoverplusdataReq)
  494. res.ApiId = "getCptData"
  495. res.ReqMethod = "PARTY"
  496. res.RegCode = "cpt180511"
  497. res.Command = "202cb962ac590"
  498. res.Param = req
  499. UDPostPush(res, "application/json")
  500. }
  501. func UDPostPush(data interface{}, contentType string) string {
  502. url := "https://wdc.unidairy.cn/copartner_uploads/"
  503. // 超时时间:5秒
  504. client := &http.Client{Timeout: 20 * time.Second}
  505. jsonStr, _ := json.Marshal(data)
  506. resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
  507. if err != nil {
  508. panic(err)
  509. }
  510. defer resp.Body.Close()
  511. result, _ := ioutil.ReadAll(resp.Body)
  512. fmt.Println(string(result))
  513. return string(result)
  514. }
  515. func getPastureInfo() ([]map[string]interface{}, error) {
  516. tx := restful.Engine.NewSession()
  517. defer tx.Close()
  518. data, err := tx.SQL("select pastureid,token from pasture where token is not null ").Query().List()
  519. if err != nil {
  520. logs.Error("syncBar-error-5:", err)
  521. return nil, err
  522. }
  523. return data, nil
  524. }
  525. type SyncInfo struct {
  526. ApiId string `json:"apiId"`
  527. Param ParamInfo `json:"param"`
  528. }
  529. type ParamInfo struct {
  530. FarmId string `json:"farmId"`
  531. RowCount string `json:"rowCount"`
  532. Method string `json:"method"`
  533. ResultData map[string]interface{} `json:"resultData"`
  534. }