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

group: 集团配方下发

Yi 1 рік тому
батько
коміт
fabb352690
6 змінених файлів з 62 додано та 54 видалено
  1. 1 1
      go.mod
  2. 10 0
      go.sum
  3. 2 7
      model/feed_formula.go
  4. 3 8
      model/feed_formula_distribute_log.go
  5. 0 6
      model/pasture_data.go
  6. 46 32
      module/backend/feed_service.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-tmr-group
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20230822100450-307557aa568a
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20230823061528-20b02d357f02
 	gitee.com/xuyiping_admin/pkg v0.0.0-20230822102440-0e489dd5d75a
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/getsentry/sentry-go v0.23.0

+ 10 - 0
go.sum

@@ -38,6 +38,16 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20230822100450-307557aa568a h1:77YHuhzd5gGnNNcEahlUWNbA+QheErdLcENBFMbX8eI=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20230822100450-307557aa568a/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823052817-08ac6a117982 h1:uUPyJacoMHIui/loBHus6GO2V/WV5I0dxEEYNnPLWpw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823052817-08ac6a117982/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823054003-157f033bf025 h1:en1mDyAroXF+oD7zhtjXur+iinoRpOS8+MAP8wS/thQ=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823054003-157f033bf025/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823054257-de7a8cf59bb4 h1:9HPV7SqlxCgRKVdbF9AXubhLHgx4z93otF3pN5CUljo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823054257-de7a8cf59bb4/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823060841-fecf079818d5 h1:5wvuR14mVWu7kEYMwTYwVksjLfoDomYbSfhfLNHIYEo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823060841-fecf079818d5/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823061528-20b02d357f02 h1:e4BzweXQdP/RdWBeqk1sshUuxBI/jcLUCNDtv/G1mXg=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230823061528-20b02d357f02/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/pkg v0.0.0-20230822102440-0e489dd5d75a h1:+SSGto+q5BKy6r9FLFQXooTNqZs4tALceFpoo2jef8w=
 gitee.com/xuyiping_admin/pkg v0.0.0-20230822102440-0e489dd5d75a/go.mod h1:vK5K2LnhWZnvZlEY9gmT6GFdq/5Y0mtxuKLNJHFJkE4=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 2 - 7
model/feed_formula.go

@@ -1,8 +1,9 @@
 package model
 
 import (
-	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 	"time"
+
+	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 )
 
 type FeedFormula struct {
@@ -81,12 +82,6 @@ func (f FeedFormulaSlice) ToPB() []*operationPb.AddFeedFormulaRequest {
 	return res
 }
 
-type DistributeData struct {
-	PastureList       []*GroupPasture
-	FeedFormulaList   []*FeedFormula
-	FeedFormulaDetail []*FeedFormulaDetail
-}
-
 func NewNewFeedFormulaByMixed(req *operationPb.MixedFeedFormulaRequest) *FeedFormula {
 	return &FeedFormula{
 		Name:               req.Name,

+ 3 - 8
model/feed_formula_distribute_log.go

@@ -16,7 +16,7 @@ func (f *FeedFormulaDistributeLog) TableName() string {
 	return "feed_formula_distribute_log"
 }
 
-func NewFeedFormulaDistributeLog(feedFormulaId, pastureId int64, isShow operationPb.IsShow_Kind) *FeedFormulaDistributeLog {
+func NewFeedFormulaDistributeLog(feedFormulaId, pastureId int64, pastureName string, isShow operationPb.IsShow_Kind) *FeedFormulaDistributeLog {
 	return &FeedFormulaDistributeLog{
 		FeedFormulaId: feedFormulaId,
 		PastureId:     pastureId,
@@ -24,15 +24,10 @@ func NewFeedFormulaDistributeLog(feedFormulaId, pastureId int64, isShow operatio
 	}
 }
 
-func NewFeedFormulaDistributeLogList(feedFormulaList []*FeedFormula, pastureId int64, pastureName string, isShow operationPb.IsShow_Kind) []*FeedFormulaDistributeLog {
+func NewFeedFormulaDistributeLogList(feedFormulaList []*operationPb.DistributeFeedRequest, pastureId int64, pastureName string, isShow operationPb.IsShow_Kind) []*FeedFormulaDistributeLog {
 	res := make([]*FeedFormulaDistributeLog, 0)
 	for _, v := range feedFormulaList {
-		res = append(res, &FeedFormulaDistributeLog{
-			FeedFormulaId: v.Id,
-			PastureId:     pastureId,
-			PastureName:   pastureName,
-			IsShow:        isShow,
-		})
+		res = append(res, NewFeedFormulaDistributeLog(int64(v.Id), pastureId, pastureName, isShow))
 	}
 	return res
 }

+ 0 - 6
model/pasture_data.go

@@ -2,12 +2,6 @@ package model
 
 import operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
 
-type DistributeFeedFormulaRequest struct {
-	PastureId         int64                `json:"pasture_id"`
-	FeedFormula       []*FeedFormula       `json:"feed_formula"`
-	FeedFormulaDetail []*FeedFormulaDetail `json:"feed_formula_detail"`
-}
-
 type CancelDistributeFeedFormulaRequest struct {
 	PastureId     int64   `json:"pasture_id"`
 	PastureDataId []int64 `json:"pasture_data_id"`

+ 46 - 32
module/backend/feed_service.go

@@ -6,9 +6,6 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
-	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
-	"gitee.com/xuyiping_admin/pkg/xerr"
 	"io"
 	"kpt-tmr-group/model"
 	"net/http"
@@ -16,6 +13,10 @@ import (
 	"sync"
 	"time"
 
+	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"gitee.com/xuyiping_admin/pkg/xerr"
+
 	"go.uber.org/multierr"
 
 	"github.com/xuri/excelize/v2"
@@ -708,23 +709,35 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 	var muError error
 
 	for _, pasture := range distributeData.PastureList {
-		go func(p *model.GroupPasture) {
+		go func(p *operationPb.AddPastureRequest) {
 			defer wg.Done()
 
-			request, response := &model.DistributeFeedFormulaRequest{
-				PastureId:         p.Id,
-				FeedFormula:       distributeData.FeedFormulaList,
-				FeedFormulaDetail: distributeData.FeedFormulaDetail,
+			// 过滤掉自己本牧场上报的配方数据
+			newDistributeFeedRequest := make([]*operationPb.DistributeFeedRequest, 0)
+			for _, v := range distributeData.FeedFormulaList {
+				if v.PastureId != p.Id {
+					newDistributeFeedRequest = append(newDistributeFeedRequest, v)
+				}
+			}
+
+			// 请求参数
+			request, response := &operationPb.DistributeDataRequest{
+				PastureId:       p.Id,
+				FeedFormulaList: newDistributeFeedRequest,
 			}, &model.PastureResponse{}
+
 			defer func() {
 				if response.Code == http.StatusOK {
-					s.DB.Create(model.NewFeedFormulaDistributeLogList(distributeData.FeedFormulaList, p.Id, p.Name, operationPb.IsShow_OK))
+					feedFormulaDistributeLog := model.NewFeedFormulaDistributeLogList(distributeData.FeedFormulaList, int64(p.Id), p.Name, operationPb.IsShow_OK)
+					if err = s.DB.Create(feedFormulaDistributeLog).Error; err != nil {
+						zaplog.Error("DistributeFeedFormula", zap.Any("feedFormulaDistributeLog", feedFormulaDistributeLog), zap.Any("err", err))
+					}
 				} else {
 					muError = multierr.Append(muError, xerr.Custom(response.Msg))
 				}
 			}()
 
-			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDistributeUrl, p.Id, request, response); err != nil {
+			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDistributeUrl, int64(p.Id), request, response); err != nil {
 				muError = multierr.Append(muError, err)
 				zaplog.Error("DistributeFeedFormula",
 					zap.Any("pasture", p),
@@ -734,7 +747,7 @@ func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb
 				)
 				b, _ := json.Marshal(request)
 				res, _ := json.Marshal(response)
-				pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Distribute"], model.FeedFormulaDistributeUrl, string(b), string(res))
+				pastureDataLog := model.NewPastureDataLog(int64(p.Id), PastureDataLogType["FeedFormula_Distribute"], model.FeedFormulaDistributeUrl, string(b), string(res))
 				s.DB.Create(pastureDataLog)
 			}
 
@@ -756,13 +769,13 @@ func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *opera
 	var muError error
 
 	for _, pasture := range distributeData.PastureList {
-		go func(p *model.GroupPasture) {
+		go func(p *operationPb.AddPastureRequest) {
 			defer wg.Done()
 
 			pastureDataId := make([]int64, 0)
 			for _, v := range distributeData.FeedFormulaList {
 				if v.PastureId == p.Id {
-					pastureDataId = append(pastureDataId, v.PastureDataId)
+					pastureDataId = append(pastureDataId, int64(v.PastureDataId))
 				}
 			}
 			if len(pastureDataId) <= 0 {
@@ -770,12 +783,12 @@ func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *opera
 			}
 
 			request := &model.CancelDistributeFeedFormulaRequest{
-				PastureId:     p.Id,
+				PastureId:     int64(p.Id),
 				PastureDataId: pastureDataId,
 			}
 			response := &model.PastureResponse{}
 
-			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaCancelDistributeUrl, p.Id, request, response); err != nil {
+			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaCancelDistributeUrl, int64(p.Id), request, response); err != nil {
 				zaplog.Error("DistributeFeedFormula",
 					zap.String("url", model.FeedFormulaCancelDistributeUrl),
 					zap.Any("pasture", p),
@@ -784,7 +797,7 @@ func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *opera
 					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))
+				pastureDataLog := model.NewPastureDataLog(int64(p.Id), PastureDataLogType["FeedFormula_Cancel_Distribute"], model.FeedFormulaCancelDistributeUrl, string(b), string(res))
 				s.DB.Create(pastureDataLog)
 			}
 
@@ -981,11 +994,10 @@ 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),
-		FeedFormulaDetail: make([]*model.FeedFormulaDetail, 0),
+func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) (*operationPb.CheckDistributeData, error) {
+	result := &operationPb.CheckDistributeData{
+		PastureList:     make([]*operationPb.AddPastureRequest, 0),
+		FeedFormulaList: make([]*operationPb.DistributeFeedRequest, 0),
 	}
 
 	if err := s.DB.Where("id IN ?", req.PastureIds).Where("is_delete = ?", operationPb.IsShow_OK).Find(&result.PastureList).Error; err != nil {
@@ -996,24 +1008,26 @@ func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationP
 		return result, xerr.WithStack(err)
 	}
 
-	feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
-	for _, v := range req.FeedFormulaIds {
+	if len(result.FeedFormulaList) != len(req.FeedFormulaIds) {
+		return result, xerr.Customf("数据错误:%v", req.FeedFormulaIds)
+	}
+
+	for _, v := range result.FeedFormulaList {
 		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...)
+		if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("feed_formula_id = ?", v.Id).Find(&feedFormulaDetail).Error; err != nil {
+			zaplog.Error("checkoutDistributeData", zap.Any("feed_formula_id", v.Id), zap.Any("err", err))
+			return result, xerr.Customf("%v", err)
 		}
+
+		if len(feedFormulaDetail) <= 0 {
+			return result, xerr.Customf("请先添加配方饲料信息: %s", v.Name)
+		}
+		v.FeedFormulaDetail = model.FeedFormulaDetailSlice(feedFormulaDetail).ToPB()
 	}
 
-	result.FeedFormulaDetail = feedFormulaDetailList
 	if len(result.PastureList) <= 0 || len(result.FeedFormulaList) <= 0 {
 		return result, xerr.Customf("数据错误")
 	}
-
 	return result, nil
 }