|
@@ -477,81 +477,85 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+
|
|
|
req.OperationName = eventCheckModel.OperationUser.Name
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
for _, cow := range eventCheckModel.CowList {
|
|
|
+ // 获取牛只最近一次配种信息
|
|
|
lastEventMating, ok, err := s.FindLastEventMatingByCowId(ctx, eventCheckModel.CurrentUser.PastureId, cow.Id)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- var (
|
|
|
- newMating *model.EventMating
|
|
|
- isReMating = false
|
|
|
- isEmptyMating = false
|
|
|
- )
|
|
|
-
|
|
|
- // 1. 自然发情牛只,未经过同期的牛只和未佩戴脖环的牛只
|
|
|
+ // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
|
|
|
if !ok {
|
|
|
- newMating = model.NewEventMatingNaturalEstrus(cow, req, eventCheckModel.CurrentUser)
|
|
|
+ newMating := model.NewEventMatingNaturalEstrus(cow, req, eventCheckModel.CurrentUser)
|
|
|
if err = tx.Create(newMating).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- } else {
|
|
|
- // 2. 所有有配种数据的牛只
|
|
|
- if lastEventMating == nil || lastEventMating.Id <= 0 {
|
|
|
- zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("CurrentUser", eventCheckModel.CurrentUser))
|
|
|
- continue
|
|
|
+ if err = s.MatingCowUpdate(ctx, cow, eventCheckModel.CurrentUser, req, false); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 所有有配种数据的牛只
|
|
|
+ if lastEventMating == nil || lastEventMating.Id <= 0 {
|
|
|
+ zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("CurrentUser", eventCheckModel.CurrentUser))
|
|
|
+ return xerr.Customf("异常牛数据: %d", cow.Id)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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 {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.2. 同期初配
|
|
|
+ IsMatingUpdate := lastEventMating.IsMatingUpdate()
|
|
|
+ if IsMatingUpdate {
|
|
|
+ lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
+ 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)
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 复配 => 本胎次配次不加1
|
|
|
- isReMating = lastEventMating.IsReMating(cow, int64(req.MatingAt))
|
|
|
- // 空怀
|
|
|
- isEmptyMating = lastEventMating.IsEmptyMating(cow, int64(req.MatingAt))
|
|
|
-
|
|
|
- // 2。1. 提交的配种数据中,定位出空怀的牛只,
|
|
|
- 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(eventCheckModel.CurrentUser.PastureId, cow, int64(req.MatingAt), req.ExposeEstrusType)
|
|
|
- newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
- 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))
|
|
|
- }
|
|
|
+ // 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)
|
|
|
}
|
|
|
- // 2.2. 同期初配
|
|
|
- if !isEmptyMating && !isReMating {
|
|
|
- lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
- 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)
|
|
|
- }
|
|
|
+ // 先创建一条新的配种数据
|
|
|
+ newMating := model.NewEventMating(eventCheckModel.CurrentUser.PastureId, cow, int64(req.MatingAt), req.ExposeEstrusType)
|
|
|
+ newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
+ 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))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 牛只基本中配种信息更新
|
|
|
- cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
|
|
|
- if err = tx.Model(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 {
|
|
|
+ if err = s.MatingCowUpdate(ctx, cow, eventCheckModel.CurrentUser, req, isReMating); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 记录日志
|
|
|
- cowLogs := s.SubmitEventLog(ctx, eventCheckModel.CurrentUser, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
- tx.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
}
|
|
|
|
|
|
// 创建冻精使用记录日志
|
|
@@ -576,6 +580,23 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (s *StoreEntry) MatingCowUpdate(ctx context.Context, cow *model.Cow, currentUser *model.SystemUser, req *pasturePb.EventMating, isReMating bool) error {
|
|
|
+ // 牛只基本中配种信息更新
|
|
|
+ cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
|
|
|
+ if err := s.DB.Model(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 {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 记录日志
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
+ if err := s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeaningBatchRequest) (err error) {
|
|
|
currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|