Pārlūkot izejas kodu

crontab: penBehavior update

Yi 2 nedēļas atpakaļ
vecāks
revīzija
f931038c4d

+ 12 - 25
module/crontab/pen_behavior.go

@@ -26,39 +26,23 @@ func (e *Entry) UpdatePenBehavior() error {
 			zaplog.Error("UpdatePenBehavior", zap.Any("pasture", pasture), zap.Any("err", err))
 			continue
 		}
-		e.PenBehaviorEnter(pasture.Id, conf.Value)
+		e.PenBehaviorEnter(pasture.Id, int(conf.Value))
 	}
 	return nil
 }
 
 // PenBehaviorEnter 栏舍行为曲线 对数据进行查缺补漏
-func (e *Entry) PenBehaviorEnter(pastureId int64, maxValue int64) {
-	var minHeatDate string
-	if err := e.DB.Model(new(model.NeckRingOriginal)).
-		Select("min(active_date) as min_heat_date").
-		Where("pasture_id = ?", pastureId).
-		Where("id >= ?", maxValue).
-		Scan(&minHeatDate).Error; err != nil {
-		zaplog.Error("PenBehaviorEnter", zap.Any("pastureId", pastureId), zap.Any("err", err))
-		return
-	}
+func (e *Entry) PenBehaviorEnter(pastureId int64, days int) {
+	minHeatDate := time.Now().Local().AddDate(0, 0, -days).Format(model.LayoutDate2)
 	e.PenBehavior(pastureId, minHeatDate)
-	var maxId int64
-	if err := e.DB.Model(new(model.NeckRingOriginal)).
-		Select("MAX(id) as id").
-		Where("pasture_id = ?", pastureId).
-		Where("id >= ?", maxValue).
-		Where("active_date = ?", minHeatDate).
-		Scan(&maxId).Error; err != nil {
-		zaplog.Error("PenBehaviorEnter", zap.Any("pastureId", pastureId), zap.Any("err", err))
+	e.UpdatePenBehaviorWeekData(pastureId, minHeatDate)
+	newDays := days - 1
+	if newDays < 0 {
+		return
 	}
-	if maxId > maxValue {
-		if err := e.UpdateSystemNeckRingConfigure(pastureId, model.MaxPenBehavior, maxId); err != nil {
-			zaplog.Error("UpdateSystemNeckRingConfigure", zap.Any("err", err))
-		}
+	if err := e.UpdateSystemNeckRingConfigure(pastureId, model.MaxPenBehavior, int64(newDays)); err != nil {
+		zaplog.Error("PenBehaviorEnter", zap.Any("pastureId", pastureId), zap.Any("days", days))
 	}
-
-	e.UpdatePenBehaviorWeekData(pastureId, minHeatDate)
 }
 
 // PenBehavior 栏舍行为曲线
@@ -66,6 +50,9 @@ func (e *Entry) PenBehavior(pastureId int64, heatDate string) {
 	frameIds := util.FrameIdSlice
 	penBehaviorList := make([]*model.PenBehaviorData, 0)
 	for _, frameId := range frameIds {
+		if frameId%10 == 8 {
+			continue
+		}
 		penBehaviorModel, err := e.getNeckRingOriginalList(pastureId, heatDate, frameId)
 		if err != nil {
 			zaplog.Error("PenBehavior",

+ 2 - 2
module/crontab/pen_behavior_day.go

@@ -59,8 +59,8 @@ func (e *Entry) BehaviorDaily(pastureId int64, days int32) {
 func (e *Entry) updateMilkDaily(pastureId int64, targetDate string) error {
 	milkDailyModelList := make([]*model.MilkDailyModel, 0)
 	if err := e.DB.Model(new(model.NeckActiveHabit)).
-		Select(`cow_id,heat_date, ROUND(AVG(filter_high), 0) as day_high, ROUND(AVG(rumina)*12, 0) as day_rumina, ROUND(AVG(intake)*12, 0) as day_intake,
-				ROUND(AVG(inactive)*12, 0) as day_inactive, ROUND(AVG(active)*12, 0) as day_active, COUNT(1) as nb`).
+		Select(`cow_id,heat_date, ROUND(AVG(filter_high), 0) as day_high, ROUND(AVG(rumina)*12, 0) as day_rumina,
+		ROUND(AVG(intake)*12, 0) as day_intake,ROUND(AVG(inactive)*12, 0) as day_inactive, ROUND(AVG(active)*12, 0) as day_active, COUNT(1) as nb`).
 		Where("pasture_id = ? AND heat_date = ?", pastureId, targetDate).
 		Where("cow_id > ?", 0).
 		Group("cow_id").

+ 7 - 2
module/mqtt/mqtt_handle.go

@@ -124,9 +124,14 @@ func (e *Entry) CreatedData(DSMLog *DataInsertNeckRingLog, originalData []byte)
 func (e *Entry) MsgDataFormat2(msg []byte, receiverMap map[string]int64) *DataInsertNeckRingLog {
 	mu.Lock()
 	defer mu.Unlock()
+
+	// 深拷贝 msg,防止底层数据被修改
+	msgCopy := make([]byte, len(msg))
+	copy(msgCopy, msg)
+
 	neckLogList := &model.NeckRingWrapper{}
-	if err := json.Unmarshal(msg, neckLogList); err != nil {
-		zaplog.Error("MsgDataFormat", zap.Any("err", err), zap.Any("msg", string(msg)))
+	if err := json.Unmarshal(msgCopy, neckLogList); err != nil {
+		zaplog.Error("MsgDataFormat", zap.Any("err", err), zap.Any("msg", string(msgCopy)))
 		return nil
 	}
 	if neckLogList.Type == "heartbeat" {

+ 5 - 63
util/util_test.go

@@ -2,7 +2,6 @@ package util
 
 import (
 	"fmt"
-	"sort"
 	"strconv"
 	"strings"
 	"testing"
@@ -536,68 +535,11 @@ type WeeklyActiveModel struct {
 }
 
 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
+	for _, frameId := range FrameIdSlice {
+		if frameId%10 == 8 {
+			continue
+		}
+		fmt.Println("x", frameId)
 	}
 
-	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))
 }