chart.go 58 KB

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