|
@@ -60,21 +60,29 @@ func (s *StoreEntry) CalvingList(ctx context.Context, req *pasturePb.SearchEvent
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalving) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
+func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalving) (err error) {
|
|
|
+ cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("CalvingCreate", zap.Any("cow_id", req.CowId), zap.Any("err", err))
|
|
|
return xerr.Custom("请选择相关牛只")
|
|
|
}
|
|
|
|
|
|
- cowList, err := s.ParseCowIds(ctx, req.CowId)
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ return xerr.Customf("获取操作人员信息失败: %s", err.Error())
|
|
|
}
|
|
|
+ req.OperationName = operationUser.Name
|
|
|
|
|
|
- if len(cowList) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
+ // 记录牛只事件日志
|
|
|
+ defer func() {
|
|
|
+ if err == nil {
|
|
|
+ // 母牛事件日志
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, cow, pasturePb.EventType_Calving, req)
|
|
|
+ s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
+ // todo 犊牛的
|
|
|
+ }
|
|
|
+ }()
|
|
|
|
|
|
- cow := cowList[0]
|
|
|
newEventCalving := &model.EventCalving{}
|
|
|
if err = s.DB.Model(new(model.EventCalving)).
|
|
|
Where("cow_id = ?", cow.Id).
|
|
@@ -82,44 +90,39 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
First(newEventCalving).Error; err != nil {
|
|
|
return xerr.Custom("该母牛信息不存在")
|
|
|
}
|
|
|
-
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- // 母牛信息
|
|
|
+ // 更新产犊事件表
|
|
|
+ newEventCalving.Update(operationUser, req, cow)
|
|
|
if err = tx.Model(new(model.EventCalving)).
|
|
|
Where("id = ?", newEventCalving.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "reality_day": int64(req.CalvingAt),
|
|
|
- "day_age": cow.DayAge,
|
|
|
- "lact": cow.Lact + 1,
|
|
|
- "pregnancy_age": cow.PregnancyAge,
|
|
|
- "calving_level": req.CalvingLevel,
|
|
|
- "bull_number": cow.LastBullNumber,
|
|
|
- "child_number": len(req.CalfItemList),
|
|
|
- }).
|
|
|
- Error; err != nil {
|
|
|
+ Updates(newEventCalving).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 犊牛信息
|
|
|
- newCalvingCalfList := model.NewEventCalvingCalf(cow.Id, newEventCalving.Id, req)
|
|
|
- for _, v := range newCalvingCalfList {
|
|
|
+
|
|
|
+ for _, v := range req.CalfItemList {
|
|
|
if v.IsLive == pasturePb.IsShow_No || v.IsAdoption == pasturePb.IsShow_No {
|
|
|
continue
|
|
|
}
|
|
|
+ // 犊牛信息
|
|
|
+ newCalvingCalf := model.NewEventCalvingCalf(int64(req.CowId), newEventCalving.Id, int64(req.CalvingAt), v)
|
|
|
// 留养犊牛
|
|
|
- newCow := model.NewCalfCow(cow.Id, cow.LastBullNumber, v)
|
|
|
+ newCow := model.NewCalfCow(cow.Id, cow.LastBullNumber, newCalvingCalf)
|
|
|
if err = tx.Create(newCow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- v.CowId = newCow.Id
|
|
|
- }
|
|
|
- if err = tx.Create(newCalvingCalfList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+
|
|
|
+ v.CowId = int32(newCow.Id)
|
|
|
+ if err = tx.Create(newCalvingCalf).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ // 更新母牛信息
|
|
|
if err = tx.Model(new(model.Cow)).Where("id = ?", cow.Id).
|
|
|
Updates(map[string]interface{}{
|
|
|
- "calving_age": 0,
|
|
|
+ "calving_age": int32(time.Now().Sub(time.Unix(int64(req.CalvingAt), 0)).Hours() / 24),
|
|
|
"mating_times": 0,
|
|
|
+ "pregnancy_age": 0,
|
|
|
"lact": cow.Lact + 1,
|
|
|
"breed_status": pasturePb.BreedStatus_Calving,
|
|
|
"is_pregnant": pasturePb.IsShow_No,
|