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