Browse Source

feed: 增加修改记录

Yi 1 year ago
parent
commit
10f5224fe9
1 changed files with 42 additions and 4 deletions
  1. 42 4
      module/backend/feed_service.go

+ 42 - 4
module/backend/feed_service.go

@@ -75,7 +75,6 @@ func (s *StoreEntry) EditFeedFormula(ctx context.Context, req *operationPb.AddFe
 		Updates(updateData).Error; err != nil {
 		return xerr.WithStack(err)
 	}
-
 	return nil
 }
 
@@ -86,6 +85,8 @@ func (s *StoreEntry) AddFeedByFeedFormula(ctx context.Context, req *operationPb.
 		return xerr.WithStack(err)
 	}
 
+	defer s.addFeedFormulaDetailAddRecode(ctx, req)
+
 	insertData := make([]*model.FeedFormulaDetail, 0)
 	for _, v := range req.List {
 		feedData := &model.Feed{Id: int64(v.ForageId)}
@@ -116,6 +117,23 @@ func (s *StoreEntry) AddFeedByFeedFormula(ctx context.Context, req *operationPb.
 	return nil
 }
 
+// addFeedFormulaDetailAddRecode 添加配方记录
+func (s *StoreEntry) addFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) {
+	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
+	for _, v := range req.List {
+		editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
+			FeedFormulaId: int64(req.FeedFormulaId),
+			PastureName:   "集团",
+			ForageName:    v.ForageName,
+			Status:        operationPb.FeedFormulaEditRecordType_INSERT,
+		})
+	}
+
+	if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
+		zaplog.Error("deleteFeedFormulaDetailAddRecode", zap.Any("CreateFeedFormulaEditRecord", err))
+	}
+}
+
 // EditFeedByFeedFormula 配方饲料编辑
 func (s *StoreEntry) EditFeedByFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaDetail) error {
 	feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
@@ -129,7 +147,7 @@ func (s *StoreEntry) EditFeedByFeedFormula(ctx context.Context, req *operationPb
 		return xerr.WithStack(err)
 	}
 	// 添加修改记录
-	defer s.EditFeedFormulaDetailAddRecode(ctx, req, feedFormulaDetail)
+	defer s.editFeedFormulaDetailAddRecode(ctx, req, feedFormulaDetail)
 
 	// 更新数据
 	updateData := &model.FeedFormulaDetail{
@@ -149,7 +167,7 @@ func (s *StoreEntry) EditFeedByFeedFormula(ctx context.Context, req *operationPb
 }
 
 // EditFeedFormulaDetailAddRecode 更新饲料配方修改记录
-func (s *StoreEntry) EditFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.AddFeedFormulaDetail, feedFormulaDetail *model.FeedFormulaDetail) {
+func (s *StoreEntry) editFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.AddFeedFormulaDetail, feedFormulaDetail *model.FeedFormulaDetail) {
 	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
 
 	editRecordData := &model.FeedFormulaEditRecord{
@@ -217,7 +235,7 @@ func (s *StoreEntry) CreateFeedFormulaEditRecord(ctx context.Context, req []*mod
 	if req == nil {
 		return nil
 	}
-	if err := s.DB.Model(new(model.FeedFormulaDetail)).Save(req).Error; err != nil {
+	if err := s.DB.Model(new(model.FeedFormulaEditRecord)).Save(req).Error; err != nil {
 		return xerr.WithStack(err)
 	}
 	return nil
@@ -252,6 +270,9 @@ func (s *StoreEntry) DeleteFeedFormulaDetail(ctx context.Context, req *operation
 		return xerr.WithStack(err)
 	}
 
+	// 增加配方记录
+	defer s.deleteFeedFormulaDetailAddRecode(ctx, req)
+
 	tr := s.DB.Transaction(func(tx *gorm.DB) error {
 		for _, v := range req.List {
 			if err := tx.Model(new(model.FeedFormulaDetail)).
@@ -267,6 +288,23 @@ func (s *StoreEntry) DeleteFeedFormulaDetail(ctx context.Context, req *operation
 	return tr
 }
 
+// deleteFeedFormulaDetailAddRecode 删除配方增加修改记录
+func (s *StoreEntry) deleteFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) {
+	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
+	for _, v := range req.List {
+		editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
+			FeedFormulaId: int64(req.FeedFormulaId),
+			PastureName:   "集团",
+			ForageName:    v.ForageName,
+			Status:        operationPb.FeedFormulaEditRecordType_DELETE,
+		})
+	}
+
+	if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
+		zaplog.Error("deleteFeedFormulaDetailAddRecode", zap.Any("CreateFeedFormulaEditRecord", err))
+	}
+}
+
 // SearchFeedFormulaDetail 查询配方饲料详情
 func (s *StoreEntry) SearchFeedFormulaDetail(ctx context.Context, req *operationPb.AddFeedFormulaDetail) (*operationPb.FeedFormulaDetailResponse, error) {
 	feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}