package backend import ( "context" "encoding/json" "fmt" "go.uber.org/zap" "kpt-tmr-group/model" "kpt-tmr-group/pkg/logger/zaplog" "kpt-tmr-group/pkg/xerr" operationPb "kpt-tmr-group/proto/go/backend/operation" "net/http" "strconv" "strings" ) // PastureDetailById 获取指定牧场详情 func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) { result := &model.GroupPasture{Id: pastureId} if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil { return nil, xerr.WithStack(err) } return result, nil } // SearchFormulaEstimateList 配方评估 func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.FormulaEstimateResponse, error) { pastureDetail, err := s.PastureDetailById(ctx, int64(req.PastureId)) if err != nil { zaplog.Error("SearchFormulaEstimateList", zap.Any("Err", err), zap.Any("req", req)) return nil, xerr.Customf("该牧场数据错误") } pastureClient := model.NewPastureClient(pastureDetail) body := &model.FormulaEstimateRequest{ Name: req.Name, Page: req.Pagination.Page, Offset: req.Pagination.PageOffset, PageCount: req.Pagination.PageSize, ReturnType: "map", ParamMaps: &model.FormulaEstimateParams{ PastureId: fmt.Sprintf("%d", req.PastureId), StartTime: req.StartTime, StopTime: req.EndTime, InputDatetime: nil, Search: fmt.Sprintf("%d", req.SearchType), TempletId: fmt.Sprintf("%d", req.TemplateId), Barid: fmt.Sprintf("%d", req.BarnId), }, } url := fmt.Sprintf("%s/%s", pastureDetail.Domain, "authdata/GetDataByName") result, err := pastureClient.DoPost(url, body) if err != nil { return nil, xerr.WithStack(err) } response := &model.FormulaEstimateResponse{} if err = json.Unmarshal(result, response); err != nil { return nil, xerr.WithStack(err) } if response.Code == 400 { if response.Msg == "登录超时" { } else { return nil, xerr.WithStack(err) } } return response, nil } func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) { dataList := &model.CommonValueRatio{ MaxValue: "", MiddleValue: "", MinValue: "", PastureName: make([]string, 0), DateDay: make([]string, 0), DataList: make([][]string, 0), PastureIds: make([]int32, 0), } res := &model.SearchAnalysisAccuracyResponse{ Code: http.StatusOK, Msg: "ok", Data: &model.AnalysisAccuracyData{ Chart: &model.Chart{ MixedFodderAccurateRatio: dataList, MixedFodderCorrectRatio: dataList, SprinkleFodderAccurateRatio: dataList, SprinkleFodderCorrectRatio: dataList, }, Table: &model.Table{ TitleList: make([]*model.TableList, 0), DataList: make([]*interface{}, 0), }, }, } res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{ Name: "title", Value: "牧场", }) analysisAccuracy := make([]*model.OptionsAnalysisAccuracy, 0) pref := s.DB.Model(new(model.AnalysisAccuracy)) if req.EndDate != "" && req.StartDate != "" { pref.Where("date_day BETWEEN ? AND ?", req.StartDate, req.EndDate) } if req.CattleParentCategoryId > 0 { pref.Where("cattle_parent_category_id = ?", req.CattleParentCategoryId) } if req.FeedFormulaId > 0 { pref.Where("feed_formula_id = ?", req.FeedFormulaId) } if len(req.PastureIds) > 0 { pref.Where("pasture_id IN ?", req.PastureIds) } if err := pref.Select("pasture_id,pasture_name,date_day,sum(iweight) as all_iweight,sum(lweight) as all_lweight,sum(oweight) as all_oweight,sum(actual_weight_minus) as all_actual_weight_minus,sum(allow_ratio) as all_allow_ratio,sum(alweight) as all_alweight"). Group("pasture_id,pasture_name,date_day").Order("pasture_name,date_day").Find(&analysisAccuracy).Debug().Error; err != nil { return nil, xerr.WithStack(err) } mixedFodderAccurateRatio := make([]float64, 0) mapPastureName := make(map[string]bool, 0) mapDateDay := make(map[string]bool, 0) mapRatio := make(map[string]bool, 0) tableList := make([]*model.TableList, 0) for k, v := range analysisAccuracy { if _, ok := mapPastureName[v.PastureName]; !ok { res.Data.Chart.MixedFodderAccurateRatio.PastureName = append(res.Data.Chart.MixedFodderAccurateRatio.PastureName, v.PastureName) res.Data.Chart.MixedFodderAccurateRatio.PastureIds = append(res.Data.Chart.MixedFodderAccurateRatio.PastureIds, int32(v.PastureId)) mapPastureName[v.PastureName] = true } dataDayFormat := strings.TrimRight(v.DateDay, "T00:00:00+08:00") if _, ok := mapDateDay[v.DateDay]; !ok { res.Data.Chart.MixedFodderAccurateRatio.DateDay = append(res.Data.Chart.MixedFodderAccurateRatio.DateDay, dataDayFormat) mapDateDay[v.DateDay] = true } valueRatio1 := float64(v.AllIweight/v.AllLweight) / 100.0 if _, ok := mapRatio[v.DateDay]; !ok { valueRatio := make([]string, 0) for _, a := range analysisAccuracy { if a.DateDay == v.DateDay { valueRatio = append(valueRatio, strconv.FormatFloat(float64(a.AllIweight/a.AllLweight)/100.0, 'f', 2, 64)) } } res.Data.Chart.MixedFodderAccurateRatio.DataList = append(res.Data.Chart.MixedFodderAccurateRatio.DataList, valueRatio) mapRatio[v.DateDay] = true tableList = append(tableList, &model.TableList{ Name: fmt.Sprintf("date%d", k), Value: dataDayFormat, }) } mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, valueRatio1) } mixedFodderAccurateRatioMaxValue, mixedFodderAccurateRatioMiddleValue, mixedFodderAccurateRatioMinValue := calculateRatio(mixedFodderAccurateRatio) res.Data.Chart.MixedFodderAccurateRatio.MaxValue = strconv.FormatFloat(mixedFodderAccurateRatioMaxValue, 'f', 2, 64) res.Data.Chart.MixedFodderAccurateRatio.MiddleValue = strconv.FormatFloat(mixedFodderAccurateRatioMiddleValue, 'f', 2, 64) res.Data.Chart.MixedFodderAccurateRatio.MinValue = strconv.FormatFloat(mixedFodderAccurateRatioMinValue, 'f', 2, 64) res.Data.Table.TitleList = append(res.Data.Table.TitleList, tableList...) return res, nil } func calculateRatio(res []float64) (float64, float64, float64) { var maxValue, middleValue, minValue, allValue float64 = 0, 0, 0, 0 for _, v := range res { if v > maxValue { maxValue = v } if v <= maxValue { minValue = v } allValue += v } middleValue = allValue / float64(len(res)) return maxValue, middleValue, minValue }