| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 | package backendimport (	"context"	"fmt"	"kpt-tmr-group/model"	"kpt-tmr-group/pkg/xerr"	operationPb "kpt-tmr-group/proto/go/backend/operation"	"net/http"	"strconv"	"strings"	"time")func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*operationPb.SearchFormulaEstimateResponse, error) {	startTime, err := time.Parse(model.LayoutTime, req.StartTime)	if err != nil {		return nil, xerr.WithStack(err)	}	endTime, err := time.Parse(model.LayoutTime, req.EndTime)	if err != nil {		return nil, xerr.WithStack(err)	}	startTimeUnix := startTime.Unix()	endTimeUnix := endTime.Unix()	formulaEstimate := make([]*model.FormulaEstimate, 0)	var count int64 = 0	pref := s.DB.Model(new(model.FormulaEstimate))	if req.Name != "" {		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))	}	if startTimeUnix > 0 && endTimeUnix > 0 && endTimeUnix >= startTimeUnix {		pref.Where("created_at BETWEEN ? AND ?", startTimeUnix, endTimeUnix)	}	if req.SearchType == 1 {		pref.Where("feed_formula_name = ?", req.Name)	} else {		pref.Where("barn_id = ?", req.Name)	}	if err = pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).		Find(&formulaEstimate).Error; err != nil {		return nil, xerr.WithStack(err)	}	return &operationPb.SearchFormulaEstimateResponse{		Code: http.StatusOK,		Msg:  "ok",		Data: &operationPb.SearchFormulaEstimate{			Page:     req.Pagination.Page,			Total:    int32(count),			PageSize: req.Pagination.PageSize,			List:     model.FormulaEstimateSlice(formulaEstimate).ToPB(),		},	}, 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}
 |