|
@@ -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,
|
|
|
},
|