21ca4269136b1e2895f78cee0d4e54ed949d7609.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. package api
  2. import (
  3. "../../pkg/util"
  4. "../../routers/restful"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "github.com/hequan2017/go-admin/pkg/e"
  9. "github.com/hequan2017/go-admin/pkg/logging"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. "../../pkg/app"
  14. //"time"
  15. "../../pkg/setting"
  16. )
  17. func GetDorm(c *gin.Context) {
  18. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getDorm")
  19. if data != nil {
  20. var feedp util.Feedp
  21. json.Unmarshal(data, &feedp)
  22. status := feedp.Status
  23. feedpData := feedp.Data
  24. if status == 0 {
  25. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  26. //fsion := gofasion.NewFasion(string(dataByte))
  27. //pastureid := fsion.Get("pastureid").ValueStr()
  28. pastureid := 3445271849683387392
  29. // 开启事务
  30. appG := app.Gin{C: c}
  31. tx := restful.Engine.NewSession()
  32. err := tx.Begin()
  33. defer func() {
  34. switch {
  35. case err != nil:
  36. logging.Error("tx.Begin 事务启动失败__error:", err)
  37. if tx != nil {
  38. tx.Rollback()
  39. }
  40. default:
  41. if tx != nil {
  42. err = tx.Commit()
  43. }
  44. }
  45. if tx != nil {
  46. tx.Close()
  47. }
  48. }()
  49. for _, val := range feedpData {
  50. penId := val.PenId
  51. penName := val.PenName
  52. cowCount := val.CowCount
  53. ccountRatio := val.CcountRatio
  54. //createdAt := val.CreatedAt
  55. //modifiedAt := val.ModifiedAt
  56. ids, err := setting.SnowIds.NextId()
  57. if err != nil {
  58. ids = time.Now().UnixNano()
  59. logging.Info("create SnowIds err", err)
  60. }
  61. exec, err := tx.Exec("insert into bar(id,pastureid,bcode,bname) "+
  62. " values(?,?,?,?)"+
  63. " ON DUPLICATE KEY UPDATE bcode=?,bname=?",
  64. ids, pastureid, penId, penName, penId, penName)
  65. if err != nil {
  66. fmt.Println(err)
  67. continue
  68. } else {
  69. fmt.Println(exec)
  70. }
  71. exec2, err := tx.Exec("insert into feedp(id,pastureid,barid,barname,ccount,ratio,ccountratio) "+
  72. " values(?,?,(select id from bar where pastureid=? and bname=?),?,(if(?='',0,?)),REPLACE(?,'%',''),REPLACE(?,'%','')*(if(?='',0,?)))"+
  73. " ON DUPLICATE KEY UPDATE barid=(select id from bar where pastureid=? and bname=?),barname=?,ccount=(if(?='',0,?)),ratio=REPLACE(?,'%',''),ccountratio=REPLACE(?,'%','')*(if(?='',0,?))",
  74. ids, pastureid, pastureid, penName, penName, cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount, pastureid, penName, penName,
  75. cowCount, cowCount, ccountRatio, ccountRatio, cowCount, cowCount)
  76. if err != nil {
  77. fmt.Println(err)
  78. continue
  79. } else {
  80. fmt.Println(exec2)
  81. }
  82. }
  83. appG.Response(http.StatusOK, e.SUCCESS, nil)
  84. }
  85. }
  86. }
  87. func GetMaterial(c *gin.Context) {
  88. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterial")
  89. if data != nil {
  90. var feed util.Feed
  91. json.Unmarshal(data, &feed)
  92. status := feed.Status
  93. feedpData := feed.Data
  94. if status == 0 {
  95. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  96. //fsion := gofasion.NewFasion(string(dataByte))
  97. //pastureid := fsion.Get("pastureid").ValueStr()
  98. pastureid := 3445271849683387392
  99. // 开启事务
  100. appG := app.Gin{C: c}
  101. tx := restful.Engine.NewSession()
  102. err := tx.Begin()
  103. defer func() {
  104. switch {
  105. case err != nil:
  106. logging.Error("tx.Begin 事务启动失败__error:", err)
  107. if tx != nil {
  108. tx.Rollback()
  109. }
  110. default:
  111. if tx != nil {
  112. err = tx.Commit()
  113. }
  114. }
  115. if tx != nil {
  116. tx.Close()
  117. }
  118. }()
  119. for _, val := range feedpData {
  120. fId := val.FId
  121. fName := val.FName
  122. price := val.Price
  123. dryMatter := val.DryMatter
  124. //autoChange := val.AutoChange
  125. //allowDev := val.AllowDev
  126. feedclassName := val.FeedclassName
  127. createdAt := val.CreatedAt
  128. modifiedAt := val.ModifiedAt
  129. fmt.Println(fName, feedclassName, createdAt, modifiedAt)
  130. ids, err := setting.SnowIds.NextId()
  131. if err != nil {
  132. ids = time.Now().UnixNano()
  133. logging.Info("create SnowIds err", err)
  134. }
  135. exec, err := tx.Exec("insert into feed(id,pastureid,feedcode,fname,fclass,fclassid,uprice,dry) "+
  136. " values(?,?,?,?,?,(select id from feedclass where fcname=? and pastureid=?),(if(?='',0,?)),(if(?='' or ?='~',0,?)))"+
  137. " ON DUPLICATE KEY UPDATE feedcode=?,fname=?,fclass=?,fclassid=(select id from feedclass where fcname=? and pastureid=?),uprice=(if(?='',0,?)),dry=(if(?='' or ?='~',0,?))",
  138. ids, pastureid, fId, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter, fId, fName, feedclassName, feedclassName, pastureid, price, price, dryMatter, dryMatter, dryMatter)
  139. if err != nil {
  140. fmt.Println(err)
  141. continue
  142. } else {
  143. fmt.Println(exec)
  144. }
  145. }
  146. appG.Response(http.StatusOK, e.SUCCESS, nil)
  147. }
  148. }
  149. }
  150. func GetMaterialCategory(c *gin.Context) {
  151. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getMaterialCategory")
  152. if data != nil {
  153. var feedClass util.FeedClass
  154. json.Unmarshal(data, &feedClass)
  155. status := feedClass.Status
  156. feedpData := feedClass.Data
  157. if status == 0 {
  158. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  159. //fsion := gofasion.NewFasion(string(dataByte))
  160. //pastureid := fsion.Get("pastureid").ValueStr()
  161. pastureid := 3445271849683387392
  162. // 开启事务
  163. appG := app.Gin{C: c}
  164. tx := restful.Engine.NewSession()
  165. err := tx.Begin()
  166. defer func() {
  167. switch {
  168. case err != nil:
  169. logging.Error("tx.Begin 事务启动失败__error:", err)
  170. if tx != nil {
  171. tx.Rollback()
  172. }
  173. default:
  174. if tx != nil {
  175. err = tx.Commit()
  176. }
  177. }
  178. if tx != nil {
  179. tx.Close()
  180. }
  181. }()
  182. for _, val := range feedpData {
  183. feedclassName := val.FeedclassName
  184. feedclassId := val.FeedclassId
  185. createdAt := val.CreatedAt
  186. modifiedAt := val.ModifiedAt
  187. fmt.Println(feedclassId, feedclassName, createdAt, modifiedAt)
  188. ids, err := setting.SnowIds.NextId()
  189. if err != nil {
  190. ids = time.Now().UnixNano()
  191. logging.Info("create SnowIds err", err)
  192. }
  193. exec, err := tx.Exec("insert into feedclass(id,pastureid,fcname,fccode,bigfeedclassid,bigfeedclassname,sort,`enable`) values(?,?,?,?,?,?,?,?)"+
  194. " ON DUPLICATE KEY UPDATE fcname=?,fccode=?",
  195. ids, pastureid, feedclassName, feedclassId, 3, "精粗各半", 0, 1, feedclassName, feedclassId)
  196. if err != nil {
  197. fmt.Println(err)
  198. continue
  199. } else {
  200. fmt.Println(exec)
  201. }
  202. }
  203. appG.Response(http.StatusOK, e.SUCCESS, nil)
  204. }
  205. }
  206. }
  207. func GetCowCategory(c *gin.Context) {
  208. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getCowCategory")
  209. if data != nil {
  210. var cowClass util.CowClass
  211. json.Unmarshal(data, &cowClass)
  212. status := cowClass.Status
  213. feedpData := cowClass.Data
  214. if status == 0 {
  215. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  216. //fsion := gofasion.NewFasion(string(dataByte))
  217. //pastureid := fsion.Get("pastureid").ValueStr()
  218. pastureid := 3445271849683387392
  219. // 开启事务
  220. appG := app.Gin{C: c}
  221. tx := restful.Engine.NewSession()
  222. err := tx.Begin()
  223. defer func() {
  224. switch {
  225. case err != nil:
  226. logging.Error("tx.Begin 事务启动失败__error:", err)
  227. if tx != nil {
  228. tx.Rollback()
  229. }
  230. default:
  231. if tx != nil {
  232. err = tx.Commit()
  233. }
  234. }
  235. if tx != nil {
  236. tx.Close()
  237. }
  238. }()
  239. for _, val := range feedpData {
  240. cowClassCode := val.CowClassCode
  241. cowClassName := val.CowClassName
  242. createdAt := val.CreatedAt
  243. modifiedAt := val.ModifiedAt
  244. fmt.Println(cowClassCode, cowClassName, createdAt, modifiedAt)
  245. ids, err := setting.SnowIds.NextId()
  246. if err != nil {
  247. ids = time.Now().UnixNano()
  248. logging.Info("create SnowIds err", err)
  249. }
  250. exec, err := tx.Exec("insert into cowclass(id,pastureid,classcode,classname) values(?,?,?,?)"+
  251. " ON DUPLICATE KEY UPDATE classcode=?,classname=?",
  252. ids, pastureid, cowClassCode, cowClassName, cowClassCode, cowClassName)
  253. if err != nil {
  254. fmt.Println(err)
  255. continue
  256. } else {
  257. fmt.Println(exec)
  258. }
  259. }
  260. appG.Response(http.StatusOK, e.SUCCESS, nil)
  261. }
  262. }
  263. }
  264. func GetFormula(c *gin.Context) {
  265. data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:88/kaps-cow/api/tmr/getFormula")
  266. if data != nil {
  267. var feedTemplet util.FeedTemplet
  268. json.Unmarshal(data, &feedTemplet)
  269. status := feedTemplet.Status
  270. feedpData := feedTemplet.Data
  271. if status == 0 {
  272. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  273. //fsion := gofasion.NewFasion(string(dataByte))
  274. //pastureid := fsion.Get("pastureid").ValueStr()
  275. pastureid := 3445271849683387392
  276. // 开启事务
  277. appG := app.Gin{C: c}
  278. tx := restful.Engine.NewSession()
  279. err := tx.Begin()
  280. defer func() {
  281. switch {
  282. case err != nil:
  283. logging.Error("tx.Begin 事务启动失败__error:", err)
  284. if tx != nil {
  285. tx.Rollback()
  286. }
  287. default:
  288. if tx != nil {
  289. err = tx.Commit()
  290. }
  291. }
  292. if tx != nil {
  293. tx.Close()
  294. }
  295. }()
  296. for _, val := range feedpData {
  297. ftId := val.FtId
  298. ftName := val.FtName
  299. feedList := val.FeedList
  300. //createdAt := val.CreatedAt
  301. //modifiedAt := val.ModifiedAt
  302. ids, err := setting.SnowIds.NextId()
  303. if err != nil {
  304. ids = time.Now().UnixNano()
  305. logging.Info("create SnowIds err", err)
  306. }
  307. exec, err := tx.Exec("insert into feedtemplet(id,pastureid,ccid,tcode,tname,fttypeid,fttype) values(?,?,null,?,?,1,'饲喂配方')"+
  308. " ON DUPLICATE KEY UPDATE tcode=?,tname=?",
  309. ids, pastureid, ftId, ftName, ftId, ftName)
  310. if err != nil {
  311. fmt.Println(err)
  312. continue
  313. } else {
  314. fmt.Println(exec)
  315. }
  316. // tx.Exec("delete from ftdetail where ftid=(select id from feedtemplet where pastureid=? and tname=?)",pastureid, ftName)
  317. for _, val := range feedList {
  318. fId := val.FId
  319. fName := val.FName
  320. fodderWeight := val.FodderWeight
  321. mixNo := val.MixNo
  322. ids, err := setting.SnowIds.NextId()
  323. if err != nil {
  324. ids = time.Now().UnixNano()
  325. logging.Info("create SnowIds err", err)
  326. }
  327. exec, err := tx.Exec("insert into ftdetail(id,pastureid,ftid,fid,fname,fweight,sort) "+
  328. " values(?,?,(select id from feedtemplet where pastureid=? and tname=? limit 1),(select id from feed where pastureid=? and feedcode=? limit 1),?,(if(?='',0,?)),(if(?='',0,?)))",
  329. ids, pastureid, pastureid, ftName, pastureid, fId, fName, fodderWeight, fodderWeight, mixNo, mixNo)
  330. if err != nil {
  331. fmt.Println(err)
  332. continue
  333. } else {
  334. fmt.Println(exec)
  335. }
  336. }
  337. }
  338. appG.Response(http.StatusOK, e.SUCCESS, nil)
  339. }
  340. }
  341. }
  342. func AddOtherDevice(c *gin.Context) {
  343. appG := app.Gin{C: c}
  344. tx := restful.Engine.NewSession()
  345. queryIdList, err1 := tx.SQL("select d.id,d.projname projName from downloadedplan d where d.pastureid=? and d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  346. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  347. 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.tcode as ftId,d.templetName as ftName,f.feedcode as fId,"+
  348. " de.Fname as fName,fc.FCNAME as feedclassName,de.SORT as sort,de.feedallowratio as allowableError, "+
  349. " cast(de.LWEIGHT as decimal(10,2)) as expWeight,cast(de.ActualWeightMinus as decimal(10,2)) as actualWeight,"+
  350. " date_format(de.intime,'%Y-%m-%d %H:%i:%s') as endTime ,d.tmrtname as tName ,'' dName "+
  351. " from downloadedplan d LEFT JOIN downloadplandtl1 de on d.id=de.PID and d.pastureid=de.pastureid "+
  352. " 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 "+
  353. " LEFT JOIN feedclass fc on f.fclassid=fc.id and fc.pastureid=d.pastureid "+
  354. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  355. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  356. if err1 == nil && err2 == nil {
  357. for i := 0; i < len(queryIdList); i++ {
  358. id := queryIdList[i]["id"]
  359. projName := queryIdList[i]["projName"]
  360. listData := make(map[string]interface{})
  361. listData["farmId"] = 4090
  362. listData["id"] = id
  363. listData["projName"] = projName
  364. feedList := []interface{}{}
  365. for j := 0; j < len(queryDataList); j++ {
  366. id2 := queryDataList[j]["id"]
  367. if id == id2 {
  368. dataList := make(map[string]interface{})
  369. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  370. dataList["times"] = queryDataList[j]["times"]
  371. dataList["loadDate"] = queryDataList[j]["loadDate"]
  372. dataList["ftId"] = queryDataList[j]["ftId"]
  373. dataList["ftName"] = queryDataList[j]["ftName"]
  374. dataList["fId"] = queryDataList[j]["fId"]
  375. dataList["tName"] = queryDataList[j]["tName"]
  376. dataList["feedclassName"] = queryDataList[j]["feedclassName"]
  377. dataList["fName"] = queryDataList[j]["fName"]
  378. dataList["sort"] = queryDataList[j]["sort"]
  379. dataList["allowableError"] = queryDataList[j]["allowableError"]
  380. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  381. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  382. lWeight, _ := strconv.ParseFloat(queryDataList[j]["lWeight"].(string), 64)
  383. dataList["expWeight"] = expWeight
  384. dataList["actualWeight"] = actualWeight
  385. dataList["lWeight"] = lWeight
  386. dataList["endTime"] = queryDataList[j]["endTime"]
  387. dataList["dName"] = queryDataList[j]["dName"]
  388. feedList = append(feedList, dataList)
  389. }
  390. }
  391. listData["data"] = feedList
  392. jsonStr, _ := json.Marshal(listData)
  393. postData := make(map[string]string)
  394. postData["formData"] = string(jsonStr)
  395. fmt.Println(&postData)
  396. data := util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDevice", &postData)
  397. appG.Response(http.StatusOK, e.SUCCESS, string(data))
  398. }
  399. }
  400. }
  401. func AddOtherDeviceFeeding(c *gin.Context) {
  402. appG := app.Gin{C: c}
  403. tx := restful.Engine.NewSession()
  404. 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 " +
  405. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid')").Query().List()
  406. 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,"+
  407. " bar.bcode as penId,d2.fname as penName,fp.CCOUNT as cowCount, d2.SORT as sort,d.templetName ftName, ft.tcode ftId,"+
  408. " d2.LWEIGHT as expWeight,d2.ActualWeightMinus as actualWeight,date_format(d2.intime,'%Y-%m-%d %H:%i:%s') as endTime,d.tmrtname as tName,'' dName "+
  409. " from downloadedplan d LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID and d.pastureid=d2.pastureid "+
  410. " 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 "+
  411. " LEFT JOIN feedtemplet ft on d.tempid=ft.id and ft.pastureid=d.pastureid "+
  412. " where d.mydate=date_format(NOW(),'%Y-%m-%d') and iscompleted=1 and d.isuse=0 " +
  413. " and d.pastureid =(SELECT column_default FROM information_schema.COLUMNS WHERE table_name = 'recweight' AND table_schema = 'tmrwatch' AND column_name = 'pastureid') ").Query().List()
  414. if err1 == nil && err2 == nil {
  415. for i := 0; i < len(queryIdList); i++ {
  416. id := queryIdList[i]["id"]
  417. projName := queryIdList[i]["projName"]
  418. listData := make(map[string]interface{})
  419. listData["farmId"] = 4090
  420. listData["id"] = id
  421. listData["projName"] = projName
  422. feedList := []interface{}{}
  423. for j := 0; j < len(queryDataList); j++ {
  424. id2 := queryDataList[j]["id"]
  425. if id == id2 {
  426. dataList := make(map[string]interface{})
  427. dataList["dropDate"] = queryDataList[j]["dropDate"]
  428. dataList["times"] = queryDataList[j]["times"]
  429. dataList["tmrNo"] = queryDataList[j]["tmrNo"]
  430. dataList["tName"] = queryDataList[j]["tName"]
  431. dataList["dName"] = queryDataList[j]["dName"]
  432. expWeight, _ := strconv.ParseFloat(queryDataList[j]["expWeight"].(string), 64)
  433. actualWeight, _ := strconv.ParseFloat((queryDataList[j]["actualWeight"]).(string), 64)
  434. dataList["expWeight"] = expWeight
  435. dataList["actualWeight"] = actualWeight
  436. dataList["endTime"] = queryDataList[j]["endTime"]
  437. dataList["penId"] = queryDataList[j]["penId"]
  438. dataList["penName"] = queryDataList[j]["penName"]
  439. dataList["cowCount"] = queryDataList[j]["cowCount"]
  440. dataList["sort"] = queryDataList[j]["sort"]
  441. dataList["ftId"] = queryDataList[j]["ftId"]
  442. dataList["ftName"] = queryDataList[j]["ftName"]
  443. feedList = append(feedList, dataList)
  444. }
  445. }
  446. listData["data"] = feedList
  447. jsonStr, _ := json.Marshal(listData)
  448. postData := make(map[string]string)
  449. postData["formData"] = string(jsonStr)
  450. fmt.Println(&postData)
  451. data := util.PostWithFormData("POST", "http://120.48.30.31:88/kaps-cow/api/tmr/addOtherDeviceFeeding", &postData)
  452. appG.Response(http.StatusOK, e.SUCCESS, string(data))
  453. }
  454. }
  455. //data := util.Send_Message1("{\"farmId\":4090}", "http://120.48.30.31:86/kaps-cow/api/tmr/getMaterial")
  456. }