Prechádzať zdrojové kódy

crontab: cowWeekly update

Yi 2 dní pred
rodič
commit
4916ff04dc

+ 1 - 62
module/crontab/cow_cron.go

@@ -6,7 +6,6 @@ import (
 	"kpt-pasture/model"
 	"kpt-pasture/module/backend"
 	"kpt-pasture/util"
-	"sort"
 	"strings"
 	"time"
 
@@ -250,7 +249,7 @@ func (e *Entry) UpdateCowInfoByPasture(pastureId int64) {
 	for _, cow := range cowList {
 		// 周活动量
 		weeklyActive := e.UpdateCowWeeklyHigh(pastureId, cow)
-		zaplog.Info("crontab", zap.Any("weeklyActive", weeklyActive))
+		zaplog.Info("crontab", zap.Any("weeklyActive", weeklyActive), zap.Any("cow", cow))
 		cow.EventUpdate(weeklyActive)
 		if err := e.DB.Model(new(model.Cow)).
 			Select("day_age", "calving_age", "pregnancy_age", "admission_age", "abortion_age", "cow_type", "weekly_active", "lactation_age", "dry_milk_age", "mating_age").
@@ -641,63 +640,3 @@ func (e *Entry) InitEventData(cowList []*model.Cow, systemBasic *model.SystemBas
 	}
 	e.CreatedCalendar(systemBasic.PastureId, calendarType, startDay, endDay, int32(len(cowList)))
 }
-
-// UpdateCowWeeklyHigh 更新牛只周活动量
-func (e *Entry) UpdateCowWeeklyHigh(pastureId int64, cow *model.Cow) int32 {
-	highData, err := e.GetSystemNeckRingConfigure(pastureId, model.High)
-	if err != nil || highData == nil || highData.Value <= 0 {
-		return 0
-	}
-
-	minWeeklyActive, er := e.GetSystemNeckRingConfigure(pastureId, model.MinWeeklyActive)
-	if er != nil || minWeeklyActive == nil || minWeeklyActive.Value <= 0 {
-		return 0
-	}
-
-	weeklyActiveModelList := make([]*model.WeeklyActiveModel, 0)
-	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).
-		Where("pasture_id = ?", pastureId).
-		Where("cow_id = ?", cow.Id).
-		Where("heat_date BETWEEN ? AND ?", startTime, endTime).
-		Where("high > ?", highData.Value).
-		Having("nb > ?", 8).
-		Order("high").
-		Group("heat_date").
-		Find(&weeklyActiveModelList).Error; err != nil {
-		zaplog.Error("crontab", zap.Any("UpdateCowWeeklyHigh", err))
-		return 0
-	}
-
-	if len(weeklyActiveModelList) < 3 {
-		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 := weeklyActiveModelList[len(weeklyActiveModelList)-1].High
-	for _, v := range weeklyActiveModelList {
-		sumHigh += v.High
-	}
-	diff := 0
-	x := int32(0)
-	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)
-}

+ 5 - 5
module/crontab/cow_indicators_health.go

@@ -161,7 +161,7 @@ func (e *Entry) MultipartyAbortionRate(pastureList []*model.AppPastureList, star
 			if v2 > 0 {
 				info = true
 				abortionRate := float32(math.Round(float64(v1)/float64(v2)*100) / 100)
-				res[pastureId] = fmt.Sprintf("%f", abortionRate)
+				res[pastureId] = fmt.Sprintf("%.2f", abortionRate)
 			}
 		}
 		if !info {
@@ -229,7 +229,7 @@ func (e *Entry) CalvingDieRate(pastureList []*model.AppPastureList, startTime, e
 		}
 
 		if count > 0 {
-			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(count)/float64(len(eventDeathList))*100)/100))
+			res[pasture.Id] = fmt.Sprintf("%.2f", float32(math.Round(float64(count)/float64(len(eventDeathList))*100)/100))
 		} else {
 			res[pasture.Id] = "0"
 		}
@@ -260,7 +260,7 @@ func (e *Entry) CalvingOutRate(pastureList []*model.AppPastureList, startTime, e
 		}
 
 		if count > 0 {
-			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(count)/float64(len(eventSaleCowList))*100)/100))
+			res[pasture.Id] = fmt.Sprintf("%.2f", float32(math.Round(float64(count)/float64(len(eventSaleCowList))*100)/100))
 		} else {
 			res[pasture.Id] = "0"
 		}
@@ -288,7 +288,7 @@ func (e *Entry) AvgDepartureWeight(pastureList []*model.AppPastureList, startTim
 		}
 
 		if sumCount > 0 {
-			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(sumCount)/float64(sumWeight/1000)*100)/100))
+			res[pasture.Id] = fmt.Sprintf("%.2f", float32(math.Round(float64(sumCount)/float64(sumWeight/1000)*100)/100))
 		} else {
 			res[pasture.Id] = "0"
 		}
@@ -329,7 +329,7 @@ func (e *Entry) AvgSlaughterCycle(pastureList []*model.AppPastureList, startTime
 		}
 
 		if sumCount > 0 {
-			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(sumCount)/float64(sumAdmissionAge)*100)/100))
+			res[pasture.Id] = fmt.Sprintf("%.2f", float32(math.Round(float64(sumCount)/float64(sumAdmissionAge)*100)/100))
 		} else {
 			res[pasture.Id] = "0"
 		}

+ 73 - 0
module/crontab/cow_weekly.go

@@ -0,0 +1,73 @@
+package crontab
+
+import (
+	"fmt"
+	"kpt-pasture/model"
+	"math"
+	"sort"
+	"time"
+
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
+)
+
+// UpdateCowWeeklyHigh 更新牛只周活动量
+func (e *Entry) UpdateCowWeeklyHigh(pastureId int64, cow *model.Cow) int32 {
+	highData, err := e.GetSystemNeckRingConfigure(pastureId, model.High)
+	if err != nil || highData == nil || highData.Value <= 0 {
+		return 0
+	}
+
+	minWeeklyActive, er := e.GetSystemNeckRingConfigure(pastureId, model.MinWeeklyActive)
+	if er != nil || minWeeklyActive == nil || minWeeklyActive.Value <= 0 {
+		return 0
+	}
+
+	weeklyActiveModelList := make([]*model.WeeklyActiveModel, 0)
+	nowTime := time.Now().Local()
+	startTime := nowTime.AddDate(0, 0, -7).Format(model.LayoutDate2)
+	endTime := nowTime.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).
+		Where("pasture_id = ?", pastureId).
+		Where("cow_id = ?", cow.Id).
+		Where("heat_date BETWEEN ? AND ?", startTime, endTime).
+		Where("high > ?", highData.Value).
+		Having("nb > ?", 8).
+		Order("high").
+		Group("heat_date").
+		Find(&weeklyActiveModelList).Error; err != nil {
+		zaplog.Error("crontab", zap.Any("UpdateCowWeeklyHigh", err))
+		return 0
+	}
+
+	if len(weeklyActiveModelList) < 3 {
+		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 := weeklyActiveModelList[countHigh-1].High
+	for _, v := range weeklyActiveModelList {
+		sumHigh += v.High
+	}
+	diff := 0
+	x := int32(0)
+	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(math.Round(avgHigh))
+}