浏览代码

Merge branch 'feature/event' of http://192.168.1.8:3000/xuyiping/kpt-pasture into feature/event

Yi 2 月之前
父节点
当前提交
02d14579db
共有 4 个文件被更改,包括 37 次插入26 次删除
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 3 8
      model/neck_ring_estrus_warning.go
  4. 29 17
      module/backend/dashboard.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250424025835-b9c007f15d44
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250425013204-c2e3c2cb22b5
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 4 - 0
go.sum

@@ -60,6 +60,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250424024203-073d6537225b h1:GUHRd7Bv
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250424024203-073d6537225b/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250424025835-b9c007f15d44 h1:bxYqBSegVy9cVdjTtjvLKK4HVT40moltROoaHVXNmZM=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250424025835-b9c007f15d44/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250425011443-a83a84c81b7d h1:/2tQo6LnyvkvN+6ZhD/n5243lDvA25dcc35q+xbCl7A=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250425011443-a83a84c81b7d/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250425013204-c2e3c2cb22b5 h1:9HdkH0TLT69nYrSpVNpK+AqRsEj6zsSAI0wyn3txEJo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250425013204-c2e3c2cb22b5/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 3 - 8
model/neck_ring_estrus_warning.go

@@ -51,8 +51,8 @@ func NewNeckRingEstrusWarning(
 	}
 }
 
-// calculatePzHour 计算最佳配置时间
-func (n *NeckRingEstrusWarning) calculatePzHour(lact int32) time.Time {
+// CalculatePzHour 计算最佳配置时间
+func (n *NeckRingEstrusWarning) CalculatePzHour(lact int32) time.Time {
 	var pzHour time.Time
 	dateTime, _ := util.TimeParseLocal(LayoutTime, n.DateTime)
 	firstTime, _ := util.TimeParseLocal(LayoutTime, n.FirstTime)
@@ -86,7 +86,7 @@ func (n NeckRingEstrusWarningSlice) ToPB(cowMap map[int64]*Cow, eventLogMap map[
 			lastBreedEventDetails = desc
 		}
 
-		pzHour := v.calculatePzHour(cow.Lact)
+		pzHour := v.CalculatePzHour(cow.Lact)
 		estrusInterval := int32(0)
 		if v.LastTime != "" {
 			lastTime, _ := util.TimeParseLocal(LayoutTime, v.LastTime)
@@ -130,8 +130,3 @@ type EstrusWarning struct {
 	Nb1              int32  `json:"nb1"`
 	Nb2              int32  `json:"nb2"`
 }
-
-type DashboardNeckRingEstrusWarning struct {
-	Level pasturePb.EstrusLevel_Kind
-	Count int32
-}

+ 29 - 17
module/backend/dashboard.go

@@ -24,7 +24,8 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
 		return nil, xerr.WithStack(err)
 	}
 
-	estrusWarningCowList := make([]*model.DashboardNeckRingEstrusWarning, 0)
+	var count int64
+	neckRingEstrusList := make([]*model.NeckRingEstrusWarning, 0)
 	estrusWarningLevelItems := map[int32]int32{
 		int32(pasturePb.EstrusLevel_Low):    0,
 		int32(pasturePb.EstrusLevel_Middle): 0,
@@ -36,28 +37,38 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
 		return nil, xerr.Customf("系统错误!")
 	}
 	if err = pref.Order("a.level DESC").
-		Select("a.level, count(a.level) as count").
-		Group("a.level").
-		Find(&estrusWarningCowList).Error; err != nil {
+		Count(&count).
+		Find(&neckRingEstrusList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
-	/*if err = s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
-		Select("a.level, count(a.level) as count").
-		Where("a.pasture_id = ?", userModel.AppPasture.Id).
-		Where("a.is_show = ?", pasturePb.IsShow_Ok).
-		Group("a.level").
-		Find(&estrusWarningCowList).Error; err != nil {
-		zaplog.Error("NeckRingWarning", zap.Any("estrusWarningNumber", err))
-	}*/
 	countEstrusWarning := 0
-	for _, v := range estrusWarningCowList {
-		estrusWarningLevelItems[int32(v.Level)] = estrusWarningLevelItems[v.Count]
-		countEstrusWarning += int(v.Count)
+	nowTime := time.Now().Local()
+	optimumMating := map[string]int32{
+		"front":  0,
+		"middle": 0,
+		"behind": 0,
+	}
+	for _, v := range neckRingEstrusList {
+		estrusWarningLevelItems[int32(v.Level)] += 1
+		countEstrusWarning += 1
+		cowInfo, _ := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, v.EarNumber)
+		pzHour := v.CalculatePzHour(cowInfo.Lact)
+		optimumMatingStartTime := pzHour.Add(-4 * time.Hour)
+		optimumMatingEndTime := pzHour.Add(4 * time.Hour)
+
+		// 判断当前时间是否在 pzHour-4h 到 pzHour+4h 之间
+		if nowTime.After(optimumMatingStartTime) && nowTime.Before(optimumMatingEndTime) {
+			optimumMating["middle"] += 1
+		}
+		if nowTime.After(optimumMatingEndTime) {
+			optimumMating["behind"] += 1
+		}
+		if nowTime.Before(optimumMatingStartTime) {
+			optimumMating["front"] += 1
+		}
 	}
 
-	zaplog.Info("NeckRingWarning", zap.Any("estrusWarningNumber", estrusWarningCowList), zap.Any("estrusWarningLevelItems", estrusWarningLevelItems))
-
 	abortionCount := int64(0)
 	pref, err = s.AbortionWarningQuery(ctx, userModel.AppPasture.Id)
 	if err != nil {
@@ -85,6 +96,7 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
 			AbortionWarningNumber:   int32(abortionCount),
 			StressWarningNumber:     0,
 			EstrusWarningLevelItems: estrusWarningLevelItems,
+			OptimumMating:           optimumMating,
 		},
 	}, nil