4dbb46bae46856d1bfd864f60efeeff84aac78d2.svn-base 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package api
  2. import (
  3. "../../pkg/logging"
  4. "../../pkg/util"
  5. "../../routers/restful"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/Anderson-Lu/gofasion/gofasion"
  9. "github.com/gin-gonic/gin"
  10. "github.com/hequan2017/go-admin/pkg/e"
  11. "github.com/robfig/cron"
  12. "io/ioutil"
  13. "log"
  14. "net/http"
  15. "strconv"
  16. "time"
  17. "../../pkg/app"
  18. //"time"
  19. "../../pkg/setting"
  20. )
  21. func GetDorm(c *gin.Context) {
  22. appG := app.Gin{C: c}
  23. if setting.ServerSetting.GRFD != "1"{ //格润富德
  24. appG.Response(http.StatusOK, e.SUCCESS, nil)
  25. return
  26. }
  27. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  28. fsion := gofasion.NewFasion(string(dataByte))
  29. pastureid := fsion.Get("pastureid").ValueStr()
  30. err := GetMaterialCategory(pastureid) //饲料分类
  31. if err != nil{
  32. appG.Response(http.StatusOK, e.ERROR, "饲料分类同步失败:"+err.Error())
  33. return
  34. }
  35. err = GetCowCategory(pastureid) // 牲畜类别
  36. if err != nil{
  37. appG.Response(http.StatusOK, e.ERROR, "牲畜类别同步失败:"+err.Error())
  38. return
  39. }
  40. err = GetDormBar(pastureid) // 栏舍
  41. if err != nil{
  42. appG.Response(http.StatusOK, e.ERROR, "栏舍同步失败:"+err.Error())
  43. return
  44. }
  45. err = GetMaterial(pastureid) //饲料
  46. if err != nil{
  47. appG.Response(http.StatusOK, e.ERROR, "饲料同步失败:"+err.Error())
  48. return
  49. }
  50. err = GetFormula(pastureid) // 配方
  51. if err != nil{
  52. appG.Response(http.StatusOK, e.ERROR, "配方同步失败:"+err.Error())
  53. return
  54. }
  55. appG.Response(http.StatusOK, e.SUCCESS, nil)
  56. }
  57. func GetDormBar(pastureid string)error {
  58. //appG := app.Gin{C: c}
  59. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  60. //fsion := gofasion.NewFasion(string(dataByte))
  61. //sqlnamestr := fsion.Get("name").ValueStr()
  62. //offset := fsion.Get("offset").ValueDefaultInt(0)
  63. //pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  64. //returntype := fsion.Get("returntype").ValueDefaultStr("map")
  65. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getDorm")
  66. if data != nil {
  67. var feedp util.Feedp
  68. json.Unmarshal(data, &feedp)
  69. status := feedp.Status
  70. feedpData := feedp.Data
  71. if status == 0 {
  72. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  73. //fsion := gofasion.NewFasion(string(dataByte))
  74. //pastureid := fsion.Get("pastureid").ValueStr()
  75. // 开启事务
  76. tx := restful.Engine.NewSession()
  77. err := tx.Begin()
  78. defer func() {
  79. switch {
  80. case err != nil:
  81. logging.Error("tx.Begin 事务启动失败__error:", err)
  82. if tx != nil {
  83. tx.Rollback()
  84. }
  85. default:
  86. if tx != nil {
  87. err = tx.Commit()
  88. }
  89. }
  90. if tx != nil {
  91. tx.Close()
  92. }
  93. }()
  94. //feedpData := feedpData[:10]
  95. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,bname FROM bar WHERE pastureid = ?",pastureid).QueryString()
  96. mt := make(map[string]string)
  97. for _, v := range valuesMap {
  98. mt[v["bname"]] = v["backup2"]
  99. }
  100. ise := false
  101. for _, val := range feedpData {
  102. penId := val.PenId
  103. penName := val.PenName
  104. cowCount := val.CowCount
  105. ccountRatio := val.CcountRatio
  106. //createdAt := val.CreatedAt
  107. modifiedAt := val.ModifiedAt
  108. isexec := false
  109. if _,ok :=mt[penName]; ok{
  110. if mt[penName] == modifiedAt{
  111. continue
  112. }
  113. }else{
  114. isexec = true
  115. }
  116. ise = true
  117. fmt.Println(penId, penName, cowCount, ccountRatio,modifiedAt,mt[penName])
  118. ids, err := setting.SnowIds.NextId()
  119. if err != nil {
  120. ids = time.Now().UnixNano()
  121. logging.Info("create SnowIds err", err)
  122. }
  123. _, err = tx.Exec("insert into bar(id,pastureid,backup1,backup2,bname,enable) "+
  124. " values(?,?,?,?,?,0)"+
  125. " ON DUPLICATE KEY UPDATE backup1=?,backup2=?",
  126. ids, pastureid, penId,modifiedAt, penName,penId,modifiedAt)
  127. if err != nil {
  128. return err
  129. }
  130. //_, err = tx.Exec("insert into feedp(id,pastureid,barid,barname,ccount,ratio,ccountratio) "+
  131. // " values(?,?,(select id from bar where pastureid=? and bname=?),?,(if(?='',0,?)),REPLACE(?,'%',''),REPLACE(?,'%','')*(if(?='',0,?)))"+
  132. // " ON DUPLICATE KEY UPDATE barname=?,ccount=(if(?='',0,?)),ratio=REPLACE(?,'%',''),ccountratio=REPLACE(?,'%','')*(if(?='',0,?))",
  133. // ids, pastureid, pastureid, penName, penName, cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount, penName,
  134. // cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount)
  135. if isexec{ //不配置配方,不去新增feedp
  136. _, err = tx.Exec(`UPDATE feedp set ccount=(if(?='',0,?)),ratio=REPLACE(?,'%',''),ccountratio=REPLACE(?,'%','')*(if(?='',0,?))/100
  137. where barid = (select id from bar where pastureid=? and bname=?) and pastureid = ?`,
  138. cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount, pastureid,penName,pastureid)
  139. if err != nil {
  140. return err
  141. }
  142. _, err = tx.Exec(`UPDATE fpdetail fpd
  143. JOIN feedp fp ON fp.pastureid = fpd.pastureid AND fp.barid =fpd.barid
  144. SET
  145. fpd.weight = weight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio)
  146. WHERE fpd.pastureid = ? and fp.barid =(select id from bar where pastureid=? and bname=?)`,pastureid,pastureid,penName)
  147. if err != nil {
  148. return err
  149. }
  150. _, err = tx.Exec("call updateLPPbyFPChange((select id from bar where pastureid=? and bname=?),?)",pastureid,penName,pastureid)
  151. if err != nil {
  152. return err
  153. }
  154. }
  155. }
  156. if ise{
  157. _, err = tx.Exec(`UPDATE fpdetail fpd
  158. JOIN feedp fp ON fp.pastureid = fpd.pastureid AND fp.barid =fpd.barid
  159. SET
  160. fp.feedweight = fp.feedweight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio),
  161. fp.ftweight = fp.ftweight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio),
  162. fpd.ccountradio =fp.ratio ,
  163. fpd.cowcount =fp.ccount
  164. WHERE fpd.pastureid = ? `,pastureid)
  165. if err != nil {
  166. return err
  167. }
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. func GetMaterial(pastureid string) error{
  174. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterial")
  175. if data != nil {
  176. var feed util.Feed
  177. json.Unmarshal(data, &feed)
  178. status := feed.Status
  179. feedpData := feed.Data
  180. if status == 0 {
  181. // 开启事务
  182. tx := restful.Engine.NewSession()
  183. err := tx.Begin()
  184. defer func() {
  185. switch {
  186. case err != nil:
  187. logging.Error("tx.Begin 事务启动失败__error:", err)
  188. if tx != nil {
  189. tx.Rollback()
  190. }
  191. default:
  192. if tx != nil {
  193. err = tx.Commit()
  194. }
  195. }
  196. if tx != nil {
  197. tx.Close()
  198. }
  199. }()
  200. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,fname FROM feed WHERE pastureid = ?",pastureid).QueryString()
  201. mt := make(map[string]string)
  202. for _, v := range valuesMap {
  203. mt[v["fname"]] = v["backup2"]
  204. }
  205. for _, val := range feedpData {
  206. fId := val.FId
  207. fName := val.FName
  208. price := val.Price
  209. dryMatter := val.DryMatter
  210. //autoChange := val.AutoChange
  211. //allowDev := val.AllowDev
  212. feedclassName := val.FeedclassName
  213. createdAt := val.CreatedAt
  214. modifiedAt := val.ModifiedAt
  215. if _,ok :=mt[fName]; ok{
  216. if mt[fName] == modifiedAt{
  217. continue
  218. }
  219. }
  220. fmt.Println(fName, feedclassName, createdAt, modifiedAt)
  221. ids, err := setting.SnowIds.NextId()
  222. if err != nil {
  223. ids = time.Now().UnixNano()
  224. logging.Info("create SnowIds err", err)
  225. }
  226. _, err = tx.Exec("insert into feed(id,pastureid,backup1,backup2,fname,fclass,fclassid,uprice,dry) "+
  227. " values(?,?,?,?,?,?,(select id from feedclass where fcname=? and pastureid=?),(if(?='',0,?)),(if(?='' or ?='~',0,?)))"+
  228. " ON DUPLICATE KEY UPDATE backup1=?,backup2=?,fname=?,fclass=?,fclassid=(select id from feedclass where fcname=? and pastureid=?),uprice=(if(?='',0,?)),dry=(if(?='' or ?='~',0,?))",
  229. ids, pastureid, fId,modifiedAt, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter, fId,modifiedAt, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter)
  230. if err != nil {
  231. return err
  232. }
  233. }
  234. }
  235. }
  236. return nil
  237. }
  238. func GetMaterialCategory(pastureid string)error {
  239. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterialCategory")
  240. if data != nil {
  241. var feedClass util.FeedClass
  242. json.Unmarshal(data, &feedClass)
  243. status := feedClass.Status
  244. feedpData := feedClass.Data
  245. if status == 0 {
  246. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  247. //fsion := gofasion.NewFasion(string(dataByte))
  248. //pastureid := fsion.Get("pastureid").ValueStr()
  249. // 开启事务
  250. tx := restful.Engine.NewSession()
  251. err := tx.Begin()
  252. defer func() {
  253. switch {
  254. case err != nil:
  255. logging.Error("tx.Begin 事务启动失败__error:", err)
  256. if tx != nil {
  257. tx.Rollback()
  258. }
  259. default:
  260. if tx != nil {
  261. err = tx.Commit()
  262. }
  263. }
  264. if tx != nil {
  265. tx.Close()
  266. }
  267. }()
  268. for _, val := range feedpData {
  269. feedclassName := val.FeedclassName
  270. feedclassId := val.FeedclassId
  271. createdAt := val.CreatedAt
  272. modifiedAt := val.ModifiedAt
  273. fmt.Println(feedclassId, feedclassName, createdAt, modifiedAt)
  274. ids, err := setting.SnowIds.NextId()
  275. if err != nil {
  276. ids = time.Now().UnixNano()
  277. logging.Info("create SnowIds err", err)
  278. }
  279. _, err = tx.Exec("insert into feedclass(id,pastureid,fcname,backup1,bigfeedclassid,bigfeedclassname,sort,`enable`) values(?,?,?,?,?,?,?,?)"+
  280. " ON DUPLICATE KEY UPDATE fcname=?,backup1=?",
  281. ids, pastureid, feedclassName, feedclassId, 3, "精粗各半", 0, 1, feedclassName, feedclassId)
  282. if err != nil {
  283. return err
  284. }
  285. }
  286. }
  287. }
  288. return nil
  289. }
  290. func GetCowCategory(pastureid string)error {
  291. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getCowCategory")
  292. if data != nil {
  293. var cowClass util.CowClass
  294. json.Unmarshal(data, &cowClass)
  295. status := cowClass.Status
  296. feedpData := cowClass.Data
  297. if status == 0 {
  298. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  299. //fsion := gofasion.NewFasion(string(dataByte))
  300. //pastureid := fsion.Get("pastureid").ValueStr()
  301. // 开启事务
  302. tx := restful.Engine.NewSession()
  303. err := tx.Begin()
  304. defer func() {
  305. switch {
  306. case err != nil:
  307. logging.Error("tx.Begin 事务启动失败__error:", err)
  308. if tx != nil {
  309. tx.Rollback()
  310. }
  311. default:
  312. if tx != nil {
  313. err = tx.Commit()
  314. }
  315. }
  316. if tx != nil {
  317. tx.Close()
  318. }
  319. }()
  320. for _, val := range feedpData {
  321. cowClassCode := val.CowClassCode
  322. cowClassName := val.CowClassName
  323. createdAt := val.CreatedAt
  324. modifiedAt := val.ModifiedAt
  325. fmt.Println(cowClassCode, cowClassName, createdAt, modifiedAt)
  326. ids, err := setting.SnowIds.NextId()
  327. if err != nil {
  328. ids = time.Now().UnixNano()
  329. logging.Info("create SnowIds err", err)
  330. }
  331. _, err = tx.Exec("insert into cowclass(id,pastureid,backup1,classname) values(?,?,?,?)"+
  332. " ON DUPLICATE KEY UPDATE backup1=?,classname=?",
  333. ids, pastureid, cowClassCode, cowClassName, cowClassCode, cowClassName)
  334. if err != nil {
  335. return err
  336. }
  337. }
  338. }
  339. }
  340. return nil
  341. }
  342. func GetFormula(pastureid string)error {
  343. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getFormula")
  344. if data != nil {
  345. var feedTemplet util.FeedTemplet
  346. json.Unmarshal(data, &feedTemplet)
  347. status := feedTemplet.Status
  348. feedpData := feedTemplet.Data
  349. println(len(feedpData))
  350. if status == 0 {
  351. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  352. //fsion := gofasion.NewFasion(string(dataByte))
  353. //pastureid := fsion.Get("pastureid").ValueStr()
  354. // 开启事务
  355. tx := restful.Engine.NewSession()
  356. err := tx.Begin()
  357. defer func() {
  358. switch {
  359. case err != nil:
  360. logging.Error("tx.Begin 事务启动失败__error:", err)
  361. if tx != nil {
  362. tx.Rollback()
  363. }
  364. default:
  365. if tx != nil {
  366. err = tx.Commit()
  367. }
  368. }
  369. if tx != nil {
  370. tx.Close()
  371. }
  372. }()
  373. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,tname FROM feedtemplet WHERE pastureid = ?",pastureid).QueryString()
  374. mt := make(map[string]string)
  375. for _, v := range valuesMap {
  376. mt[v["tname"]] = v["backup2"]
  377. }
  378. for _, val := range feedpData {
  379. ftId := val.FtId
  380. ftName := val.FtName
  381. feedList := val.FeedList
  382. //createdAt := val.CreatedAt
  383. modifiedAt := val.ModifiedAt
  384. println(val.FtName)
  385. if _,ok :=mt[ftName]; ok{
  386. if mt[ftName] == modifiedAt{
  387. continue
  388. }
  389. }
  390. println(val.FtName)
  391. ids, err := setting.SnowIds.NextId()
  392. if err != nil {
  393. ids = time.Now().UnixNano()
  394. logging.Info("create SnowIds err", err)
  395. }
  396. _, err = tx.Exec("insert into feedtemplet(id,pastureid,ccid,backup1,backup2,tname,fttypeid,fttype) values(?,?,null,?,?,?,1,'饲喂配方')"+
  397. " ON DUPLICATE KEY UPDATE backup1=?,tname=?,backup2= ?",
  398. ids, pastureid, ftId,modifiedAt, ftName, ftId, ftName,modifiedAt)
  399. if err != nil {
  400. return err
  401. }
  402. tx.Exec("delete from ftdetail where ftid=(select id from feedtemplet where pastureid=? and tname=?)",pastureid, ftName)
  403. for _, val := range feedList {
  404. fId := val.FId
  405. fName := val.FName
  406. fodderWeight := val.FodderWeight
  407. mixNo := val.MixNo
  408. ids, err := setting.SnowIds.NextId()
  409. if err != nil {
  410. ids = time.Now().UnixNano()
  411. logging.Info("create SnowIds err", err)
  412. }
  413. _, err = tx.Exec("insert into ftdetail(id,pastureid,ftid,fid,fname,fweight,sort) "+
  414. " values(?,?,(select id from feedtemplet where pastureid=? and tname=? limit 1),(select id from feed where pastureid=? and backup1=? limit 1),?,(if(?='',0,?)),(if(?='',0,?)))" +
  415. "ON DUPLICATE KEY UPDATE fweight=(if(?='',0,?)),sort=(if(?='',0,?))",
  416. ids, pastureid, pastureid, ftName, pastureid, fId, fName, fodderWeight, fodderWeight, mixNo, mixNo,fodderWeight,fodderWeight, mixNo, mixNo)
  417. if err != nil {
  418. return err
  419. }
  420. _, err = tx.Exec("call updateFPbyFTChange((select id from feedtemplet where pastureid=? and tname=? limit 1),?,(select fttypeid from feedtemplet where pastureid=? and tname=? limit 1))",pastureid,ftName,pastureid,pastureid,ftName)
  421. if err != nil {
  422. return err
  423. }
  424. }
  425. }
  426. }
  427. }
  428. return nil
  429. }
  430. func AddOtherDevice() {
  431. tx := restful.Engine.NewSession()
  432. err := tx.Begin()
  433. defer func() {
  434. switch {
  435. case err != nil:
  436. logging.Error("tx.Begin 事务启动失败__error:", err)
  437. if tx != nil {
  438. tx.Rollback()
  439. }
  440. default:
  441. if tx != nil {
  442. err = tx.Commit()
  443. }
  444. }
  445. if tx != nil {
  446. tx.Close()
  447. }
  448. }()
  449. queryIdList, err1 := tx.SQL("select d.id,d.projname projName from downloadedplan d where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  450. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  451. queryDataList, err2 := tx.SQL("select d.id id,d.projname projName,round(d.lweight,2) lWeight,date_format(d.mydate,'%Y-%m-%d') as loadDate,d.SORT as tmrNo,d.Times as times,ft.backup1 as ftId,d.templetName as ftName,f.backup1 as fId,"+
  452. " de.Fname as fName,fc.FCNAME as feedclassName,de.SORT as sort,de.feedallowratio as allowableError, "+
  453. " cast(ifnull(de.LWEIGHT,0) as decimal(10,2)) as expWeight,cast(ifnull(de.ActualWeightMinus,0) as decimal(10,2)) as actualWeight,"+
  454. " date_format(de.intime,'%Y-%m-%d %H:%i:%s') as endTime ,d.tmrtname as tName ,'' dName "+
  455. " from downloadedplan d LEFT JOIN downloadplandtl1 de on d.id=de.PID and d.pastureid=de.pastureid "+
  456. " LEFT JOIN feed f on de.fid=f.id and f.pastureid=d.pastureid LEFT JOIN feedtemplet ft on d.tempid=ft.id and ft.pastureid=d.pastureid "+
  457. " LEFT JOIN feedclass fc on f.fclassid=fc.id and fc.pastureid=d.pastureid "+
  458. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  459. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  460. if err1 == nil && err2 == nil {
  461. for i := 0; i < len(queryIdList); i++ {
  462. id := queryIdList[i]["id"]
  463. projName := queryIdList[i]["projName"]
  464. listData := make(map[string]interface{})
  465. listData["farmId"] = 4090
  466. listData["id"] = id
  467. listData["projName"] = projName
  468. feedList := []interface{}{}
  469. for j := 0; j < len(queryDataList); j++ {
  470. id2 := queryDataList[j]["id"]
  471. if id == id2 {
  472. dataList := make(map[string]interface{})
  473. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  474. dataList["times"] = queryDataList[j]["times"]
  475. dataList["loadDate"] = queryDataList[j]["loadDate"]
  476. dataList["ftId"] = queryDataList[j]["ftId"]
  477. dataList["ftName"] = queryDataList[j]["ftName"]
  478. dataList["fId"] = queryDataList[j]["fId"]
  479. dataList["tName"] = queryDataList[j]["tName"]
  480. dataList["feedclassName"] = queryDataList[j]["feedclassName"]
  481. dataList["fName"] = queryDataList[j]["fName"]
  482. dataList["sort"] = queryDataList[j]["sort"]
  483. dataList["allowableError"] = queryDataList[j]["allowableError"]
  484. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  485. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  486. lWeight, _ := strconv.ParseFloat(queryDataList[j]["lWeight"].(string), 64)
  487. dataList["expWeight"] = expWeight
  488. dataList["actualWeight"] = actualWeight
  489. dataList["lWeight"] = lWeight
  490. dataList["endTime"] = queryDataList[j]["endTime"]
  491. dataList["dName"] = queryDataList[j]["dName"]
  492. feedList = append(feedList, dataList)
  493. }
  494. }
  495. listData["data"] = feedList
  496. jsonStr, _ := json.Marshal(listData)
  497. postData := make(map[string]string)
  498. postData["formData"] = string(jsonStr)
  499. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDevice", &postData)
  500. if err != nil{
  501. fmt.Println("AddOtherDevice err:",err)
  502. logging.Error("AddOtherDevice err:",err)
  503. }
  504. }
  505. }
  506. }
  507. func AddOtherDeviceFeeding() {
  508. tx := restful.Engine.NewSession()
  509. err := tx.Begin()
  510. defer func() {
  511. switch {
  512. case err != nil:
  513. logging.Error("tx.Begin 事务启动失败__error:", err)
  514. if tx != nil {
  515. tx.Rollback()
  516. }
  517. default:
  518. if tx != nil {
  519. err = tx.Commit()
  520. }
  521. }
  522. if tx != nil {
  523. tx.Close()
  524. }
  525. }()
  526. queryIdList, err1 := tx.SQL("select d.id,d.projname projName from downloadedplan d where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  527. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid')").Query().List()
  528. queryDataList, err2 := tx.SQL("SELECT d.id id,d.projname projName,date_format(d.mydate,'%Y-%m-%d') as dropDate,d.Times as times,d.SORT as tmrNo,"+
  529. " bar.backup1 as penId,d2.fname as penName,fp.CCOUNT as cowCount, d2.SORT as sort,d.templetName ftName, ft.backup1 ftId,"+
  530. " ifnull(d2.LWEIGHT,0) as expWeight,d2.ActualWeightMinus as actualWeight,date_format(d2.intime,'%Y-%m-%d %H:%i:%s') as endTime,d.tmrtname as tName,'' dName "+
  531. " from downloadedplan d LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID and d.pastureid=d2.pastureid "+
  532. " LEFT JOIN bar on d2.fbarid=bar.id and bar.pastureid=d2.pastureid LEFT JOIN feedp fp on d2.fbarid=fp.barid and fp.pastureid=d2.pastureid "+
  533. " LEFT JOIN feedtemplet ft on d.tempid=ft.id and ft.pastureid=d.pastureid "+
  534. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  535. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  536. if err1 == nil && err2 == nil {
  537. for i := 0; i < len(queryIdList); i++ {
  538. id := queryIdList[i]["id"]
  539. projName := queryIdList[i]["projName"]
  540. listData := make(map[string]interface{})
  541. listData["farmId"] = 4090
  542. listData["id"] = id
  543. listData["projName"] = projName
  544. feedList := []interface{}{}
  545. for j := 0; j < len(queryDataList); j++ {
  546. id2 := queryDataList[j]["id"]
  547. if id == id2 {
  548. dataList := make(map[string]interface{})
  549. dataList["dropDate"] = queryDataList[j]["dropDate"]
  550. dataList["times"] = queryDataList[j]["times"]
  551. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  552. dataList["tName"] = queryDataList[j]["tName"]
  553. dataList["dName"] = queryDataList[j]["dName"]
  554. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  555. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  556. dataList["expWeight"] = expWeight
  557. dataList["actualWeight"] = actualWeight
  558. dataList["endTime"] = queryDataList[j]["endTime"]
  559. dataList["penId"] = queryDataList[j]["penId"]
  560. dataList["penName"] = queryDataList[j]["penName"]
  561. dataList["cowCount"] = queryDataList[j]["cowCount"]
  562. dataList["sort"] = queryDataList[j]["sort"]
  563. dataList["ftId"] = queryDataList[j]["ftId"]
  564. dataList["ftName"] = queryDataList[j]["ftName"]
  565. feedList = append(feedList, dataList)
  566. }
  567. }
  568. listData["data"] = feedList
  569. jsonStr, _ := json.Marshal(listData)
  570. postData := make(map[string]string)
  571. postData["formData"] = string(jsonStr)
  572. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDeviceFeeding", &postData)
  573. if err != nil{
  574. fmt.Println("AddOtherDeviceFeeding err:",err)
  575. logging.Error("AddOtherDeviceFeeding err:",err)
  576. }else {
  577. _,err = tx.Exec(`update downloadedplan set isuse=1 where mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and isuse=0
  578. and pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight'
  579. AND table_schema = 'tmrwatch' AND column_name = 'pastureid') `)
  580. if err != nil{
  581. logging.Error("update downloadedplan err:",err)
  582. }
  583. }
  584. }
  585. }
  586. //data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:86/kaps-cow/api/tmr/getMaterial")
  587. }
  588. func PostTmrData(){
  589. defer func() { // 必须要先声明defer
  590. if err := recover(); err != nil {
  591. fmt.Printf("CronTest pnic err%+v \n", err)
  592. logging.Error("panic recover err ", err)
  593. //println("pnic err",err.(error).Error()) // 这里的err其实就是panic传入的内容,55
  594. }
  595. }()
  596. println("开始执行")
  597. AddOtherDevice()
  598. AddOtherDeviceFeeding()
  599. }
  600. func CronGRFD() {
  601. defer func() { // 必须要先声明defer
  602. if err := recover(); err != nil {
  603. fmt.Printf("CronTest pnic err%+v \n", err)
  604. logging.Error("panic recover err ", err)
  605. }
  606. }()
  607. log.Println("Starting CronWXwork...")
  608. c := cron.New()
  609. _, err := c.AddFunc("0/3 * * * *", PostTmrData) //* * * * *, 从分钟开始 每5分钟
  610. if err != nil {
  611. println("cron4 err", err.Error())
  612. }
  613. c.Start()
  614. }