package api import ( "fmt" "io/ioutil" "net/http" "sort" "strconv" "time" "tmr-watch/http/handle/restful" "tmr-watch/pkg/app" "tmr-watch/pkg/e" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/astaxie/beego/logs" "github.com/gin-gonic/gin" ) func GetFeedEffMR(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() bigcowclass := parammaps.Get("bigcowclass").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT ROUND(IFNULL(SUM((select sum(actualweightminus) from downloadplandtl2 where fbarid = d.barid and date = d.date) * (SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = d.pastureid AND fd.barid = d.barid AND fd.operatetime = (SELECT MAX(operatetime) FROM ftdry fd1 WHERE fd1.pastureid = d.pastureid AND fd1.barid = d.barid AND fd1.operatetime <=d.date )) ),0),2) field1, round( sum(ifnull(bm.product*bm.cowsum,0)),2) field2, -- 产奶量 %s 日期,(select tname from feedtempletdate where date = d.date and id = d.ftid) 名称 FROM feedpdate d join feedtempletdate ftd on d.ftid = ftd.id and ftd.date = d.date LEFT JOIN barmilk bm ON bm.pastureid =d.pastureid AND bm.barid = d.barid AND bm.productdate = d.date join cowclass cc on cc.id = bm.cowclassid WHERE d.date BETWEEN ? AND ? and d.pastureid = ? and (cc.classname = ? or ? = '' ) ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.date, '%Y-%m-%d') ") sqlStr += " GROUP BY d.ftid,DATE_FORMAT(d.date, '%Y-%m-%d') ORDER BY d.date ,d.ftid" data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, bigcowclass, bigcowclass).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( (( DATEDIFF(d.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY d.ftid,日期 ORDER BY d.ftid, d.date " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, bigcowclass, bigcowclass).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.date, '%Y-%m') ") sqlStr += " GROUP BY d.ftid,MONTH(d.date) ORDER BY d.ftid,d.date " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, bigcowclass, bigcowclass).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } queryData := make(map[string]interface{}, 0) // if status == "0" { // dateMap := make(map[string]bool) // ftMap := make(map[string]bool) var ftname []string var dates []string for _, item := range data { // dateMap[item["日期"].(string)] = true // ftMap[item["名称"].(string)] = true if len(ftname) == 0 { ftname = append(ftname, item["名称"].(string)) } if len(dates) == 0 { dates = append(dates, item["日期"].(string)) } dateExist := false ftExist := false for _, date := range dates { if date == item["日期"].(string) { dateExist = true break } } if !dateExist { dates = append(dates, item["日期"].(string)) } for _, tname := range ftname { if tname == item["名称"].(string) { ftExist = true break } } if !ftExist { ftname = append(ftname, item["名称"].(string)) } } var dataArr1 []interface{} var dataArr2 []interface{} sort.Strings(dates) for _, tname := range ftname { var field1 []interface{} var field2 []interface{} for _, date := range dates { exist := false for _, item := range data { if item["日期"].(string) == date && item["名称"].(string) == tname { exist = true field1 = append(field1, item["field1"]) field2 = append(field2, item["field2"]) break } } if !exist { field1 = append(field1, "0") field2 = append(field2, 0) } } dataArr1 = append(dataArr1, field1) dataArr2 = append(dataArr2, field2) } for _, item := range data { if _, ok := item["field1"]; ok { item["采食量"] = item["field1"] } else { item["采食量"] = "0" } if _, ok := item["field2"]; ok { item["产奶量"] = item["field2"] } else { item["产奶量"] = 0 } } queryData["dataArr1"] = dataArr1 queryData["dataArr2"] = dataArr2 queryData["date"] = dates queryData["ftname"] = ftname queryData["data"] = data // } // queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) } func GetFeedEffMRJT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT IFNULL(SUM(d2.actualweightminus),0) 准确率, -- 采食量 (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称, %s 日期 FROM downloadedplan d LEFT JOIN downloadplandtl2 d2 ON d2.pastureid = d.pastureid AND d2.pid = d.id LEFT JOIN barmilk bm ON bm.pastureid =d.pastureid AND bm.barid = d2.fbarid AND bm.productdate = d.mydate JOIN (SELECT pastureid FROM groups_pasture WHERE groupsid = ? UNION SELECT pastureid FROM pasture WHERE groupsid = ? ) tem ON tem.pastureid = d.pastureid WHERE d.mydate BETWEEN ? AND ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 AND IF(bm.idIS NOT NULL,bm.bigcowclass = '泌乳牛',1=1) GROUP BY d.pastureid,日期 ORDER BY 名称, 日期 ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate " data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( (( DATEDIFF(mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 ORDER BY d.mydate " data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ") // sqlStr += " GROUP BY MONTH(d.mydate) ORDER BY d.mydate " data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) } func GetFeedEffSL(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT ft.tname 名称, IFNULL(SUM(d2.actualweightminus),0) 撒料量, IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = ft.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 剩料量, 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)准确率, %s 日期 FROM downloadedplan d JOIN downloadplandtl2 d2 ON d2.pastureid = d.pastureid AND d2.pid = d.id LEFT JOIN feedpdate fp on fp.barid = d2.fbarid and d.pastureid = fp.pastureid AND d.mydate = fp.date join feedtempletdate ft on ft.id = fp.ftid AND d.mydate = ft.date WHERE d.mydate BETWEEN ? AND ? and d.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(ft.date, '%Y-%m-%d') ") sqlStr += " GROUP BY fp.ftid, DATE_FORMAT(ft.date, '%Y-%m-%d') ORDER BY ft.tname ,ft.date " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(ft.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY fp.ftid , 日期 ORDER BY ft.tname ,ft.date " fmt.Println(sqlStr) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(ft.date, '%Y-%m') ") sqlStr += " GROUP BY fp.ftid , DATE_FORMAT(ft.date, '%Y-%m') ORDER BY ft.tname ,ft.date " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } func GetFeedEffSLJT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称, IFNULL(SUM(d.oweight),0) 撒料量, IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 剩料量, 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)准确率, -- 剩料率 %s 日期 FROM feedpdate fp RIGHT JOIN downloadedplan d ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname LEFT JOIN barmilk bm ON bm.pastureid =fp.pastureid AND bm.barid = fp.barid AND bm.productdate = fp.date JOIN (SELECT pastureid FROM groups_pasture WHERE groupsid = ? UNION SELECT pastureid FROM pasture WHERE groupsid = ? ) tem ON tem.pastureid = d.pastureid WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3 GROUP BY d.pastureid ,日期 ORDER BY 名称 ,日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") fmt.Println(sqlStr) data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } func GetFeedEffHL(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT ft.tname 名称, ifnull(ROUND(SUM(UNIX_TIMESTAMP((SELECT MAX(intime) FROM downloadplandtl1_exec d2 WHERE d.mydate=d2.date AND d.pastureid = d2.pastureid AND d2.pid=d.id))-UNIX_TIMESTAMP(d.intime))/60,2),0) 准确率 , %s 日期, ifnull(ROUND(SUM(UNIX_TIMESTAMP((SELECT MAX(intime) FROM downloadplandtl1_exec d2 WHERE d.mydate=d2.date AND d.pastureid = d2.pastureid AND d2.pid=d.id))-UNIX_TIMESTAMP(d.intime))/60,2),0) 混料时间(分钟) FROM downloadedplan d join feedtempletdate ft on ft.id = d.tempid and ft.date = d.mydate WHERE d.mydate BETWEEN ? AND ? AND d.pastureid = ? AND d.lpplantype IN (0,1) ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ") sqlStr += " GROUP BY ft.id,DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY ft.id,d.mydate " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY ft.id,日期 ORDER BY ft.id,d.mydate " fmt.Println(sqlStr) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ") sqlStr += " GROUP BY ft.id,DATE_FORMAT(d.mydate, '%Y-%m') ORDER BY ft.id,d.mydate" // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } func GetFeedEffHLJT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT (SELECT pasture_name FROM pasture WHERE pastureid = d.pastureid) 名称, %s 日期, IFNULL(ROUND(SUM(UNIX_TIMESTAMP((SELECT MAX(intime) FROM downloadplandtl1_exec d2 WHERE d.mydate=d2.date AND d.pastureid = d2.pastureid AND d2.pid=d.id))-UNIX_TIMESTAMP(d.intime))/60,2),0) 准确率-- 混料时间 FROM downloadedplan d JOIN (SELECT pastureid FROM groups_pasture WHERE groupsid = ? UNION SELECT pastureid FROM pasture WHERE groupsid = ? ) tem ON tem.pastureid = d.pastureid WHERE d.mydate BETWEEN ? AND ? AND d.lpplantype<3 GROUP BY d.pastureid,日期 ORDER BY 名称,日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m-%d') ORDER BY d.mydate " data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.mydate, '%Y-%m') ") sqlStr += " GROUP BY DATE_FORMAT(d.mydate, '%Y-%m') ORDER BY d.mydate " data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) } func GetFeedEffZH(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT 日期, 配方模板 名称, IFNULL(ROUND(IFNULL(产奶量/实际牛头数,0)/((撒料量 - 今日剩料量)*TMR干物质/实际牛头数)*100,2),0) 准确率 -- 饲料转化率 FROM ( SELECT %s 日期, d.templetname 配方模板, (SELECT SUM(ccount) FROM feedpdate fpt WHERE fpt.pastureid = d.pastureid AND fpt.date = d.mydate AND fpt.ftid = d.tempid)实际牛头数, @tid := d.tempid tempid, @da := d.mydate, (SELECT SUM(bm.product*bm.cowsum) FROM (SELECT DISTINCT d21.fbarid,d21.date,d21.pastureid,d1.tempid FROM downloadedplan d1 JOIN downloadplandtl2 d21 ON d21.pastureid = d1.pastureid AND d21.pid = d1.id WHERE d1.mydate BETWEEN ? AND ? AND d1.pastureid = ? AND d1.lpplantype IN (0,1,2,5) AND d21.type = 0 GROUP BY d1.tempid,d1.mydate) ww LEFT JOIN barmilk bm ON bm.pastureid =ww.pastureid AND bm.barid = ww.fbarid AND bm.productdate = ( SELECT MAX(productdate) FROM barmilk WHERE barmilk.productdate <= ww.date AND barmilk.pastureid = ww.pastureid AND barmilk.barid = ww.fbarid ) WHERE ww.tempid = @tid AND ww.date = @da ) 产奶量, SUM(d2.actualweightminus) 撒料量, SUM(IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0)) 今日剩料量, 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干物质 FROM downloadedplan d JOIN downloadplandtl2 d2 ON d2.pastureid = d.pastureid AND d2.pid = d.id LEFT JOIN feedpdate fp ON fp.barid = d2.fbarid AND d.pastureid = fp.pastureid AND d.mydate = fp.date WHERE d.mydate BETWEEN ? AND ? AND d.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ") sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 " fmt.Println(sqlStr) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ") sqlStr += " GROUP BY d.tempid,日期 ) tem ORDER BY 名称,日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } func GetFeedEffZHJT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = ` SELECT 日期, (SELECT pasture_name FROM pasture WHERE pastureid = tem.pastureid) 名称, IFNULL(ROUND((产奶量)/((撒料量 - 今日剩料量)*TMR干物质)*100,2),'0.00') 准确率 -- 饲料转化率 FROM ( SELECT d.pastureid, %s 日期, fp.ftname 配方模板,SUM(bm.product)产奶量, SUM(d.oweight) 撒料量, IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 今日剩料量, IFNULL((SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = fp.pastureid AND fd.barid = fp.barid AND fd.operatetime = fp.date),0) TMR干物质 FROM feedpdate fp INNER JOIN downloadedplan d ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname LEFT JOIN barmilk bm 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) JOIN (SELECT pastureid FROM groups_pasture WHERE groupsid = ? UNION SELECT pastureid FROM pasture WHERE groupsid = ? ) tem ON tem.pastureid = d.pastureid WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3 GROUP BY d.pastureid,日期 ) tem ORDER BY 名称,日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } func GetFeedEffCBFT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT 日期, 配方 名称, IFNULL(ROUND(实际混料量*配方单价/产奶量,2),0) 准确率 -- 公斤奶饲料成本 FROM ( SELECT %s 日期, fp.ftname 配方, (SELECT SUM(d1e.actualweightminus) FROM downloadplandtl1_exec d1e JOIN downloadedplan dv ON dv.pastureid= d1e.pastureid AND dv.id = d1e.pid WHERE d1e.pastureid = d.pastureid AND dv.mydate = d.mydate AND d1e.type = 0 AND dv.tempid= d.tempid)实际混料量, @tid := d.tempid tempid, @da := d.mydate, (SELECT SUM(bm.product*bm.cowsum) FROM (SELECT DISTINCT d21.fbarid,d21.date,d21.pastureid,d1.tempid FROM downloadedplan d1 JOIN downloadplandtl2 d21 ON d21.pastureid = d1.pastureid AND d21.pid = d1.id WHERE d1.mydate BETWEEN ? AND ? AND d1.pastureid = ? AND d1.lpplantype IN (0,1,2,5) AND d21.type = 0 GROUP BY d1.tempid,d1.mydate) ww LEFT JOIN barmilk bm ON bm.pastureid =ww.pastureid AND bm.barid = ww.fbarid AND bm.productdate = ( SELECT MAX(productdate) FROM barmilk WHERE barmilk.productdate <= ww.date AND barmilk.pastureid = ww.pastureid AND barmilk.barid = ww.fbarid ) WHERE ww.tempid = @tid AND ww.date = @da ) 产奶量, (SELECT SUM( IF(ftd.preftid<>0, (SELECT SUM(f1.uprice* ftd1.fweight)/SUM(ftd1.fweight) FROM ftdetaildate ftd1 LEFT JOIN feed f1 ON f1.pastureid = ftd1.pastureid AND f1.id = ftd1.fid WHERE ftd1.pastureid = ftd.pastureid AND ftd1.date = ftd.date AND ftd1.ftid = ftd.preftid AND ftd1.version = (SELECT MAX(VERSION) FROM ftdetaildate ftd2 WHERE ftd2.pastureid = ftd.pastureid AND ftd2.date = ftd.date AND ftd2.ftid = ftd.preftid)), f.uprice)*ftd.fweight)/SUM(ftd.fweight) FROM ftdetaildate ftd LEFT JOIN feed f ON f.pastureid = ftd.pastureid AND f.id = ftd.fid WHERE ftd.pastureid = fp.pastureid AND ftd.date = fp.date AND ftd.ftid = fp.ftid AND ftd.version = d.tversion ) 配方单价 FROM downloadedplan d JOIN downloadplandtl2 d2 ON d2.pastureid = d.pastureid AND d2.pid = d.id LEFT JOIN feedpdate fp ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND fp.barid = d2.fbarid WHERE fp.date BETWEEN ? AND ? AND fp.pastureid = ? AND d.lpplantype IN (0,1,2,5) AND d2.type = 0 ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ") sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 " fmt.Println(sqlStr) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ") sqlStr += " GROUP BY d.tempid,日期) tem ORDER BY 名称,日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } //成本分析-集团 func GetFeedEffCBAllJT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = ` SELECT 日期, (SELECT pasture_name FROM pasture WHERE pastureid = tem.pastureid) 名称, IFNULL(ROUND((产奶量)/((撒料量 - 今日剩料量)*TMR干物质)*100,2),'0.00') 准确率 -- 饲料转化率 FROM ( SELECT d.pastureid, %s 日期, fp.ftname 配方模板,SUM(bm.product)产奶量, SUM(d.oweight) 撒料量, IFNULL((SELECT SUM(remain) FROM barfeedremain bf WHERE bf.pastureid = fp.pastureid AND bf.barid = fp.barid AND bf.remaindate = fp.date),0) 今日剩料量, IFNULL((SELECT AVG(actweight) FROM ftdry fd WHERE fd.pastureid = fp.pastureid AND fd.barid = fp.barid AND fd.operatetime = fp.date),0) TMR干物质 FROM feedpdate fp INNER JOIN downloadedplan d ON d.pastureid = fp.pastureid AND d.mydate = fp.date AND d.templetname = fp.ftname LEFT JOIN barmilk bm 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) JOIN (SELECT pastureid FROM groups_pasture WHERE groupsid = ? UNION SELECT pastureid FROM pasture WHERE groupsid = ? ) tem ON tem.pastureid = d.pastureid WHERE fp.date BETWEEN ? AND ? AND d.lpplantype<3 GROUP BY d.pastureid,日期 ) tem ORDER BY 名称,日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m-%d') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else if status == "1" { timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(fp.date,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(fp.date, '%Y-%m') ") data, err = tx.SQL(sqlStr, pastureId, pastureId, startDate, endDate).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } // getAccuracyAllFT func GetAccuracyAllFT(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT %s 日期, d.templetname 名称, SUM(de.lweight) 理论量, IFNULL(SUM(de.actualweightminus),0) 实际量, ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率, COUNT(de.id) 添加饲料次数, SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.feedallowratio,1,0)) 添加正确数, IF(COUNT(de.id)>0,SUM(IF(ABS(de.actualweightminus-de.lweight)<=de.feedallowratio,1,0))/COUNT(de.id),0) 正确率 FROM downloadedplan d JOIN downloadplandtl1_exec de ON d.pastureid = de.pastureid and d.id=de.pid WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0 -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 )) ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") sqlStr += " GROUP BY 日期,d.templetname ORDER BY d.templetname ,d.Mydate " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } // getAccuracyAllJH func GetAccuracyAllJH(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT 日期, SUM(计划取消重量) 计划取消重量, ROUND((SUM(实际量)-SUM(计划取消重量))/SUM(理论量)*100,2) field3, -- 准确率(去除取消重量) IFNULL(ROUND(IF(SUM(实际量)>SUM(理论量),SUM(理论量)/SUM(实际量),SUM(实际量)/SUM(理论量))*100,2),0) field1, -- 准确率 SUM(理论量) 理论量, SUM(实际量) 实际量, SUM(计划数) 计划数, SUM(正确数) 正确数, IFNULL(ROUND(SUM(正确数)/SUM(计划数)*100,2),0) field2 FROM ( SELECT IFNULL((SELECT SUM(actualweightminus) FROM downloadplandtl1_exec de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid AND ((ABS(de1.actualweightminus-de1.lweight)/de1.lweight)>=3 AND (de1.lweight >=30 OR de1.actualweightminus >=30 AND de1.type = 0)) ),0) 计划取消重量, %s 日期, d.lweight 理论量, IFNULL(d.iweight,0) 实际量, (SELECT COUNT(*) FROM downloadplandtl1 de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid AND de1.type = 0) 计划数, -- 添加饲料次数, (SELECT SUM(IF(ABS(de1.actualweightminus-de1.lweight)<=de1.feedallowratio AND de1.actualweightminus<>0,1,0)) FROM downloadplandtl1 de1 WHERE de1.pid = d.id AND d.pastureid = de1.pastureid AND de1.type = 0) 正确数 FROM downloadedplan d WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype in (0,1,2,5) -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 )) ) tem ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) } // getAccuracyAllNQ func GetAccuracyAllNQ(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() 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) ,'无类别')名称 , %s 日期, SUM(de.lweight) 理论量,SUM(de.actualweightminus) 实际量, ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率 FROM downloadedplan d JOIN downloadplandtl2 de ON d.id=de.pid AND d.pastureid = de.pastureid WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0 -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 )) GROUP BY 日期,(SELECT cowclass FROM barmilk WHERE pastureid = de.pastureid AND barid = de.fbarid AND productdate <= de.date ORDER BY productdate DESC LIMIT 1) ORDER BY 名称 ,d.Mydate ` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") // sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } // getAccuracyAllCC func GetAccuracyAllCC(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT d.projname 名称,%s 日期, SUM(de.lweight) 理论量,SUM(de.actualweightminus) 实际量, ROUND(IF(SUM(de.lweight)>SUM(de.actualweightminus),SUM(de.actualweightminus)/SUM(de.lweight),SUM(de.lweight)/SUM(de.actualweightminus))*100,2) 准确率 FROM downloadedplan d right JOIN downloadplandtl1_exec de ON d.pastureid = de.pastureid and d.id=de.pid WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) AND de.type = 0 -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 )) GROUP BY d.projname,日期 order by d.projname ,d.Mydate` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") // sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "1" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } // getAccuracyAllHL func GetAccuracyAllHL(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT 日期, SUM(自动次数) field1, -- 自动次数, ifnull( SUM(实际自动),0) 实际自动, ifnull(SUM(理论自动),0)理论自动, SUM(手动次数) field2, -- 手动次数, SUM(实际手动) 实际手动,SUM(理论手动) 理论手动, IFNULL(ROUND(IF(SUM(实际自动)>SUM(理论自动),SUM(理论自动)/SUM(实际自动),SUM(实际自动)/SUM(理论自动))*100,2),0) field3, -- 自动准确率, IFNULL(ROUND(IF(SUM(实际手动)>SUM(理论手动),SUM(理论手动)/SUM(实际手动),SUM(实际手动)/SUM(理论手动))*100,2),0) field4 -- 手动准确率 FROM ( SELECT %s 日期, (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) 实际自动, (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) 理论自动, (SELECT COUNT(*) FROM downloadplandtl1_exec de WHERE d.id=de.pid AND d.pastureid = de.pastureid AND de.buttontype = 1 AND de.type = 0) 自动次数, (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) 实际手动, (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) 理论手动, (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) 手动次数 FROM downloadedplan d WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)>3 and (de.lweight >=30 or de.actualweightminus >=30 )) )d GROUP BY 日期 ORDER BY 日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") // sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) } // getAccuracyAllLS func GetAccuracyAllLS(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() ftId := parammaps.Get("ftId").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT concat(de.fname,'-',d.times,'班') 名称,de.fname,%s 日期, IFNULL(DATE_FORMAT(de.intime,'%%H.%%i'),'') 准确率 -- 撒料时间 FROM downloadedplan d right JOIN downloadplandtl2 de ON d.pastureid = de.pastureid and d.id=de.pid 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 ,?)) -- 加上就是不含取消操作 -- AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)<=3 OR (de.lweight <30 AND de.actualweightminus <30 )) GROUP BY d.times,日期,de.fname ORDER BY concat(de.fname,d.times) ,日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, ftId, ftId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, ftId, ftId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") // sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId, ftId, ftId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } if status == "0" { queryData, _ := getAccuracy(data, "2006-01-02") appG.Response(http.StatusOK, e.SUCCESS, queryData) } else { queryData, _ := getAccuracy(data, "2006-01") appG.Response(http.StatusOK, e.SUCCESS, queryData) } } //getAccuracyAllQX func GetAccuracyAllQX(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) sqlName := fsion.Get("name").ValueStr() parammaps := fsion.Get("parammaps") status := parammaps.Get("status").ValueStr() startDate := parammaps.Get("startTime").ValueStr() endDate := parammaps.Get("stopTime").ValueStr() pastureId := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() var sqlStr = `SELECT de.fname 名称,%s 日期, count(de.id) field1 -- 取消次数 FROM downloadedplan d LEFT JOIN downloadplandtl1_exec de ON d.id=de.pid AND d.pastureid = de.pastureid AND ((ABS(de.actualweightminus-de.lweight)/de.lweight)>3 AND (de.lweight >=30 OR de.actualweightminus >=30 )) AND de.type = 0 WHERE d.mydate BETWEEN ? AND ? AND d.pastureid=? AND d.lpplantype IN (0,1,2,5) -- 加上就是不含取消操作 GROUP BY 日期` var data []map[string]interface{} var err error if status == "0" { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m-%d') ") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else if status == "1" { // sqlStr := chartMap[sqlName] timeTemplate := "2006-01-02" start, _ := time.ParseInLocation(timeTemplate, startDate, time.Local) week := 0 if int(start.Weekday()) == 0 { week = 6 } else { week = int(start.Weekday()) - 1 } sqlStr = fmt.Sprintf(sqlStr, "FLOOR( ((DATEDIFF(d.Mydate,'"+startDate+"') +"+strconv.Itoa(week)+")/7)+1)") // sqlStr += " GROUP BY 日期 " data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } else { sqlStr = fmt.Sprintf(sqlStr, " DATE_FORMAT(d.Mydate, '%Y-%m') ") // sqlStr += " GROUP BY 日期 " // timeTemplate := "2006-01-02" // end, _ := time.ParseInLocation(timeTemplate, endDate, time.Local) // endDate = end.AddDate(0, 1, -1).Format(timeTemplate) data, err = tx.SQL(sqlStr, startDate, endDate, pastureId).Query().List() if err != nil { logs.Error(sqlName, " : ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) } // appG.Response(http.StatusOK, e.SUCCESS, data) } queryData, err := getAccuracyV2(data) appG.Response(http.StatusOK, e.SUCCESS, queryData) }