Pārlūkot izejas kodu

crontab: neckRingEstrus firstTime updte

Yi 1 nedēļu atpakaļ
vecāks
revīzija
1d4d53f755

+ 1 - 1
module/crontab/health_waning.go

@@ -104,7 +104,7 @@ func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32)
 			zap.Any("healthValue", healthValue),
 			zap.Any("cowInfo", cowInfo),
 		)
-		if newScore > healthValue {
+		if newScore >= healthValue {
 			continue
 		}
 

+ 19 - 16
module/crontab/neck_ring_estrus.go

@@ -146,7 +146,6 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 			dayHigh := int32(maxCft) + cow21Estrus.HadJust
 			lastEstrusDate := cow21Estrus.ActiveDate
 			checkResult := getResult(before3Data, maxCft, cow21Estrus)
-			isPeak := pasturePb.IsShow_Ok
 			activeTime := lastActiveDate.Format(model.LayoutTime)
 
 			if e.HistoryNeckRingEstrus(pastureId, cowInfo.NeckRingNumber, activeTime) {
@@ -158,7 +157,6 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 				zap.Any("b48", b48),
 				zap.Any("checkResult", checkResult),
 				zap.Any("isShow", isShow),
-				zap.Any("isPeak", isPeak),
 				zap.Any("lastEstrusDate", lastEstrusDate),
 				zap.Any("activeDate", lastActiveDate),
 				zap.Any("dayHigh", dayHigh),
@@ -174,7 +172,7 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 			newNeckRingEstrus.ActiveTime = activeTime
 			newNeckRingEstrus.DayHigh = dayHigh
 			newNeckRingEstrus.MaxHigh = maxHigh
-			newNeckRingEstrus.IsPeak = isPeak
+			newNeckRingEstrus.IsPeak = pasturePb.IsShow_No
 			neckRingEstrusList = append(neckRingEstrusList, newNeckRingEstrus)
 		}
 	}
@@ -191,17 +189,20 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 // UpdateNewNeckRingEstrus 更新牛只首次发情时间和是否是高峰期
 func (e *Entry) UpdateNewNeckRingEstrus(pastureId int64, xToday *XToday, nowTime time.Time) {
 	e.UpdateEstrusFirstTime1(pastureId)
-	e.UpdateEstrusIsPeak(pastureId)
-	e.UpdateEstrusFirstTime2(pastureId, xToday, nowTime)
+	e.UpdateEstrusFirstTime2(pastureId, xToday)
 	e.UpdateEstrusFirstTime3(pastureId, nowTime)
+	e.UpdateEstrusIsPeak(pastureId)
 }
 
 // UpdateEstrusFirstTime1 更新牛只首次发情时间
 func (e *Entry) UpdateEstrusFirstTime1(pastureId int64) {
 	// 获取牛只首次发情时间为空的记录
 	neckRingEstrusList := e.FindNeckRingEstrusByFirstTimeEmpty(pastureId)
+	zaplog.Info("UpdateEstrusFirstTime1", zap.Any("neckRingEstrusList", neckRingEstrusList))
+
 	for _, v := range neckRingEstrusList {
-		cowEstrusStartData := e.FindCowEstrusFirstTime1(pastureId, v.CowId)
+		cowEstrusStartData := e.FindCowEstrusFirstTime1(pastureId, v)
+		zaplog.Info("UpdateEstrusFirstTime1", zap.Any("cowEstrusStartData", cowEstrusStartData))
 		if cowEstrusStartData != nil && cowEstrusStartData.FirstTime != "" {
 			if err := e.DB.Model(new(model.NeckRingEstrus)).
 				Where("id = ?", v.Id).
@@ -216,20 +217,22 @@ func (e *Entry) UpdateEstrusFirstTime1(pastureId int64) {
 	}
 }
 
-func (e *Entry) UpdateEstrusFirstTime2(pastureId int64, xToday *XToday, nowTime time.Time) {
+func (e *Entry) UpdateEstrusFirstTime2(pastureId int64, xToday *XToday) {
 	neckRingEstrusList := e.FindNeckRingEstrusByFirstTimeEmpty(pastureId)
 	for _, v := range neckRingEstrusList {
+		if v.FirstTime != "" {
+			continue
+		}
+
 		// 获取牛只最近12小时内的活动记录
 		activeTime, _ := util.TimeParseLocal(model.LayoutTime, v.ActiveTime)
 		startTime := activeTime.Add(-12 * time.Hour)
-
 		// 查询符合条件的活动记录
 		var firstTime string
 		if err := e.DB.Model(new(model.NeckActiveHabit)).
 			Select("MIN(active_time) as first_time").
 			Where("pasture_id = ?", pastureId).
 			Where("cow_id = ?", v.CowId).
-			Where("heat_date = ?", activeTime.Format(model.LayoutDate2)).
 			Where("active_time BETWEEN ? AND ?", startTime.Format(model.LayoutTime), v.ActiveTime).
 			Where("cft >= ?", xToday.ActiveLow).
 			Scan(&firstTime).Error; err != nil {
@@ -336,17 +339,17 @@ func (e *Entry) UpdateEstrusIsPeak(pastureId int64) {
 }
 
 // FindCowEstrusFirstTime1 查找牛只昨天是否有发情数据
-func (e *Entry) FindCowEstrusFirstTime1(pastureId, cowId int64) *EstrusStartData {
+func (e *Entry) FindCowEstrusFirstTime1(pastureId int64, neckRingEstrus *model.NeckRingEstrus) *EstrusStartData {
 	firstTimeEventEstrus := &EstrusStartData{}
-	nowTime := time.Now().Local()
-	startDate := fmt.Sprintf("%s 00:00:00", nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2))
-	endDate := fmt.Sprintf("%s 23:59:59", nowTime.Format(model.LayoutDate2))
+	activeAt := util.DateTimeParseLocalUnix2(neckRingEstrus.ActiveTime)
+	startDate := fmt.Sprintf("%s 00:00:00", activeAt.AddDate(0, 0, -1).Format(model.LayoutDate2))
 	if err := e.DB.Model(new(model.NeckRingEstrus)).
 		Select("cow_id,first_time").
-		Where("active_time BETWEEN ? AND ?", startDate, endDate).
+		Where("active_time BETWEEN ? AND ?", startDate, neckRingEstrus.ActiveTime).
 		Where("pasture_id = ?", pastureId).
-		Where("cow_id = ?", cowId).
-		Order("first_time ASC").
+		Where("cow_id = ?", neckRingEstrus.CowId).
+		Where("id != ?", neckRingEstrus.Id).
+		Order("first_time,id ASC").
 		First(&firstTimeEventEstrus).Error; err != nil {
 		return nil
 	}

+ 1 - 0
module/crontab/sql.go

@@ -358,6 +358,7 @@ func (e *Entry) FindNeckRingEstrusByFirstTimeEmpty(pastureId int64) []*model.Nec
 		Where("pasture_id = ?", pastureId).
 		Find(&neckRingEstrusList).Error; err != nil {
 		zaplog.Error("FindNeckRingEstrusFirstTime", zap.Any("err", err))
+		return neckRingEstrusList
 	}
 	return neckRingEstrusList
 }

+ 7 - 0
util/util.go

@@ -159,6 +159,13 @@ func DateTimeParseLocalUnix(DayTime string) int64 {
 	return theTime.Unix()
 }
 
+// DateTimeParseLocalUnix2
+// eg 2025-10-13 15:04:05 => 1676998245
+func DateTimeParseLocalUnix2(DayTime string) time.Time {
+	theTime, _ := TimeParseLocal(LayoutTime, DayTime)
+	return theTime
+}
+
 // GetMonthRemainDay 获取当前月还剩几天
 func GetMonthRemainDay() int {
 	now := time.Now().Local()