Prechádzať zdrojové kódy

crontab: neckRingEstrus update firstTime

Yi 1 deň pred
rodič
commit
261843ee6e

+ 15 - 3
module/backend/calendar.go

@@ -388,8 +388,19 @@ func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.I
 	newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
 	var count int64
 	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
-		Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.breed_status,b.pen_name,b.cow_type,
-		DATE_FORMAT(FROM_UNIXTIME(b.last_mating_at),'%Y-%m-%d')as mating_at_format,pregnancy_age,DATEDIFF(CURDATE(), FROM_UNIXTIME(last_mating_at)) AS mating_age,
+		Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.pen_name,b.cow_type,
+		DATE_FORMAT(FROM_UNIXTIME(b.last_mating_at),'%Y-%m-%d')as mating_at_format,pregnancy_age,DATEDIFF(CURDATE(), 
+		FROM_UNIXTIME(last_mating_at)) AS mating_age,
+		CASE b.breed_status
+		WHEN 1 THEN '未配'
+		WHEN 2 THEN '已配'
+		WHEN 3 THEN '已孕'
+		WHEN 4 THEN '空怀'
+		WHEN 5 THEN '流产'
+		WHEN 6 THEN '产犊'
+		WHEN 7 THEN '禁配'
+		ELSE '其他'
+		END AS breed_status_name,
 		CASE a.pregnant_check_name
         WHEN 'pregnant_check_for_first' THEN '初检'
         WHEN 'pregnant_check_for_second' THEN '复检'
@@ -432,7 +443,7 @@ func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.I
 			Total:    int32(count),
 			Page:     pagination.Page,
 			PageSize: pagination.PageSize,
-			HeaderSort: []string{"id", "cowId", "earNumber", "cowTypeName", "penName", "lact", "dayAge", "planDay",
+			HeaderSort: []string{"id", "cowId", "earNumber", "cowTypeName", "penName", "lact", "dayAge", "breedStatusName", "planDay",
 				"checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
 			Header: map[string]string{
 				"id":              "编号",
@@ -451,6 +462,7 @@ func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.I
 				"matingAge":       "配后天数",
 				"bullId":          "配种公牛",
 				"pregnancyAge":    "怀孕天数",
+				"breedStatusName": "繁殖状态",
 			},
 			List: newPregnancyCheckItems,
 		},

+ 29 - 5
module/crontab/neck_ring_estrus.go

@@ -62,7 +62,7 @@ func (e *Entry) EntryCowEstrus(pastureId int64) (err error) {
 	}
 	nowTime := time.Now().Local()
 	e.CowEstrusWarning(pastureId, xToday, nowTime)
-	e.UpdateNewNeckRingEstrus(pastureId, nowTime)
+	e.UpdateNewNeckRingEstrus(pastureId, xToday, nowTime)
 	return nil
 }
 
@@ -186,11 +186,11 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 	}
 }
 
-func (e *Entry) UpdateNewNeckRingEstrus(pastureId int64, nowTime time.Time) {
+func (e *Entry) UpdateNewNeckRingEstrus(pastureId int64, xToday *XToday, nowTime time.Time) {
 	// 更新牛只首次发情时间
 	e.UpdateEstrusFirstTime1(pastureId, nowTime)
 	e.UpdateEstrusIsPeak(pastureId, nowTime)
-	e.UpdateEstrusFirstTime2(pastureId, nowTime)
+	e.UpdateEstrusFirstTime2(pastureId, xToday, nowTime)
 	e.UpdateEstrusFirstTime3(pastureId, nowTime)
 }
 
@@ -213,10 +213,34 @@ func (e *Entry) UpdateEstrusFirstTime1(pastureId int64, xToday time.Time) {
 	}
 }
 
-func (e *Entry) UpdateEstrusFirstTime2(pastureId int64, xToday time.Time) {
+func (e *Entry) UpdateEstrusFirstTime2(pastureId int64, xToday *XToday, nowTime time.Time) {
 	neckRingEstrusList := e.FindNeckRingEstrusByFirstTimeEmpty(pastureId)
 	for _, v := range neckRingEstrusList {
-		zaplog.Info("UpdateEstrusFirstTime2", zap.Any("v", v))
+		// 获取牛只最近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 {
+			zaplog.Error("UpdateEstrusFirstTime2", zap.Any("FindFirstTime", err))
+			continue
+		}
+
+		if firstTime != "" {
+			if err := e.DB.Model(new(model.NeckRingEstrus)).
+				Where("id = ?", v.Id).
+				Update("first_time", firstTime).Error; err != nil {
+				zaplog.Error("UpdateEstrusFirstTime2", zap.Any("Update", err))
+			}
+		}
 	}
 }