|
@@ -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
|
|
|
}
|
|
|
|