Browse Source

feed_edit: 配方详情

Yi 1 year ago
parent
commit
d7d826baf6
4 changed files with 45 additions and 24 deletions
  1. 6 0
      http/handler/feed/feed_formula.go
  2. 3 2
      model/feed_formula.go
  3. 3 2
      model/pasture_data.go
  4. 33 20
      module/backend/feed_service.go

+ 6 - 0
http/handler/feed/feed_formula.go

@@ -211,6 +211,12 @@ func SearchFeedDetail(c *gin.Context) {
 		return
 	}
 
+	req.Pagination = &operationPb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
 	if list, err := middleware.BackendOperation(c).OpsService.SearchFeedFormulaDetail(c, &req); err != nil {
 		apierr.ClassifiedAbort(c, err)
 		return

+ 3 - 2
model/feed_formula.go

@@ -82,6 +82,7 @@ func (f FeedFormulaSlice) ToPB() []*operationPb.AddFeedFormulaRequest {
 }
 
 type DistributeData struct {
-	PastureList     []*GroupPasture
-	FeedFormulaList []*FeedFormula
+	PastureList       []*GroupPasture
+	FeedFormulaList   []*FeedFormula
+	FeedFormulaDetail []*FeedFormulaDetail
 }

+ 3 - 2
model/pasture_data.go

@@ -1,8 +1,9 @@
 package model
 
 type DistributeFeedFormulaRequest struct {
-	PastureId int64          `json:"pasture_id"`
-	Body      []*FeedFormula `json:"body"`
+	PastureId         int64                `json:"pasture_id"`
+	FeedFormula       []*FeedFormula       `json:"feed_formula"`
+	FeedFormulaDetail []*FeedFormulaDetail `json:"feed_formula_detail"`
 }
 
 type CancelDistributeFeedFormulaRequest struct {

+ 33 - 20
module/backend/feed_service.go

@@ -322,14 +322,17 @@ func (s *StoreEntry) deleteFeedFormulaDetailAddRecode(ctx context.Context, req *
 // SearchFeedFormulaDetail 查询配方饲料详情
 func (s *StoreEntry) SearchFeedFormulaDetail(ctx context.Context, req *operationPb.AddFeedFormulaDetail) (*operationPb.FeedFormulaDetailResponse, error) {
 	feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
-	if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
+	if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Debug().Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
 	var count int64
-	feedFormulaDetail := make([]*model.FeedFormulaDetail, 0)
+	feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
+
+	pref := s.DB.Model(new(model.FeedFormulaDetail)).
+		Where("is_show = ?", operationPb.IsShow_OK).
+		Where("feed_formula_id = ?", req.FeedFormulaId)
 
-	pref := s.DB.Model(new(model.FeedFormulaDetail)).Where("is_show = ?", operationPb.IsShow_OK).Where("feed_formula_id = ?", req.FeedFormulaId)
 	if req.ForageName != "" {
 		pref.Where("forage_name = ?", req.ForageName)
 	}
@@ -346,19 +349,23 @@ func (s *StoreEntry) SearchFeedFormulaDetail(ctx context.Context, req *operation
 		pref.Where("is_lock_cow_count_ratio = ?", req.IsLockCowCountRatio)
 	}
 
+	if req.StirDelay > 0 {
+		pref.Where("stir_delay = ?", req.StirDelay)
+	}
+
 	if req.Sort > 0 {
 		pref.Where("sort = ?", req.Sort)
 	}
 
-	if err := pref.Order("sort").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
-		Find(&feedFormulaDetail).Error; err != nil {
+	if err := pref.Order("sort").Count(&count).Limit(int(req.Pagination.PageSize)).
+		Offset(int(req.Pagination.PageOffset)).Find(&feedFormulaDetailList).Debug().Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
 	return &operationPb.FeedFormulaDetailResponse{
 		Code: http.StatusOK,
 		Msg:  "ok",
-		Data: model.FeedFormulaDetailSlice(feedFormulaDetail).ToPB(),
+		Data: model.FeedFormulaDetailSlice(feedFormulaDetailList).ToPB(),
 	}, nil
 
 }
@@ -698,20 +705,11 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 	for _, pasture := range distributeData.PastureList {
 		go func(p *model.GroupPasture) {
 			defer wg.Done()
-			// 过滤已下发的
-			body := make([]*model.FeedFormula, 0)
-			for _, v := range distributeData.FeedFormulaList {
-				if ok := s.checkoutDistributeLog(ctx, p.Id, v.Id); !ok {
-					body = append(body, v)
-				}
-			}
-			if len(body) <= 0 {
-				return
-			}
 
 			request := &model.DistributeFeedFormulaRequest{
-				PastureId: p.Id,
-				Body:      body,
+				PastureId:         p.Id,
+				FeedFormula:       distributeData.FeedFormulaList,
+				FeedFormulaDetail: distributeData.FeedFormulaDetail,
 			}
 			response := &model.PastureResponse{}
 			defer func() {
@@ -971,8 +969,9 @@ func (s *StoreEntry) PastureFeedFormulaIsModify(ctx context.Context, feedFormula
 
 func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) (*model.DistributeData, error) {
 	result := &model.DistributeData{
-		PastureList:     make([]*model.GroupPasture, 0),
-		FeedFormulaList: make([]*model.FeedFormula, 0),
+		PastureList:       make([]*model.GroupPasture, 0),
+		FeedFormulaList:   make([]*model.FeedFormula, 0),
+		FeedFormulaDetail: make([]*model.FeedFormulaDetail, 0),
 	}
 
 	if err := s.DB.Where("id IN ?", req.PastureIds).Where("is_delete = ?", operationPb.IsShow_OK).Find(&result.PastureList).Error; err != nil {
@@ -983,6 +982,20 @@ func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationP
 		return result, xerr.WithStack(err)
 	}
 
+	feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
+	for _, v := range req.FeedFormulaIds {
+		feedFormulaDetail := make([]*model.FeedFormulaDetail, 0)
+		if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("feed_formula_id = ?", v).Find(&feedFormulaDetail).Error; err != nil {
+			if errors.Is(err, gorm.ErrRecordNotFound) {
+				return result, xerr.Customf("该配方没有添加相关饲料:%d", v)
+			}
+			return result, xerr.WithStack(err)
+		} else {
+			feedFormulaDetailList = append(feedFormulaDetailList, feedFormulaDetail...)
+		}
+	}
+
+	result.FeedFormulaDetail = feedFormulaDetailList
 	if len(result.PastureList) <= 0 || len(result.FeedFormulaList) <= 0 {
 		return result, xerr.Customf("数据错误")
 	}