chart.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. package api
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "tmr-watch/http/handle/restful"
  9. "tmr-watch/pkg/app"
  10. "tmr-watch/pkg/e"
  11. "github.com/Anderson-Lu/gofasion/gofasion"
  12. "github.com/astaxie/beego/logs"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func GetFeedEffMR(c *gin.Context) {
  16. appG := app.Gin{C: c}
  17. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  18. fsion := gofasion.NewFasion(string(dataByte))
  19. sqlName := fsion.Get("name").ValueStr()
  20. parammaps := fsion.Get("parammaps")
  21. status := parammaps.Get("status").ValueStr()
  22. startDate := parammaps.Get("startTime").ValueStr()
  23. endDate := parammaps.Get("stopTime").ValueStr()
  24. pastureId := parammaps.Get("pastureid").ValueStr()
  25. tx := restful.Engine.NewSession()
  26. defer tx.Close()
  27. var sqlStr = `SELECT ROUND(IFNULL(SUM(d2.actualweightminus
  28. * (SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = d2.pastureid AND fd.barid = d2.fbarid
  29. AND fd.operatetime = (SELECT MAX(operatetime) FROM ftdry fd1 WHERE fd1.pastureid = d2.pastureid
  30. AND fd1.barid = d2.fbarid AND fd1.operatetime <=d2.date ))
  31. ),0),2) field1,
  32. ROUND(IFNULL(
  33. (SELECT SUM(bm.product*bm.cowsum) FROM
  34. (SELECT DISTINCT d21.fbarid,d21.date,d21.pastureid,d21.fname,IFNULL(d21.cowclassid,-1)cowclassid FROM
  35. downloadedplan d1
  36. JOIN downloadplandtl2 d21
  37. ON d21.pastureid = d1.pastureid AND d21.pid = d1.id
  38. WHERE d1.mydate between ? and ? AND d1.pastureid =? AND d1.lpplantype IN (0,1,2,5) AND d21.type = 0
  39. ) ww
  40. LEFT JOIN barmilk bm
  41. ON bm.pastureid =ww.pastureid AND bm.barid = ww.fbarid AND bm.productdate
  42. =
  43. (
  44. SELECT MAX(productdate) FROM barmilk
  45. WHERE barmilk.productdate <= ww.date
  46. AND barmilk.pastureid = ww.pastureid
  47. AND barmilk.barid = ww.fbarid
  48. )
  49. WHERE ww.date = d.mydate and bm.bigcowclass = '泌乳牛'
  50. )
  51. ,0),2) field2, -- 产奶量
  52. %s 日期
  53. FROM
  54. downloadedplan d
  55. LEFT JOIN downloadplandtl2 d2
  56. ON d2.pastureid = d.pastureid AND d2.pid = d.id
  57. LEFT JOIN barmilk bm
  58. ON bm.pastureid =d.pastureid AND bm.barid = d2.fbarid AND bm.productdate =
  59. (SELECT MAX(productdate) FROM barmilk WHERE barmilk.productdate <=d.mydate
  60. AND barmilk.pastureid = d.pastureid AND barmilk.barid = d2.fbarid)
  61. WHERE d.pastureid = ? AND d.mydate BETWEEN ? AND ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0
  62. AND bm.bigcowclass = '泌乳牛'`
  63. var data []map[string]interface{}
  64. var err error
  65. if status == "0" {
  66. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ")
  67. sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate "
  68. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, pastureId, startDate, endDate).Query().List()
  69. if err != nil {
  70. logs.Error(sqlName, " : ", err)
  71. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  72. }
  73. } else if status == "1" {
  74. timeTemplate := "2006-01-02"
  75. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  76. week := 0
  77. if int(start.Weekday()) == 0 {
  78. week = 6
  79. } else {
  80. week = int(start.Weekday()) - 1
  81. }
  82. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( (( DATEDIFF(mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  83. sqlStr += " GROUP BY 日期 ORDER BY d.mydate "
  84. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, pastureId, startDate, endDate).Query().List()
  85. if err != nil {
  86. logs.Error(sqlName, " : ", err)
  87. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  88. }
  89. } else {
  90. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ")
  91. sqlStr += " GROUP BY MONTH(d.mydate) ORDER BY d.mydate "
  92. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, pastureId, startDate, endDate).Query().List()
  93. if err != nil {
  94. logs.Error(sqlName, " : ", err)
  95. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  96. }
  97. }
  98. queryData, err := getAccuracyV2(data)
  99. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  100. }
  101. func GetFeedEffMRJT(c *gin.Context) {
  102. appG := app.Gin{C: c}
  103. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  104. fsion := gofasion.NewFasion(string(dataByte))
  105. sqlName := fsion.Get("name").ValueStr()
  106. parammaps := fsion.Get("parammaps")
  107. status := parammaps.Get("status").ValueStr()
  108. startDate := parammaps.Get("startTime").ValueStr()
  109. endDate := parammaps.Get("stopTime").ValueStr()
  110. pastureId := parammaps.Get("pastureid").ValueStr()
  111. tx := restful.Engine.NewSession()
  112. defer tx.Close()
  113. var sqlStr = `SELECT IFNULL(SUM(d2.actualweightminus),0) 准确率, -- 采食量
  114. (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称,
  115. %s 日期
  116. FROM
  117. downloadedplan d
  118. LEFT JOIN downloadplandtl2 d2
  119. ON d2.pastureid = d.pastureid AND d2.pid = d.id
  120. LEFT JOIN barmilk bm
  121. ON bm.pastureid =d.pastureid AND bm.barid = d2.fbarid AND bm.productdate = d.mydate
  122. JOIN
  123. (SELECT pastureid FROM groups_pasture WHERE groupsid = ?
  124. UNION
  125. SELECT pastureid FROM pasture WHERE groupsid = ?
  126. ) tem ON tem.pastureid = d.pastureid
  127. WHERE d.mydate BETWEEN ? AND ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0
  128. AND IF(bm.idIS NOT NULL,bm.bigcowclass = '泌乳牛',1=1)
  129. GROUP BY d.pastureid,日期 ORDER BY 名称, 日期 `
  130. var data []map[string]interface{}
  131. var err error
  132. if status == "0" {
  133. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ")
  134. // sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate "
  135. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  136. if err != nil {
  137. logs.Error(sqlName, " : ", err)
  138. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  139. }
  140. } else if status == "1" {
  141. timeTemplate := "2006-01-02"
  142. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  143. week := 0
  144. if int(start.Weekday()) == 0 {
  145. week = 6
  146. } else {
  147. week = int(start.Weekday()) - 1
  148. }
  149. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( (( DATEDIFF(mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  150. // sqlStr += " GROUP BY 日期 ORDER BY d.mydate "
  151. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  152. if err != nil {
  153. logs.Error(sqlName, " : ", err)
  154. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  155. }
  156. } else {
  157. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ")
  158. // sqlStr += " GROUP BY MONTH(d.mydate) ORDER BY d.mydate "
  159. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  160. if err != nil {
  161. logs.Error(sqlName, " : ", err)
  162. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  163. }
  164. }
  165. queryData, err := getAccuracyV2(data)
  166. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  167. }
  168. func GetFeedEffSL(c *gin.Context) {
  169. appG := app.Gin{C: c}
  170. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  171. fsion := gofasion.NewFasion(string(dataByte))
  172. sqlName := fsion.Get("name").ValueStr()
  173. parammaps := fsion.Get("parammaps")
  174. status := parammaps.Get("status").ValueStr()
  175. startDate := parammaps.Get("startTime").ValueStr()
  176. endDate := parammaps.Get("stopTime").ValueStr()
  177. pastureId := parammaps.Get("pastureid").ValueStr()
  178. tx := restful.Engine.NewSession()
  179. defer tx.Close()
  180. var sqlStr = `SELECT
  181. ft.tname 名称,
  182. IFNULL(SUM(d2.actualweightminus),0) 撒料量,
  183. IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = ft.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 剩料量,
  184. ifnull(ROUND(IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0)/SUM(d2.actualweightminus),2) ,0)准确率,
  185. %s 日期
  186. FROM
  187. downloadedplan d
  188. JOIN downloadplandtl2 d2
  189. ON d2.pastureid = d.pastureid AND d2.pid = d.id
  190. join feedtempletdate ft on ft.id = d.tempid AND d.mydate = ft.date
  191. LEFT JOIN feedpdate fp
  192. on fp.barid = d2.fbarid and d.pastureid = fp.pastureid AND d.mydate = fp.date
  193. WHERE d.mydate BETWEEN ? AND ? AND ft.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 `
  194. var data []map[string]interface{}
  195. var err error
  196. if status == "0" {
  197. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(ft.date, '%Y-%m-%d') ")
  198. sqlStr += " GROUP BY d.tempid, DATE_FORMAT(ft.date, '%Y-%m-%d') ORDER BY ft.tname ,ft.date "
  199. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  200. if err != nil {
  201. logs.Error(sqlName, " : ", err)
  202. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  203. }
  204. // appG.Response(http.StatusOK, e.SUCCESS, data)
  205. } else if status == "1" {
  206. // sqlStr := chartMap[sqlName]
  207. timeTemplate := "2006-01-02"
  208. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  209. week := 0
  210. if int(start.Weekday()) == 0 {
  211. week = 6
  212. } else {
  213. week = int(start.Weekday()) - 1
  214. }
  215. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(ft.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  216. sqlStr += " GROUP BY d.tempid , 日期 ORDER BY ft.tname ,ft.date "
  217. fmt.Println(sqlStr)
  218. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  219. if err != nil {
  220. logs.Error(sqlName, " : ", err)
  221. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  222. }
  223. // appG.Response(http.StatusOK, e.SUCCESS, data)
  224. } else {
  225. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(ft.date, '%Y-%m') ")
  226. sqlStr += " GROUP BY d.tempid , DATE_FORMAT(ft.date, '%Y-%m') ORDER BY ft.tname ,ft.date "
  227. // timeTemplate := "2006-01-02"
  228. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  229. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  230. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  231. if err != nil {
  232. logs.Error(sqlName, " : ", err)
  233. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  234. }
  235. // appG.Response(http.StatusOK, e.SUCCESS, data)
  236. }
  237. queryData, err := getAccuracy(data)
  238. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  239. }
  240. func GetFeedEffSLJT(c *gin.Context) {
  241. appG := app.Gin{C: c}
  242. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  243. fsion := gofasion.NewFasion(string(dataByte))
  244. sqlName := fsion.Get("name").ValueStr()
  245. parammaps := fsion.Get("parammaps")
  246. status := parammaps.Get("status").ValueStr()
  247. startDate := parammaps.Get("startTime").ValueStr()
  248. endDate := parammaps.Get("stopTime").ValueStr()
  249. pastureId := parammaps.Get("pastureid").ValueStr()
  250. tx := restful.Engine.NewSession()
  251. defer tx.Close()
  252. var sqlStr = `SELECT
  253. (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称,
  254. IFNULL(SUM(d.oweight),0) 撒料量,
  255. IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 剩料量,
  256. ifnull(ROUND(IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0)/SUM(d.oweight),2),0)准确率, -- 剩料率
  257. %s 日期
  258. FROM
  259. feedpdate fp
  260. RIGHT JOIN downloadedplan d
  261. ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname
  262. LEFT JOIN barmilk bm
  263. ON bm.pastureid =fp.pastureid AND bm.barid = fp.barid AND bm.productdate = fp.date
  264. JOIN
  265. (SELECT pastureid FROM groups_pasture WHERE groupsid = ?
  266. UNION
  267. SELECT pastureid FROM pasture WHERE groupsid = ?
  268. ) tem
  269. ON tem.pastureid = d.pastureid
  270. WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3
  271. GROUP BY d.pastureid ,日期
  272. ORDER BY 名称 ,日期`
  273. var data []map[string]interface{}
  274. var err error
  275. if status == "0" {
  276. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ")
  277. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  278. if err != nil {
  279. logs.Error(sqlName, " : ", err)
  280. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  281. }
  282. } else if status == "1" {
  283. // sqlStr := chartMap[sqlName]
  284. timeTemplate := "2006-01-02"
  285. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  286. week := 0
  287. if int(start.Weekday()) == 0 {
  288. week = 6
  289. } else {
  290. week = int(start.Weekday()) - 1
  291. }
  292. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  293. fmt.Println(sqlStr)
  294. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  295. if err != nil {
  296. logs.Error(sqlName, " : ", err)
  297. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  298. }
  299. } else {
  300. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ")
  301. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  302. if err != nil {
  303. logs.Error(sqlName, " : ", err)
  304. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  305. }
  306. // appG.Response(http.StatusOK, e.SUCCESS, data)
  307. }
  308. queryData, err := getAccuracy(data)
  309. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  310. }
  311. func GetFeedEffHL(c *gin.Context) {
  312. appG := app.Gin{C: c}
  313. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  314. fsion := gofasion.NewFasion(string(dataByte))
  315. sqlName := fsion.Get("name").ValueStr()
  316. parammaps := fsion.Get("parammaps")
  317. status := parammaps.Get("status").ValueStr()
  318. startDate := parammaps.Get("startTime").ValueStr()
  319. endDate := parammaps.Get("stopTime").ValueStr()
  320. pastureId := parammaps.Get("pastureid").ValueStr()
  321. tx := restful.Engine.NewSession()
  322. defer tx.Close()
  323. var sqlStr = `SELECT
  324. %s 日期,
  325. ifnull(ROUND(SUM(UNIX_TIMESTAMP((SELECT MAX(intime) FROM downloadplandtl1_exec d2
  326. WHERE d.mydate=d2.date AND d.pastureid = d2.pastureid AND d2.pid=d.id))-UNIX_TIMESTAMP(d.intime))/60,2),0) field1 -- 混料时间
  327. FROM
  328. downloadedplan d
  329. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid = ? AND d.lpplantype IN (0,1) `
  330. var data []map[string]interface{}
  331. var err error
  332. if status == "0" {
  333. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ")
  334. sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate "
  335. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  336. if err != nil {
  337. logs.Error(sqlName, " : ", err)
  338. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  339. }
  340. // appG.Response(http.StatusOK, e.SUCCESS, data)
  341. } else if status == "1" {
  342. // sqlStr := chartMap[sqlName]
  343. timeTemplate := "2006-01-02"
  344. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  345. week := 0
  346. if int(start.Weekday()) == 0 {
  347. week = 6
  348. } else {
  349. week = int(start.Weekday()) - 1
  350. }
  351. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  352. sqlStr += " GROUP BY 日期 ORDER BY 日期 "
  353. fmt.Println(sqlStr)
  354. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  355. if err != nil {
  356. logs.Error(sqlName, " : ", err)
  357. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  358. }
  359. // appG.Response(http.StatusOK, e.SUCCESS, data)
  360. } else {
  361. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ")
  362. sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m') ORDER BY d.mydate "
  363. // timeTemplate := "2006-01-02"
  364. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  365. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  366. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  367. if err != nil {
  368. logs.Error(sqlName, " : ", err)
  369. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  370. }
  371. // appG.Response(http.StatusOK, e.SUCCESS, data)
  372. }
  373. queryData, err := getAccuracyV2(data)
  374. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  375. }
  376. func GetFeedEffHLJT(c *gin.Context) {
  377. appG := app.Gin{C: c}
  378. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  379. fsion := gofasion.NewFasion(string(dataByte))
  380. sqlName := fsion.Get("name").ValueStr()
  381. parammaps := fsion.Get("parammaps")
  382. status := parammaps.Get("status").ValueStr()
  383. startDate := parammaps.Get("startTime").ValueStr()
  384. endDate := parammaps.Get("stopTime").ValueStr()
  385. pastureId := parammaps.Get("pastureid").ValueStr()
  386. tx := restful.Engine.NewSession()
  387. defer tx.Close()
  388. var sqlStr = `SELECT
  389. (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称,
  390. %s 日期,
  391. IFNULL(ROUND(SUM(UNIX_TIMESTAMP((SELECT MAX(intime) FROM downloadplandtl1_exec d2
  392. WHERE d.mydate=d2.date AND d.pastureid = d2.pastureid AND d2.pid=d.id))-UNIX_TIMESTAMP(d.intime))/60,2),0) 准确率-- 混料时间
  393. FROM
  394. downloadedplan d
  395. JOIN
  396. (SELECT pastureid FROM groups_pasture WHERE groupsid = ?
  397. UNION
  398. SELECT pastureid FROM pasture WHERE groupsid = ?
  399. ) tem
  400. ON tem.pastureid = d.pastureid
  401. WHERE d.mydate BETWEEN ? AND ? AND d.lpplantype<3
  402. GROUP BY d.pastureid,日期
  403. ORDER BY 名称,日期`
  404. var data []map[string]interface{}
  405. var err error
  406. if status == "0" {
  407. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ")
  408. // sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate "
  409. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  410. if err != nil {
  411. logs.Error(sqlName, " : ", err)
  412. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  413. }
  414. } else if status == "1" {
  415. // sqlStr := chartMap[sqlName]
  416. timeTemplate := "2006-01-02"
  417. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  418. week := 0
  419. if int(start.Weekday()) == 0 {
  420. week = 6
  421. } else {
  422. week = int(start.Weekday()) - 1
  423. }
  424. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  425. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  426. if err != nil {
  427. logs.Error(sqlName, " : ", err)
  428. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  429. }
  430. } else {
  431. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ")
  432. sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m') ORDER BY d.mydate "
  433. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  434. if err != nil {
  435. logs.Error(sqlName, " : ", err)
  436. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  437. }
  438. }
  439. queryData, err := getAccuracyV2(data)
  440. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  441. }
  442. func GetFeedEffZH(c *gin.Context) {
  443. appG := app.Gin{C: c}
  444. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  445. fsion := gofasion.NewFasion(string(dataByte))
  446. sqlName := fsion.Get("name").ValueStr()
  447. parammaps := fsion.Get("parammaps")
  448. status := parammaps.Get("status").ValueStr()
  449. startDate := parammaps.Get("startTime").ValueStr()
  450. endDate := parammaps.Get("stopTime").ValueStr()
  451. pastureId := parammaps.Get("pastureid").ValueStr()
  452. tx := restful.Engine.NewSession()
  453. defer tx.Close()
  454. var sqlStr = `SELECT
  455. 日期,
  456. 配方模板 名称,
  457. IFNULL(ROUND(IFNULL(产奶量/实际牛头数,0)/((撒料量 - 今日剩料量)*TMR干物质/实际牛头数)*100,2),0) 准确率 -- 饲料转化率
  458. FROM (
  459. SELECT
  460. %s 日期,
  461. d.templetname 配方模板,
  462. (SELECT SUM(ccount) FROM feedpdate fpt WHERE fpt.pastureid = d.pastureid AND fpt.date = d.mydate AND fpt.ftid = d.tempid)实际牛头数,
  463. @tid := d.tempid tempid,
  464. @da := d.mydate,
  465. (SELECT SUM(bm.product*bm.cowsum) FROM
  466. (SELECT DISTINCT d21.fbarid,d21.date,d21.pastureid,d1.tempid FROM
  467. downloadedplan d1
  468. JOIN downloadplandtl2 d21
  469. ON d21.pastureid = d1.pastureid AND d21.pid = d1.id
  470. WHERE d1.mydate BETWEEN ? AND ? AND d1.pastureid = ? AND d1.lpplantype IN (0,1,2,5) AND d21.type = 0
  471. GROUP BY d1.tempid,d1.mydate) ww
  472. LEFT JOIN barmilk bm
  473. ON bm.pastureid =ww.pastureid AND bm.barid = ww.fbarid AND bm.productdate
  474. =
  475. (
  476. SELECT MAX(productdate) FROM barmilk
  477. WHERE barmilk.productdate <= ww.date
  478. AND barmilk.pastureid = ww.pastureid
  479. AND barmilk.barid = ww.fbarid
  480. )
  481. WHERE ww.tempid = @tid AND ww.date = @da
  482. ) 产奶量,
  483. SUM(d2.actualweightminus) 撒料量,
  484. SUM(IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0)) 今日剩料量,
  485. SUM(IFNULL((SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = fp.pastureid AND fd.barid = fp.barid AND fd.operatetime = (SELECT MAX(operatetime) FROM ftdry fd1 WHERE fd1.pastureid = fp.pastureid AND fd1.barid = fp.barid AND fd1.operatetime <=fp.date )),0)*d2.actualweightminus)/SUM(d2.actualweightminus) TMR干物质
  486. FROM
  487. downloadedplan d
  488. JOIN downloadplandtl2 d2
  489. ON d2.pastureid = d.pastureid AND d2.pid = d.id
  490. LEFT JOIN feedpdate fp
  491. ON fp.barid = d2.fbarid AND d.pastureid = fp.pastureid AND d.mydate = fp.date
  492. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 `
  493. var data []map[string]interface{}
  494. var err error
  495. if status == "0" {
  496. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ")
  497. sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 "
  498. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  499. if err != nil {
  500. logs.Error(sqlName, " : ", err)
  501. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  502. }
  503. // appG.Response(http.StatusOK, e.SUCCESS, data)
  504. } else if status == "1" {
  505. // sqlStr := chartMap[sqlName]
  506. timeTemplate := "2006-01-02"
  507. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  508. week := 0
  509. if int(start.Weekday()) == 0 {
  510. week = 6
  511. } else {
  512. week = int(start.Weekday()) - 1
  513. }
  514. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  515. sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 "
  516. fmt.Println(sqlStr)
  517. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  518. if err != nil {
  519. logs.Error(sqlName, " : ", err)
  520. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  521. }
  522. // appG.Response(http.StatusOK, e.SUCCESS, data)
  523. } else {
  524. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ")
  525. sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 "
  526. // timeTemplate := "2006-01-02"
  527. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  528. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  529. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  530. if err != nil {
  531. logs.Error(sqlName, " : ", err)
  532. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  533. }
  534. // appG.Response(http.StatusOK, e.SUCCESS, data)
  535. }
  536. queryData, err := getAccuracy(data)
  537. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  538. }
  539. func GetFeedEffZHJT(c *gin.Context) {
  540. appG := app.Gin{C: c}
  541. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  542. fsion := gofasion.NewFasion(string(dataByte))
  543. sqlName := fsion.Get("name").ValueStr()
  544. parammaps := fsion.Get("parammaps")
  545. status := parammaps.Get("status").ValueStr()
  546. startDate := parammaps.Get("startTime").ValueStr()
  547. endDate := parammaps.Get("stopTime").ValueStr()
  548. pastureId := parammaps.Get("pastureid").ValueStr()
  549. tx := restful.Engine.NewSession()
  550. defer tx.Close()
  551. var sqlStr = `
  552. SELECT
  553. 日期,
  554. (SELECT pasture_name FROM pasture WHERE pastureid = tem.pastureid) 名称,
  555. IFNULL(ROUND((产奶量)/((撒料量 - 今日剩料量)*TMR干物质)*100,2),'0.00') 准确率 -- 饲料转化率
  556. FROM (
  557. SELECT
  558. d.pastureid,
  559. %s 日期,
  560. fp.ftname 配方模板,SUM(bm.product)产奶量,
  561. SUM(d.oweight) 撒料量,
  562. IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 今日剩料量,
  563. IFNULL((SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = fp.pastureid AND fd.barid = fp.barid AND fd.operatetime = fp.date),0) TMR干物质
  564. FROM
  565. feedpdate fp
  566. INNER JOIN downloadedplan d
  567. ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname
  568. LEFT JOIN barmilk bm
  569. ON bm.pastureid =fp.pastureid AND bm.barid = fp.barid AND bm.productdate = (SELECT MAX(productdate) FROM barmilk WHERE barmilk.productdate <=fp.date AND barmilk.pastureid = fp.pastureid and barmilk.barid = fp.barid)
  570. JOIN
  571. (SELECT pastureid FROM groups_pasture WHERE groupsid = ?
  572. UNION
  573. SELECT pastureid FROM pasture WHERE groupsid = ?
  574. ) tem
  575. ON tem.pastureid = d.pastureid
  576. WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3
  577. GROUP BY d.pastureid,日期 ) tem
  578. ORDER BY 名称,日期`
  579. var data []map[string]interface{}
  580. var err error
  581. if status == "0" {
  582. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ")
  583. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  584. if err != nil {
  585. logs.Error(sqlName, " : ", err)
  586. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  587. }
  588. } else if status == "1" {
  589. timeTemplate := "2006-01-02"
  590. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  591. week := 0
  592. if int(start.Weekday()) == 0 {
  593. week = 6
  594. } else {
  595. week = int(start.Weekday()) - 1
  596. }
  597. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  598. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  599. if err != nil {
  600. logs.Error(sqlName, " : ", err)
  601. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  602. }
  603. } else {
  604. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ")
  605. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  606. if err != nil {
  607. logs.Error(sqlName, " : ", err)
  608. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  609. }
  610. }
  611. queryData, err := getAccuracy(data)
  612. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  613. }
  614. func GetFeedEffCBFT(c *gin.Context) {
  615. appG := app.Gin{C: c}
  616. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  617. fsion := gofasion.NewFasion(string(dataByte))
  618. sqlName := fsion.Get("name").ValueStr()
  619. parammaps := fsion.Get("parammaps")
  620. status := parammaps.Get("status").ValueStr()
  621. startDate := parammaps.Get("startTime").ValueStr()
  622. endDate := parammaps.Get("stopTime").ValueStr()
  623. pastureId := parammaps.Get("pastureid").ValueStr()
  624. tx := restful.Engine.NewSession()
  625. defer tx.Close()
  626. var sqlStr = `SELECT
  627. 日期,
  628. 配方 名称,
  629. IFNULL(ROUND(实际混料量*配方单价/产奶量,2),0) 准确率 -- 公斤奶饲料成本
  630. FROM (
  631. SELECT
  632. %s 日期,
  633. fp.ftname 配方,
  634. (SELECT SUM(d1e.actualweightminus) FROM downloadplandtl1_exec d1e
  635. JOIN downloadedplan dv ON dv.pastureid= d1e.pastureid AND dv.id = d1e.pid
  636. WHERE d1e.pastureid = d.pastureid AND dv.mydate = d.mydate AND d1e.type = 0 AND dv.tempid= d.tempid)实际混料量,
  637. @tid := d.tempid tempid,
  638. @da := d.mydate,
  639. (SELECT SUM(bm.product*bm.cowsum) FROM
  640. (SELECT DISTINCT d21.fbarid,d21.date,d21.pastureid,d1.tempid FROM
  641. downloadedplan d1
  642. JOIN downloadplandtl2 d21
  643. ON d21.pastureid = d1.pastureid AND d21.pid = d1.id
  644. WHERE d1.mydate BETWEEN ? AND ? AND d1.pastureid = ? AND d1.lpplantype IN (0,1,2,5) AND d21.type = 0
  645. GROUP BY d1.tempid,d1.mydate) ww
  646. LEFT JOIN barmilk bm
  647. ON bm.pastureid =ww.pastureid AND bm.barid = ww.fbarid AND bm.productdate
  648. =
  649. (
  650. SELECT MAX(productdate) FROM barmilk
  651. WHERE barmilk.productdate <= ww.date
  652. AND barmilk.pastureid = ww.pastureid
  653. AND barmilk.barid = ww.fbarid
  654. )
  655. WHERE ww.tempid = @tid AND ww.date = @da
  656. ) 产奶量,
  657. (SELECT SUM(
  658. IF(ftd.preftid<>0,
  659. (SELECT SUM(f1.uprice* ftd1.fweight)/SUM(ftd1.fweight) FROM ftdetaildate ftd1
  660. LEFT JOIN feed f1
  661. ON f1.pastureid = ftd1.pastureid AND f1.id = ftd1.fid
  662. WHERE ftd1.pastureid = ftd.pastureid AND ftd1.date = ftd.date AND ftd1.ftid = ftd.preftid
  663. AND ftd1.version = (SELECT MAX(VERSION) FROM ftdetaildate ftd2 WHERE ftd2.pastureid = ftd.pastureid AND ftd2.date = ftd.date AND ftd2.ftid = ftd.preftid)),
  664. f.uprice)*ftd.fweight)/SUM(ftd.fweight) FROM ftdetaildate ftd
  665. LEFT JOIN feed f
  666. ON f.pastureid = ftd.pastureid AND f.id = ftd.fid
  667. WHERE ftd.pastureid = fp.pastureid AND ftd.date = fp.date AND ftd.ftid = fp.ftid AND ftd.version = d.tversion
  668. ) 配方单价
  669. FROM
  670. downloadedplan d
  671. JOIN downloadplandtl2 d2
  672. ON d2.pastureid = d.pastureid AND d2.pid = d.id
  673. LEFT JOIN feedpdate fp
  674. ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND fp.barid = d2.fbarid
  675. WHERE fp.date BETWEEN ? AND ? AND fp.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 `
  676. var data []map[string]interface{}
  677. var err error
  678. if status == "0" {
  679. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ")
  680. sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 "
  681. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  682. if err != nil {
  683. logs.Error(sqlName, " : ", err)
  684. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  685. }
  686. // appG.Response(http.StatusOK, e.SUCCESS, data)
  687. } else if status == "1" {
  688. // sqlStr := chartMap[sqlName]
  689. timeTemplate := "2006-01-02"
  690. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  691. week := 0
  692. if int(start.Weekday()) == 0 {
  693. week = 6
  694. } else {
  695. week = int(start.Weekday()) - 1
  696. }
  697. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  698. sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 "
  699. fmt.Println(sqlStr)
  700. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  701. if err != nil {
  702. logs.Error(sqlName, " : ", err)
  703. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  704. }
  705. // appG.Response(http.StatusOK, e.SUCCESS, data)
  706. } else {
  707. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ")
  708. sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 "
  709. // timeTemplate := "2006-01-02"
  710. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  711. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  712. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List()
  713. if err != nil {
  714. logs.Error(sqlName, " : ", err)
  715. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  716. }
  717. // appG.Response(http.StatusOK, e.SUCCESS, data)
  718. }
  719. queryData, err := getAccuracy(data)
  720. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  721. }
  722. //成本分析-集团
  723. func GetFeedEffCBAllJT(c *gin.Context) {
  724. appG := app.Gin{C: c}
  725. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  726. fsion := gofasion.NewFasion(string(dataByte))
  727. sqlName := fsion.Get("name").ValueStr()
  728. parammaps := fsion.Get("parammaps")
  729. status := parammaps.Get("status").ValueStr()
  730. startDate := parammaps.Get("startTime").ValueStr()
  731. endDate := parammaps.Get("stopTime").ValueStr()
  732. pastureId := parammaps.Get("pastureid").ValueStr()
  733. tx := restful.Engine.NewSession()
  734. defer tx.Close()
  735. var sqlStr = `
  736. SELECT
  737. 日期,
  738. (SELECT pasture_name FROM pasture WHERE pastureid = tem.pastureid) 名称,
  739. IFNULL(ROUND((产奶量)/((撒料量 - 今日剩料量)*TMR干物质)*100,2),'0.00') 准确率 -- 饲料转化率
  740. FROM (
  741. SELECT
  742. d.pastureid,
  743. %s 日期,
  744. fp.ftname 配方模板,SUM(bm.product)产奶量,
  745. SUM(d.oweight) 撒料量,
  746. IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 今日剩料量,
  747. IFNULL((SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = fp.pastureid AND fd.barid = fp.barid AND fd.operatetime = fp.date),0) TMR干物质
  748. FROM
  749. feedpdate fp
  750. INNER JOIN downloadedplan d
  751. ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname
  752. LEFT JOIN barmilk bm
  753. ON bm.pastureid =fp.pastureid AND bm.barid = fp.barid AND bm.productdate = (SELECT MAX(productdate) FROM barmilk WHERE barmilk.productdate <=fp.date AND barmilk.pastureid = fp.pastureid and barmilk.barid = fp.barid)
  754. JOIN
  755. (SELECT pastureid FROM groups_pasture WHERE groupsid = ?
  756. UNION
  757. SELECT pastureid FROM pasture WHERE groupsid = ?
  758. ) tem
  759. ON tem.pastureid = d.pastureid
  760. WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3
  761. GROUP BY d.pastureid,日期 ) tem
  762. ORDER BY 名称,日期`
  763. var data []map[string]interface{}
  764. var err error
  765. if status == "0" {
  766. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ")
  767. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  768. if err != nil {
  769. logs.Error(sqlName, " : ", err)
  770. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  771. }
  772. } else if status == "1" {
  773. timeTemplate := "2006-01-02"
  774. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  775. week := 0
  776. if int(start.Weekday()) == 0 {
  777. week = 6
  778. } else {
  779. week = int(start.Weekday()) - 1
  780. }
  781. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  782. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  783. if err != nil {
  784. logs.Error(sqlName, " : ", err)
  785. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  786. }
  787. } else {
  788. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ")
  789. data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List()
  790. if err != nil {
  791. logs.Error(sqlName, " : ", err)
  792. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  793. }
  794. }
  795. queryData, err := getAccuracy(data)
  796. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  797. }
  798. // getAccuracyAllFT
  799. func GetAccuracyAllFT(c *gin.Context) {
  800. appG := app.Gin{C: c}
  801. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  802. fsion := gofasion.NewFasion(string(dataByte))
  803. sqlName := fsion.Get("name").ValueStr()
  804. parammaps := fsion.Get("parammaps")
  805. status := parammaps.Get("status").ValueStr()
  806. startDate := parammaps.Get("startTime").ValueStr()
  807. endDate := parammaps.Get("stopTime").ValueStr()
  808. pastureId := parammaps.Get("pastureid").ValueStr()
  809. tx := restful.Engine.NewSession()
  810. defer tx.Close()
  811. var sqlStr = `SELECT %s 日期,
  812. d.templetname 名称,
  813. SUM(de.lweight) 理论量,
  814. IFNULL(SUM(de.actualweightminus),0) 实际量,
  815. ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率,
  816. COUNT(de.id) 添加饲料次数,
  817. SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.feedallowratio,1,0)) 添加正确数,
  818. IF(COUNT(de.id)>0,SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.feedallowratio,1,0))/COUNT(de.id),0) 正确率
  819. FROM downloadedplan d JOIN downloadplandtl1_exec de ON d.pastureid = de.pastureid and d.id=de.pid
  820. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0
  821. -- 加上就是不含取消操作
  822. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 ))
  823. `
  824. var data []map[string]interface{}
  825. var err error
  826. if status == "0" {
  827. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  828. sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate "
  829. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  830. if err != nil {
  831. logs.Error(sqlName, " : ", err)
  832. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  833. }
  834. // appG.Response(http.StatusOK, e.SUCCESS, data)
  835. } else if status == "1" {
  836. // sqlStr := chartMap[sqlName]
  837. timeTemplate := "2006-01-02"
  838. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  839. week := 0
  840. if int(start.Weekday()) == 0 {
  841. week = 6
  842. } else {
  843. week = int(start.Weekday()) - 1
  844. }
  845. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  846. sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate "
  847. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  848. if err != nil {
  849. logs.Error(sqlName, " : ", err)
  850. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  851. }
  852. // appG.Response(http.StatusOK, e.SUCCESS, data)
  853. } else {
  854. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  855. sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate "
  856. // timeTemplate := "2006-01-02"
  857. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  858. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  859. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  860. if err != nil {
  861. logs.Error(sqlName, " : ", err)
  862. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  863. }
  864. // appG.Response(http.StatusOK, e.SUCCESS, data)
  865. }
  866. queryData, err := getAccuracy(data)
  867. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  868. }
  869. // getAccuracyAllJH
  870. func GetAccuracyAllJH(c *gin.Context) {
  871. appG := app.Gin{C: c}
  872. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  873. fsion := gofasion.NewFasion(string(dataByte))
  874. sqlName := fsion.Get("name").ValueStr()
  875. parammaps := fsion.Get("parammaps")
  876. status := parammaps.Get("status").ValueStr()
  877. startDate := parammaps.Get("startTime").ValueStr()
  878. endDate := parammaps.Get("stopTime").ValueStr()
  879. pastureId := parammaps.Get("pastureid").ValueStr()
  880. tx := restful.Engine.NewSession()
  881. defer tx.Close()
  882. var sqlStr = `SELECT
  883. 日期,
  884. SUM(计划取消重量) 计划取消重量,
  885. ROUND((SUM(实际量)-SUM(计划取消重量))/SUM(理论量)*100,2) field3, -- 准确率(去除取消重量)
  886. IFNULL(ROUND(IF(SUM(实际量)>SUM(理论量),SUM(理论量)/SUM(实际量),SUM(实际量)/SUM(理论量))*100,2),0) field1, -- 准确率
  887. SUM(理论量) 理论量,
  888. SUM(实际量) 实际量,
  889. SUM(计划数) 计划数,
  890. SUM(正确数) 正确数,
  891. IFNULL(ROUND(SUM(正确数)/SUM(计划数)*100,2),0) field2
  892. FROM
  893. (
  894. SELECT
  895. IFNULL((SELECT SUM(actualweightminus) FROM downloadplandtl1_exec de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid
  896. AND ((ABS(de1.actualweightminus-de1.lweight)/de1.lweight)>=3 AND (de1.lweight >=30 OR de1.actualweightminus >=30 AND de1.type = 0))
  897. ),0) 计划取消重量,
  898. %s 日期,
  899. d.lweight 理论量,
  900. IFNULL(d.iweight,0) 实际量,
  901. (SELECT COUNT(*) FROM downloadplandtl1 de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid AND de1.type = 0) 计划数, -- 添加饲料次数,
  902. (SELECT SUM(IF(ABS(de1.actualweightminus-de1.lweight)<=de1.feedallowratio AND de1.actualweightminus<>0,1,0))
  903. FROM downloadplandtl1 de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid AND de1.type = 0) 正确数
  904. FROM downloadedplan d
  905. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype in (0,1,2,5)
  906. -- 加上就是不含取消操作
  907. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 ))
  908. ) tem `
  909. var data []map[string]interface{}
  910. var err error
  911. if status == "0" {
  912. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  913. sqlStr += " GROUP BY 日期 "
  914. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  915. if err != nil {
  916. logs.Error(sqlName, " : ", err)
  917. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  918. }
  919. // appG.Response(http.StatusOK, e.SUCCESS, data)
  920. } else if status == "1" {
  921. // sqlStr := chartMap[sqlName]
  922. timeTemplate := "2006-01-02"
  923. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  924. week := 0
  925. if int(start.Weekday()) == 0 {
  926. week = 6
  927. } else {
  928. week = int(start.Weekday()) - 1
  929. }
  930. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  931. sqlStr += " GROUP BY 日期 "
  932. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  933. if err != nil {
  934. logs.Error(sqlName, " : ", err)
  935. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  936. }
  937. // appG.Response(http.StatusOK, e.SUCCESS, data)
  938. } else {
  939. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  940. sqlStr += " GROUP BY 日期 "
  941. // timeTemplate := "2006-01-02"
  942. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  943. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  944. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  945. if err != nil {
  946. logs.Error(sqlName, " : ", err)
  947. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  948. }
  949. // appG.Response(http.StatusOK, e.SUCCESS, data)
  950. }
  951. queryData, err := getAccuracyV2(data)
  952. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  953. }
  954. // getAccuracyAllNQ
  955. func GetAccuracyAllNQ(c *gin.Context) {
  956. appG := app.Gin{C: c}
  957. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  958. fsion := gofasion.NewFasion(string(dataByte))
  959. sqlName := fsion.Get("name").ValueStr()
  960. parammaps := fsion.Get("parammaps")
  961. status := parammaps.Get("status").ValueStr()
  962. startDate := parammaps.Get("startTime").ValueStr()
  963. endDate := parammaps.Get("stopTime").ValueStr()
  964. pastureId := parammaps.Get("pastureid").ValueStr()
  965. tx := restful.Engine.NewSession()
  966. defer tx.Close()
  967. var sqlStr = `SELECT IFNULL((SELECT cowclass FROM barmilk WHERE pastureid = de.pastureid AND barid = de.fbarid AND productdate <= de.date ORDER BY productdate DESC LIMIT 1) ,'无类别')名称 ,
  968. %s 日期,
  969. SUM(de.lweight) 理论量,SUM(de.actualweightminus) 实际量,
  970. ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率
  971. FROM downloadedplan d JOIN downloadplandtl2 de ON d.id=de.pid AND d.pastureid = de.pastureid
  972. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0
  973. -- 加上就是不含取消操作
  974. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 ))
  975. GROUP BY 日期,(SELECT cowclass FROM barmilk WHERE pastureid = de.pastureid AND barid = de.fbarid AND productdate <= de.date ORDER BY productdate DESC LIMIT 1)
  976. ORDER BY 名称 ,d.Mydate `
  977. var data []map[string]interface{}
  978. var err error
  979. if status == "0" {
  980. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  981. // sqlStr += " GROUP BY 日期 "
  982. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  983. if err != nil {
  984. logs.Error(sqlName, " : ", err)
  985. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  986. }
  987. // appG.Response(http.StatusOK, e.SUCCESS, data)
  988. } else if status == "1" {
  989. // sqlStr := chartMap[sqlName]
  990. timeTemplate := "2006-01-02"
  991. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  992. week := 0
  993. if int(start.Weekday()) == 0 {
  994. week = 6
  995. } else {
  996. week = int(start.Weekday()) - 1
  997. }
  998. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  999. // sqlStr += " GROUP BY 日期 "
  1000. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1001. if err != nil {
  1002. logs.Error(sqlName, " : ", err)
  1003. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1004. }
  1005. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1006. } else {
  1007. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  1008. // sqlStr += " GROUP BY 日期 "
  1009. // timeTemplate := "2006-01-02"
  1010. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  1011. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  1012. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1013. if err != nil {
  1014. logs.Error(sqlName, " : ", err)
  1015. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1016. }
  1017. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1018. }
  1019. queryData, err := getAccuracy(data)
  1020. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1021. }
  1022. // getAccuracyAllCC
  1023. func GetAccuracyAllCC(c *gin.Context) {
  1024. appG := app.Gin{C: c}
  1025. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1026. fsion := gofasion.NewFasion(string(dataByte))
  1027. sqlName := fsion.Get("name").ValueStr()
  1028. parammaps := fsion.Get("parammaps")
  1029. status := parammaps.Get("status").ValueStr()
  1030. startDate := parammaps.Get("startTime").ValueStr()
  1031. endDate := parammaps.Get("stopTime").ValueStr()
  1032. pastureId := parammaps.Get("pastureid").ValueStr()
  1033. tx := restful.Engine.NewSession()
  1034. defer tx.Close()
  1035. var sqlStr = `SELECT d.projname 名称,%s 日期,
  1036. SUM(de.lweight) 理论量,SUM(de.actualweightminus) 实际量,
  1037. ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率
  1038. FROM downloadedplan d right JOIN downloadplandtl1_exec de ON d.pastureid = de.pastureid and d.id=de.pid
  1039. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0
  1040. -- 加上就是不含取消操作
  1041. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 ))
  1042. GROUP BY d.projname,日期
  1043. order by d.projname ,d.Mydate`
  1044. var data []map[string]interface{}
  1045. var err error
  1046. if status == "0" {
  1047. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  1048. // sqlStr += " GROUP BY 日期 "
  1049. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1050. if err != nil {
  1051. logs.Error(sqlName, " : ", err)
  1052. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1053. }
  1054. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1055. } else if status == "1" {
  1056. // sqlStr := chartMap[sqlName]
  1057. timeTemplate := "2006-01-02"
  1058. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  1059. week := 0
  1060. if int(start.Weekday()) == 0 {
  1061. week = 6
  1062. } else {
  1063. week = int(start.Weekday()) - 1
  1064. }
  1065. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  1066. // sqlStr += " GROUP BY 日期 "
  1067. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1068. if err != nil {
  1069. logs.Error(sqlName, " : ", err)
  1070. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1071. }
  1072. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1073. } else {
  1074. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  1075. // sqlStr += " GROUP BY 日期 "
  1076. // timeTemplate := "2006-01-02"
  1077. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  1078. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  1079. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1080. if err != nil {
  1081. logs.Error(sqlName, " : ", err)
  1082. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1083. }
  1084. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1085. }
  1086. queryData, err := getAccuracy(data)
  1087. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1088. }
  1089. // getAccuracyAllHL
  1090. func GetAccuracyAllHL(c *gin.Context) {
  1091. appG := app.Gin{C: c}
  1092. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1093. fsion := gofasion.NewFasion(string(dataByte))
  1094. sqlName := fsion.Get("name").ValueStr()
  1095. parammaps := fsion.Get("parammaps")
  1096. status := parammaps.Get("status").ValueStr()
  1097. startDate := parammaps.Get("startTime").ValueStr()
  1098. endDate := parammaps.Get("stopTime").ValueStr()
  1099. pastureId := parammaps.Get("pastureid").ValueStr()
  1100. tx := restful.Engine.NewSession()
  1101. defer tx.Close()
  1102. var sqlStr = `SELECT
  1103. 日期,
  1104. SUM(自动次数) field1, -- 自动次数,
  1105. ifnull( SUM(实际自动),0) 实际自动,
  1106. ifnull(SUM(理论自动),0)理论自动,
  1107. SUM(手动次数) field2, -- 手动次数,
  1108. SUM(实际手动) 实际手动,SUM(理论手动) 理论手动,
  1109. IFNULL(ROUND(IF(SUM(实际自动)>SUM(理论自动),SUM(理论自动)/SUM(实际自动),SUM(实际自动)/SUM(理论自动))*100,2),0) field3, -- 自动准确率,
  1110. IFNULL(ROUND(IF(SUM(实际手动)>SUM(理论手动),SUM(理论手动)/SUM(实际手动),SUM(实际手动)/SUM(理论手动))*100,2),0) field4 -- 手动准确率
  1111. FROM (
  1112. SELECT %s 日期,
  1113. (SELECT SUM(actualweightminus) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype = 1 AND actualweightminus>0 AND de.type = 0) 实际自动,
  1114. (SELECT SUM(lweight) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype = 1 AND de.type = 0) 理论自动,
  1115. (SELECT COUNT(*) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype = 1 AND de.type = 0) 自动次数,
  1116. (SELECT SUM(actualweightminus) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype IN (2,3) AND actualweightminus>0 AND de.type = 0) 实际手动,
  1117. (SELECT SUM(lweight) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype IN (2,3) AND de.type = 0) 理论手动,
  1118. (SELECT COUNT(*) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype IN (2,3) AND de.type = 0) 手动次数
  1119. FROM downloadedplan d
  1120. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5)
  1121. -- 加上就是不含取消操作
  1122. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)>3 and (de.lweight >=30 or de.actualweightminus >=30 ))
  1123. )d
  1124. GROUP BY 日期
  1125. ORDER BY 日期`
  1126. var data []map[string]interface{}
  1127. var err error
  1128. if status == "0" {
  1129. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  1130. // sqlStr += " GROUP BY 日期 "
  1131. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1132. if err != nil {
  1133. logs.Error(sqlName, " : ", err)
  1134. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1135. }
  1136. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1137. } else if status == "1" {
  1138. // sqlStr := chartMap[sqlName]
  1139. timeTemplate := "2006-01-02"
  1140. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  1141. week := 0
  1142. if int(start.Weekday()) == 0 {
  1143. week = 6
  1144. } else {
  1145. week = int(start.Weekday()) - 1
  1146. }
  1147. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  1148. // sqlStr += " GROUP BY 日期 "
  1149. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1150. if err != nil {
  1151. logs.Error(sqlName, " : ", err)
  1152. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1153. }
  1154. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1155. } else {
  1156. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  1157. // sqlStr += " GROUP BY 日期 "
  1158. // timeTemplate := "2006-01-02"
  1159. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  1160. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  1161. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1162. if err != nil {
  1163. logs.Error(sqlName, " : ", err)
  1164. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1165. }
  1166. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1167. }
  1168. queryData, err := getAccuracyV2(data)
  1169. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1170. }
  1171. // getAccuracyAllLS
  1172. func GetAccuracyAllLS(c *gin.Context) {
  1173. appG := app.Gin{C: c}
  1174. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1175. fsion := gofasion.NewFasion(string(dataByte))
  1176. sqlName := fsion.Get("name").ValueStr()
  1177. parammaps := fsion.Get("parammaps")
  1178. status := parammaps.Get("status").ValueStr()
  1179. startDate := parammaps.Get("startTime").ValueStr()
  1180. endDate := parammaps.Get("stopTime").ValueStr()
  1181. pastureId := parammaps.Get("pastureid").ValueStr()
  1182. tx := restful.Engine.NewSession()
  1183. defer tx.Close()
  1184. var sqlStr = `SELECT concat(de.fname,'-',d.times,'班') 名称,de.fname,%s 日期,
  1185. IFNULL(DATE_FORMAT(de.intime,'%%H.%%i'),'') 准确率 -- 撒料时间
  1186. FROM downloadedplan d right JOIN downloadplandtl2 de ON d.pastureid = de.pastureid and d.id=de.pid
  1187. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0
  1188. -- 加上就是不含取消操作
  1189. -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 ))
  1190. GROUP BY d.times,日期,de.fname
  1191. ORDER BY concat(de.fname,d.times) ,日期`
  1192. var data []map[string]interface{}
  1193. var err error
  1194. if status == "0" {
  1195. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  1196. // sqlStr += " GROUP BY 日期 "
  1197. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1198. if err != nil {
  1199. logs.Error(sqlName, " : ", err)
  1200. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1201. }
  1202. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1203. } else if status == "1" {
  1204. // sqlStr := chartMap[sqlName]
  1205. timeTemplate := "2006-01-02"
  1206. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  1207. week := 0
  1208. if int(start.Weekday()) == 0 {
  1209. week = 6
  1210. } else {
  1211. week = int(start.Weekday()) - 1
  1212. }
  1213. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  1214. // sqlStr += " GROUP BY 日期 "
  1215. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1216. if err != nil {
  1217. logs.Error(sqlName, " : ", err)
  1218. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1219. }
  1220. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1221. } else {
  1222. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  1223. // sqlStr += " GROUP BY 日期 "
  1224. // timeTemplate := "2006-01-02"
  1225. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  1226. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  1227. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1228. if err != nil {
  1229. logs.Error(sqlName, " : ", err)
  1230. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1231. }
  1232. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1233. }
  1234. queryData, err := getAccuracy(data)
  1235. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1236. }
  1237. //getAccuracyAllQX
  1238. func GetAccuracyAllQX(c *gin.Context) {
  1239. appG := app.Gin{C: c}
  1240. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1241. fsion := gofasion.NewFasion(string(dataByte))
  1242. sqlName := fsion.Get("name").ValueStr()
  1243. parammaps := fsion.Get("parammaps")
  1244. status := parammaps.Get("status").ValueStr()
  1245. startDate := parammaps.Get("startTime").ValueStr()
  1246. endDate := parammaps.Get("stopTime").ValueStr()
  1247. pastureId := parammaps.Get("pastureid").ValueStr()
  1248. tx := restful.Engine.NewSession()
  1249. defer tx.Close()
  1250. var sqlStr = `SELECT de.fname 名称,%s 日期,
  1251. count(de.id) field1 -- 取消次数
  1252. FROM downloadedplan d LEFT JOIN downloadplandtl1_exec de ON d.id=de.pid AND d.pastureid = de.pastureid
  1253. AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)>3 AND (de.lweight >=30 OR de.actualweightminus >=30 ))
  1254. AND de.type = 0
  1255. WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5)
  1256. -- 加上就是不含取消操作
  1257. GROUP BY 日期`
  1258. var data []map[string]interface{}
  1259. var err error
  1260. if status == "0" {
  1261. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ")
  1262. // sqlStr += " GROUP BY 日期 "
  1263. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1264. if err != nil {
  1265. logs.Error(sqlName, " : ", err)
  1266. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1267. }
  1268. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1269. } else if status == "1" {
  1270. // sqlStr := chartMap[sqlName]
  1271. timeTemplate := "2006-01-02"
  1272. start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local)
  1273. week := 0
  1274. if int(start.Weekday()) == 0 {
  1275. week = 6
  1276. } else {
  1277. week = int(start.Weekday()) - 1
  1278. }
  1279. sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)")
  1280. // sqlStr += " GROUP BY 日期 "
  1281. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1282. if err != nil {
  1283. logs.Error(sqlName, " : ", err)
  1284. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1285. }
  1286. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1287. } else {
  1288. sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ")
  1289. // sqlStr += " GROUP BY 日期 "
  1290. // timeTemplate := "2006-01-02"
  1291. // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local)
  1292. // endDate = end.AddDate(0, 1, -1).Format(timeTemplate)
  1293. data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List()
  1294. if err != nil {
  1295. logs.Error(sqlName, " : ", err)
  1296. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1297. }
  1298. // appG.Response(http.StatusOK, e.SUCCESS, data)
  1299. }
  1300. queryData, err := getAccuracyV2(data)
  1301. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1302. }