udsync.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 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, feedtempletCode, 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,ccount,ftid,ftname)VALUES(?,?,?,?,?,?)
  205. ON DUPLICATE KEY UPDATE ccount = ? ,ftid = ? ,ftname = ? `
  206. _, err = tx.SQL(insertSql, pastureid, barName, barId, cowCount, ftId, tname, cowCount, ftId, tname).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. }
  317. func httpGet(farmId, method string) []interface{} {
  318. url := fmt.Sprintf("https://wdc.unidairy.cn/copartner_downloads/?farmId=%s&method=%s", farmId, method)
  319. res, err := http.Get(url)
  320. if err != nil {
  321. return nil
  322. }
  323. robots, err := ioutil.ReadAll(res.Body)
  324. res.Body.Close()
  325. if err != nil {
  326. return nil
  327. }
  328. var data map[string][]interface{}
  329. json.Unmarshal(robots, &data)
  330. return data["msg"]
  331. }
  332. func UDFeedtempletinfo(token, pastureid string) {
  333. tx := restful.Engine.NewSession()
  334. defer tx.Close()
  335. sqlstr := `SELECT
  336. ft.ID AS recipeId,
  337. ft.TNAME AS recipeName,
  338. f.feedcode AS ingId,
  339. f.FNAME AS ingName,
  340. round( f.dry * ftd.FWEIGHT, 2 ) AS dmQty,
  341. ftd.FWEIGHT AS afQty,
  342. ftd.SORT AS mixNo,
  343. ifnull(round( f.Uprice * ftd.FWEIGHT, 2 ),"") AS recipeCost,
  344. ifnull(fc.FCNAME,"") AS ingType,
  345. f.AllowRatio AS allowableError
  346. FROM
  347. feedtemplet ft
  348. LEFT JOIN ftdetail ftd ON ft.id = ftd.FTID
  349. LEFT JOIN feed f ON ftd.FID = f.id and ft.pastureid = f.pastureid
  350. LEFT JOIN feedclass fc ON f.FCLASS = fc.id where ft.pastureid = ? `
  351. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  352. if err != nil {
  353. logs.Error("syncBar-error-5:", err)
  354. return
  355. }
  356. req := make(map[string]interface{})
  357. req["farmId"] = token
  358. req["rowCount"] = len(data)
  359. req["method"] = "getfeedtempletinfo"
  360. req["resultData"] = data
  361. res := new(UDUploadoverplusdataReq)
  362. res.ApiId = "getKPTData"
  363. // res.ReqMethod = "PARTY"
  364. // res.RegCode = "cpt180511"
  365. // res.Command = "202cb962ac590"
  366. res.Param = req
  367. UDPostPush(res, "application/json")
  368. }
  369. func UDUploadadddata(token, pastureid string) {
  370. logging.Info("UDUploadadddata-pastureid", pastureid)
  371. tx := restful.Engine.NewSession()
  372. defer tx.Close()
  373. sqlstr := `select date_format(d.mydate,'%Y-%m-%d') loadDate,d.SORT tmrNo,d.Times loadShift,
  374. IFNULL(ft.id,((SELECT feedtempletid FROM downloadplandtl2 d2 WHERE d2.pid=d.id LIMIT 1))) recipeId,
  375. IFNULL(d.templetName ,((SELECT feedtempletName FROM downloadplandtl2 d2 WHERE d2.pid=d.id LIMIT 1))) recipeName,
  376. ifnull(f.feedcode,"") as ingId,de.Fname as ingName,ifnull(fc.FCNAME,"") ingType,
  377. ifnull(f.dry,"") dmPct,de.SORT mixNo,de.feedallowratio allowableError, de.LWEIGHT expWeight,
  378. 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,
  379. date_format(de.InTime,'%Y-%m-%d %H:%i:%s') endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  380. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  381. ORDER BY dr.operatetime DESC LIMIT 1),"") tmrName ,d.tmrtname equipmentId
  382. from downloadedplan d
  383. left join downloadplandtl1 de on de.pid = d.id and de.type = 0
  384. LEFT JOIN feed f on de.Fid=f.id and f.pastureid = de.pastureid LEFT JOIN feedtemplet ft on d.templetName=ft.TNAME
  385. LEFT JOIN feedclass fc on f.FCLASS=fc.id
  386. where d.mydate=date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType=0 and d.pastureid = ?
  387. union all
  388. select date_format(d.mydate,'%Y-%m-%d') loadDate,d.SORT tmrNo,d.Times loadShift,
  389. IFNULL(ft.id,((SELECT feedtempletid FROM downloadplandtl2 d2 WHERE d2.flpid=d.id LIMIT 1))) recipeId,
  390. IFNULL(d.templetName ,((SELECT feedtempletName FROM downloadplandtl2 d2 WHERE d2.flpid=d.id LIMIT 1))) recipeName,
  391. ifnull(f.feedcode,"") as ingId,de.Fname as ingName,ifnull(fc.FCNAME,"") ingType,
  392. ifnull(f.dry,"") dmPct,de.SORT mixNo,de.feedallowratio allowableError, de.LWEIGHT expWeight,
  393. 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,
  394. date_format(de.InTime,'%Y-%m-%d %H:%i:%s') endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  395. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  396. ORDER BY dr.operatetime DESC LIMIT 1),"") tmrName ,d.tmrtname equipmentId
  397. from downloadedplan d
  398. left join downloadplandtl1 de on de.pid = d.id and de.type = 0
  399. LEFT JOIN feed f on de.Fid=f.id and f.pastureid = de.pastureid LEFT JOIN feedtemplet ft on d.templetName=ft.TNAME
  400. LEFT JOIN feedclass fc on f.FCLASS=fc.id
  401. where d.mydate=date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType=1 and d.pastureid = ?
  402. ORDER BY tmrno`
  403. data, err := tx.SQL(sqlstr, pastureid, pastureid).Query().List()
  404. if err != nil {
  405. logs.Error("UDUploadadddata-error-5:", err)
  406. return
  407. }
  408. req := make(map[string]interface{})
  409. req["farmId"] = token
  410. req["rowCount"] = len(data)
  411. req["method"] = "uploadadddata"
  412. req["resultData"] = data
  413. res := new(UDUploadoverplusdataReq)
  414. res.ApiId = "getKPTData"
  415. res.Param = req
  416. UDPostPush(res, "application/json")
  417. }
  418. func UDuploadnewdiliverdata(token, pastureid string) {
  419. tx := restful.Engine.NewSession()
  420. defer tx.Close()
  421. sqlstr := `SELECT
  422. d.id,IFNULL(ft.id,d2.feedtempletid ) recipeId,
  423. IFNULL(d.templetName,d2.feedtempletName ) recipeName,
  424. 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,
  425. d.Times as dropShift,b.bcode as penId,d2.Fname as penName,fp.CCOUNT as cowCount, d2.SORT as feedingNo,
  426. ROUND(d2.lweight * (dd.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = dd.pid and type = 0) ),2) as expWeight,
  427. d2.ActualWeightMinus as actualWeight, ifnull(date_format(timestamp(d2.InTime,CONCAT('-',d2.processTime)),'%Y-%m-%d %H:%i:%s'),"") as startTime,
  428. date_format(d2.InTime,'%Y-%m-%d %H:%i:%s') as endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
  429. WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
  430. ORDER BY dr.operatetime DESC LIMIT 1),"") as tmrName ,d.tmrtname equipmentId ,
  431. dd.fname,
  432. ROUND(d2.actualweightminus * (dd.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = dd.pid and type = 0) ),2) feedWeight
  433. from downloadedplan d
  434. LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID
  435. LEFT JOIN (select dd.pid as lppid,dd1.* from downloadedplan dd join downloadplandtl1 dd1 on dd1.pid = dd.id and dd1.type = 0
  436. where dd.mydate= date_format(now(),'%Y-%m-%d') and dd.IsCompleted=1 and dd.lpplanType =1 ) dd on dd.lppid = d.pid
  437. LEFT JOIN feedp fp on d2.FBarID=fp.id
  438. left join bar b on fp.barid = b.id and d.pastureid = b.pastureid
  439. left JOIN feedtemplet ft on d.templetName=ft.TNAME
  440. where d.mydate= date_format(now(),'%Y-%m-%d') and d.IsCompleted=1 and d.lpplanType!=1 and d.pastureid = ? order by tmrno `
  441. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  442. if err != nil {
  443. logs.Error("UDuploadnewdiliverdata-error-1:", err)
  444. return
  445. }
  446. req := make(map[string]interface{})
  447. req["farmId"] = token
  448. req["rowCount"] = len(data)
  449. req["method"] = "uploadnewdiliverdata"
  450. // req["method"] = "uploaddiliverdata"
  451. req["resultData"] = data
  452. res := new(UDUploadoverplusdataReq)
  453. res.ApiId = "getKPTData"
  454. res.Param = req
  455. UDPostPush(res, "application/json")
  456. }
  457. type UDUploadoverplusdataReq struct {
  458. ApiId string `json:"apiId"`
  459. Param interface{} `json:"param"`
  460. ReqMethod string `json:"reqMethod"`
  461. RegCode string `json:"regCode"`
  462. Command string `json:"command"`
  463. }
  464. func UDUploadoverplusdata(token, pastureid string) {
  465. tx := restful.Engine.NewSession()
  466. defer tx.Close()
  467. sqlstr := `SELECT
  468. '' startTime,
  469. '' endTime,
  470. ifnull(br.RemainDate,"") AS overplusDate,
  471. b.bcode AS penId,
  472. fp.barname AS penName,
  473. ifnull(br.Remain,"") AS actualWeight
  474. FROM
  475. barfeedremain br
  476. LEFT JOIN feedp fp ON br.barid = fp.id
  477. left join bar b on fp.barid = b.id
  478. WHERE
  479. RemainDate = date_format(NOW(),'%Y-%m-%d') and br.pastureid = ? `
  480. data, err := tx.SQL(sqlstr, pastureid).Query().List()
  481. if err != nil {
  482. logs.Error("syncBar-error-5:", err)
  483. return
  484. }
  485. req := make(map[string]interface{})
  486. req["farmId"] = token
  487. req["rowCount"] = len(data)
  488. req["method"] = "uploadoverplusdata"
  489. req["code"] = "1"
  490. req["errMessage"] = ""
  491. req["resultData"] = data
  492. res := new(UDUploadoverplusdataReq)
  493. res.ApiId = "getCptData"
  494. res.ReqMethod = "PARTY"
  495. res.RegCode = "cpt180511"
  496. res.Command = "202cb962ac590"
  497. res.Param = req
  498. UDPostPush(res, "application/json")
  499. }
  500. func UDPostPush(data interface{}, contentType string) string {
  501. url := "https://wdc.unidairy.cn/copartner_uploads/"
  502. // 超时时间:5秒
  503. client := &http.Client{Timeout: 20 * time.Second}
  504. jsonStr, _ := json.Marshal(data)
  505. resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
  506. if err != nil {
  507. panic(err)
  508. }
  509. defer resp.Body.Close()
  510. result, _ := ioutil.ReadAll(resp.Body)
  511. fmt.Println(string(result))
  512. return string(result)
  513. }
  514. func getPastureInfo() ([]map[string]interface{}, error) {
  515. tx := restful.Engine.NewSession()
  516. defer tx.Close()
  517. data, err := tx.SQL("select pastureid,token from pasture where token is not null ").Query().List()
  518. if err != nil {
  519. logs.Error("syncBar-error-5:", err)
  520. return nil, err
  521. }
  522. return data, nil
  523. }
  524. type SyncInfo struct {
  525. ApiId string `json:"apiId"`
  526. Param ParamInfo `json:"param"`
  527. }
  528. type ParamInfo struct {
  529. FarmId string `json:"farmId"`
  530. RowCount string `json:"rowCount"`
  531. Method string `json:"method"`
  532. ResultData map[string]interface{} `json:"resultData"`
  533. }