Переглянути джерело

feedformula: 配方更新同步牧场版本号

Yi 1 рік тому
батько
коміт
5a0296bc63

+ 18 - 0
model/feed_formula.go

@@ -86,3 +86,21 @@ type DistributeData struct {
 	FeedFormulaList   []*FeedFormula
 	FeedFormulaDetail []*FeedFormulaDetail
 }
+
+func NewNewFeedFormulaByMixed(req *operationPb.MixedFeedFormulaRequest) *FeedFormula {
+	return &FeedFormula{
+		Name:               req.Name,
+		Colour:             req.Colour,
+		CattleCategoryId:   operationPb.CattleCategoryParent_Kind(req.CattleCategoryId),
+		CattleCategoryName: req.CattleCategoryName,
+		FormulaTypeId:      req.FormulaTypeId,
+		FormulaTypeName:    req.FormulaTypeName,
+		DataSourceId:       operationPb.DataSource_Kind(req.DataSourceId),
+		DataSourceName:     operationPb.DataSource_Kind_name[req.DataSourceId],
+		Remarks:            req.Remarks,
+		PastureName:        "集团",
+		IsShow:             operationPb.IsShow_OK,
+		IsModify:           operationPb.IsShow_OK,
+		IsDelete:           operationPb.IsShow_OK,
+	}
+}

+ 1 - 0
model/group_pasture.go

@@ -40,6 +40,7 @@ const (
 	CattleCategoryDeleteURl        = "pasture/cattle_category/delete"
 	ForageCategoryDeleteURl        = "pasture/cattle_category/delete"
 	FeedUsageURl                   = "pasture/feed/usage"
+	FeedFormulaVersionUpdateUrl    = "pasture/feed_formula/version"
 )
 
 var (

+ 14 - 0
model/pasture_data.go

@@ -202,3 +202,17 @@ type FeedTemplateDetail struct {
 	SplitFtPreId   int64   `json:"split_ft_pre_id"`
 	Deviation      int32   `json:"deviation"`
 }
+
+type FeedFormulaUpdateVersionRequest struct {
+	FeedTemplateId int64 `json:"feed_template_id"` // 配方id
+	Version        int64 `json:"version"`          // 版本号
+	Belong         int32 `json:"belong"`           // 配方归属  0 无效 1 归属集团端 2 归属牧场端
+}
+
+type FeedFormulaUpdateVersionResponse struct {
+	Code int32  `json:"code"`
+	Msg  string `json:"msg"`
+	Data struct {
+		Success bool `json:"success"`
+	} `json:"data"`
+}

+ 15 - 0
module/backend/edit_record_service.go

@@ -0,0 +1,15 @@
+package backend
+
+import (
+	"context"
+	"kpt-tmr-group/model"
+	"kpt-tmr-group/pkg/xerr"
+)
+
+func (s *StoreEntry) GetEditRecordLastGroupId(ctx context.Context) (*model.FeedFormulaEditRecord, error) {
+	editRecord := &model.FeedFormulaEditRecord{}
+	if err := s.DB.Model(new(model.FeedFormulaEditRecord)).Order("group_id desc").Take(editRecord).Error; err != nil {
+		return editRecord, xerr.WithStack(err)
+	}
+	return editRecord, nil
+}

+ 77 - 18
module/backend/feed_service.go

@@ -116,12 +116,12 @@ func (s *StoreEntry) AddFeedByFeedFormula(ctx context.Context, req *operationPb.
 	if err := s.DB.Model(new(model.FeedFormulaDetail)).Save(insertData).Error; err != nil {
 		return xerr.WithStack(err)
 	}
-
 	return nil
 }
 
 // addFeedFormulaDetailAddRecode 添加配方记录
 func (s *StoreEntry) addFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) {
+	editRecord, _ := s.GetEditRecordLastGroupId(ctx)
 	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
 	for _, v := range req.List {
 		editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
@@ -129,6 +129,7 @@ func (s *StoreEntry) addFeedFormulaDetailAddRecode(ctx context.Context, req *ope
 			PastureName:   "集团",
 			ForageName:    v.ForageName,
 			Status:        operationPb.FeedFormulaEditRecordType_INSERT,
+			GroupId:       editRecord.GroupId + 1,
 		})
 	}
 
@@ -390,22 +391,9 @@ func (s *StoreEntry) MixedFeedFormula(ctx context.Context, req *operationPb.Mixe
 	}
 
 	tr := s.DB.Transaction(func(tx *gorm.DB) error {
-		newFeedFormulaData := &model.FeedFormula{
-			Name:               req.Name,
-			Colour:             req.Colour,
-			EncodeNumber:       s.EncodeNumber(ctx),
-			CattleCategoryId:   operationPb.CattleCategoryParent_Kind(req.CattleCategoryId),
-			CattleCategoryName: req.CattleCategoryName,
-			FormulaTypeId:      req.FormulaTypeId,
-			FormulaTypeName:    req.FormulaTypeName,
-			DataSourceId:       operationPb.DataSource_Kind(req.DataSourceId),
-			DataSourceName:     operationPb.DataSource_Kind_name[req.DataSourceId],
-			Remarks:            req.Remarks,
-			PastureName:        "集团",
-			IsShow:             operationPb.IsShow_OK,
-			IsModify:           operationPb.IsShow_OK,
-			IsDelete:           operationPb.IsShow_OK,
-		}
+		newFeedFormulaData := model.NewNewFeedFormulaByMixed(req)
+		newFeedFormulaData.EncodeNumber = s.EncodeNumber(ctx)
+
 		if err := s.DB.Model(new(model.FeedFormula)).Create(newFeedFormulaData).Error; err != nil {
 			return xerr.WithStack(err)
 		}
@@ -1043,8 +1031,79 @@ func (s *StoreEntry) SearchFeedFormalDetailById(ctx context.Context, feedFormula
 	return res, nil
 }
 
+// UpdateFeedFormalVersion 更新版本库并通知牧场端
 func (s *StoreEntry) UpdateFeedFormalVersion(ctx context.Context, req *model.FeedFormula) {
 	if err := s.DB.Model(req).UpdateColumn("version", gorm.Expr("version + ?", 1)).Error; err != nil {
-		zaplog.Error("UpdateFeedFormalVersion", zap.Any("err", err))
+		zaplog.Error("UpdateFeedFormalVersion-UpdateColumn", zap.Any("err", err))
+	}
+
+	// 获取该配方下发记录表
+	feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)
+	if err := s.DB.Table(new(model.FeedFormulaDistributeLog).TableName()).
+		Where("feed_formula_id = ?", req.Id).
+		Where("is_show = ?", operationPb.IsShow_OK).
+		Group("pasture_id").
+		Find(&feedFormulaDistributeLogList).Debug().Error; err != nil {
+		if !errors.Is(err, gorm.ErrRecordNotFound) {
+			zaplog.Error("UpdateFeedFormalVersion-feedFormulaDistributeLog", zap.Any("err", err))
+		}
+		return
+	}
+	if len(feedFormulaDistributeLogList) > 0 {
+		wg := sync.WaitGroup{}
+		wg.Add(len(feedFormulaDistributeLogList))
+		for _, v := range feedFormulaDistributeLogList {
+			go func(feedFormulaDistributeLog *model.FeedFormulaDistributeLog) {
+				defer wg.Done()
+				// 更新牧场端配方版本
+				s.UpdatePastureFeedDetailVersionLog(ctx, feedFormulaDistributeLog, req)
+			}(v)
+		}
+		wg.Wait()
+	}
+}
+
+func (s *StoreEntry) UpdatePastureFeedDetailVersionLog(ctx context.Context, distributeLog *model.FeedFormulaDistributeLog, req *model.FeedFormula) {
+	pastureId := distributeLog.PastureId
+	groupPasture, err := s.GetGroupPastureById(ctx, pastureId)
+	if err != nil {
+		zaplog.Error("UpdateFeedFormalVersion", zap.Any("GetGroupPastureById", pastureId), zap.Any("err", err))
+		return
+	}
+	if groupPasture.IsDistribution != operationPb.IsShow_OK {
+		return
+	}
+	var (
+		belong         int32 = 1
+		feedTemplateId       = req.Id
+	)
+
+	if req.PastureDataId > 0 {
+		belong = 2
+		feedTemplateId = req.PastureDataId
+	}
+
+	response := &model.FeedFormulaUpdateVersionResponse{}
+	body := &model.FeedFormulaUpdateVersionRequest{
+		FeedTemplateId: feedTemplateId,
+		Version:        req.Version,
+		Belong:         belong,
+	}
+
+	zaplog.Info("UpdateFeedFormalVersion", zap.Any("body", body))
+	if _, err = s.PastureHttpClient(ctx, model.FeedFormulaVersionUpdateUrl, pastureId, body, response); err != nil {
+		zaplog.Error("UpdateFeedFormalVersion-http",
+			zap.String("url", model.FeedFormulaVersionUpdateUrl),
+			zap.Any("pasture", groupPasture), zap.Any("body", body),
+			zap.Any("err", err), zap.Any("response", response))
+		return
+	}
+
+	if response.Code != http.StatusOK {
+		zaplog.Error("UpdateFeedFormalVersion-response",
+			zap.String("url", model.DashboardExecTimeUrl),
+			zap.Any("pasture", groupPasture), zap.Any("body", body),
+			zap.Any("err", err), zap.Any("response", response))
+		return
 	}
 }

+ 12 - 0
module/backend/pasture_service.go

@@ -157,6 +157,18 @@ func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationP
 	}, nil
 }
 
+// GetGroupPastureById 根据id获取牧场端数据
+func (s *StoreEntry) GetGroupPastureById(ctx context.Context, id int64) (*model.GroupPasture, error) {
+	groupPasture := &model.GroupPasture{Id: id}
+	if err := s.DB.Model(new(model.GroupPasture)).
+		Where("is_delete = ?", operationPb.IsShow_OK).
+		Where("is_show = ?", operationPb.IsShow_OK).
+		First(groupPasture).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return groupPasture, nil
+}
+
 // DataSyncGroupPastureList 用户数据下发的牧场
 func (s *StoreEntry) DataSyncGroupPastureList(ctx context.Context) ([]*model.GroupPasture, error) {
 	groupPastureList := make([]*model.GroupPasture, 0)