Browse Source

pasture: 兼容历史数据

Yi 1 year ago
parent
commit
43d34abb1a

+ 51 - 26
module/backend/dashboard_service.go

@@ -22,43 +22,53 @@ const compareTime = 10 * 60
 
 // PasturePrefAnalysisData  PasturePrefExecTimeData PastureSprinkleFeedTime TODO  后期三个函数封装一下
 func (s *StoreEntry) PasturePrefAnalysisData(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (map[int64]*model.PastureAnalysisAccuracyData, error) {
-	groupPastureList, err := s.FindGroupPastureListByIds(ctx, req.PastureIds)
-	if err != nil {
-		return nil, xerr.WithStack(err)
-	}
-
 	res := make(map[int64]*model.PastureAnalysisAccuracyData, 0)
+
 	wg := sync.WaitGroup{}
-	wg.Add(len(groupPastureList))
-	var muError error
-	for _, pasture := range groupPastureList {
-		go func(p *model.GroupPasture) {
+	wg.Add(len(req.PastureIds))
+
+	for _, pastureId := range req.PastureIds {
+		go func(pId int32) {
 			defer wg.Done()
+
 			response := &model.PastureAnalysisAccuracyResponse{}
+			groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
+			if err != nil {
+				zaplog.Error("PasturePrefAnalysisData",
+					zap.Any("GetGroupPastureListById", err),
+					zap.Any("pId", pId))
+				return
+			}
+
 			body := &model.DashboardAccuracyRequest{
-				PastureId:              int32(p.Id),
+				PastureId:              int32(groupPasture.PastureId),
 				FeedFormulaId:          req.FeedFormulaId,
 				CattleParentCategoryId: int32(req.CattleParentCategoryId),
 				StartDate:              req.StartDate,
 				EndDate:                req.EndDate,
 			}
-			if _, err = s.PastureHttpClient(ctx, model.DashboardAccuracyUrl, p.Id, body, response); err != nil {
-				muError = multierr.Append(muError, err)
+			_, err = s.PastureHttpClient(ctx, model.DashboardAccuracyUrl, int64(pId), body, response)
+			if err != nil {
 				zaplog.Error("DistributeFeedFormula",
 					zap.String("url", model.DashboardAccuracyUrl),
-					zap.Any("pasture", p), zap.Any("body", body),
+					zap.Any("pasture", groupPasture), zap.Any("body", body),
 					zap.Any("err", err), zap.Any("response", response))
 				b, _ := json.Marshal(body)
 				resB, _ := json.Marshal(response)
-				pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Distribute"], model.DashboardAccuracyUrl, string(b), string(resB))
+				pastureDataLog := model.NewPastureDataLog(groupPasture.Id, PastureDataLogType["FeedFormula_Distribute"], model.DashboardAccuracyUrl, string(b), string(resB))
 				s.DB.Create(pastureDataLog)
+				return
 			}
 			if response.Code != http.StatusOK {
-				muError = multierr.Append(muError, xerr.Custom(response.Msg))
+				zaplog.Error("DistributeFeedFormula-response",
+					zap.String("url", model.DashboardAccuracyUrl),
+					zap.Any("pasture", groupPasture), zap.Any("body", body),
+					zap.Any("response", response), zap.Any("response", response))
+				return
 			}
-			res[p.Id] = response.Data
+			res[groupPasture.Id] = response.Data
 
-		}(pasture)
+		}(pastureId)
 	}
 	wg.Wait()
 	return res, nil
@@ -69,21 +79,25 @@ func (s *StoreEntry) PasturePrefExecTimeData(ctx context.Context, req *operation
 	res := make(map[string]*model.ExecTimeData, 0)
 	wg := sync.WaitGroup{}
 	wg.Add(len(req.PastureIds))
-	var muError error
+
 	for _, pastureId := range req.PastureIds {
 		go func(pId int32) {
 			defer wg.Done()
+
+			groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
+			if err != nil {
+				return
+			}
+
 			response := &model.PastureExecTimeData{}
 			body := &model.DashboardAccuracyRequest{
-				PastureId:              pId,
+				PastureId:              int32(groupPasture.PastureId),
 				FeedFormulaId:          req.FeedFormulaId,
 				CattleParentCategoryId: int32(req.CattleParentCategoryId),
 				StartDate:              req.StartDate,
 				EndDate:                req.EndDate,
 			}
-			groupPasture, err := s.PastureHttpClient(ctx, model.DashboardExecTimeUrl, int64(pId), body, response)
-			if err != nil {
-				muError = multierr.Append(muError, err)
+			if _, err = s.PastureHttpClient(ctx, model.DashboardExecTimeUrl, int64(pId), body, response); err != nil {
 				zaplog.Error("PasturePrefExecTimeData",
 					zap.String("url", model.DashboardExecTimeUrl),
 					zap.Any("pasture", groupPasture), zap.Any("body", body),
@@ -92,9 +106,14 @@ func (s *StoreEntry) PasturePrefExecTimeData(ctx context.Context, req *operation
 				resB, _ := json.Marshal(response)
 				pastureDataLog := model.NewPastureDataLog(groupPasture.Id, PastureDataLogType["PasturePrefExecTimeData"], model.DashboardExecTimeUrl, string(b), string(resB))
 				s.DB.Create(pastureDataLog)
+				return
 			}
 			if response.Code != http.StatusOK {
-				muError = multierr.Append(muError, xerr.Custom(response.Msg))
+				zaplog.Error("PasturePrefExecTimeData-response",
+					zap.String("url", model.DashboardExecTimeUrl),
+					zap.Any("pasture", groupPasture), zap.Any("body", body),
+					zap.Any("err", err), zap.Any("response", response))
+				return
 			}
 			res[groupPasture.Name] = response.Data
 
@@ -113,15 +132,21 @@ func (s *StoreEntry) PastureSprinkleFeedTime(ctx context.Context, req *operation
 	for _, pasture := range req.PastureIds {
 		go func(pId int32) {
 			defer wg.Done()
+
+			groupPasture, err := s.GetGroupPastureListById(ctx, int64(pId))
+			if err != nil {
+				zaplog.Error("PastureSprinkleFeedTime", zap.Any("GetGroupPastureListById", err))
+				return
+			}
+
 			response := &model.PastureSprinkleStatisticsDataList{}
 			body := &model.DashboardAccuracyRequest{
-				PastureId:     pId,
+				PastureId:     int32(groupPasture.PastureId),
 				FeedFormulaId: req.FeedFormulaId,
 				StartDate:     req.StartDate,
 				EndDate:       req.EndDate,
 			}
-			groupPasture, err := s.PastureHttpClient(ctx, model.DashboardSprinkleFeedTimeUrl, int64(pId), body, response)
-			if err != nil {
+			if _, err = s.PastureHttpClient(ctx, model.DashboardSprinkleFeedTimeUrl, int64(pId), body, response); err != nil {
 				muError = multierr.Append(muError, err)
 				zaplog.Error("PastureSprinkleFeedTime",
 					zap.String("url", model.DashboardSprinkleFeedTimeUrl),

+ 1 - 1
module/backend/pasture_service.go

@@ -80,7 +80,7 @@ func (s *StoreEntry) FindGroupPastureListByIds(ctx context.Context, pastureIds [
 
 func (s *StoreEntry) GetGroupPastureListById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
 	groupPasture := &model.GroupPasture{
-		Id: int64(pastureId),
+		Id: pastureId,
 	}
 	if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
 		First(&groupPasture).Error; err != nil {

+ 97 - 0
module/backend/statistic_service.go

@@ -57,6 +57,14 @@ func (s *StoreEntry) PastureHttpClient(ctx context.Context, apiUrl string, pastu
 
 // SearchFormulaEstimateList 配方评估
 func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -81,6 +89,14 @@ func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operati
 
 // SearchInventoryStatistics 库存管理-库存统计
 func (s *StoreEntry) SearchInventoryStatistics(ctx context.Context, req *operationPb.SearchInventoryStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -194,6 +210,14 @@ func (s *StoreEntry) InventoryStatisticsExcelExport(ctx context.Context, req *op
 
 // SearchUserMaterialsStatistics 库存管理-用料分析
 func (s *StoreEntry) SearchUserMaterialsStatistics(ctx context.Context, req *operationPb.SearchUserMaterialsStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -299,6 +323,14 @@ func (s *StoreEntry) UserMaterialsStatisticsExcelExport(ctx context.Context, req
 
 // SearchPriceStatistics 库存管理-价格分析
 func (s *StoreEntry) SearchPriceStatistics(ctx context.Context, req *operationPb.SearchPriceStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -464,6 +496,14 @@ func FeedStatisticsConversions(req interface{}) *model.FeedStatisticsConversions
 
 // FeedChartStatistics 饲喂效率图表分析
 func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.FeedChartStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.FeedChartParams{
 		ParamMaps: &model.ParamMaps{
 			PastureId: fmt.Sprintf("%d", req.PastureId),
@@ -485,6 +525,14 @@ func (s *StoreEntry) FeedChartStatistics(ctx context.Context, req *operationPb.F
 
 // CowsAnalysis 饲喂效率-牛群评估
 func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnalysisRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -506,6 +554,14 @@ func (s *StoreEntry) CowsAnalysis(ctx context.Context, req *operationPb.CowsAnal
 
 // SearchAccuracyAggStatistics 准确性分析-汇总分析
 func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *operationPb.AccuracyAggStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.FeedChartParams{
 		ParamMaps: &model.AccuracyAggParams{
 			PastureId: fmt.Sprintf("%d", req.PastureId),
@@ -541,10 +597,19 @@ func (s *StoreEntry) SearchAccuracyAggStatistics(ctx context.Context, req *opera
 
 // SearchMixFeedStatistics 准确性分析-混料统计
 func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operationPb.MixFeedStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	times := ""
 	if req.ClassNumber > 0 {
 		times = fmt.Sprintf("%d", req.ClassNumber)
 	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -579,6 +644,14 @@ func (s *StoreEntry) SearchMixFeedStatistics(ctx context.Context, req *operation
 
 // SearchSprinkleStatistics 准确性分析-撒料统计
 func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operationPb.SprinkleStatisticsRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	times := ""
 	if req.ClassNumber > 0 {
 		times = fmt.Sprintf("%d", req.ClassNumber)
@@ -618,6 +691,14 @@ func (s *StoreEntry) SearchSprinkleStatistics(ctx context.Context, req *operatio
 
 // SearchProcessAnalysis 过程分析
 func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb.ProcessAnalysisRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,
@@ -652,6 +733,14 @@ func (s *StoreEntry) SearchProcessAnalysis(ctx context.Context, req *operationPb
 
 // GetDataByName 共同接口
 func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetDataByNameRequest) (*model.PastureCommonResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name: req.ApiName,
 		ParamMaps: &model.GetDataByNameParams{
@@ -669,6 +758,14 @@ func (s *StoreEntry) GetDataByName(ctx context.Context, req *operationPb.GetData
 
 // GetTrainNumber 获取班次
 func (s *StoreEntry) GetTrainNumber(ctx context.Context, req *operationPb.TrainNumberRequest) (*operationPb.TrainNumberResponse, error) {
+	groupPasture, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if groupPasture.PastureId > 0 {
+		req.PastureId = int32(groupPasture.PastureId)
+	}
+
 	body := &model.PastureCommonRequest{
 		Name:       req.ApiName,
 		Page:       req.Pagination.Page,