udsync.go 19 KB

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