354a95c09fb52cc7ff8ea8fbb97c54e21cac1c19.svn-base 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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(pastureid,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,bcode,backup1,backup2,bname,enable) "+
  124. " values(?,?,?,?,?,?,1)"+
  125. " ON DUPLICATE KEY UPDATE bcode=?,backup2=?",
  126. ids, pastureid, penId,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(?,'%'," +
  132. "''),REPLACE(?,'%','')*(if(?='',0,?))/100)"+
  133. " ON DUPLICATE KEY UPDATE barname=?,ccount=if(?='',0,?),ccountratio=ratio*ccount/100",
  134. ids, pastureid, pastureid, penName, penName, cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount, penName,
  135. cowCount, cowCount)
  136. if err != nil {
  137. return err
  138. }
  139. if !isexec{ //第一次新增栏舍,不去更新feedp
  140. //_, err = tx.Exec(`UPDATE feedp set ccount=(if(?='',0,?)),ratio=REPLACE(?,'%',''),ccountratio=REPLACE(?,'%','')*(if(?='',0,?))/100
  141. // where barid = (select id from bar where pastureid=? and bname=?) and pastureid = ?`,
  142. // cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount, pastureid,penName,pastureid)
  143. //
  144. //if err != nil {
  145. // return err
  146. //}
  147. _, err = tx.Exec(`UPDATE fpdetail fpd
  148. JOIN feedp fp ON fp.pastureid = fpd.pastureid AND fp.barid =fpd.barid
  149. SET
  150. fpd.weight = weight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio)
  151. WHERE fpd.pastureid = ? and fp.barid =(select id from bar where pastureid=? and bname=?)`,pastureid,pastureid,penName)
  152. if err != nil {
  153. return err
  154. }
  155. _, err = tx.Exec("call updateLPPbyFPChange((select id from bar where pastureid=? and bname=?),?)",pastureid,penName,pastureid)
  156. if err != nil {
  157. return err
  158. }
  159. }
  160. }
  161. if ise{
  162. _, err = tx.Exec(`UPDATE fpdetail fpd
  163. JOIN feedp fp ON fp.pastureid = fpd.pastureid AND fp.barid =fpd.barid
  164. SET
  165. fp.feedweight = fp.feedweight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio),
  166. fp.ftweight = fp.ftweight *(fp.ccount*fp.ratio/fpd.cowcount/fpd.ccountradio),
  167. fpd.ccountradio =fp.ratio ,
  168. fpd.cowcount =fp.ccount
  169. WHERE fpd.pastureid = ? `,pastureid)
  170. if err != nil {
  171. return err
  172. }
  173. }
  174. }
  175. }
  176. return nil
  177. }
  178. func GetMaterial(pastureid string) error{
  179. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterial")
  180. if data != nil {
  181. var feed util.Feed
  182. json.Unmarshal(data, &feed)
  183. status := feed.Status
  184. feedpData := feed.Data
  185. if status == 0 {
  186. // 开启事务
  187. tx := restful.Engine.NewSession()
  188. err := tx.Begin()
  189. defer func() {
  190. switch {
  191. case err != nil:
  192. logging.Error("tx.Begin 事务启动失败__error:", err)
  193. if tx != nil {
  194. tx.Rollback()
  195. }
  196. default:
  197. if tx != nil {
  198. err = tx.Commit()
  199. }
  200. }
  201. if tx != nil {
  202. tx.Close()
  203. }
  204. }()
  205. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,fname FROM feed WHERE pastureid = ?",pastureid).QueryString()
  206. mt := make(map[string]string)
  207. for _, v := range valuesMap {
  208. mt[v["fname"]] = v["backup2"]
  209. }
  210. for _, val := range feedpData {
  211. fId := val.FId
  212. fName := val.FName
  213. price := val.Price
  214. dryMatter := val.DryMatter
  215. //autoChange := val.AutoChange
  216. //allowDev := val.AllowDev
  217. feedclassName := val.FeedclassName
  218. createdAt := val.CreatedAt
  219. modifiedAt := val.ModifiedAt
  220. if _,ok :=mt[fName]; ok{
  221. if mt[fName] == modifiedAt{
  222. continue
  223. }
  224. }
  225. fmt.Println(pastureid,fName, feedclassName, createdAt, modifiedAt)
  226. ids, err := setting.SnowIds.NextId()
  227. if err != nil {
  228. ids = time.Now().UnixNano()
  229. logging.Info("create SnowIds err", err)
  230. }
  231. _, err = tx.Exec("insert into feed(id,pastureid,feedcode,backup1,backup2,fname,fclass,fclassid,uprice,dry) "+
  232. " values(?,?,?,?,?,?,?,(select id from feedclass where fcname=? and pastureid=?),(if(?='',0,?)),(if(?='' or ?='~',0,?)))"+
  233. " ON DUPLICATE KEY UPDATE feedcode=?,backup2=?,fname=?,fclass=?,fclassid=(select id from feedclass where fcname=? and pastureid=?)," +
  234. " uprice=(if(?='',uprice,?)),dry=(if(?='' or ?='~',dry,?))",
  235. ids, pastureid, fId,fId,modifiedAt, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter,fId,modifiedAt, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter)
  236. if err != nil {
  237. return err
  238. }
  239. _, err = tx.Exec(" insert into feednur(id,pastureid,fid,fname,price,dry,fndate) "+
  240. " values(?,?,(select id from feed where fname=? and pastureid=?),?,(if(?='',0,?)),(if(?='' or ?='~',0,?)),now() ) "+
  241. " ON DUPLICATE KEY UPDATE price=(if(?='',0,?)),dry=(if(?='' or ?='~',0,?)),fndate=now() ",
  242. ids, pastureid, fName,pastureid, fName, price, price, dryMatter, dryMatter, dryMatter, price, price, dryMatter, dryMatter, dryMatter)
  243. if err != nil {
  244. fmt.Println(err)
  245. return err
  246. }
  247. }
  248. }
  249. }
  250. return nil
  251. }
  252. func GetMaterialCategory(pastureid string)error {
  253. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterialCategory")
  254. if data != nil {
  255. var feedClass util.FeedClass
  256. json.Unmarshal(data, &feedClass)
  257. status := feedClass.Status
  258. feedpData := feedClass.Data
  259. if status == 0 {
  260. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  261. //fsion := gofasion.NewFasion(string(dataByte))
  262. //pastureid := fsion.Get("pastureid").ValueStr()
  263. // 开启事务
  264. tx := restful.Engine.NewSession()
  265. err := tx.Begin()
  266. defer func() {
  267. switch {
  268. case err != nil:
  269. logging.Error("tx.Begin 事务启动失败__error:", err)
  270. if tx != nil {
  271. tx.Rollback()
  272. }
  273. default:
  274. if tx != nil {
  275. err = tx.Commit()
  276. }
  277. }
  278. if tx != nil {
  279. tx.Close()
  280. }
  281. }()
  282. for _, val := range feedpData {
  283. feedclassName := val.FeedclassName
  284. feedclassId := val.FeedclassId
  285. createdAt := val.CreatedAt
  286. modifiedAt := val.ModifiedAt
  287. fmt.Println(pastureid,feedclassId,feedclassName,createdAt,modifiedAt)
  288. ids, err := setting.SnowIds.NextId()
  289. if err != nil {
  290. ids = time.Now().UnixNano()
  291. logging.Info("create SnowIds err", err)
  292. }
  293. _, err = tx.Exec("insert into feedclass(id,pastureid,fcname,fccode,backup1,bigfeedclassid,bigfeedclassname,sort,`enable`) values(?,?,?,?,?,?,?,?,?)"+
  294. " ON DUPLICATE KEY UPDATE fccode=?,backup1=?",
  295. ids, pastureid, feedclassName, feedclassId,feedclassId, 3, "精粗各半", 0, 1, feedclassId, feedclassId)
  296. if err != nil {
  297. return err
  298. }
  299. }
  300. }
  301. }
  302. return nil
  303. }
  304. func GetCowCategory(pastureid string)error {
  305. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getCowCategory")
  306. if data != nil {
  307. var cowClass util.CowClass
  308. json.Unmarshal(data, &cowClass)
  309. status := cowClass.Status
  310. feedpData := cowClass.Data
  311. if status == 0 {
  312. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  313. //fsion := gofasion.NewFasion(string(dataByte))
  314. //pastureid := fsion.Get("pastureid").ValueStr()
  315. // 开启事务
  316. tx := restful.Engine.NewSession()
  317. err := tx.Begin()
  318. defer func() {
  319. switch {
  320. case err != nil:
  321. logging.Error("tx.Begin 事务启动失败__error:", err)
  322. if tx != nil {
  323. tx.Rollback()
  324. }
  325. default:
  326. if tx != nil {
  327. err = tx.Commit()
  328. }
  329. }
  330. if tx != nil {
  331. tx.Close()
  332. }
  333. }()
  334. for _, val := range feedpData {
  335. cowClassCode := val.CowClassCode
  336. cowClassName := val.CowClassName
  337. createdAt := val.CreatedAt
  338. modifiedAt := val.ModifiedAt
  339. fmt.Println(pastureid,cowClassCode, cowClassName, createdAt, modifiedAt)
  340. ids, err := setting.SnowIds.NextId()
  341. if err != nil {
  342. ids = time.Now().UnixNano()
  343. logging.Info("create SnowIds err", err)
  344. }
  345. _, err = tx.Exec("insert into cowclass(id,pastureid,classcode,backup1,classname) values(?,?,?,?,?) "+
  346. " ON DUPLICATE KEY UPDATE classcode=?",
  347. ids, pastureid, cowClassCode,cowClassCode, cowClassName,cowClassCode)
  348. if err != nil {
  349. return err
  350. }
  351. }
  352. }
  353. }
  354. return nil
  355. }
  356. func GetFormula(pastureid string)error {
  357. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getFormula")
  358. if data != nil {
  359. var feedTemplet util.FeedTemplet
  360. json.Unmarshal(data, &feedTemplet)
  361. status := feedTemplet.Status
  362. feedpData := feedTemplet.Data
  363. println(len(feedpData))
  364. if status == 0 {
  365. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  366. //fsion := gofasion.NewFasion(string(dataByte))
  367. //pastureid := fsion.Get("pastureid").ValueStr()
  368. // 开启事务
  369. tx := restful.Engine.NewSession()
  370. err := tx.Begin()
  371. defer func() {
  372. switch {
  373. case err != nil:
  374. logging.Error("tx.Begin 事务启动失败__error:", err)
  375. if tx != nil {
  376. tx.Rollback()
  377. }
  378. default:
  379. if tx != nil {
  380. err = tx.Commit()
  381. }
  382. }
  383. if tx != nil {
  384. tx.Close()
  385. }
  386. }()
  387. valuesMap, _ := tx.SQL("SELECT ifnull(backup2,'-1')backup2,tname FROM feedtemplet WHERE pastureid = ?",pastureid).QueryString()
  388. mt := make(map[string]string)
  389. for _, v := range valuesMap {
  390. mt[v["tname"]] = v["backup2"]
  391. }
  392. for _, val := range feedpData {
  393. ftId := val.FtId
  394. ftName := val.FtName
  395. feedList := val.FeedList
  396. //createdAt := val.CreatedAt
  397. modifiedAt := val.ModifiedAt
  398. if _,ok :=mt[ftName]; ok{
  399. if mt[ftName] == modifiedAt{
  400. continue
  401. }
  402. }
  403. println(val.FtName)
  404. ids, err := setting.SnowIds.NextId()
  405. if err != nil {
  406. ids = time.Now().UnixNano()
  407. logging.Info("create SnowIds err", err)
  408. }
  409. _, err = tx.Exec("insert into feedtemplet(id,pastureid,ccid,tcode,backup1,backup2,tname,fttypeid,fttype) values(?,?,null,?,?,?,?,1,'饲喂配方') "+
  410. " ON DUPLICATE KEY UPDATE tcode=?,backup2= ?",
  411. ids, pastureid, ftId,ftId,modifiedAt, ftName, ftId,modifiedAt)
  412. if err != nil {
  413. return err
  414. }
  415. valuesMap2, _ := tx.SQL("select id,ftid,fname,fid,fweight,autosecond,autosecondname from ftdetail where ftid= " +
  416. " (select id from feedtemplet where pastureid=? and tname=? limit 1 )",pastureid, ftName).QueryString()
  417. tx.Exec("delete from ftdetail where ftid=(select id from feedtemplet where pastureid=? and tname=?) ",pastureid, ftName)
  418. for _, val := range feedList {
  419. fId := val.FId
  420. fName := val.FName
  421. fodderWeight := val.FodderWeight
  422. mixNo := val.MixNo
  423. allowableError := val.AllowableError
  424. autosecond := val.Autosecond
  425. tx.Exec("update feed set allowratio=if(?='',allowratio,?) where pastureid=? and fname=? ",allowableError,allowableError,pastureid, fName)
  426. if valuesMap2!=nil{
  427. for _, v := range valuesMap2 {
  428. fName2 := v["fname"]
  429. autosecond2 := v["autosecond"]
  430. if(fName==fName2 && (autosecond2 != "")){
  431. autosecond=autosecond2
  432. }
  433. }
  434. }
  435. ids, err := setting.SnowIds.NextId()
  436. if err != nil {
  437. ids = time.Now().UnixNano()
  438. logging.Info("create SnowIds err", err)
  439. }
  440. _, err = tx.Exec("insert into ftdetail(id,pastureid,ftid,fid,fname,fweight,sort,autosecond,autosecondname,feedgroup) "+
  441. " values(?,?,(select id from feedtemplet where pastureid=? and tname=? limit 1)," +
  442. " (select id from feed where pastureid=? and backup1=? limit 1),?,?,?,(if(?='',0,?)),?,?) " ,
  443. ids, pastureid, pastureid, ftName, pastureid, fId, fName, fodderWeight, mixNo,autosecond,autosecond, autosecond,fName)
  444. if err != nil {
  445. return err
  446. }
  447. _, 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)
  448. if err != nil {
  449. return err
  450. }
  451. }
  452. }
  453. }
  454. }
  455. return nil
  456. }
  457. func AddOtherDevice() {
  458. tx := restful.Engine.NewSession()
  459. err := tx.Begin()
  460. defer func() {
  461. switch {
  462. case err != nil:
  463. logging.Error("tx.Begin 事务启动失败__error:", err)
  464. if tx != nil {
  465. tx.Rollback()
  466. }
  467. default:
  468. if tx != nil {
  469. err = tx.Commit()
  470. }
  471. }
  472. if tx != nil {
  473. tx.Close()
  474. }
  475. }()
  476. 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 " +
  477. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  478. 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,"+
  479. " de.Fname as fName,fc.FCNAME as feedclassName,de.SORT as sort,de.feedallowratio as allowableError, "+
  480. " cast(ifnull(de.LWEIGHT,0) as decimal(10,2)) as expWeight,cast(ifnull(de.ActualWeightMinus,0) as decimal(10,2)) as actualWeight,"+
  481. " date_format(de.intime,'%Y-%m-%d %H:%i:%s') as endTime ,d.tmrtname as tName ,'' dName "+
  482. " from downloadedplan d LEFT JOIN downloadplandtl1 de on d.id=de.PID and d.pastureid=de.pastureid "+
  483. " 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 "+
  484. " LEFT JOIN feedclass fc on f.fclassid=fc.id and fc.pastureid=d.pastureid "+
  485. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  486. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  487. if err1 == nil && err2 == nil {
  488. for i := 0; i < len(queryIdList); i++ {
  489. id := queryIdList[i]["id"]
  490. projName := queryIdList[i]["projName"]
  491. listData := make(map[string]interface{})
  492. listData["farmId"] = 4090
  493. listData["id"] = id
  494. listData["projName"] = projName
  495. feedList := []interface{}{}
  496. for j := 0; j < len(queryDataList); j++ {
  497. id2 := queryDataList[j]["id"]
  498. if id == id2 {
  499. dataList := make(map[string]interface{})
  500. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  501. dataList["times"] = queryDataList[j]["times"]
  502. dataList["loadDate"] = queryDataList[j]["loadDate"]
  503. dataList["ftId"] = queryDataList[j]["ftId"]
  504. dataList["ftName"] = queryDataList[j]["ftName"]
  505. dataList["fId"] = queryDataList[j]["fId"]
  506. dataList["tName"] = queryDataList[j]["tName"]
  507. dataList["feedclassName"] = queryDataList[j]["feedclassName"]
  508. dataList["fName"] = queryDataList[j]["fName"]
  509. dataList["sort"] = queryDataList[j]["sort"]
  510. dataList["allowableError"] = queryDataList[j]["allowableError"]
  511. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  512. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  513. lWeight, _ := strconv.ParseFloat(queryDataList[j]["lWeight"].(string), 64)
  514. dataList["expWeight"] = expWeight
  515. dataList["actualWeight"] = actualWeight
  516. dataList["lWeight"] = lWeight
  517. dataList["endTime"] = queryDataList[j]["endTime"]
  518. dataList["dName"] = queryDataList[j]["dName"]
  519. feedList = append(feedList, dataList)
  520. }
  521. }
  522. listData["data"] = feedList
  523. jsonStr, _ := json.Marshal(listData)
  524. postData := make(map[string]string)
  525. postData["formData"] = string(jsonStr)
  526. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDevice", &postData)
  527. if err != nil{
  528. fmt.Println("AddOtherDevice err:",err)
  529. logging.Error("AddOtherDevice err:",err)
  530. }
  531. }
  532. }
  533. }
  534. func AddOtherDeviceFeeding() {
  535. tx := restful.Engine.NewSession()
  536. err := tx.Begin()
  537. defer func() {
  538. switch {
  539. case err != nil:
  540. logging.Error("tx.Begin 事务启动失败__error:", err)
  541. if tx != nil {
  542. tx.Rollback()
  543. }
  544. default:
  545. if tx != nil {
  546. err = tx.Commit()
  547. }
  548. }
  549. if tx != nil {
  550. tx.Close()
  551. }
  552. }()
  553. 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 " +
  554. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid')").Query().List()
  555. 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,"+
  556. " bar.backup1 as penId,d2.fname as penName,fp.CCOUNT as cowCount, d2.SORT as sort,d.templetName ftName, ft.backup1 ftId,"+
  557. " 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 "+
  558. " from downloadedplan d LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID and d.pastureid=d2.pastureid "+
  559. " 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 "+
  560. " LEFT JOIN feedtemplet ft on d.tempid=ft.id and ft.pastureid=d.pastureid "+
  561. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  562. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  563. if err1 == nil && err2 == nil {
  564. for i := 0; i < len(queryIdList); i++ {
  565. id := queryIdList[i]["id"]
  566. projName := queryIdList[i]["projName"]
  567. listData := make(map[string]interface{})
  568. listData["farmId"] = 4090
  569. listData["id"] = id
  570. listData["projName"] = projName
  571. feedList := []interface{}{}
  572. for j := 0; j < len(queryDataList); j++ {
  573. id2 := queryDataList[j]["id"]
  574. if id == id2 {
  575. dataList := make(map[string]interface{})
  576. dataList["dropDate"] = queryDataList[j]["dropDate"]
  577. dataList["times"] = queryDataList[j]["times"]
  578. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  579. dataList["tName"] = queryDataList[j]["tName"]
  580. dataList["dName"] = queryDataList[j]["dName"]
  581. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  582. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  583. dataList["expWeight"] = expWeight
  584. dataList["actualWeight"] = actualWeight
  585. dataList["endTime"] = queryDataList[j]["endTime"]
  586. dataList["penId"] = queryDataList[j]["penId"]
  587. dataList["penName"] = queryDataList[j]["penName"]
  588. dataList["cowCount"] = queryDataList[j]["cowCount"]
  589. dataList["sort"] = queryDataList[j]["sort"]
  590. dataList["ftId"] = queryDataList[j]["ftId"]
  591. dataList["ftName"] = queryDataList[j]["ftName"]
  592. feedList = append(feedList, dataList)
  593. }
  594. }
  595. listData["data"] = feedList
  596. jsonStr, _ := json.Marshal(listData)
  597. postData := make(map[string]string)
  598. postData["formData"] = string(jsonStr)
  599. err = util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDeviceFeeding", &postData)
  600. if err != nil{
  601. fmt.Println("AddOtherDeviceFeeding err:",err)
  602. logging.Error("AddOtherDeviceFeeding err:",err)
  603. }else {
  604. _,err = tx.Exec(`update downloadedplan set isuse=1 where mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and isuse=0
  605. and pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight'
  606. AND table_schema = 'tmrwatch' AND column_name = 'pastureid') `)
  607. if err != nil{
  608. logging.Error("update downloadedplan err:",err)
  609. }
  610. }
  611. }
  612. }
  613. //data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:86/kaps-cow/api/tmr/getMaterial")
  614. }
  615. func PostTmrData(){
  616. defer func() { // 必须要先声明defer
  617. if err := recover(); err != nil {
  618. fmt.Printf("CronTest pnic err%+v \n", err)
  619. logging.Error("panic recover err ", err)
  620. //println("pnic err",err.(error).Error()) // 这里的err其实就是panic传入的内容,55
  621. }
  622. }()
  623. println("开始执行")
  624. AddOtherDevice()
  625. AddOtherDeviceFeeding()
  626. }
  627. func CronGRFD() {
  628. defer func() { // 必须要先声明defer
  629. if err := recover(); err != nil {
  630. fmt.Printf("CronTest pnic err%+v \n", err)
  631. logging.Error("panic recover err ", err)
  632. }
  633. }()
  634. log.Println("Starting CronWXwork...")
  635. c := cron.New()
  636. _, err := c.AddFunc("0/3 * * * *", PostTmrData) //* * * * *, 从分钟开始 每5分钟
  637. if err != nil {
  638. println("cron4 err", err.Error())
  639. }
  640. c.Start()
  641. }