فهرست منبع

warning: estrus item update

Yi 3 روز پیش
والد
کامیت
b84a5d4ad7
4فایلهای تغییر یافته به همراه37 افزوده شده و 16 حذف شده
  1. 33 14
      module/backend/neck_ring_warning.go
  2. 1 0
      module/crontab/neck_ring_calculate.go
  3. 1 1
      module/crontab/neck_ring_estrus.go
  4. 2 1
      module/crontab/sql.go

+ 33 - 14
module/backend/neck_ring_warning.go

@@ -6,6 +6,7 @@ import (
 	"kpt-pasture/model"
 	"kpt-pasture/util"
 	"net/http"
+	"sort"
 	"time"
 
 	"go.uber.org/zap"
@@ -22,7 +23,7 @@ func (s *StoreEntry) NeckRingWarningEstrusOrAbortionCowList(ctx context.Context,
 		return nil, xerr.WithStack(err)
 	}
 
-	var count int64
+	var count int32
 	neckRingEstrusList := make([]*model.NeckRingEstrusWarning, 0)
 	var pref *gorm.DB
 	switch req.Kind {
@@ -50,13 +51,17 @@ func (s *StoreEntry) NeckRingWarningEstrusOrAbortionCowList(ctx context.Context,
 		pref.Where("b.pen_id IN ?", req.PenIds)
 	}
 
-	if err = pref.Group("a.cow_id").
-		Order("a.date_time,a.level DESC").
-		Count(&count).
-		Limit(int(pagination.PageSize)).
-		Offset(int(pagination.PageOffset)).
-		Find(&neckRingEstrusList).Error; err != nil {
-		return nil, xerr.WithStack(err)
+	// 按照发情时间和发情等级倒叙排序
+	if req.MatingWindowPeriod > 0 {
+		if err = pref.Group("a.cow_id").
+			Find(&neckRingEstrusList).Error; err != nil {
+			return nil, xerr.WithStack(err)
+		}
+	} else {
+		if err = pref.Group("a.cow_id").
+			Find(&neckRingEstrusList).Error; err != nil {
+			return nil, xerr.WithStack(err)
+		}
 	}
 
 	cowMap := make(map[int64]*model.Cow)
@@ -71,25 +76,39 @@ func (s *StoreEntry) NeckRingWarningEstrusOrAbortionCowList(ctx context.Context,
 		for _, log := range lastEventLogList {
 			eventLogMap[log.CowId] = log.EventDescription
 		}
-	}
 
-	if len(cowIds) > 0 {
 		cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
 		for _, cow := range cowList {
 			cowMap[cow.Id] = cow
 		}
 	}
-
 	list := model.NeckRingEstrusWarningSlice(neckRingEstrusList).ToPB(cowMap, eventLogMap, req.MatingWindowPeriod)
-	if req.MatingWindowPeriod > 0 {
-		count = int64(len(list))
+	// 排序 先按照高峰时间正序排,然后再按照发情等级倒叙排
+	sort.Slice(list, func(i, j int) bool {
+		if list[i].PostPeakTimeForHours != list[j].PostPeakTimeForHours {
+			return list[i].PostPeakTimeForHours < list[j].PostPeakTimeForHours
+		}
+		return list[i].Level > list[j].Level
+	})
+	// 分页
+	count = int32(len(list))
+	if count > pagination.PageOffset {
+		end := pagination.PageOffset + pagination.PageSize
+		if end > count {
+			end = count
+		}
+		list = list[pagination.PageOffset:end]
+	} else {
+		// 如果偏移量已超过列表长度,返回空列表
+		list = list[:0]
 	}
+
 	return &pasturePb.EstrusResponse{
 		Code: http.StatusOK,
 		Msg:  "ok",
 		Data: &pasturePb.EstrusData{
 			List:     list,
-			Total:    int32(count),
+			Total:    count,
 			PageSize: pagination.PageSize,
 			Page:     pagination.Page,
 		},

+ 1 - 0
module/crontab/neck_ring_calculate.go

@@ -452,6 +452,7 @@ func (e *Entry) UpdateChangeAdJust(pastureId int64, processIds []int64) {
 	}
 }
 
+// UpdateCft 更新群体校正修正
 func (e *Entry) UpdateCft(pastureId int64, processIds []int64) {
 	neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
 	if err := e.DB.Model(new(model.NeckActiveHabit)).

+ 1 - 1
module/crontab/neck_ring_estrus.go

@@ -95,7 +95,7 @@ func (e *Entry) CowEstrusWarning(pastureId int64, xToday *XToday, nowTime time.T
 	neckRingEstrusList := make([]*model.NeckRingEstrus, 0)
 	for cowId, cowHabitList := range neckActiveHabitMap {
 		// 最近3天最大发情记录,小于该变化趋势的不再插入
-		before3Data := e.GetBeforeThreeDaysCowEstrus(cowId, nowTime.AddDate(0, 0, -2).Format(model.LayoutTime))
+		before3Data := e.GetBeforeThreeDaysCowEstrus(pastureId, cowId, nowTime.AddDate(0, 0, -2).Format(model.LayoutTime))
 
 		// 判断最近50天内是否存在发情记录(发情等级>=2),如果18~25天@xadjust21,如果36~50天@xadjust42
 		cow21Estrus := e.GetTwoEstrus(pastureId, cowId, nowTime.AddDate(0, 0, -100).Format(model.LayoutTime), nowTime.AddDate(0, 0, -2).Format(model.LayoutTime))

+ 2 - 1
module/crontab/sql.go

@@ -101,11 +101,12 @@ func (e *Entry) GetPenMapList(pastureId int64) (map[int32]*model.Pen, error) {
 }
 
 // GetBeforeThreeDaysCowEstrus 获取值得时间之前三天内最大发情记录
-func (e *Entry) GetBeforeThreeDaysCowEstrus(cowId int64, activeTime string) *model.NeckRingEstrus {
+func (e *Entry) GetBeforeThreeDaysCowEstrus(pastureId, cowId int64, activeTime string) *model.NeckRingEstrus {
 	neckRingEstrus := &model.NeckRingEstrus{}
 	if err := e.DB.Model(new(model.NeckRingEstrus)).
 		Select("MAX(max_high) as max_high, cow_id, MAX(day_high) as day_high, MAX(IF(check_result=1,3,check_result)) AS check_result,active_time").
 		Where("cow_id = ?", cowId).
+		Where("pasture_id = ?", pastureId).
 		Where("active_time >= ?", activeTime).
 		First(neckRingEstrus).Error; err != nil {
 		return neckRingEstrus