|
@@ -153,7 +153,7 @@ func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pastureP
|
|
|
}
|
|
|
}
|
|
|
// 记录日志
|
|
|
- cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, item)
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Pregnancy_Check, item)
|
|
|
if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
@@ -215,7 +215,7 @@ func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.Eve
|
|
|
}
|
|
|
|
|
|
// 记录日志
|
|
|
- cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, item)
|
|
|
+ cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Abort, item)
|
|
|
if err = tx.Table(cowLog.TableName()).Create(cowLog).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
@@ -318,114 +318,134 @@ func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventR
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-// MatingCreate 牛只配种
|
|
|
-func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMating) (err error) {
|
|
|
+// MatingBatch 牛只配种
|
|
|
+func (s *StoreEntry) MatingBatch(ctx context.Context, req *pasturePb.EventMatingBatch) (err error) {
|
|
|
userModel, err := s.GetUserModel(ctx)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- eventMatingCheckModel, err := s.MatingCreateCheck(ctx, userModel.AppPasture.Id, req)
|
|
|
+ eventMatingCheckModelList, err := s.MatingCreateCheck(ctx, userModel.AppPasture.Id, req)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- req.OperationName = eventMatingCheckModel.OperationUser.Name
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- for _, cow := range eventMatingCheckModel.CowList {
|
|
|
- // 获取牛只最近一次配种信息
|
|
|
- lastEventMating, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, cow.Id)
|
|
|
- if err != nil {
|
|
|
- // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
- newMating := model.NewEventMatingNaturalEstrus(userModel.AppPasture.Id, cow, req, userModel.SystemUser)
|
|
|
-
|
|
|
- if err = tx.Create(newMating).Error; err != nil {
|
|
|
+ for _, item := range eventMatingCheckModelList {
|
|
|
+ // 脖环发情揭发数据
|
|
|
+ if item.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
|
|
|
+ // 新增发情事件数据
|
|
|
+ newEstrus := model.NewEventEstrus(
|
|
|
+ userModel.AppPasture.Id, item.Cow, pasturePb.ExposeEstrusType_Neck_Ring,
|
|
|
+ pasturePb.IsShow_Ok, pasturePb.IsShow_Ok, item.MatingAt,
|
|
|
+ item.OperationUser, userModel.SystemUser,
|
|
|
+ )
|
|
|
+ if err = tx.Create(newEstrus).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 新增配种事件数据
|
|
|
+ newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Neck_Ring)
|
|
|
+ newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, false, item.OperationUser, userModel.SystemUser)
|
|
|
+ if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, false); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err = tx.Model(newMating).Create(newMating).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 获取牛只最近一次配种信息
|
|
|
+ lastEventMating, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, item.Cow.Id)
|
|
|
+ if err != nil {
|
|
|
+ // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ newMating := model.NewEventMatingNaturalEstrus(userModel.AppPasture.Id, item, userModel.SystemUser)
|
|
|
+ if err = tx.Create(newMating).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, false); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 所有有配种数据的牛只
|
|
|
+ if lastEventMating == nil || lastEventMating.Id <= 0 || lastEventMating.Status != pasturePb.IsShow_No {
|
|
|
+ zaplog.Error("MatingCreate", zap.Any("cow", item.Cow), zap.Any("UserModel", userModel))
|
|
|
+ return xerr.Customf("牛只配种数据异常: %d", item.Cow.EarNumber)
|
|
|
+ }
|
|
|
|
|
|
- if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, false); err != nil {
|
|
|
+ // 2.1 复配 => 本胎次配次不加1
|
|
|
+ isReMating := lastEventMating.IsReMating(item.Cow, item.MatingAt)
|
|
|
+ if isReMating {
|
|
|
+ lastEventMating.EventReMatingUpdate(item.MatingAt)
|
|
|
+ if err = tx.Model(lastEventMating).
|
|
|
+ Select("mating_result", "mating_result_at", "status").
|
|
|
+ Where("id = ?", lastEventMating.Id).
|
|
|
+ Updates(lastEventMating).Error; err != nil {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.2. 同期初配
|
|
|
+ IsMatingUpdate := lastEventMating.IsMatingUpdate()
|
|
|
+ if IsMatingUpdate {
|
|
|
+ lastEventMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
|
|
|
+ if err = tx.Model(lastEventMating).
|
|
|
+ Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
|
|
|
+ Where("id = ?", lastEventMating.Id).
|
|
|
+ Updates(lastEventMating).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- continue
|
|
|
- } else {
|
|
|
- return xerr.WithStack(err)
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 2. 所有有配种数据的牛只
|
|
|
- if lastEventMating == nil || lastEventMating.Id <= 0 || lastEventMating.Status != pasturePb.IsShow_No {
|
|
|
- zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("UserModel", userModel))
|
|
|
- return xerr.Customf("牛只配种数据异常: %d", cow.EarNumber)
|
|
|
- }
|
|
|
+ // 2.3. 提交的配种数据中,定位出空怀的牛只,
|
|
|
+ isEmptyMating := lastEventMating.IsEmptyMating(item.Cow, item.MatingAt)
|
|
|
+ if isEmptyMating {
|
|
|
+ // 把上次配种结果信息更新成空怀
|
|
|
+ lastEventMating.EventMatingResultUpdate(pasturePb.MatingResult_Empty, item.MatingAt)
|
|
|
+ if err = tx.Model(lastEventMating).
|
|
|
+ Select("mating_result", "mating_result_at").
|
|
|
+ Where("id = ?", lastEventMating.Id).
|
|
|
+ Updates(lastEventMating).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 先创建一条新的配种数据
|
|
|
+ newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Natural_Estrus)
|
|
|
+ newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
|
|
|
+ if err = tx.Model(newMating).Create(newMating).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
|
|
|
- // 2.1 复配 => 本胎次配次不加1
|
|
|
- isReMating := lastEventMating.IsReMating(cow, int64(req.MatingAt))
|
|
|
- if isReMating {
|
|
|
- lastEventMating.EventReMatingUpdate(int64(req.MatingAt))
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_result", "mating_result_at", "status").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
+ // 更新牛只配种结果日志
|
|
|
+ if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, s.MatingResultMap()[pasturePb.MatingResult_Empty]); err != nil {
|
|
|
+ zaplog.Error("MatingCreate", zap.Any("UpdateEventCowLogByCowId", err), zap.Any("cow", item.Cow))
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 2.2. 同期初配
|
|
|
- IsMatingUpdate := lastEventMating.IsMatingUpdate()
|
|
|
- if IsMatingUpdate {
|
|
|
- lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
+ // 牛只基本中配种信息更新
|
|
|
+ if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, isReMating); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 2.3. 提交的配种数据中,定位出空怀的牛只,
|
|
|
- isEmptyMating := lastEventMating.IsEmptyMating(cow, int64(req.MatingAt))
|
|
|
- if isEmptyMating {
|
|
|
- // 把上次配种结果信息更新成空怀
|
|
|
- lastEventMating.EventMatingResultUpdate(pasturePb.MatingResult_Empty, int64(req.MatingAt))
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_result", "mating_result_at").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 先创建一条新的配种数据
|
|
|
- newMating := model.NewEventMating(userModel.AppPasture.Id, cow, int64(req.MatingAt), req.ExposeEstrusType)
|
|
|
- newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
|
|
|
- if err = tx.Model(newMating).Create(newMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 更新牛只配种结果日志
|
|
|
- if err = s.UpdateMatingResultEventCowLogByCowId(ctx, cow.Id, s.MatingResultMap()[pasturePb.MatingResult_Empty]); err != nil {
|
|
|
- zaplog.Error("MatingCreate", zap.Any("UpdateEventCowLogByCowId", err), zap.Any("cow", cow))
|
|
|
- }
|
|
|
+ // 减去精液的数量
|
|
|
+ item.FrozenSemen.EventQuantityUpdate(item.FrozenSemenCount)
|
|
|
+ if err = tx.Model(item.FrozenSemen).
|
|
|
+ Select("quantity").
|
|
|
+ Where("id = ?", item.FrozenSemen.Id).
|
|
|
+ Updates(item.FrozenSemen).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- // 牛只基本中配种信息更新
|
|
|
- if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, isReMating); err != nil {
|
|
|
+ // 创建冻精使用记录日志
|
|
|
+ itemFrozenSemenLog := model.NewEventFrozenSemenLog(userModel.AppPasture.Id, item)
|
|
|
+ if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 创建冻精使用记录日志
|
|
|
- itemFrozenSemenLog := model.NewEventFrozenSemenLog(eventMatingCheckModel.FrozenSemen.PastureId, req)
|
|
|
- if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 减去精液的数量
|
|
|
- eventMatingCheckModel.FrozenSemen.EventQuantityUpdate(req.FrozenSemenCount)
|
|
|
- if err = tx.Model(eventMatingCheckModel.FrozenSemen).
|
|
|
- Select("quantity").
|
|
|
- Where("id = ?", eventMatingCheckModel.FrozenSemen.Id).
|
|
|
- Updates(eventMatingCheckModel.FrozenSemen).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
return nil
|
|
|
}); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -434,17 +454,17 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, cow *model.Cow, req *pasturePb.EventMating, isReMating bool) error {
|
|
|
+func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, item *model.EventMatingCheckBatchModel, isReMating bool) error {
|
|
|
// 牛只基本中配种信息更新
|
|
|
- cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
|
|
|
- if err := s.DB.Model(cow).
|
|
|
+ item.Cow.EventMatingUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating)
|
|
|
+ if err := s.DB.Model(item.Cow).
|
|
|
Select("last_mating_at", "mating_times", "last_bull_number", "first_mating_at", "is_pregnant", "breed_status").
|
|
|
- Where("id = ?", cow.Id).
|
|
|
- Updates(cow).Error; err != nil {
|
|
|
+ Where("id = ?", item.Cow.Id).
|
|
|
+ Updates(item.Cow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
// 记录日志
|
|
|
- cowLogs := s.SubmitEventLog(ctx, pastureId, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, pastureId, item.Cow, pasturePb.EventType_Mating, item)
|
|
|
if err := s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
@@ -516,7 +536,7 @@ func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeani
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
// 记录事件日志
|
|
|
- cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_Weaning, pasturePb.ExposeEstrusType_Invalid, req)
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_Weaning, req)
|
|
|
if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|