material.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "tmr-watch/conf/setting"
  9. "tmr-watch/http/handle/restful"
  10. "tmr-watch/pkg/app"
  11. "tmr-watch/pkg/e"
  12. "github.com/Anderson-Lu/gofasion/gofasion"
  13. "github.com/astaxie/beego/logs"
  14. "github.com/gin-gonic/gin"
  15. )
  16. type downloadedplan struct {
  17. Id int64 `json:"id"`
  18. Mydate string `json:"mydate"`
  19. Projname string `json:"projname"`
  20. Datacaptureno string `json:"datacaptureno"`
  21. Lweight string `json:"lweight"`
  22. Havebutton string `json:"havebutton"`
  23. Feedpname string `json:"feedpname"`
  24. Iscompleted string `json:"iscompleted"`
  25. Tmrtname string `json:"tmrtname"`
  26. Remark string `json:"remark"`
  27. Lpplantype string `json:"lpplantype"`
  28. Sort string `json:"sort"`
  29. Shifts string `json:"shifts"`
  30. }
  31. type ReqGetDownloadedplanMaterial struct {
  32. Pastureid int64 `json:"pastureid"`
  33. Datacaptureno int64 `json:"datacaptureno"`
  34. Date string `json:"date"`
  35. }
  36. func GetDownloadedplanMaterial1(c *gin.Context) {
  37. // var reqInfo ReqGetDownloadedplanMaterial
  38. // err := c.Bind(&reqInfo)
  39. appG := app.Gin{C: c}
  40. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  41. fsion := gofasion.NewFasion(string(dataByte))
  42. date := fsion.Get("date").ValueStr()
  43. // pastureid := fsion.Get("pastureid").ValueStr()
  44. datacaptureno := fsion.Get("datacaptureno").ValueStr()
  45. id := fsion.Get("id").ValueStr()
  46. tx := restful.Engine.NewSession()
  47. defer tx.Close()
  48. pastureinfo := new(udPastureInfo)
  49. err := tx.SQL(`select column_default as pastureid,(select werks from pasture where pastureid = column_default) werks from information_schema.COLUMNS
  50. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  51. if err != nil {
  52. logs.Error(err)
  53. return
  54. }
  55. pastureid := pastureinfo.Pastureid
  56. tclassid := ""
  57. tmrList, err := tx.SQL(` select tclassid from tmr where datacaptureno = ? `, datacaptureno).QueryString()
  58. if err != nil {
  59. logs.Error(err)
  60. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  61. return
  62. }
  63. for _, tmr := range tmrList {
  64. tclassid = tmr["tclassid"]
  65. }
  66. downloadedplanInfo := new(downloadedplan)
  67. err = tx.SQL(` select d.lpplantype,d.times shifts,d.id,date_format(d.mydate ,'%Y-%m-%d') as mydate ,d.projname,d.datacaptureno,d.lweight,d.havebutton,d.feedpname,d.iscompleted,d.remark,d.tmrtname from downloadedplan d
  68. where d.mydate = ? and d.pastureid = ? and (d.datacaptureno is null or d.datacaptureno = ?) and d.iscompleted = 0
  69. and ( (? IN(1,2) AND d.lpplanType IN (0,1)) OR (? IN (1,3) AND d.lpplanType IN (0,2)))
  70. and if(d.lpplantype = 2,if((select count(1) from downloadplandtl1 d1
  71. where d1.pid = (select id from downloadedplan dd where dd.mydate = d.mydate
  72. and dd.pastureId = d.pastureid and dd.lpplanType IN (0,1) and dd.pid = d.pid and dd.times = d.times )
  73. and d1.type = 0 and d1.havebuttom = 0 ) >0 ,1,0 ),0) = 0 and (?= '' or d.id = ? )
  74. order by d.sort `,
  75. date, pastureid, datacaptureno, tclassid, tclassid, id, id).GetFirst(downloadedplanInfo).Error
  76. if err != nil {
  77. logs.Error(err)
  78. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  79. return
  80. }
  81. if downloadedplanInfo.Id == 0 {
  82. appG.Response(http.StatusOK, e.THROUGHOUt_THE_DAY_TO_END, nil)
  83. return
  84. }
  85. _, err = tx.SQL(` update downloadedplan set havebutton = 1 ,intime = now() where id = ? and havebutton != 1 `, downloadedplanInfo.Id).Execute()
  86. if err != nil {
  87. logs.Error(err)
  88. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  89. return
  90. }
  91. _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = ifnull((select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1),0 ) where datacaptureno = ? and pastureid = ? `,
  92. downloadedplanInfo.Id, date, downloadedplanInfo.Id, datacaptureno, pastureid).Execute()
  93. if err != nil {
  94. logs.Error(err)
  95. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  96. return
  97. }
  98. appG.Response(http.StatusOK, e.SUCCESS, downloadedplanInfo)
  99. }
  100. func GetSubPlan(c *gin.Context) {
  101. appG := app.Gin{C: c}
  102. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  103. parammaps := gofasion.NewFasion(string(dataByte))
  104. pid := parammaps.Get("pid").ValueStr()
  105. // pastureid := parammaps.Get("pastureid").ValueStr()
  106. tx := restful.Engine.NewSession()
  107. defer tx.Close()
  108. pastureinfo := new(udPastureInfo)
  109. err := tx.SQL(`select column_default as pastureid,(select werks from pasture where pastureid = column_default) werks from information_schema.COLUMNS
  110. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  111. if err != nil {
  112. logs.Error(err)
  113. return
  114. }
  115. pastureid := pastureinfo.Pastureid
  116. downloadplandtl1List, err := tx.SQL(` select DATE_FORMAT(d1.begintime, '%Y-%m-%d %H:%i:%S') as begintime,
  117. DATE_FORMAT(d1.intime, '%Y-%m-%d %H:%i:%S') as intime ,f.id as feedid,d1.fname,d1.feedcode,f.autosecond,
  118. f.autozone,d1.lweight,d1.sort,d1.buttontype,d1.actualweightminus,f.confirmstart,f.jmp from downloadplandtl1 d1
  119. left join feed f
  120. on f.feedcode = d1.feedcode and f.pastureid = d1.pastureid where d1.pid = ? and d1.pastureid = ? `, pid, pastureid).Query().List()
  121. if err != nil {
  122. logs.Error(err)
  123. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  124. return
  125. }
  126. downloadplandtl2List, err := tx.SQL(` select d2.sort,DATE_FORMAT(d2.begintime, '%Y-%m-%d %H:%i:%S') as begintime ,DATE_FORMAT(d2.intime, '%Y-%m-%d %H:%i:%S') as intime
  127. ,d2.id,d2.pid,d2.fbarid,d2.fname as fbarname,d2.lweight,d.lpplantype,d2.actualweightminus ,ifnull(b.autozone,0) as autozone,ifnull(b.autosecond,"") as autosecond,ifnull(b.isstart,1) as isstart
  128. from downloadplandtl2 d2
  129. join downloadedplan d on d.id = d2.pid
  130. left join bar b on d2.fbarid = b.id where d2.pid = ? and d2.pastureid = ? `, pid, pastureid).Query().List()
  131. if err != nil {
  132. logs.Error(err)
  133. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  134. return
  135. }
  136. d1List := make([]map[string]interface{}, 0)
  137. d2List := make([]map[string]interface{}, 0)
  138. for _, d2item := range downloadplandtl2List {
  139. data := make(map[string]interface{}, 0)
  140. data["fbarid"] = d2item["fbarid"]
  141. data["fbarname"] = d2item["fbarname"]
  142. data["lweight"] = d2item["lweight"]
  143. data["lpplantype"] = d2item["lpplantype"]
  144. data["id"] = d2item["id"]
  145. data["actualweightminus"] = d2item["actualweightminus"]
  146. data["intime"] = d2item["intime"]
  147. data["begintime"] = d2item["begintime"]
  148. data["sort"] = d2item["sort"]
  149. data["autozone"] = d2item["autozone"]
  150. data["autosecond"] = d2item["autosecond"]
  151. data["isstart"] = d2item["isstart"]
  152. // data["jmp"] = d2item["jmp"]
  153. d2List = append(d2List, data)
  154. }
  155. for _, d1item := range downloadplandtl1List {
  156. d1 := make(map[string]interface{}, 0)
  157. // lweight, _ := strconv.ParseFloat(d1item["lweight"].(string), 64)
  158. d1["fname"] = d1item["fname"]
  159. d1["feedcode"] = d1item["feedcode"]
  160. d1["autosecond"] = d1item["autosecond"]
  161. d1["autozone"] = d1item["autozone"]
  162. d1["sort"] = d1item["sort"]
  163. d1["buttontype"] = d1item["buttontype"]
  164. d1["feedid"] = d1item["feedid"]
  165. d1["lweight"] = d1item["lweight"]
  166. d1["intime"] = d1item["intime"]
  167. d1["begintime"] = d1item["begintime"]
  168. d1["actualweightminus"] = d1item["actualweightminus"]
  169. d1["confirmstart"] = d1item["confirmstart"]
  170. d1["jmp"] = d1item["jmp"]
  171. d1List = append(d1List, d1)
  172. }
  173. resData := make(map[string]interface{}, 0)
  174. resData["mixing"] = d1List
  175. resData["spillage"] = d2List
  176. // data["plan"] = d1List
  177. // dataList = append(dataList, data)
  178. appG.Response(http.StatusOK, e.SUCCESS, resData)
  179. }
  180. func CompletePlan(c *gin.Context) {
  181. appG := app.Gin{C: c}
  182. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  183. parammaps := gofasion.NewFasion(string(dataByte))
  184. pid := parammaps.Get("pid").ValueStr()
  185. feedid := parammaps.Get("feedid").ValueStr()
  186. lastactualweight := parammaps.Get("lastactualweight").ValueDefaultFloat64(0)
  187. actualweightminus := parammaps.Get("actualweightminus").ValueDefaultFloat64(0)
  188. actualweight := parammaps.Get("actualweight").ValueDefaultFloat64(0)
  189. datacaptureno := parammaps.Get("datacaptureno").ValueStr()
  190. begintime := parammaps.Get("begintime").ValueStr()
  191. intime := parammaps.Get("intime").ValueStr()
  192. sort := parammaps.Get("sort").ValueStr()
  193. buttontype := parammaps.Get("buttontype").ValueStr()
  194. lpplantype := parammaps.Get("lpplantype").ValueDefaultInt64(0)
  195. d2id := parammaps.Get("d2id").ValueStr()
  196. // pastureid := parammaps.Get("pastureid").ValueStr()
  197. tx := restful.Engine.NewSession()
  198. defer tx.Close()
  199. fmt.Println(string(dataByte))
  200. pastureinfo := new(udPastureInfo)
  201. err := tx.SQL(`select column_default as pastureid,(select werks from pasture where pastureid = column_default) werks from information_schema.COLUMNS
  202. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  203. if err != nil {
  204. logs.Error(err)
  205. return
  206. }
  207. pastureid := pastureinfo.Pastureid
  208. _, err = tx.SQL(` update downloadedplan set tmrid = ( select id from tmr where datacaptureno = ? and pastureid = ?) ,
  209. tmrtname = ( select eqcode from tmr where datacaptureno = ? and pastureid = ?) where id = ? and tmrid != ( select id from tmr where datacaptureno = ? and pastureid = ?)`,
  210. datacaptureno, pastureid, datacaptureno, pastureid, pid, datacaptureno, pastureid).Execute()
  211. if err != nil {
  212. logs.Error(err)
  213. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  214. return
  215. }
  216. if lpplantype == 0 {
  217. _, err := tx.SQL(` update downloadplandtl1 set begintime = ? , actualweightminus = ifnull(actualweightminus,0) +? ,
  218. intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,
  219. havebuttom = 1,lastactualweight = ? where pid = ?
  220. and sort = ? and feedcode = (select feedcode from feed where id = ? ) and (intime is null )`,
  221. begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, pid, sort, feedid).Execute()
  222. if err != nil {
  223. logs.Error(err)
  224. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  225. return
  226. }
  227. _, err = tx.SQL(` update downloadplandtl1_exec set begintime = ? ,actualweightminus = ifnull(actualweightminus,0)+ ? ,
  228. intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ? where pid = ?
  229. and sort = ? and (intime is null ) `,
  230. begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, pid, sort).Execute()
  231. if err != nil {
  232. logs.Error(err)
  233. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  234. return
  235. }
  236. d2List, err := tx.SQL(` select * from downloadplandtl2 where pid = ? order by sort `, pid).Query().List()
  237. if err != nil {
  238. logs.Error(err)
  239. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  240. return
  241. }
  242. var sumlweight float64
  243. for _, d2 := range d2List {
  244. lweight, _ := strconv.ParseFloat(d2["lweight"].(string), 64)
  245. sumlweight += lweight
  246. }
  247. for _, d2 := range d2List {
  248. if actualweightminus <= 0 {
  249. break
  250. }
  251. lweight, _ := strconv.ParseFloat(d2["lweight"].(string), 64)
  252. actualweight := lweight / sumlweight * actualweightminus
  253. last := lweight / sumlweight * lastactualweight
  254. _, err := tx.SQL(` update downloadplandtl2 set actualweightminus = ifnull(actualweightminus,0)+?, actualweight = ?,
  255. lastactualweight = ? , intime = ?,begintime = ? ,havebuttom = 1,buttontype = ? where id = ? and (intime is null ) `, actualweight,
  256. actualweight, last, intime, begintime, buttontype, d2["id"]).Execute()
  257. if err != nil {
  258. logs.Error(err)
  259. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  260. return
  261. }
  262. }
  263. execExist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and (intime is null ) `, pid).Exist()
  264. if err != nil {
  265. logs.Error(err)
  266. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  267. return
  268. }
  269. if !execExist {
  270. _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute()
  271. if err != nil {
  272. logs.Error(err)
  273. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  274. return
  275. }
  276. }
  277. _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort =
  278. (select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `,
  279. pid, intime, pid, datacaptureno, pastureid).Execute()
  280. if err != nil {
  281. logs.Error(err)
  282. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  283. return
  284. }
  285. } else if lpplantype == 1 {
  286. _, err := tx.SQL(` update downloadplandtl2 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?,
  287. lastactualweight = ? , intime = ? ,begintime = ?,havebuttom = 1,buttontype = ? where id = ? and (intime is null ) `,
  288. actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, d2id).Execute()
  289. if err != nil {
  290. logs.Error(err)
  291. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  292. return
  293. }
  294. _, err = tx.SQL(` update downloadplandtl1_exec set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?,
  295. lastactualweight = ? , intime = ?,begintime= ? ,havebuttom = 1,buttontype= ? where pid = ? and (intime is null ) and sort = ? `,
  296. actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, pid, sort).Execute()
  297. if err != nil {
  298. logs.Error(err)
  299. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  300. return
  301. }
  302. _, err = tx.SQL(` update downloadplandtl1 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?,
  303. lastactualweight = ? , intime = ?,begintime=? ,havebuttom = 1,buttontype = ? where pid = ? and (intime is null ) and sort = ? `,
  304. actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, pid, sort).Execute()
  305. if err != nil {
  306. logs.Error(err)
  307. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  308. return
  309. }
  310. exist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and intime is null `, pid).Exist()
  311. if err != nil {
  312. logs.Error(err)
  313. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  314. return
  315. }
  316. if !exist {
  317. _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute()
  318. if err != nil {
  319. logs.Error(err)
  320. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  321. return
  322. }
  323. }
  324. _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = (select sort from downloadplandtl2
  325. where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `,
  326. pid, intime, pid, datacaptureno, pastureid).Execute()
  327. if err != nil {
  328. logs.Error(err)
  329. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  330. return
  331. }
  332. } else if lpplantype == 2 {
  333. _, err := tx.SQL(` update downloadplandtl1 set begintime = ? , actualweightminus =ifnull(actualweightminus,0)+ ? ,
  334. intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ?,begintime = ? where pid = ?
  335. and sort = ? and feedcode = (select feedcode from feed where id = ? ) and (intime is null )`,
  336. begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, begintime, pid, sort, feedid).Execute()
  337. if err != nil {
  338. logs.Error(err)
  339. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  340. return
  341. }
  342. _, err = tx.SQL(` update downloadplandtl1_exec set begintime = ? ,actualweightminus =ifnull(actualweightminus,0)+ ? ,
  343. intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ?,begintime = ? where pid = ?
  344. and sort = ? and (intime is null ) `,
  345. begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, begintime, pid, sort).Execute()
  346. if err != nil {
  347. logs.Error(err)
  348. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  349. return
  350. }
  351. _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort =
  352. (select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `,
  353. pid, intime, pid, datacaptureno, pastureid).Execute()
  354. if err != nil {
  355. logs.Error(err)
  356. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  357. return
  358. }
  359. } else {
  360. _, err := tx.SQL(` update downloadplandtl2 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?,
  361. lastactualweight = ? , intime = ? ,havebuttom = 1,buttontype = ?,begintime = ? where id = ? and (intime is null ) `,
  362. actualweightminus, actualweight, lastactualweight, intime, buttontype, begintime, d2id).Execute()
  363. if err != nil {
  364. logs.Error(err)
  365. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  366. return
  367. }
  368. exist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and intime is null`, pid).Exist()
  369. if err != nil {
  370. logs.Error(err)
  371. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  372. return
  373. }
  374. if !exist {
  375. _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute()
  376. if err != nil {
  377. logs.Error(err)
  378. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  379. return
  380. }
  381. }
  382. _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort =
  383. (select sort from downloadplandtl2 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `,
  384. pid, intime, pid, datacaptureno, pastureid).Execute()
  385. if err != nil {
  386. logs.Error(err)
  387. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  388. return
  389. }
  390. }
  391. appG.Response(http.StatusOK, e.SUCCESS, true)
  392. }
  393. func AddRealTimeWeight(c *gin.Context) {
  394. appG := app.Gin{C: c}
  395. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  396. fsion := gofasion.NewFasion(string(dataByte))
  397. pid := fsion.Get("pid").ValueStr()
  398. // pastureid := fsion.Get("pastureid").ValueStr()
  399. weightjson := fsion.Get("weightjson").ValueStr()
  400. // weightjson
  401. tx := restful.Engine.NewSession()
  402. defer tx.Close()
  403. pastureinfo := new(udPastureInfo)
  404. err := tx.SQL(`select column_default as pastureid,(select werks from pasture where pastureid = column_default) werks from information_schema.COLUMNS
  405. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  406. if err != nil {
  407. logs.Error(err)
  408. return
  409. }
  410. pastureid := pastureinfo.Pastureid
  411. dataList, err := tx.SQL(` select weightjson from recweightmuster where pid = ? and pastureid = ? `, pid, pastureid).QueryString()
  412. if err != nil {
  413. logs.Error("AddRealTimeWeight-error1: ", err)
  414. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  415. return
  416. }
  417. weightList := make([]map[string]interface{}, 0)
  418. if len(dataList) > 0 {
  419. weight := ""
  420. for _, item := range dataList {
  421. weight = item["weightjson"]
  422. }
  423. list := make([]map[string]interface{}, 0)
  424. err = json.Unmarshal([]byte(weight), &weightList)
  425. if err != nil {
  426. logs.Error("AddRealTimeWeight-error2: ", err)
  427. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  428. return
  429. }
  430. err = json.Unmarshal([]byte(weightjson), &list)
  431. if err != nil {
  432. logs.Error("AddRealTimeWeight-error3: ", err)
  433. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  434. return
  435. }
  436. weightList = append(weightList, list...)
  437. } else {
  438. json.Unmarshal([]byte(weightjson), &weightList)
  439. }
  440. weightbyte, err := json.Marshal(weightList)
  441. if err != nil {
  442. logs.Error("AddRealTimeWeight-error4: ", err)
  443. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  444. return
  445. }
  446. _, err = tx.SQL(`REPLACE INTO recweightmuster (pid, pastureid, weightjson)values (?,?,?)`, pid, pastureid, string(weightbyte)).Execute()
  447. if err != nil {
  448. logs.Error("AddRealTimeWeight-error5: ", err)
  449. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  450. return
  451. }
  452. appG.Response(http.StatusOK, e.SUCCESS, true)
  453. }
  454. func GetDownloadedplanMaterialList(c *gin.Context) {
  455. appG := app.Gin{C: c}
  456. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  457. fsion := gofasion.NewFasion(string(dataByte))
  458. // pastureid := fsion.Get("pastureid").ValueStr()
  459. date := fsion.Get("date").ValueStr()
  460. datacaptureno := fsion.Get("datacaptureno").ValueStr()
  461. // date := reqInfo.Date
  462. // datacaptureno := reqInfo.Datacaptureno
  463. tx := restful.Engine.NewSession()
  464. defer tx.Close()
  465. pastureinfo := new(udPastureInfo)
  466. err := tx.SQL(`select column_default as pastureid,(select werks from pasture where pastureid = column_default) werks from information_schema.COLUMNS
  467. WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
  468. if err != nil {
  469. logs.Error(err)
  470. return
  471. }
  472. pastureid := pastureinfo.Pastureid
  473. tclassid := ""
  474. tmrList, err := tx.SQL(` select tclassid from tmr where datacaptureno = ? `, datacaptureno).QueryString()
  475. if err != nil {
  476. logs.Error(err)
  477. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  478. return
  479. }
  480. for _, tmr := range tmrList {
  481. tclassid = tmr["tclassid"]
  482. }
  483. downloadedplanInfo := make([]*downloadedplan, 0)
  484. err = tx.SQL(` select d.lpplantype,d.times,d.sort,d.times shifts,d.id,date_format(d.mydate ,'%Y-%m-%d') as mydate ,d.projname,d.datacaptureno,d.lweight,d.havebutton,d.feedpname,d.iscompleted,d.remark,d.tmrtname from downloadedplan d
  485. where d.mydate = ? and d.pastureid = ? and (d.datacaptureno is null or d.datacaptureno = ?)
  486. and ( (? IN(1,2) AND d.lpplanType IN (0,1)) OR (? IN (1,3) AND d.lpplanType IN (0,2)))
  487. and if(d.lpplantype = 2,if((select count(1) from downloadplandtl1 d1
  488. where d1.pid = (select id from downloadedplan dd where dd.mydate = d.mydate
  489. and dd.pastureId = d.pastureid and dd.lpplanType IN (0,1) and dd.pid = d.pid and dd.times = d.times )
  490. and d1.type = 0 and d1.havebuttom = 0 ) >0 ,1,0 ),0) = 0
  491. order by d.sort `,
  492. date, pastureid, datacaptureno, tclassid, tclassid).Find(&downloadedplanInfo)
  493. if err != nil {
  494. logs.Error(err)
  495. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  496. return
  497. }
  498. appG.Response(http.StatusOK, e.SUCCESS, downloadedplanInfo)
  499. }