Przeglądaj źródła

weight: 称重增加体高字段

Yi 4 miesięcy temu
rodzic
commit
d690c5b230
5 zmienionych plików z 25 dodań i 13 usunięć
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 3 1
      model/event_weight.go
  4. 14 10
      module/backend/event_base.go
  5. 3 1
      module/backend/sql.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-20241128094754-04053736c8ab
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20241128102506-727966a0f004
 	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

@@ -54,6 +54,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20241127090200-9343aa730c3f h1:iRYyVkDA
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241127090200-9343aa730c3f/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241128094754-04053736c8ab h1:9pYdwjsGBoT8frMz42gDq4muVRkGJuidkaMmLVgpICg=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241128094754-04053736c8ab/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241128101510-609ac4e83ada h1:4rHApLDEK0ueIUf3fpIdM5dXVz3S/uhn2fF7rmecgac=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241128101510-609ac4e83ada/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241128102506-727966a0f004 h1:0kHmrqRNqiJuzIGIk+dkAsTd/7iAlPRKPYN5h2aaeTo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241128102506-727966a0f004/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 - 1
model/event_weight.go

@@ -11,6 +11,7 @@ type EventWeight struct {
 	PenId         int32  `json:"penId"`
 	Lact          int32  `json:"lact"`
 	Weight        int32  `json:"weight"`
+	Height        int32  `json:"height"`
 	WeightAt      int64  `json:"weightAt"`
 	Remarks       string `json:"remarks"`
 	OperationId   int32  `json:"operationId"`
@@ -25,11 +26,12 @@ func (c *EventWeight) TableName() string {
 	return "event_weight"
 }
 
-func NewEventWeight(cow *Cow, currentUser *SystemUser, weight int32, req *pasturePb.EventWeight) *EventWeight {
+func NewEventWeight(cow *Cow, currentUser *SystemUser, weight, height int32, req *pasturePb.EventWeight) *EventWeight {
 	return &EventWeight{
 		CowId:         cow.Id,
 		EarNumber:     cow.EarNumber,
 		Weight:        weight,
+		Height:        height,
 		Lact:          cow.Lact,
 		DayAge:        cow.GetDayAge(),
 		WeightAt:      int64(req.WeightAt),

+ 14 - 10
module/backend/event_base.go

@@ -281,6 +281,11 @@ func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight
 		return xerr.Customf("获取当前登录用户失败: %s", err.Error())
 	}
 
+	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+	req.OperationName = operationUser.Name
 	weightEvent := make([]*model.EventWeight, 0)
 	for _, item := range req.WeightItems {
 		cow, err := s.GetCowInfoByCowId(ctx, int64(item.CowId))
@@ -288,24 +293,23 @@ func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight
 			return xerr.WithStack(err)
 		}
 		var weight = int32(item.Weight * 100)
-
-		operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
-		if err != nil {
-			return xerr.WithStack(err)
-		}
-		req.OperationName = operationUser.Name
-		weightEvent = append(weightEvent, model.NewEventWeight(cow, currentUser, weight, req))
+		weightEvent = append(weightEvent, model.NewEventWeight(cow, currentUser, weight, int32(item.Height), req))
 	}
+
 	if len(weightEvent) <= 0 {
 		return nil
 	}
 
 	if err = s.DB.Transaction(func(tx *gorm.DB) error {
 		for _, item := range weightEvent {
-			if err = tx.Model(new(model.Cow)).Where("id = ?", item.CowId).
+			if err = tx.Model(new(model.Cow)).
+				Where("id = ?", item.CowId).
+				Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
 				Updates(map[string]interface{}{
-					"last_weight_at": item.WeightAt,
-					"current_weight": item.Weight,
+					"last_second_weight_at": gorm.Expr("last_weight_at"),
+					"last_second_weight":    gorm.Expr("current_weight"),
+					"last_weight_at":        item.WeightAt,
+					"current_weight":        item.Weight,
 				}).Error; err != nil {
 				return xerr.WithStack(err)
 			}

+ 3 - 1
module/backend/sql.go

@@ -112,7 +112,9 @@ func (s *StoreEntry) GetCowList(ctx context.Context, cowIds []int64) ([]*model.C
 
 func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
 	cowData := &model.Cow{Id: cowId}
-	if err := s.DB.Model(new(model.Cow)).Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).First(cowData).Error; err != nil {
+	if err := s.DB.Model(new(model.Cow)).
+		Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
+		First(cowData).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
 			return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
 		}