Bladeren bron

crontab: milkDaily

Yi 1 dag geleden
bovenliggende
commit
e38584b38d
4 gewijzigde bestanden met toevoegingen van 95 en 18 verwijderingen
  1. 16 13
      module/crontab/cow_cron.go
  2. 4 3
      module/crontab/neck_ring_calculate.go
  3. 3 2
      module/crontab/sql.go
  4. 72 0
      util/util_test.go

+ 16 - 13
module/crontab/cow_cron.go

@@ -6,6 +6,7 @@ import (
 	"kpt-pasture/model"
 	"kpt-pasture/module/backend"
 	"kpt-pasture/util"
+	"sort"
 	"strings"
 	"time"
 
@@ -538,8 +539,8 @@ func (e *Entry) UpdateCowWeeklyHigh(cow *model.Cow) int32 {
 	}
 
 	weeklyActiveModelList := make([]*model.WeeklyActiveModel, 0)
-	startTime := time.Now().Local().AddDate(0, 0, -8).Format(model.LayoutDate2)
-	endTime := time.Now().Local().AddDate(0, 0, -2).Format(model.LayoutDate2)
+	startTime := time.Now().Local().AddDate(0, 0, -7).Format(model.LayoutDate2)
+	endTime := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
 	selectStr := fmt.Sprintf(`cow_id,heat_date,count(1) AS nb,IF(ROUND(AVG(high))>%d, ROUND(AVG(high)), %d) AS high`, minWeeklyActive.Value, minWeeklyActive.Value)
 	if err = e.DB.Model(new(model.NeckActiveHabit)).
 		Select(selectStr).
@@ -558,25 +559,27 @@ func (e *Entry) UpdateCowWeeklyHigh(cow *model.Cow) int32 {
 		return 0
 	}
 
+	// 1. 先按High值排序
+	sort.Slice(weeklyActiveModelList, func(i, j int) bool {
+		return weeklyActiveModelList[i].High < weeklyActiveModelList[j].High
+	})
+
 	countHigh := len(weeklyActiveModelList)
 	sumHigh := int32(0)
 	minHigh := weeklyActiveModelList[0].High
-	maxHigh := int32(0)
+	maxHigh := weeklyActiveModelList[len(weeklyActiveModelList)-1].High
 	for _, v := range weeklyActiveModelList {
 		sumHigh += v.High
-		if v.High > maxHigh {
-			maxHigh = v.High
-		}
-
-		if v.High < minHigh {
-			minHigh = v.High
-		}
 	}
-	diff := 3
+	diff := 0
 	x := int32(0)
-	if countHigh == 3 {
-		diff = 2
+	if countHigh > 3 {
+		diff = 3
 		x = weeklyActiveModelList[1].High
+	} else if countHigh == 3 {
+		diff = 2
+	} else {
+		return 0
 	}
 	avgHigh := float64(sumHigh-minHigh-maxHigh-x) / float64(countHigh-diff)
 	return int32(avgHigh)

+ 4 - 3
module/crontab/neck_ring_calculate.go

@@ -208,9 +208,9 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, processIds []int64, xT
 		Where("id IN (?)", processIds).
 		Where("change_filter = ?", model.InitChangeFilter).
 		Where("change_high > ?", MinChangeHigh).
-		Order("heat_date,neck_ring_number,frameid").
+		Order("neck_ring_number,heat_date,frameid").
 		Find(&newChangeFilterList).Error; err != nil {
-		zaplog.Error("SecondUpdateChangeFilter", zap.Any("error", err), zap.Any("xToday", xToday))
+		zaplog.Error("SecondUpdateChangeFilter", zap.Any("error", err))
 		return
 	}
 
@@ -227,6 +227,7 @@ func (e *Entry) SecondUpdateChangeFilter(pastureId int64, processIds []int64, xT
 
 		xChangeDiscount := float64(xToday.XChangeDiscount) / 10
 		xRuminaDisc := float64(xToday.XRuminaDisc) / 10
+
 		secondFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
 		if secondFilterData.ChangeFilter <= MinChangeFilter {
 			secondFilterData.ChangeFilter = 0
@@ -300,7 +301,7 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
 				"ROUND(STD(IF(change_filter>=60, 60, change_filter))) as std_filter", "COUNT(1) as nb").
 			Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
 			Where("pasture_id = ?", pastureId).
-			Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
+			Where(e.DB.Where("high > ?", 12).Or("rumina >= ?", xToday.Rumina)).
 			Where("active_time <= ?", beginDayDate.Add(-12*time.Hour).Format(model.LayoutTime)).
 			Where("change_filter > ?", MinChangeFilter).
 			Where("neck_ring_number = ?", v.NeckRingNumber).

+ 3 - 2
module/crontab/sql.go

@@ -307,14 +307,15 @@ func (e *Entry) FindSumHabitData(pastureId int64, neckRingNumber, heatDate strin
 			"IF(COUNT(1)>6, ROUND(AVG(active)*12,0), 0) as sum_active",
 			"MAX(change_filter) as sum_max_high",
 			fmt.Sprintf("MIN(IF(change_filter > %d, change_filter, 0)) as sum_min_high", model.DefaultChangeFilter),
-			fmt.Sprintf("MIN( CASE WHEN filter_chew > %d THEN filter_chew WHEN filter_rumina >= %d THEN filter_rumina ELSE 0 END) as sum_min_chew", model.DefaultChangeFilter, model.DefaultRuminaFilter),
+			fmt.Sprintf("MIN( CASE WHEN chew_filter > %d THEN chew_filter WHEN rumina_filter >= %d THEN rumina_filter ELSE 0 END) as sum_min_chew", model.DefaultChangeFilter, model.DefaultRuminaFilter),
 		).
 		Where("pasture_id = ?", pastureId).
 		Where("heat_date BETWEEN ? AND ?", before1DayDate, heatDate).
 		Where("active_time BETWEEN ? AND ?", activeStartTime, activeTime).
 		Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
 		Where("neck_ring_number = ?", neckRingNumber).
-		Group("neck_ring_number").First(sumHabitData).Debug().Error; err != nil {
+		Group("neck_ring_number").
+		First(sumHabitData).Debug().Error; err != nil {
 		zaplog.Error("WeeklyActiveAvgUpdate-2",
 			zap.Any("error", err),
 			zap.Any("neckRingNumber", neckRingNumber),

+ 72 - 0
util/util_test.go

@@ -2,6 +2,7 @@ package util
 
 import (
 	"fmt"
+	"sort"
 	"strconv"
 	"strings"
 	"testing"
@@ -527,5 +528,76 @@ func TestSubstr(t *testing.T) {
 	fmt.Println("milkWeight1:", milkWeight1, err)
 }
 
+type WeeklyActiveModel struct {
+	CowId    int64  `json:"cow_id"`
+	HeatDate string `json:"heat_date"`
+	Nb       int32  `json:"nb"`
+	High     int32  `json:"high"`
+}
+
 func Test_demo(t *testing.T) {
+	weeklyActiveModelList := make([]*WeeklyActiveModel, 0)
+	weeklyActiveModelList = append(weeklyActiveModelList, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-08",
+		Nb:       12,
+		High:     1633,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-09",
+		Nb:       12,
+		High:     1697,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-10",
+		Nb:       12,
+		High:     1588,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-11",
+		Nb:       12,
+		High:     1789,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-12",
+		Nb:       12,
+		High:     1822,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-13",
+		Nb:       12,
+		High:     1672,
+	}, &WeeklyActiveModel{
+		CowId:    8859,
+		HeatDate: "2025-04-14",
+		Nb:       12,
+		High:     1817,
+	})
+
+	// 1. 先按High值排序
+	sort.Slice(weeklyActiveModelList, func(i, j int) bool {
+		return weeklyActiveModelList[i].High < weeklyActiveModelList[j].High
+	})
+
+	countHigh := len(weeklyActiveModelList)
+	sumHigh := int32(0)
+	minHigh := weeklyActiveModelList[0].High
+	maxHigh := weeklyActiveModelList[len(weeklyActiveModelList)-1].High
+	for _, v := range weeklyActiveModelList {
+		sumHigh += v.High
+	}
+	diff := 2
+	x := int32(0)
+	if countHigh > 3 {
+		diff = 3
+		x = weeklyActiveModelList[1].High
+	}
+
+	fmt.Println("sumHigh", sumHigh)
+	fmt.Println("minHigh", minHigh)
+	fmt.Println("maxHigh", maxHigh)
+	fmt.Println("x", x)
+	fmt.Println("diff", diff, "countHigh", countHigh)
+	avgHigh := float64(sumHigh-minHigh-maxHigh-x) / float64(countHigh-diff)
+	fmt.Println(int32(avgHigh))
 }