|
@@ -579,19 +579,34 @@ func (s *StoreEntry) CreateWeight(ctx context.Context, req *pasturePb.WeightEven
|
|
|
return xerr.Custom("请选择相关牛只")
|
|
|
}
|
|
|
currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
weightEvent := make([]*model.EventWeight, 0)
|
|
|
- for _, cowIdStr := range cowList {
|
|
|
- cowId, _ := strconv.ParseInt(cowIdStr, 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
+ for _, item := range req.WeightItems {
|
|
|
+ cow, err := s.GetCowInfo(ctx, int64(item.CowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- weightEvent = append(weightEvent, model.NewEventWeight(cow, currentSystemUser.Id, req))
|
|
|
+ var weight = int32(item.Weight * 100)
|
|
|
+ weightEvent = append(weightEvent, model.NewEventWeight(cow, currentSystemUser, weight, req))
|
|
|
}
|
|
|
if len(weightEvent) <= 0 {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- return s.DB.Create(weightEvent).Error
|
|
|
+ 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).Updates(map[string]interface{}{
|
|
|
+ "last_weight_at": item.WeightAt,
|
|
|
+ "current_weight": item.Weight,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if err := tx.Create(weightEvent).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|