瀏覽代碼

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

Yi 1 月之前
父節點
當前提交
dbc00c2cb1
共有 6 個文件被更改,包括 125 次插入4 次删除
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 48 0
      model/pen_behavior_day.go
  4. 37 3
      module/backend/analysis_more.go
  5. 34 0
      module/backend/config_data_base.go
  6. 1 0
      module/backend/enum_options.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-20250423080234-2c477a4a0bec
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250424025835-b9c007f15d44
 	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

@@ -56,6 +56,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250423070335-3521c22802be h1:fcqMFBsu
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250423070335-3521c22802be/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250423080234-2c477a4a0bec h1:iOaGH+q4slB4oUliGCXVnmBDGLM/+80A2+7VSWPdp0U=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250423080234-2c477a4a0bec/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250424024203-073d6537225b h1:GUHRd7BvPy/dL+heSFd1r16Y3yZlnzqsD/FHDHAOfsg=
+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/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=

+ 48 - 0
model/pen_behavior_day.go

@@ -1,5 +1,7 @@
 package model
 
+import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
 type PenBehaviorDay struct {
 	Id          int64   `json:"id"`
 	PastureId   int64   `json:"pastureId"`
@@ -46,3 +48,49 @@ func NewPenBehaviorDay(pastureId int64, heatDate string, penId int32, penName st
 }
 
 type PenBehaviorDaySlice []*PenBehaviorDay
+
+type PenBehaviorDayModel struct {
+	HeatDate      string  `json:"heatDate"`
+	PenId         int32   `json:"penId"`
+	PenName       string  `json:"penName"`
+	CowCount      int32   `json:"cowCount"`
+	DayMilk       float32 `json:"dayMilk"`
+	DayHigh       int32   `json:"dayHigh"`
+	DayRumina     int32   `json:"dayRumina"`
+	DayIntake     int32   `json:"dayIntake"`
+	DayInactive   int32   `json:"dayInactive"`
+	DayChew       int32   `json:"dayChew"`
+	DayActive     int32   `json:"dayActive"`
+	DayImmobility int32   `json:"dayImmobility"`
+	RuminaStd     int32   `json:"ruminaStd"`
+}
+
+type PenBehaviorDayModelSlice []*PenBehaviorDayModel
+
+func (p PenBehaviorDayModelSlice) ToPB(dataTimeRange []string) *pasturePb.BarnMonitorItem {
+	res := &pasturePb.BarnMonitorItem{
+		Headers:       dataTimeRange,
+		DateTime:      make([]string, 0),
+		DayMilk:       make([]float32, 0),
+		DayRumina:     make([]int32, 0),
+		DayIntake:     make([]int32, 0),
+		DayImmobility: make([]int32, 0),
+		DayGasp:       make([]int32, 0),
+		DayChew:       make([]int32, 0),
+		DayStd:        make([]int32, 0),
+	}
+	for _, dt := range dataTimeRange {
+		for _, v := range p {
+			if dt == v.HeatDate {
+				res.DateTime = append(res.DateTime, v.HeatDate)
+				res.DayMilk = append(res.DayMilk, v.DayMilk)
+				res.DayRumina = append(res.DayRumina, v.DayRumina)
+				res.DayIntake = append(res.DayIntake, v.DayIntake)
+				res.DayImmobility = append(res.DayImmobility, v.DayImmobility)
+				res.DayChew = append(res.DayChew, v.DayChew)
+				res.DayStd = append(res.DayStd, v.RuminaStd)
+			}
+		}
+	}
+	return res
+}

+ 37 - 3
module/backend/analysis_more.go

@@ -2,8 +2,8 @@ package backend
 
 import (
 	"context"
-	"fmt"
 	"kpt-pasture/model"
+	"kpt-pasture/util"
 	"net/http"
 	"time"
 
@@ -44,7 +44,41 @@ func (s *StoreEntry) PenBehaviorDaily(ctx context.Context, req *pasturePb.BarnMo
 	if err != nil {
 		return nil, xerr.WithStack(err)
 	}
+	if req.StartAt == 0 || req.EndAt == 0 || req.EndAt < req.StartAt {
+		return nil, xerr.Customf("时间范围错误")
+	}
+
+	startDate := time.Unix(int64(req.StartAt), 0).Local().Format(model.LayoutDate2)
+	endDate := time.Unix(int64(req.EndAt), 0).Local().Format(model.LayoutDate2)
 
-	fmt.Println("userModel", userModel)
-	return nil, err
+	dataTimeRange, err := util.GetDaysBetween(startDate, endDate)
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	penBehaviorDayModelList := make([]*model.PenBehaviorDayModel, 0)
+	pref := s.DB.Model(new(model.PenBehaviorDay)).
+		Select(`distinct(heat_date) as heat_date,pen_id,pen_name,rumina_std,cow_count,day_rumina,day_intake,
+			day_inactive,day_milk,day_rumina+day_intake as day_chew,24*60 - day_active as day_immobility`).
+		Where("pasture_id = ?", userModel.AppPasture.Id).
+		Where("heat_date BETWEEN ? AND ?", startDate, endDate)
+	if len(req.BarnIds) == 0 && req.BehaviorKind > 0 {
+		if err = pref.Where("pen_id IN (?)", req.BarnIds).
+			Order("heat_date").
+			Find(penBehaviorDayModelList).Error; err != nil {
+			return nil, xerr.WithStack(err)
+		}
+	} else {
+		if err = pref.Group("heat_date,pen_id").
+			Order("heat_date,pen_id").
+			Find(penBehaviorDayModelList).Error; err != nil {
+			return nil, xerr.WithStack(err)
+		}
+	}
+
+	return &pasturePb.BarnMonitorResponse{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: model.PenBehaviorDayModelSlice(penBehaviorDayModelList).ToPB(dataTimeRange),
+	}, err
 }

+ 34 - 0
module/backend/config_data_base.go

@@ -409,3 +409,37 @@ func (s *StoreEntry) WarningHealthLevel(isAll string) []*pasturePb.ConfigOptions
 	})
 	return configOptions
 }
+
+func (s *StoreEntry) Behavior(isAll string) []*pasturePb.ConfigOptionsList {
+	configOptions := make([]*pasturePb.ConfigOptionsList, 0)
+	if isAll == model.IsAllYes {
+		configOptions = append(configOptions,
+			&pasturePb.ConfigOptionsList{
+				Value:    int32(0),
+				Label:    "全部",
+				Disabled: true,
+			})
+	}
+	configOptions = append(configOptions, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.Behavior_Rumina),
+		Label:    "反刍",
+		Disabled: true,
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.Behavior_Intake),
+		Label:    "采食",
+		Disabled: true,
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.Behavior_Reset),
+		Label:    "休息",
+		Disabled: true,
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.Behavior_Stationary),
+		Label:    "静止",
+		Disabled: true,
+	}, &pasturePb.ConfigOptionsList{
+		Value:    int32(pasturePb.Behavior_Milk),
+		Label:    "奶量",
+		Disabled: true,
+	})
+	return configOptions
+}

+ 1 - 0
module/backend/enum_options.go

@@ -221,6 +221,7 @@ func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName, i
 		"cowOutReason":               s.CowOutReasonList,
 		"deathDestination":           s.CowDeathDestinationList,
 		"warningHealthLevel":         s.WarningHealthLevel,
+		"behavior":                   s.Behavior,
 	}
 
 	getConfigFunc, ok := getConfigFuncMap[optionsName]