|
@@ -0,0 +1,203 @@
|
|
|
|
+package group
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "sort"
|
|
|
|
+ "strings"
|
|
|
|
+ "time"
|
|
|
|
+ "tmr-watch/http/handle/restful"
|
|
|
|
+ "tmr-watch/models"
|
|
|
|
+ "tmr-watch/module"
|
|
|
|
+ "tmr-watch/pkg/util"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+// DistributeFeedFormulaService 饲料配方下发牧场端
|
|
|
|
+func DistributeFeedFormulaService(req *models.PastureBodyRequest) error {
|
|
|
|
+ feedTemplateList := make([]*models.FeedTemplate, len(req.Body))
|
|
|
|
+ for i, b := range req.Body {
|
|
|
|
+ feedTemplateList[i] = &models.FeedTemplate{
|
|
|
|
+ PastureId: req.PastureId,
|
|
|
|
+ TCode: b.EncodeNumber,
|
|
|
|
+ TName: b.Name,
|
|
|
|
+ TColor: b.Colour,
|
|
|
|
+ CCid: int64(b.CattleCategoryId),
|
|
|
|
+ CCName: b.CattleCategoryName,
|
|
|
|
+ FTType: "",
|
|
|
|
+ Source: "",
|
|
|
|
+ Remark: b.Remarks,
|
|
|
|
+ Enable: 1,
|
|
|
|
+ Weight: 0,
|
|
|
|
+ DryWeight: 0,
|
|
|
|
+ Version: b.Version,
|
|
|
|
+ SaveTime: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
|
+ IsIssue: 0,
|
|
|
|
+ IssueVersion: 0,
|
|
|
|
+ IssueId: 0,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if _, err := restful.Engine.Insert(feedTemplateList); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// AnalysisAccuracyService 首页准确率分析
|
|
|
|
+func AnalysisAccuracyService(req *models.AnalysisAccuracyRequest) (*models.AnalysisAccuracyResponse, error) {
|
|
|
|
+ response := &models.AnalysisAccuracyResponse{
|
|
|
|
+ MixedFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
|
|
|
|
+ MixedFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
|
|
|
|
+ SprinkleFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
|
|
|
|
+ SprinkleFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0),
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 混料和撒料准确率
|
|
|
|
+ mixedFodderDataList, err := module.MixedFodderData(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 混料正确率
|
|
|
|
+ mixedFodderCorrectDataList, err := module.MixedFodderCorrectData(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 撒料正确率
|
|
|
|
+ sprinkleFodderCorrectDataList, err := module.SprinkleFodderCorrectData(req)
|
|
|
|
+
|
|
|
|
+ timeList := util.TimeBetween(req.StartDate, req.EndDate)
|
|
|
|
+ for _, dayTime := range timeList {
|
|
|
|
+ var mixedInfo, mixedCorrectInfo, sprinkleInfo bool
|
|
|
|
+ for _, mixed := range mixedFodderDataList {
|
|
|
|
+ myDate := mixed.MyDate.Format(util.LayoutDateFormat)
|
|
|
|
+ if myDate != dayTime {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ // 混料准确率
|
|
|
|
+ var mixedFodderAccurateRatio float64 = 0
|
|
|
|
+ if mixed.Lweight > 0 && mixed.Iweight > 0 {
|
|
|
|
+ mixedFodderAccurateRatio = float64(mixed.Iweight) / mixed.Lweight * 100
|
|
|
|
+ }
|
|
|
|
+ mixedValue := &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: mixedFodderAccurateRatio,
|
|
|
|
+ }
|
|
|
|
+ response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, mixedValue)
|
|
|
|
+
|
|
|
|
+ // 撒料准确率
|
|
|
|
+ var sprinkleFodderAccurateRatio float64 = 0
|
|
|
|
+ if mixed.Lweight > 0 && mixed.Iweight > 0 {
|
|
|
|
+ sprinkleFodderAccurateRatio = float64(mixed.Oweight) / mixed.Lweight * 100
|
|
|
|
+ }
|
|
|
|
+ sprinkleValue := &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: sprinkleFodderAccurateRatio,
|
|
|
|
+ }
|
|
|
|
+ response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, sprinkleValue)
|
|
|
|
+ mixedInfo = true
|
|
|
|
+ }
|
|
|
|
+ if !mixedInfo {
|
|
|
|
+ noInfo := &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: 0,
|
|
|
|
+ }
|
|
|
|
+ response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, noInfo)
|
|
|
|
+ response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, noInfo)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, mixedFodder := range mixedFodderCorrectDataList {
|
|
|
|
+ myDate := strings.ReplaceAll(mixedFodder.Date, "T00:00:00+08:00", "")
|
|
|
|
+ if myDate != dayTime {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if mixedFodder.MixedCorrectNumber == 0 || mixedFodder.UseMixedFodderOptionNumber == 0 {
|
|
|
|
+ response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: 0,
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: float64(mixedFodder.MixedCorrectNumber) / float64(mixedFodder.UseMixedFodderOptionNumber) * 100,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ mixedCorrectInfo = true
|
|
|
|
+ }
|
|
|
|
+ if !mixedCorrectInfo {
|
|
|
|
+ response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: 0,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, sprinkle := range sprinkleFodderCorrectDataList {
|
|
|
|
+ myDate := strings.ReplaceAll(sprinkle.Date, "T00:00:00+08:00", "")
|
|
|
|
+ if myDate != dayTime {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if sprinkle.SprinkleCorrectNumber == 0 || sprinkle.UseSprinkleOptionNumber == 0 {
|
|
|
|
+ response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: 0,
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: float64(sprinkle.SprinkleCorrectNumber) / float64(sprinkle.UseSprinkleOptionNumber) * 100,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ sprinkleInfo = true
|
|
|
|
+ }
|
|
|
|
+ if !sprinkleInfo {
|
|
|
|
+ response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, &models.PastureAnalysisAccuracyDataValue{
|
|
|
|
+ DayTime: dayTime,
|
|
|
|
+ Ratio: 0,
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return response, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ProcessAnalysisService 过程分析
|
|
|
|
+func ProcessAnalysisService(req *models.AnalysisAccuracyRequest) (*models.ProcessAnalysisResponse, error) {
|
|
|
|
+ response := &models.ProcessAnalysisResponse{
|
|
|
|
+ AddFeedTime: &models.ProcessAnalysisDataValue{},
|
|
|
|
+ SprinkleTime: &models.ProcessAnalysisDataValue{},
|
|
|
|
+ StirTime: &models.ProcessAnalysisDataValue{},
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ processDataList, err := module.ProcessAnalysisData(req)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ addFeedList := make([]int, 0)
|
|
|
|
+ sprinkleFeedList := make([]int, 0)
|
|
|
|
+ for _, v := range processDataList {
|
|
|
|
+ addFeedList = append(addFeedList, util.TimeParseToMinutes(v.ExecProcessTime))
|
|
|
|
+ sprinkleFeedList = append(sprinkleFeedList, util.TimeParseToMinutes(v.L2ProcessTime))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(addFeedList) > 0 {
|
|
|
|
+ sort.Ints(addFeedList)
|
|
|
|
+ response.AddFeedTime.MaxValue = fmt.Sprintf("%.2f", float64(addFeedList[len(addFeedList)-1])/60)
|
|
|
|
+ response.AddFeedTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[len(addFeedList)/2+1:])/60)
|
|
|
|
+ response.AddFeedTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList)/60)
|
|
|
|
+ response.AddFeedTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[0:len(addFeedList)/2])/60)
|
|
|
|
+ response.AddFeedTime.MinValue = fmt.Sprintf("%.2f", float64(addFeedList[0])/60)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(sprinkleFeedList) > 0 {
|
|
|
|
+ sort.Ints(sprinkleFeedList)
|
|
|
|
+ response.SprinkleTime.MaxValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[len(sprinkleFeedList)-1])/60)
|
|
|
|
+ response.SprinkleTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[len(sprinkleFeedList)/2+1:])/60)
|
|
|
|
+ response.SprinkleTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList)/60)
|
|
|
|
+ response.SprinkleTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[0:len(sprinkleFeedList)/2])/60)
|
|
|
|
+ response.SprinkleTime.MinValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[0])/60)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return response, nil
|
|
|
|
+}
|