|
@@ -2,8 +2,10 @@ package module
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"tmr-watch/http/handle/restful"
|
|
|
"tmr-watch/models"
|
|
|
+ "tmr-watch/pkg/logging"
|
|
|
)
|
|
|
|
|
|
const (
|
|
@@ -350,3 +352,90 @@ func FeedTemplateList(req *models.FeedFormulaListRequest) ([]*models.FeedTemplat
|
|
|
}
|
|
|
return res, total, nil
|
|
|
}
|
|
|
+
|
|
|
+func FeedTemplateUsageDetail(req *models.FeedFormulaUsageRequest) (*models.FeedFormulaUsageResponse, error) {
|
|
|
+ mixedDetail, err := getMixedDetail(req.PastureId, req.FeedFormulaId, req.StartTime, req.EndTime)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ sprinkleDetail, err := getSprinkleDetail(req.PastureId, req.FeedFormulaId, req.StartTime, req.EndTime)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ response := &models.FeedFormulaUsageResponse{
|
|
|
+ MixedFodderAccurateRatio: mixedDetail.MixedFodderAccurateRatio,
|
|
|
+ MixedFodderCorrectRatio: mixedDetail.MixedFodderCorrectNumber,
|
|
|
+ SprinkleFodderAccurateRatio: sprinkleDetail.SprinkleFodderAccurateRatio,
|
|
|
+ SprinkleFodderCorrectRatio: sprinkleDetail.SprinkleFodderCorrectRatio,
|
|
|
+ AddFeedTime: "",
|
|
|
+ SprinkleTime: "",
|
|
|
+ StirTime: "",
|
|
|
+ LastEditTime: "",
|
|
|
+ }
|
|
|
+ return response, nil
|
|
|
+}
|
|
|
+
|
|
|
+func getMixedDetail(pastureId, feedFormulaId int32, startTime, endTime string) (*models.MixedDetail, error) {
|
|
|
+ sql := fmt.Sprintf("SELECT * FROM ( SELECT DATE(de.`date`) as plan_time,IFNULL((SELECT d.`templetname` "+
|
|
|
+ "FROM `downloadedplan` d WHERE d.pastureid = de.pastureid AND d.id = de.pid),de.`fname`) as feed_formula_name,"+
|
|
|
+ "ROUND(SUM(de.`lweight`),2) as l_weight,ROUND(SUM(de.`actualweightminus`),2) as reality_weight,"+
|
|
|
+ "SUM(1) as plan_mixed_opts,SUM(de.`havebuttom`) as reality_mixed_opts, CONCAT(ROUND(SUM(de.`havebuttom`)/SUM(1)*100,2),'%') as mixed_ops_ratio,"+
|
|
|
+ "SUM(IF(de.`buttontype` =1,1,0)) as mixed_auto_jump_number , SUM(IF(de.`buttontype`>1,1,0)) as mixed_manual_jump_number , "+
|
|
|
+ "ROUND(ABS(SUM(de.`lweight`)-SUM(de.`actualweightminus`)),2) as mix_error_number,"+
|
|
|
+ " CONCAT( IF (SUM(de.`actualweightminus`)>SUM(de.`lweight`),ROUND((SUM(de.`lweight`)/SUM(de.`actualweightminus`)*100),2) ,"+
|
|
|
+ "ROUND((SUM(de.`actualweightminus`)/SUM(de.`lweight`)*100) ,2)),'%') as mixed_fodder_accurate_ratio,"+
|
|
|
+ "IF (SUM(de.`actualweightminus`)>SUM(de.`lweight`),ROUND((SUM(de.`lweight`)/SUM(de.`actualweightminus`)*100),2) ,"+
|
|
|
+ "ROUND((SUM(de.`actualweightminus`)/SUM(de.`lweight`)*100) ,2)) hlzq,"+
|
|
|
+ "SUM(CASE WHEN (ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)>3 AND de.`lweight` >30 AND de.`actualweightminus` >30 THEN 1 ELSE 0 END) as cancel_number,"+
|
|
|
+ "ROUND(STD(ABS(de.`actualweightminus`-de.`lweight`)) ,2) as variance_ratio,"+
|
|
|
+ "IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0)),0) as mixed_fodder_correct_number,"+
|
|
|
+ "CONCAT(ROUND(IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0))/SUM(1),0)*100,2),'%') as mixed_fodder_correct_ratio,"+
|
|
|
+ "ROUND(IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0))/SUM(1),0)*100,2) hlzql,"+
|
|
|
+ "TRIM(de.pid) pid,"+
|
|
|
+ "CONCAT(ROUND(IFNULL(SUM(IF(((ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)<=3 OR (de.`lweight` <30 AND de.`actualweightminus` <30 )),"+
|
|
|
+ "IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`feedallowratio` AND de.`actualweightminus`<>0,1,0),0))/SUM(IF(((ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)<=3 OR (de.`lweight` <30 AND de.`actualweightminus` <30 )),1,0 )),0)*100,2),'%') as remove_cancel_correct_ratio "+
|
|
|
+ "FROM `downloadplandtl1` de WHERE de.pastureid= ? and de.intime is not null AND (SELECT d.`lpplantype` FROM `downloadedplan` d WHERE d.pastureid = de.pastureid AND d.id = de.pid) IN (0,1,4) and de.`date` >= ? AND de.`date` <= ? "+
|
|
|
+ "GROUP BY de.dateORDER BY mixed_fodder_accurate_ratio DESC,feed_formula_name) tmr HAVING feed_formula_id = ?", pastureId, startTime, endTime, feedFormulaId)
|
|
|
+ logging.Debug("getMixedDetail-sql", sql)
|
|
|
+ dataList := &models.MixedDetail{}
|
|
|
+ if err := restful.Engine.NewSession().SQL(sql).Find(dataList); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return dataList, nil
|
|
|
+}
|
|
|
+
|
|
|
+func getSprinkleDetail(pastureId, feedFormulaId int32, startTime, endTime string) (*models.SprinkleDetail, error) {
|
|
|
+ sql := fmt.Sprintf("SELECT * FROM (SELECT DATE(d.`mydate`) AS plan_time,IFNULL((SELECT d.`templetname` FROM `downloadedplan` d "+
|
|
|
+ "WHERE d.pastureid = de.pastureid AND d.id = de.pid),de.`fname`) AS feed_formula_name,"+
|
|
|
+ "IFNULL((SELECT d.`tempid` FROM `downloadedplan` d WHERE d.pastureid = de.pastureid AND d.id = de.pid),0) AS feed_formula_id,"+
|
|
|
+ "d.tmrtname AS tmr_name,"+
|
|
|
+ "IFNULL(TIMEDIFF ((SELECT MAX(intime) FROM downloadplandtl2 d2 WHERE d2.`pid`=de.`pid` AND d2.pastureid = de.pastureid ),(SELECT MAX(intime) FROM downloadplandtl1_exec d2 WHERE d2.`pid`=de.`pid` AND d2.pastureid = de.pastureid )),'00:00:00') AS mixed_time,"+
|
|
|
+ "IFNULL(TIMEDIFF ((SELECT MIN(intime) FROM downloadplandtl2 d2 WHERE d2.`pid`=de.`pid` AND d2.pastureid = de.pastureid ),(SELECT MAX(intime) FROM downloadplandtl1_exec d2 WHERE d2.`pid`=de.`pid` AND d2.pastureid = de.pastureid )),'00:00:00') AS wait_time,"+
|
|
|
+ "SUM(de.`lweight`) AS l_weight,ROUND(SUM(de.`actualweightminus`)) AS reality_weight,"+
|
|
|
+ "SUM(1) AS plan_sprinkle_opts,"+
|
|
|
+ "SUM(de.`havebuttom`) AS reality_sprinkle_opts, "+
|
|
|
+ "CONCAT(ROUND(SUM(de.`havebuttom`)/SUM(1)*100,2),'%') AS sprinkle_ops_ratio, "+
|
|
|
+ "SUM(IF(de.`buttontype` =1,1,0)) AS sprinkle_auto_jump_number , "+
|
|
|
+ "SUM(IF(de.`buttontype`>1,1,0)) AS sprinkle_manual_jump_number ,"+
|
|
|
+ "ABS(SUM(de.`actualweightminus`)-SUM(de.`lweight`)) AS sprinkle_error_number,"+
|
|
|
+ " CONCAT( IF (SUM(de.`actualweightminus`)>SUM(de.`lweight`),ROUND((SUM(de.`lweight`)/SUM(de.`actualweightminus`)*100),2) ,"+
|
|
|
+ "ROUND((SUM(de.`actualweightminus`)/SUM(de.`lweight`)*100) ,2)),'%') AS sprinkle_fodder_accurate_ratio,"+
|
|
|
+ "IF (SUM(de.`actualweightminus`)>SUM(de.`lweight`),ROUND((SUM(de.`lweight`)/SUM(de.`actualweightminus`)*100),2) ,"+
|
|
|
+ "ROUND((SUM(de.`actualweightminus`)/SUM(de.`lweight`)*100) ,2)) slzq,"+
|
|
|
+ "SUM(CASE WHEN (ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)>3 AND de.`lweight` >30 AND de.`actualweightminus` >30 THEN 1 ELSE 0 END) AS cancel_number,"+
|
|
|
+ "IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`allowratio` AND de.`actualweightminus`<>0,1,0)),0) AS sprinkle_fodder_correct_number,"+
|
|
|
+ "CONCAT(ROUND(IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`allowratio` AND de.`actualweightminus`<>0,1,0))/SUM(1),0)*100,2),'%') AS sprinkle_fodder_correct_ratio,"+
|
|
|
+ "ROUND(IFNULL(SUM(IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`allowratio` AND de.`actualweightminus`<>0,1,0))/SUM(1),0)*100,2) as slzql,"+
|
|
|
+ "TRIM(de.pid) pid,"+
|
|
|
+ "CONCAT(ROUND(IFNULL(SUM(IF(((ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)<=3 OR (de.`lweight` <30 AND de.`actualweightminus` <30 )),"+
|
|
|
+ "IF(ABS(de.`actualweightminus`-de.`lweight`)<=de.`allowratio` AND de.`actualweightminus`<>0,1,0),0))/SUM(IF(((ABS(de.`actualweightminus`-de.`lweight`)/de.`lweight`)<=3 OR (de.`lweight` <30 AND de.`actualweightminus` <30 )),1,0 )),0)*100,2),'%') AS remove_cancel_correct_ratio "+
|
|
|
+ "FROM `downloadplandtl2` de JOIN `downloadedplan` d ON d.`id`=de.`pid` AND d.pastureid = de.pastureid WHERE d.pastureid = ? AND DATE(d.`mydate`) BETWEEN ? AND '? AND d.lpplantype IN (0,2)) tmr HAVING feed_formula_id = ? ",
|
|
|
+ pastureId, startTime, endTime, feedFormulaId)
|
|
|
+ logging.Debug("getSprinkleDetail-sql", sql)
|
|
|
+ dataList := &models.SprinkleDetail{}
|
|
|
+ if err := restful.Engine.NewSession().SQL(sql).Find(dataList); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return dataList, nil
|
|
|
+}
|