|
@@ -77,7 +77,7 @@ func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRe
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterRequest) error {
|
|
|
+func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterRequest) (err error) {
|
|
|
userModel, err := s.GetUserModel(ctx)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -91,26 +91,31 @@ func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterR
|
|
|
}
|
|
|
|
|
|
penMap := s.PenMap(ctx, userModel.AppPasture.Id)
|
|
|
+ newCow := model.NewCow(userModel.AppPasture.Id, req, penMap)
|
|
|
+ defer func() {
|
|
|
+ if err == nil {
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, newCow, pasturePb.EventType_Enter, pasturePb.ExposeEstrusType_Invalid, req)
|
|
|
+ s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
+ }
|
|
|
+ }()
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- newCowData := model.NewCow(userModel.AppPasture.Id, req, penMap)
|
|
|
- if err = tx.Create(newCowData).Error; err != nil {
|
|
|
+ if err = tx.Create(newCow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- newEventEnter := model.NewEventEnter(userModel.AppPasture.Id, newCowData.Id, req)
|
|
|
+ newEventEnter := model.NewEventEnter(userModel.AppPasture.Id, newCow.Id, req)
|
|
|
if err = tx.Create(newEventEnter).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
eventWeight := model.NewEventWeight(
|
|
|
userModel.AppPasture.Id,
|
|
|
- newCowData,
|
|
|
+ newCow,
|
|
|
userModel.SystemUser,
|
|
|
- int32(req.Weight*1000),
|
|
|
- 0,
|
|
|
&pasturePb.EventWeight{
|
|
|
WeightAt: req.EnterAt,
|
|
|
Remarks: "入场体重",
|
|
|
OperationId: req.OperationId,
|
|
|
OperationName: req.OperationName,
|
|
|
+ Weight: req.Weight,
|
|
|
})
|
|
|
if err = tx.Create(eventWeight).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -126,9 +131,9 @@ func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.Searc
|
|
|
eventGroupTransferList := make([]*pasturePb.EventTransferGroupData, 0)
|
|
|
var count int64 = 0
|
|
|
pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventTransferGroup).TableName())).
|
|
|
- Select(`a.id,a.cow_id,a.pen_in_id as transfer_in_pen_id,a.pen_out_id as transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,a.transfer_reason_name,
|
|
|
- a.transfer_date,a.created_at,a.operation_id,a.operation_name,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
|
|
|
- f.lact,f.ear_number`).
|
|
|
+ Select(`a.id,a.cow_id,a.pen_in_id as transfer_in_pen_id,a.pen_out_id as transfer_out_pen_id,a.lact,a.remarks,
|
|
|
+ a.transfer_reason_id,a.transfer_reason_name,a.transfer_date,a.created_at,a.operation_id,a.operation_name,
|
|
|
+ b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,f.lact,f.ear_number`).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS b ON a.pen_in_id = b.id", new(model.Pen).TableName())).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS c on a.pen_out_id = c.id", new(model.Pen).TableName())).
|
|
|
Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = f.id", new(model.Cow).TableName()))
|
|
@@ -188,7 +193,7 @@ func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.Tra
|
|
|
}
|
|
|
|
|
|
defer func() {
|
|
|
- if err != nil {
|
|
|
+ if err == nil {
|
|
|
for _, etg := range newEventTransferGroupModelList {
|
|
|
cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, etg.Cow, pasturePb.EventType_Transfer_Ben, pasturePb.ExposeEstrusType_Invalid, etg.EventTransferGroup)
|
|
|
s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
@@ -266,6 +271,7 @@ func (s *StoreEntry) CreateBodyScore(ctx context.Context, req *pasturePb.BodySco
|
|
|
if len(bodyScourEvent) <= 0 {
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
return s.DB.Create(bodyScourEvent).Error
|
|
|
}
|
|
|
|
|
@@ -299,9 +305,9 @@ func (s *StoreEntry) WeightList(ctx context.Context, req *pasturePb.SearchEventR
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight) (err error) {
|
|
|
- if len(req.WeightItems) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
+func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.BatchEventWeight) (err error) {
|
|
|
+ if len(req.Items) <= 0 {
|
|
|
+ return xerr.Custom("称重数据不能为空")
|
|
|
}
|
|
|
|
|
|
userModel, err := s.GetUserModel(ctx)
|
|
@@ -309,32 +315,25 @@ func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- req.OperationName = operationUser.Name
|
|
|
-
|
|
|
defer func() {
|
|
|
if err == nil {
|
|
|
// 记录事件日志
|
|
|
- for _, item := range req.WeightItems {
|
|
|
+ for _, item := range req.Items {
|
|
|
cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
|
|
|
- cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Weight, pasturePb.ExposeEstrusType_Invalid, req)
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Weight, pasturePb.ExposeEstrusType_Invalid, item)
|
|
|
s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
}
|
|
|
}
|
|
|
}()
|
|
|
cow := &model.Cow{}
|
|
|
- for _, item := range req.WeightItems {
|
|
|
+ for _, item := range req.Items {
|
|
|
cow, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- item.WeightAt = req.WeightAt
|
|
|
|
|
|
// 更新牛只信息
|
|
|
- cow.EventWeightUpdate(int64(item.Weight), int64(req.WeightAt))
|
|
|
+ cow.EventWeightUpdate(int64(item.Weight), int64(item.WeightAt))
|
|
|
if err = s.DB.Model(new(model.Cow)).
|
|
|
Select("last_second_weight_at", "last_second_weight", "last_weight_at", "current_weight").
|
|
|
Where("id = ?", cow.Id).
|
|
@@ -342,9 +341,10 @@ func (s *StoreEntry) WeightBatch(ctx context.Context, req *pasturePb.EventWeight
|
|
|
Updates(cow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
+ operationUser, _ := s.GetSystemUserById(ctx, int64(item.OperationId))
|
|
|
+ item.OperationName = operationUser.Name
|
|
|
// 创建牛只的体重记录
|
|
|
- eventWeight := model.NewEventWeight(userModel.AppPasture.Id, cow, userModel.SystemUser, int32(item.Weight*1000), item.Height, req)
|
|
|
+ eventWeight := model.NewEventWeight(userModel.AppPasture.Id, cow, userModel.SystemUser, item)
|
|
|
if err = s.DB.Create(eventWeight).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|