Forráskód Böngészése

feed: 取消配方下发

Yi 1 éve
szülő
commit
af152634b0

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

@@ -275,6 +275,26 @@ func DistributeFeedFormula(c *gin.Context) {
 	})
 }
 
+// CancelDistributeFeedFormula 取消配方下发
+func CancelDistributeFeedFormula(c *gin.Context) {
+	req := &operationPb.DistributeFeedFormulaRequest{}
+	if err := ginutil.BindProto(c, req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CancelDistributeFeedFormula(c, req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}
+
 // Usage 配方使用情况
 func Usage(c *gin.Context) {
 	var req operationPb.FeedFormulaUsageRequest

+ 1 - 0
http/route/ops_api.go

@@ -62,6 +62,7 @@ func OpsAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		opsRoute.POST("/feed_formula/excel_template", feed.ExcelTemplateFeedFormula)
 		opsRoute.GET("/feed_formula/encode_number", feed.EncodeNumber)
 		opsRoute.POST("/feed_formula/distribute", feed.DistributeFeedFormula)
+		opsRoute.POST("/feed_formula/cancel/distribute", feed.CancelDistributeFeedFormula)
 		opsRoute.POST("/feed_formula/usage", feed.Usage)
 
 		//统计分析 statistic analysis

+ 13 - 12
model/group_pasture.go

@@ -56,18 +56,19 @@ const (
 )
 
 const (
-	FeedFormulaDistributeUrl      = "pasture/feed_formula/distribute"
-	FeedFormulaIsModifyUrl        = "pasture/feed_formula/is_modify"
-	FeedFormulaListAsyncUrl       = "pasture/feed_formula/list"
-	DashboardAccuracyUrl          = "pasture/dashboard/accuracy_data"
-	DashboardExecTimeUrl          = "pasture/dashboard/process_analysis"
-	DashboardSprinkleFeedTimeUrl  = "pasture/dashboard/sprinkle_statistics"
-	PastureAccountDistributionURl = "pasture/account/distribute"
-	ForageCategoryDistributionURl = "pasture/forage_category/distribute"
-	CattleCategoryDistributionURl = "pasture/cattle_category/distribute"
-	CattleCategoryDeleteURl       = "pasture/cattle_category/delete"
-	ForageCategoryDeleteURl       = "pasture/cattle_category/delete"
-	FeedUsageURl                  = "pasture/feed/usage"
+	FeedFormulaDistributeUrl       = "pasture/feed_formula/distribute"
+	FeedFormulaCancelDistributeUrl = "pasture/feed_formula/cancel/distribute"
+	FeedFormulaIsModifyUrl         = "pasture/feed_formula/is_modify"
+	FeedFormulaListAsyncUrl        = "pasture/feed_formula/list"
+	DashboardAccuracyUrl           = "pasture/dashboard/accuracy_data"
+	DashboardExecTimeUrl           = "pasture/dashboard/process_analysis"
+	DashboardSprinkleFeedTimeUrl   = "pasture/dashboard/sprinkle_statistics"
+	PastureAccountDistributionURl  = "pasture/account/distribute"
+	ForageCategoryDistributionURl  = "pasture/forage_category/distribute"
+	CattleCategoryDistributionURl  = "pasture/cattle_category/distribute"
+	CattleCategoryDeleteURl        = "pasture/cattle_category/delete"
+	ForageCategoryDeleteURl        = "pasture/cattle_category/delete"
+	FeedUsageURl                   = "pasture/feed/usage"
 )
 
 var (

+ 5 - 0
model/pasture_data.go

@@ -5,6 +5,11 @@ type DistributeFeedFormulaRequest struct {
 	Body      []*FeedFormula `json:"body"`
 }
 
+type CancelDistributeFeedFormulaRequest struct {
+	PastureId     int64   `json:"pasture_id"`
+	PastureDataId []int64 `json:"pasture_data_id"`
+}
+
 type PastureResponse struct {
 	Code int32       `json:"code"`
 	Msg  string      `json:"msg"`

+ 49 - 6
module/backend/feed_service.go

@@ -26,8 +26,9 @@ import (
 const EncodeNumberPrefix = "encode_number"
 
 var PastureDataLogType = map[string]int32{
-	"FeedFormula_Distribute": 1,
-	"FeedFormula_IsModify":   2,
+	"FeedFormula_Distribute":        1,
+	"FeedFormula_IsModify":          2,
+	"FeedFormula_Cancel_Distribute": 3,
 }
 
 // CreateFeedFormula 添加数据
@@ -342,9 +343,6 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 	if err != nil {
 		return xerr.WithStack(err)
 	}
-	if len(distributeData.PastureList) <= 0 {
-		return nil
-	}
 
 	wg := sync.WaitGroup{}
 	wg.Add(len(distributeData.PastureList))
@@ -352,6 +350,7 @@ 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 {
@@ -374,7 +373,6 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 				} else {
 					muError = multierr.Append(muError, xerr.Custom(response.Msg))
 				}
-				wg.Done()
 			}()
 
 			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDistributeUrl, p.Id, request, response); err != nil {
@@ -392,6 +390,51 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 	return muError
 }
 
+// CancelDistributeFeedFormula 取消配方下发牧场
+func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error {
+	distributeData, err := s.checkoutDistributeData(ctx, req)
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+
+	wg := sync.WaitGroup{}
+	wg.Add(len(distributeData.PastureList))
+	var muError error
+
+	for _, pasture := range distributeData.PastureList {
+		go func(p *model.GroupPasture) {
+			defer wg.Done()
+
+			pastureDataId := make([]int64, 0)
+			for _, v := range distributeData.FeedFormulaList {
+				if v.PastureId == p.Id {
+					pastureDataId = append(pastureDataId, v.PastureDataId)
+				}
+			}
+			if len(pastureDataId) <= 0 {
+				return
+			}
+
+			request := &model.CancelDistributeFeedFormulaRequest{
+				PastureId:     p.Id,
+				PastureDataId: pastureDataId,
+			}
+			response := &model.PastureResponse{}
+
+			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaCancelDistributeUrl, p.Id, request, response); err != nil {
+				zaplog.Error("DistributeFeedFormula", zap.Any("pasture", p), zap.Any("body", distributeData.FeedFormulaList), zap.Any("err", err), zap.Any("response", response))
+				b, _ := json.Marshal(request)
+				res, _ := json.Marshal(response)
+				pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Cancel_Distribute"], model.FeedFormulaCancelDistributeUrl, string(b), string(res))
+				s.DB.Create(pastureDataLog)
+			}
+
+		}(pasture)
+	}
+	wg.Wait()
+	return muError
+}
+
 // FeedFormulaUsage 配方使用概况
 func (s *StoreEntry) FeedFormulaUsage(ctx context.Context, req *operationPb.FeedFormulaUsageRequest) (*operationPb.FeedFormulaUsageResponse, error) {
 	feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)

+ 1 - 0
module/backend/interface.go

@@ -97,6 +97,7 @@ type PastureService interface {
 	ExcelTemplateFeedFormula(ctx context.Context) (*bytes.Buffer, error)
 	EncodeNumber(ctx context.Context) string
 	DistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error
+	CancelDistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error
 	FeedFormulaUsage(ctx context.Context, req *operationPb.FeedFormulaUsageRequest) (*operationPb.FeedFormulaUsageResponse, error)
 }