|
@@ -205,27 +205,69 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- matingList := make([]*model.EventMating, 0)
|
|
|
- eventFrozenSemenLogList := make([]*model.EventFrozenSemenLog, 0)
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
|
|
|
- frozenSemen := &model.EventFrozenSemen{}
|
|
|
+ eventFrozenSemenLogList := make([]*model.FrozenSemenLog, 0)
|
|
|
+ currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ frozenSemen := &model.FrozenSemen{}
|
|
|
if err = s.DB.Where("bull_id = ?", req.BullId).First(frozenSemen).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err = tx.Create(matingList).Error; err != nil {
|
|
|
+ sameTimeCowIds := make([]int64, 0)
|
|
|
+ matingInsertList := make([]*model.EventMating, 0)
|
|
|
+ matingUpdateIds := make([]int64, 0)
|
|
|
+ nowTime := time.Now()
|
|
|
+ for _, cow := range cowList {
|
|
|
+ var count int64 = 0
|
|
|
+ itemEventMating := &model.EventMating{}
|
|
|
+ if err = s.DB.Where("lact = ?", cow.Lact).
|
|
|
+ Where("mating_at <= ").
|
|
|
+ Where("cow_id = ?", cow.Id).First(itemEventMating).Count(&count).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- for _, cow := range cowList {
|
|
|
- matingList = append(matingList, model.NewEventMating(cow, currentUser, req))
|
|
|
- eventFrozenSemenLogList = append(eventFrozenSemenLogList, model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)))
|
|
|
- if err = tx.Model(&model.SameTimeCow{}).Where("lact = ?", cow.Lact).Where("cow_id = ?", cow.Id).Updates(map[string]interface{}{
|
|
|
- "status": pasturePb.IsShow_No,
|
|
|
- "end_at": time.Now().Unix(),
|
|
|
- }).Error; err != nil {
|
|
|
+
|
|
|
+ itemBeforeTwoDays := nowTime.Sub(time.Unix(itemEventMating.MatingAt, 0)).Hours()
|
|
|
+ if count > 0 && itemBeforeTwoDays > 48 {
|
|
|
+ matingUpdateIds = append(matingUpdateIds, itemEventMating.Id)
|
|
|
+ } else {
|
|
|
+ matingInsertList = append(matingInsertList, model.NewEventMating(cow, currentUser, req))
|
|
|
+ }
|
|
|
+
|
|
|
+ eventFrozenSemenLogList = append(
|
|
|
+ eventFrozenSemenLogList,
|
|
|
+ model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)),
|
|
|
+ )
|
|
|
+ sameTimeCow := &model.SameTimeCow{}
|
|
|
+ if err = s.DB.Where("lact = ?", cow.Lact).
|
|
|
+ Where("cow_id = ?", cow.Id).
|
|
|
+ Where("status = ?", pasturePb.SameTimeStatus_In_Progress).
|
|
|
+ First(sameTimeCow).Error; err != nil {
|
|
|
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if sameTimeCow.Id > 0 {
|
|
|
+ sameTimeCowIds = append(sameTimeCowIds, sameTimeCow.Id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+
|
|
|
+
|
|
|
+ if len(matingInsertList) > 0 {
|
|
|
+ if err = tx.Create(matingInsertList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if len(matingUpdateIds) > 0 {
|
|
|
+ if err = tx.Model(new(model.EventMating)).
|
|
|
+ Where("id IN ?", matingUpdateIds).
|
|
|
+ Where("mating_result = ?", pasturePb.MatingResult_Unknown).
|
|
|
+ Update("mating_result", pasturePb.MatingResult_ReMatch).
|
|
|
+ Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
@@ -235,9 +277,21 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err = tx.Table(new(model.EventFrozenSemen).TableName()).
|
|
|
+
|
|
|
+ if len(sameTimeCowIds) > 0 {
|
|
|
+ if err = tx.Table(new(model.SameTimeCow).TableName()).
|
|
|
+ Where("id IN ?", sameTimeCowIds).
|
|
|
+ UpdateColumn("status", pasturePb.SameTimeStatus_End).Error; err != nil {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if err = tx.Table(new(model.FrozenSemen).TableName()).
|
|
|
Where("bull_id = ?", req.BullId).
|
|
|
- Where("quantity > 0").UpdateColumn("quantity", gorm.Expr("quantity - ?", len(matingList))).Error; err != nil {
|
|
|
+ Where("quantity > 0").
|
|
|
+ UpdateColumn("quantity",
|
|
|
+ gorm.Expr("quantity - ?", len(matingUpdateIds)+len(matingInsertList)),
|
|
|
+ ).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
@@ -300,9 +354,9 @@ func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstru
|
|
|
}
|
|
|
|
|
|
func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
|
|
|
- frozenSemenList := make([]*model.EventFrozenSemen, 0)
|
|
|
+ frozenSemenList := make([]*model.FrozenSemen, 0)
|
|
|
var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventFrozenSemen).TableName())
|
|
|
+ pref := s.DB.Table(new(model.FrozenSemen).TableName())
|
|
|
if req.BullId != "" {
|
|
|
pref.Where("bull_id = ?", req.BullId)
|
|
|
}
|
|
@@ -325,7 +379,7 @@ func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenS
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
|
Data: &pasturePb.SearchFrozenSemenData{
|
|
|
- List: model.EventFrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
|
|
|
+ List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
|
|
|
Total: int32(count),
|
|
|
PageSize: pagination.PageSize,
|
|
|
Page: pagination.Page,
|
|
@@ -336,7 +390,7 @@ func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenS
|
|
|
func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
|
|
|
currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
req.CowKindName = s.CowKindMap()[req.CowKind]
|
|
|
- newFrozenSemen := model.NewEventFrozenSemen(req, currentUser)
|
|
|
+ newFrozenSemen := model.NewFrozenSemen(req, currentUser)
|
|
|
if err := s.DB.Create(newFrozenSemen).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|