|
@@ -406,6 +406,7 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.MatingEven
|
|
|
}
|
|
|
cowList := strings.Split(req.CowId, ",")
|
|
|
matingList := make([]*model.EventMating, 0)
|
|
|
+ eventFrozenSemenLogList := make([]*model.EventFrozenSemenLog, 0)
|
|
|
currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
|
|
|
for _, v := range cowList {
|
|
@@ -415,11 +416,34 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.MatingEven
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
matingList = append(matingList, model.NewEventMating(cow, currentUser, req))
|
|
|
+ eventFrozenSemenLogList = append(eventFrozenSemenLogList, model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)))
|
|
|
}
|
|
|
|
|
|
- if err := s.DB.Create(matingList).Error; err != nil {
|
|
|
+ frozenSemen := &model.EventFrozenSemen{}
|
|
|
+ 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 {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建冻精使用记录日志
|
|
|
+ if err := tx.Create(eventFrozenSemenLogList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := tx.Table(new(model.EventFrozenSemen).TableName()).
|
|
|
+ Where("bull_id = ?", req.BullId).
|
|
|
+ Where("quantity > 0").UpdateColumn("quantity", gorm.Expr("quantity - ?", len(matingList))).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -457,7 +481,7 @@ func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EstrusEven
|
|
|
return xerr.Custom("请选择相关牛只")
|
|
|
}
|
|
|
cowList := strings.Split(req.CowId, ",")
|
|
|
- estruList := make([]*model.EventEstrus, 0)
|
|
|
+ estrusList := make([]*model.EventEstrus, 0)
|
|
|
currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
|
|
|
for _, v := range cowList {
|
|
@@ -466,10 +490,53 @@ func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EstrusEven
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- estruList = append(estruList, model.NewEventEstrus(cow, currentUser, req))
|
|
|
+ estrusList = append(estrusList, model.NewEventEstrus(cow, currentUser, req))
|
|
|
}
|
|
|
|
|
|
- if err := s.DB.Create(estruList).Error; err != nil {
|
|
|
+ if err := s.DB.Create(estrusList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
|
|
|
+ frozenSemenList := make([]*model.EventFrozenSemen, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Table(new(model.EventFrozenSemen).TableName())
|
|
|
+ if req.BullId != "" {
|
|
|
+ pref.Where("bull_id = ?", req.BullId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.Producer != "" {
|
|
|
+ pref.Where("producer = ?", req.Producer)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Order("id desc").
|
|
|
+ Count(&count).Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Find(&frozenSemenList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ frozenSemenTypeMap := s.FrozenSemenTypeMap()
|
|
|
+ unitMap := s.UnitMap()
|
|
|
+
|
|
|
+ return &pasturePb.FrozenSemenResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SearchFrozenSemenData{
|
|
|
+ List: model.EventFrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
|
|
|
+ currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
+ newFrozenSemen := model.NewEventFrozenSemen(req, currentUser)
|
|
|
+ if err := s.DB.Create(newFrozenSemen).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|