4ce5e8ec1857b907e9fdd07d94745d796d0685f2.svn-base 24 KB

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