3f6aadb3e3c27df5b302442d3ab3e763797b8c25.svn-base 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 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. if status == 0 {
  350. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  351. //fsion := gofasion.NewFasion(string(dataByte))
  352. //pastureid := fsion.Get("pastureid").ValueStr()
  353. // 开启事务
  354. tx := restful.Engine.NewSession()
  355. err := tx.Begin()
  356. defer func() {
  357. switch {
  358. case err != nil:
  359. logging.Error("tx.Begin 事务启动失败__error:", err)
  360. if tx != nil {
  361. tx.Rollback()
  362. }
  363. default:
  364. if tx != nil {
  365. err = tx.Commit()
  366. }
  367. }
  368. if tx != nil {
  369. tx.Close()
  370. }
  371. }()
  372. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,tname FROM feedtemplet WHERE pastureid = ?",pastureid).QueryString()
  373. mt := make(map[string]string)
  374. for _, v := range valuesMap {
  375. mt[v["tname"]] = v["backup2"]
  376. }
  377. for _, val := range feedpData {
  378. ftId := val.FtId
  379. ftName := val.FtName
  380. feedList := val.FeedList
  381. //createdAt := val.CreatedAt
  382. modifiedAt := val.ModifiedAt
  383. if _,ok :=mt[ftName]; ok{
  384. if mt[ftName] == modifiedAt{
  385. continue
  386. }
  387. }
  388. ids, err := setting.SnowIds.NextId()
  389. if err != nil {
  390. ids = time.Now().UnixNano()
  391. logging.Info("create SnowIds err", err)
  392. }
  393. _, err = tx.Exec("insert into feedtemplet(id,pastureid,ccid,backup1,backup2,tname,fttypeid,fttype) values(?,?,null,?,?,?,1,'饲喂配方')"+
  394. " ON DUPLICATE KEY UPDATE backup1=?,tname=?,backup2= ?",
  395. ids, pastureid, ftId,modifiedAt, ftName, ftId, ftName,modifiedAt)
  396. if err != nil {
  397. return err
  398. }
  399. tx.Exec("delete from ftdetail where ftid=(select id from feedtemplet where pastureid=? and tname=?)",pastureid, ftName)
  400. for _, val := range feedList {
  401. fId := val.FId
  402. fName := val.FName
  403. fodderWeight := val.FodderWeight
  404. mixNo := val.MixNo
  405. ids, err := setting.SnowIds.NextId()
  406. if err != nil {
  407. ids = time.Now().UnixNano()
  408. logging.Info("create SnowIds err", err)
  409. }
  410. _, err = tx.Exec("insert into ftdetail(id,pastureid,ftid,fid,fname,fweight,sort) "+
  411. " 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,?)))" +
  412. "ON DUPLICATE KEY UPDATE fweight=(if(?='',0,?)),sort=(if(?='',0,?))",
  413. ids, pastureid, pastureid, ftName, pastureid, fId, fName, fodderWeight, fodderWeight, mixNo, mixNo,fodderWeight,fodderWeight, mixNo, mixNo)
  414. if err != nil {
  415. return err
  416. }
  417. _, 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)
  418. if err != nil {
  419. return err
  420. }
  421. }
  422. }
  423. }
  424. }
  425. return nil
  426. }
  427. func AddOtherDevice() {
  428. tx := restful.Engine.NewSession()
  429. err := tx.Begin()
  430. defer func() {
  431. switch {
  432. case err != nil:
  433. logging.Error("tx.Begin 事务启动失败__error:", err)
  434. if tx != nil {
  435. tx.Rollback()
  436. }
  437. default:
  438. if tx != nil {
  439. err = tx.Commit()
  440. }
  441. }
  442. if tx != nil {
  443. tx.Close()
  444. }
  445. }()
  446. 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 " +
  447. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  448. 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,"+
  449. " de.Fname as fName,fc.FCNAME as feedclassName,de.SORT as sort,de.feedallowratio as allowableError, "+
  450. " cast(ifnull(de.LWEIGHT,0) as decimal(10,2)) as expWeight,cast(ifnull(de.ActualWeightMinus,0) as decimal(10,2)) as actualWeight,"+
  451. " date_format(de.intime,'%Y-%m-%d %H:%i:%s') as endTime ,d.tmrtname as tName ,'' dName "+
  452. " from downloadedplan d LEFT JOIN downloadplandtl1 de on d.id=de.PID and d.pastureid=de.pastureid "+
  453. " 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 "+
  454. " LEFT JOIN feedclass fc on f.fclassid=fc.id and fc.pastureid=d.pastureid "+
  455. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  456. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  457. if err1 == nil && err2 == nil {
  458. for i := 0; i < len(queryIdList); i++ {
  459. id := queryIdList[i]["id"]
  460. projName := queryIdList[i]["projName"]
  461. listData := make(map[string]interface{})
  462. listData["farmId"] = 4090
  463. listData["id"] = id
  464. listData["projName"] = projName
  465. feedList := []interface{}{}
  466. for j := 0; j < len(queryDataList); j++ {
  467. id2 := queryDataList[j]["id"]
  468. if id == id2 {
  469. dataList := make(map[string]interface{})
  470. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  471. dataList["times"] = queryDataList[j]["times"]
  472. dataList["loadDate"] = queryDataList[j]["loadDate"]
  473. dataList["ftId"] = queryDataList[j]["ftId"]
  474. dataList["ftName"] = queryDataList[j]["ftName"]
  475. dataList["fId"] = queryDataList[j]["fId"]
  476. dataList["tName"] = queryDataList[j]["tName"]
  477. dataList["feedclassName"] = queryDataList[j]["feedclassName"]
  478. dataList["fName"] = queryDataList[j]["fName"]
  479. dataList["sort"] = queryDataList[j]["sort"]
  480. dataList["allowableError"] = queryDataList[j]["allowableError"]
  481. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  482. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  483. lWeight, _ := strconv.ParseFloat(queryDataList[j]["lWeight"].(string), 64)
  484. dataList["expWeight"] = expWeight
  485. dataList["actualWeight"] = actualWeight
  486. dataList["lWeight"] = lWeight
  487. dataList["endTime"] = queryDataList[j]["endTime"]
  488. dataList["dName"] = queryDataList[j]["dName"]
  489. feedList = append(feedList, dataList)
  490. }
  491. }
  492. listData["data"] = feedList
  493. jsonStr, _ := json.Marshal(listData)
  494. postData := make(map[string]string)
  495. postData["formData"] = string(jsonStr)
  496. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDevice", &postData)
  497. if err != nil{
  498. fmt.Println("AddOtherDevice err:",err)
  499. logging.Error("AddOtherDevice err:",err)
  500. }
  501. }
  502. }
  503. }
  504. func AddOtherDeviceFeeding() {
  505. tx := restful.Engine.NewSession()
  506. err := tx.Begin()
  507. defer func() {
  508. switch {
  509. case err != nil:
  510. logging.Error("tx.Begin 事务启动失败__error:", err)
  511. if tx != nil {
  512. tx.Rollback()
  513. }
  514. default:
  515. if tx != nil {
  516. err = tx.Commit()
  517. }
  518. }
  519. if tx != nil {
  520. tx.Close()
  521. }
  522. }()
  523. 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 " +
  524. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid')").Query().List()
  525. 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,"+
  526. " bar.backup1 as penId,d2.fname as penName,fp.CCOUNT as cowCount, d2.SORT as sort,d.templetName ftName, ft.backup1 ftId,"+
  527. " 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 "+
  528. " from downloadedplan d LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID and d.pastureid=d2.pastureid "+
  529. " 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 "+
  530. " LEFT JOIN feedtemplet ft on d.tempid=ft.id and ft.pastureid=d.pastureid "+
  531. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  532. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  533. if err1 == nil && err2 == nil {
  534. for i := 0; i < len(queryIdList); i++ {
  535. id := queryIdList[i]["id"]
  536. projName := queryIdList[i]["projName"]
  537. listData := make(map[string]interface{})
  538. listData["farmId"] = 4090
  539. listData["id"] = id
  540. listData["projName"] = projName
  541. feedList := []interface{}{}
  542. for j := 0; j < len(queryDataList); j++ {
  543. id2 := queryDataList[j]["id"]
  544. if id == id2 {
  545. dataList := make(map[string]interface{})
  546. dataList["dropDate"] = queryDataList[j]["dropDate"]
  547. dataList["times"] = queryDataList[j]["times"]
  548. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  549. dataList["tName"] = queryDataList[j]["tName"]
  550. dataList["dName"] = queryDataList[j]["dName"]
  551. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  552. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  553. dataList["expWeight"] = expWeight
  554. dataList["actualWeight"] = actualWeight
  555. dataList["endTime"] = queryDataList[j]["endTime"]
  556. dataList["penId"] = queryDataList[j]["penId"]
  557. dataList["penName"] = queryDataList[j]["penName"]
  558. dataList["cowCount"] = queryDataList[j]["cowCount"]
  559. dataList["sort"] = queryDataList[j]["sort"]
  560. dataList["ftId"] = queryDataList[j]["ftId"]
  561. dataList["ftName"] = queryDataList[j]["ftName"]
  562. feedList = append(feedList, dataList)
  563. }
  564. }
  565. listData["data"] = feedList
  566. jsonStr, _ := json.Marshal(listData)
  567. postData := make(map[string]string)
  568. postData["formData"] = string(jsonStr)
  569. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDeviceFeeding", &postData)
  570. if err != nil{
  571. fmt.Println("AddOtherDeviceFeeding err:",err)
  572. logging.Error("AddOtherDeviceFeeding err:",err)
  573. }else {
  574. _,err = tx.Exec(`update downloadedplan set isuse=1 where mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and isuse=0
  575. and pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight'
  576. AND table_schema = 'tmrwatch' AND column_name = 'pastureid') `)
  577. if err != nil{
  578. logging.Error("update downloadedplan err:",err)
  579. }
  580. }
  581. }
  582. }
  583. //data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:86/kaps-cow/api/tmr/getMaterial")
  584. }
  585. func PostTmrData(){
  586. defer func() { // 必须要先声明defer
  587. if err := recover(); err != nil {
  588. fmt.Printf("CronTest pnic err%+v \n", err)
  589. logging.Error("panic recover err ", err)
  590. //println("pnic err",err.(error).Error()) // 这里的err其实就是panic传入的内容,55
  591. }
  592. }()
  593. println("开始执行")
  594. AddOtherDevice()
  595. AddOtherDeviceFeeding()
  596. }
  597. func CronGRFD() {
  598. defer func() { // 必须要先声明defer
  599. if err := recover(); err != nil {
  600. fmt.Printf("CronTest pnic err%+v \n", err)
  601. logging.Error("panic recover err ", err)
  602. }
  603. }()
  604. log.Println("Starting CronWXwork...")
  605. c := cron.New()
  606. _, err := c.AddFunc("0/3 * * * *", PostTmrData) //* * * * *, 从分钟开始 每5分钟
  607. if err != nil {
  608. println("cron4 err", err.Error())
  609. }
  610. c.Start()
  611. }