|
@@ -82,16 +82,17 @@ func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pastureP
|
|
|
breedStatus := pasturePb.BreedStatus_Pregnant
|
|
|
isPregnant := pasturePb.IsShow_Ok
|
|
|
matingResult := pasturePb.MatingResult_Pregnant
|
|
|
+
|
|
|
if item.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
|
|
|
breedStatus = pasturePb.BreedStatus_Empty
|
|
|
isPregnant = pasturePb.IsShow_No
|
|
|
matingResult = pasturePb.MatingResult_Empty
|
|
|
- }
|
|
|
|
|
|
- // 更新上一次配种结果数据,如何是复检无胎则更新为流产
|
|
|
- if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond {
|
|
|
- breedStatus = pasturePb.BreedStatus_Abort
|
|
|
- matingResult = pasturePb.MatingResult_Abort
|
|
|
+ // 更新上一次配种结果数据,如何是复检无胎则更新为流产
|
|
|
+ if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond {
|
|
|
+ breedStatus = pasturePb.BreedStatus_Abort
|
|
|
+ matingResult = pasturePb.MatingResult_Abort
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
item.Cow.EventPregnantCheckUpdate(breedStatus, int64(item.PregnantCheckAt), isPregnant, item.EventPregnancyCheck.PregnantCheckName)
|
|
@@ -341,97 +342,11 @@ func (s *StoreEntry) MatingBatch(ctx context.Context, req *pasturePb.EventMating
|
|
|
// 脖环发情揭发数据
|
|
|
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 {
|
|
|
+ if err = s.NeckRingEstrusToMating(ctx, userModel, tx, item); 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)
|
|
|
- }
|
|
|
-
|
|
|
- // 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)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 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)
|
|
|
- }
|
|
|
-
|
|
|
- // 更新牛只配种结果日志
|
|
|
- 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))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 牛只基本中配种信息更新
|
|
|
- if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, isReMating); err != nil {
|
|
|
+ if err = s.NaturalEstrusToMating(ctx, userModel, tx, item); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
@@ -460,6 +375,109 @@ func (s *StoreEntry) MatingBatch(ctx context.Context, req *pasturePb.EventMating
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+// NeckRingEstrusToMating 脖环揭发发情去配种牛只
|
|
|
+func (s *StoreEntry) NeckRingEstrusToMating(ctx context.Context, userModel *model.UserModel, tx *gorm.DB, item *model.EventMatingCheckBatchModel) error {
|
|
|
+ // 新增发情事件数据
|
|
|
+ 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.Model(new(model.EventEstrus)).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)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// NaturalEstrusToMating 自然发情或者同期去配种牛只
|
|
|
+func (s *StoreEntry) NaturalEstrusToMating(ctx context.Context, userModel *model.UserModel, tx *gorm.DB, item *model.EventMatingCheckBatchModel) error {
|
|
|
+ // 获取牛只最近一次配种信息
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ } 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新牛只配种结果日志
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 牛只基本中配种信息更新
|
|
|
+ if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, isReMating); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, item *model.EventMatingCheckBatchModel, isReMating bool) error {
|
|
|
// 牛只基本中配种信息更新
|
|
|
item.Cow.EventMatingUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating)
|