chart.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. package api
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "../../pkg/app"
  9. "../../pkg/e"
  10. "../../routers/restful"
  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. }