123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- package backend
- import (
- "context"
- "encoding/json"
- "fmt"
- "kpt-tmr-group/model"
- "kpt-tmr-group/pkg/logger/zaplog"
- "kpt-tmr-group/pkg/tool"
- "kpt-tmr-group/pkg/xerr"
- operationPb "kpt-tmr-group/proto/go/backend/operation"
- "net/http"
- "sort"
- "sync"
- "time"
- "go.uber.org/multierr"
- "go.uber.org/zap"
- )
- 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) {
- response := &model.PastureAnalysisAccuracyResponse{}
- body := &model.DashboardAccuracyRequest{
- PastureId: int32(p.Id),
- FeedFormulaId: req.FeedFormulaId,
- CattleParentCategoryId: int32(req.CattleParentCategoryId),
- StartDate: req.StartDate,
- EndDate: req.EndDate,
- }
- if err = s.PastureHttpClient(ctx, DashboardAccuracyUrl, p.Id, body, response); err != nil {
- muError = multierr.Append(muError, err)
- zaplog.Error("DistributeFeedFormula",
- zap.String("url", DashboardAccuracyUrl),
- zap.Any("pasture", p), 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"], FeedFormulaDistributeUrl, string(b), string(resB))
- s.DB.Create(pastureDataLog)
- }
- if response.Code != http.StatusOK {
- muError = multierr.Append(muError, xerr.Custom(response.Msg))
- }
- res[p.Id] = response.Data
- wg.Done()
- }(pasture)
- }
- wg.Wait()
- return res, nil
- }
- func (s *StoreEntry) PasturePrefExecTimeData(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (map[string]*model.ExecTimeData, error) {
- groupPastureList, err := s.FindGroupPastureListByIds(ctx, req.PastureIds)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- res := make(map[string]*model.ExecTimeData, 0)
- wg := sync.WaitGroup{}
- wg.Add(len(groupPastureList))
- var muError error
- for _, pasture := range groupPastureList {
- go func(p *model.GroupPasture) {
- response := &model.PastureExecTimeData{}
- body := &model.DashboardAccuracyRequest{
- PastureId: int32(p.Id),
- FeedFormulaId: req.FeedFormulaId,
- CattleParentCategoryId: int32(req.CattleParentCategoryId),
- StartDate: req.StartDate,
- EndDate: req.EndDate,
- }
- if err = s.PastureHttpClient(ctx, DashboardExecTimeUrl, p.Id, body, response); err != nil {
- muError = multierr.Append(muError, err)
- zaplog.Error("PasturePrefExecTimeData",
- zap.String("url", DashboardExecTimeUrl),
- zap.Any("pasture", p), 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["PasturePrefExecTimeData"], DashboardExecTimeUrl, string(b), string(resB))
- s.DB.Create(pastureDataLog)
- }
- if response.Code != http.StatusOK {
- muError = multierr.Append(muError, xerr.Custom(response.Msg))
- }
- res[p.Name] = response.Data
- wg.Done()
- }(pasture)
- }
- wg.Wait()
- return res, nil
- }
- func (s *StoreEntry) PastureSprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (map[string][]*model.SprinkleStatisticsDataList, error) {
- groupPastureList, err := s.FindGroupPastureListByIds(ctx, req.PastureIds)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- res := make(map[string][]*model.SprinkleStatisticsDataList, 0)
- wg := sync.WaitGroup{}
- wg.Add(len(groupPastureList))
- var muError error
- for _, pasture := range groupPastureList {
- go func(p *model.GroupPasture) {
- response := &model.PastureSprinkleStatisticsDataList{}
- body := &model.DashboardAccuracyRequest{
- PastureId: int32(p.Id),
- FeedFormulaId: req.FeedFormulaId,
- StartDate: req.StartDate,
- EndDate: req.EndDate,
- }
- if err = s.PastureHttpClient(ctx, DashboardSprinkleFeedTimeUrl, p.Id, body, response); err != nil {
- muError = multierr.Append(muError, err)
- zaplog.Error("PastureSprinkleFeedTime",
- zap.String("url", DashboardSprinkleFeedTimeUrl),
- zap.Any("pasture", p), 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["PasturePrefExecTimeData"], DashboardSprinkleFeedTimeUrl, string(b), string(resB))
- s.DB.Create(pastureDataLog)
- }
- if response.Code != http.StatusOK {
- muError = multierr.Append(muError, xerr.Custom(response.Msg))
- }
- res[p.Name] = response.Data
- wg.Done()
- }(pasture)
- }
- wg.Wait()
- return res, nil
- }
- func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
- res := &model.SearchAnalysisAccuracyResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &model.AnalysisAccuracyData{
- Chart: &model.Chart{},
- Table: &model.Table{
- TitleList: make([]*model.TableList, 0),
- DataList: &model.DataList{},
- },
- },
- }
- res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
- Name: "title",
- Value: "牧场",
- })
- pastureAnalysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- mixedFodderAccurateRatio, mixedFodderCorrectRatio, sprinkleFodderAccurateRatio, sprinkleFodderCorrectRatio :=
- &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}, &model.CommonValueRatio{}
- maTitleValueList, mcTitleValueList, saTitleValueList, scTitleValueList := make([]float64, 0), make([]float64, 0), make([]float64, 0), make([]float64, 0)
- mTopOneName := ""
- for pastureId, data := range pastureAnalysisAccuracy {
- groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
- if err != nil {
- zaplog.Error("SearchAnalysisAccuracy GetGroupPastureListById",
- zap.Any("pastureId", pastureId), zap.Any("error", err))
- continue
- }
- if data == nil {
- continue
- }
- mixedFodderAccurateRatioDataList := make([]string, 0)
- for _, v := range data.MixedFodderAccurateRatio {
- mixedFodderAccurateRatioDataList = append(mixedFodderAccurateRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
- mixedFodderAccurateRatio.DateDay = append(mixedFodderAccurateRatio.DateDay, v.DayTime)
- maTitleValueList = append(maTitleValueList, v.Ratio)
- }
- mixedFodderAccurateRatio.DataList = append(mixedFodderAccurateRatio.DataList, mixedFodderAccurateRatioDataList)
- mixedFodderAccurateRatio.PastureIds = append(mixedFodderAccurateRatio.PastureIds, int32(pastureId))
- mixedFodderAccurateRatio.PastureName = append(mixedFodderAccurateRatio.PastureName, groupPasture.Name)
- mixedFodderCorrectRatioDataList := make([]string, 0)
- for _, v := range data.MixedFodderCorrectRatio {
- mixedFodderCorrectRatioDataList = append(mixedFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
- mixedFodderCorrectRatio.DateDay = append(mixedFodderCorrectRatio.DateDay, v.DayTime)
- mcTitleValueList = append(mcTitleValueList, v.Ratio)
- }
- mixedFodderCorrectRatio.DataList = append(mixedFodderCorrectRatio.DataList, mixedFodderCorrectRatioDataList)
- mixedFodderCorrectRatio.PastureIds = append(mixedFodderCorrectRatio.PastureIds, int32(pastureId))
- mixedFodderCorrectRatio.PastureName = append(mixedFodderCorrectRatio.PastureName, groupPasture.Name)
- sprinkleFodderRatioDataList := make([]string, 0)
- for _, v := range data.SprinkleFodderAccurateRatio {
- sprinkleFodderRatioDataList = append(sprinkleFodderRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
- sprinkleFodderAccurateRatio.DateDay = append(sprinkleFodderAccurateRatio.DateDay, v.DayTime)
- saTitleValueList = append(saTitleValueList, v.Ratio)
- }
- sprinkleFodderAccurateRatio.DataList = append(sprinkleFodderAccurateRatio.DataList, sprinkleFodderRatioDataList)
- sprinkleFodderAccurateRatio.PastureIds = append(sprinkleFodderAccurateRatio.PastureIds, int32(pastureId))
- sprinkleFodderAccurateRatio.PastureName = append(sprinkleFodderAccurateRatio.PastureName, groupPasture.Name)
- sprinkleFodderCorrectRatioDataList := make([]string, 0)
- for _, v := range data.SprinkleFodderCorrectRatio {
- sprinkleFodderCorrectRatioDataList = append(sprinkleFodderCorrectRatioDataList, fmt.Sprintf("%.2f", v.Ratio))
- sprinkleFodderCorrectRatio.DateDay = append(sprinkleFodderCorrectRatio.DateDay, v.DayTime)
- scTitleValueList = append(scTitleValueList, v.Ratio)
- }
- sprinkleFodderCorrectRatio.DataList = append(sprinkleFodderCorrectRatio.DataList, sprinkleFodderCorrectRatioDataList)
- sprinkleFodderCorrectRatio.PastureIds = append(sprinkleFodderCorrectRatio.PastureIds, int32(pastureId))
- sprinkleFodderCorrectRatio.PastureName = append(sprinkleFodderCorrectRatio.PastureName, groupPasture.Name)
- }
- sort.Float64s(maTitleValueList)
- mixedFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", maTitleValueList[len(maTitleValueList)-1])
- mixedFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", maTitleValueList[0])
- mixedFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(maTitleValueList))
- mixedFodderAccurateRatio.TopOneName = mTopOneName
- sort.Float64s(mcTitleValueList)
- mixedFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", mcTitleValueList[len(mcTitleValueList)-1])
- mixedFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", mcTitleValueList[0])
- mixedFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(mcTitleValueList))
- mixedFodderCorrectRatio.TopOneName = mTopOneName
- sort.Float64s(saTitleValueList)
- sprinkleFodderAccurateRatio.MaxValue = fmt.Sprintf("%.2f", saTitleValueList[len(saTitleValueList)-1])
- sprinkleFodderAccurateRatio.MinValue = fmt.Sprintf("%.2f", saTitleValueList[0])
- sprinkleFodderAccurateRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(saTitleValueList))
- sprinkleFodderAccurateRatio.TopOneName = mTopOneName
- sort.Float64s(scTitleValueList)
- sprinkleFodderCorrectRatio.MaxValue = fmt.Sprintf("%.2f", scTitleValueList[len(scTitleValueList)-1])
- sprinkleFodderCorrectRatio.MinValue = fmt.Sprintf("%.2f", scTitleValueList[0])
- sprinkleFodderCorrectRatio.MiddleValue = fmt.Sprintf("%.2f", tool.Median(scTitleValueList))
- sprinkleFodderCorrectRatio.TopOneName = mTopOneName
- chart := &model.Chart{
- MixedFodderAccurateRatio: mixedFodderAccurateRatio,
- MixedFodderCorrectRatio: mixedFodderCorrectRatio,
- SprinkleFodderAccurateRatio: sprinkleFodderAccurateRatio,
- SprinkleFodderCorrectRatio: sprinkleFodderCorrectRatio,
- }
- res.Data.Chart = chart
- res.Data.Table = s.TitleList(ctx, pastureAnalysisAccuracy)
- return res, nil
- }
- // TopPasture 牧场排名
- func (s *StoreEntry) TopPasture(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.GetPastureTopResponse, error) {
- res := &model.GetPastureTopResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &model.PastureTop{
- MixedFodderAccurateRatio: make([]*model.PastureTopData, 0),
- MixedFodderCorrectRatio: make([]*model.PastureTopData, 0),
- SprinkleFodderAccurateRatio: make([]*model.PastureTopData, 0),
- SprinkleFodderCorrectRatio: make([]*model.PastureTopData, 0),
- },
- }
- analysisAccuracy, err := s.PasturePrefAnalysisData(ctx, req)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- mixedFodderAccurateRatio := make([]*model.PastureTopData, 0)
- mixedFodderCorrectRatio := make([]*model.PastureTopData, 0)
- sprinkleFodderAccurateRatio := make([]*model.PastureTopData, 0)
- sprinkleFodderCorrectRatio := make([]*model.PastureTopData, 0)
- for pastureId, data := range analysisAccuracy {
- groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
- if err != nil {
- zaplog.Error("TopPasture", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
- continue
- }
- if data == nil {
- continue
- }
- allMaRatio, allMcRatio, allSaRatio, allScRatio := 0.0, 0.0, 0.0, 0.0
- for _, v := range data.MixedFodderAccurateRatio {
- allMaRatio += v.Ratio
- }
- mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, &model.PastureTopData{
- PastureName: groupPasture.Name,
- Ratio: allMaRatio / float64(len(data.MixedFodderAccurateRatio)),
- })
- for _, v := range data.MixedFodderCorrectRatio {
- allMcRatio += v.Ratio
- }
- mixedFodderCorrectRatio = append(mixedFodderCorrectRatio, &model.PastureTopData{
- PastureName: groupPasture.Name,
- Ratio: allMaRatio / float64(len(data.MixedFodderCorrectRatio)),
- })
- for _, v := range data.SprinkleFodderAccurateRatio {
- allSaRatio += v.Ratio
- }
- sprinkleFodderAccurateRatio = append(sprinkleFodderAccurateRatio, &model.PastureTopData{
- PastureName: groupPasture.Name,
- Ratio: allSaRatio / float64(len(data.SprinkleFodderAccurateRatio)),
- })
- for _, v := range data.SprinkleFodderCorrectRatio {
- allScRatio += v.Ratio
- }
- sprinkleFodderCorrectRatio = append(sprinkleFodderCorrectRatio, &model.PastureTopData{
- PastureName: groupPasture.Name,
- Ratio: allScRatio / float64(len(data.SprinkleFodderCorrectRatio)),
- })
- }
- sort.Slice(mixedFodderAccurateRatio, func(i, j int) bool {
- return mixedFodderAccurateRatio[i].Ratio > mixedFodderAccurateRatio[j].Ratio
- })
- sort.Slice(mixedFodderCorrectRatio, func(i, j int) bool {
- return mixedFodderCorrectRatio[i].Ratio > mixedFodderCorrectRatio[j].Ratio
- })
- sort.Slice(sprinkleFodderAccurateRatio, func(i, j int) bool {
- return sprinkleFodderAccurateRatio[i].Ratio > sprinkleFodderAccurateRatio[j].Ratio
- })
- sort.Slice(sprinkleFodderCorrectRatio, func(i, j int) bool {
- return sprinkleFodderCorrectRatio[i].Ratio > sprinkleFodderCorrectRatio[j].Ratio
- })
- res.Data.MixedFodderAccurateRatio = mixedFodderAccurateRatio
- res.Data.MixedFodderCorrectRatio = mixedFodderCorrectRatio
- res.Data.SprinkleFodderAccurateRatio = sprinkleFodderAccurateRatio
- res.Data.SprinkleFodderCorrectRatio = sprinkleFodderCorrectRatio
- return res, nil
- }
- func (s *StoreEntry) TitleList(ctx context.Context, pastureAnalysisList map[int64]*model.PastureAnalysisAccuracyData) *model.Table {
- res := &model.Table{
- TitleList: make([]*model.TableList, 0),
- DataList: &model.DataList{
- MixedFodderAccurateRatio: make([]map[string]string, 0),
- MixedFodderCorrectRatio: make([]map[string]string, 0),
- SprinkleFodderAccurateRatio: make([]map[string]string, 0),
- SprinkleFodderCorrectRatio: make([]map[string]string, 0),
- },
- }
- for pastureId, data := range pastureAnalysisList {
- groupPasture, err := s.GetGroupPastureListById(ctx, pastureId)
- if err != nil {
- zaplog.Info("TitleList", zap.Any("GetGroupPastureListById", pastureId), zap.Any("err", err))
- continue
- }
- if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
- res.TitleList = append(res.TitleList, &model.TableList{
- Name: "title",
- Value: "牧场",
- })
- }
- maMap := map[string]string{
- "title": groupPasture.Name,
- }
- for i, v := range data.MixedFodderCorrectRatio {
- maMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
- if len(res.TitleList) <= len(data.MixedFodderAccurateRatio) {
- res.TitleList = append(res.TitleList, &model.TableList{
- Name: fmt.Sprintf("data%d", i+1),
- Value: v.DayTime,
- })
- }
- }
- res.DataList.MixedFodderAccurateRatio = append(res.DataList.MixedFodderAccurateRatio, maMap)
- mcMap := map[string]string{
- "title": groupPasture.Name,
- }
- for i, v := range data.MixedFodderCorrectRatio {
- mcMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
- }
- res.DataList.MixedFodderCorrectRatio = append(res.DataList.MixedFodderCorrectRatio, mcMap)
- saMap := map[string]string{
- "title": groupPasture.Name,
- }
- for i, v := range data.SprinkleFodderAccurateRatio {
- saMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
- }
- res.DataList.SprinkleFodderAccurateRatio = append(res.DataList.SprinkleFodderAccurateRatio, saMap)
- scMap := map[string]string{
- "title": groupPasture.Name,
- }
- for i, v := range data.SprinkleFodderCorrectRatio {
- scMap[fmt.Sprintf("data%d", i+1)] = fmt.Sprintf("%.2f", v.Ratio)
- }
- res.DataList.SprinkleFodderCorrectRatio = append(res.DataList.SprinkleFodderCorrectRatio, scMap)
- }
- return res
- }
- func (s *StoreEntry) ExecutionTime(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.ExecTimeResponse, error) {
- res := &model.ExecTimeResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &model.ExecTimeDataList{
- Chart: &model.ExecTimeDataListChart{
- Title: make([]string, 0),
- AddFeedTime: make([][]string, 0),
- SprinkleTime: make([][]string, 0),
- StirTime: make([][]string, 0),
- },
- TableList: make([]map[string]string, 0),
- },
- }
- pastureExecTime, err := s.PasturePrefExecTimeData(ctx, req)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- for pastureName, execTime := range pastureExecTime {
- res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
- addFeedTimeStr, sprinkleTimeStr, stirTimeStr := make([]string, 0), make([]string, 0), make([]string, 0)
- if execTime != nil {
- addFeedTimeStr = append(addFeedTimeStr, execTime.AddFeedTime.MaxValue, execTime.AddFeedTime.UpMiddleValue,
- execTime.AddFeedTime.MiddleValue, execTime.AddFeedTime.DownMiddleValue, execTime.AddFeedTime.MinValue)
- sprinkleTimeStr = append(sprinkleTimeStr, execTime.SprinkleTime.MaxValue, execTime.SprinkleTime.UpMiddleValue,
- execTime.SprinkleTime.MiddleValue, execTime.SprinkleTime.DownMiddleValue, execTime.SprinkleTime.MinValue)
- stirTimeStr = append(stirTimeStr, execTime.StirTime.MaxValue, execTime.StirTime.UpMiddleValue,
- execTime.StirTime.MiddleValue, execTime.StirTime.DownMiddleValue, execTime.StirTime.MinValue)
- }
- res.Data.Chart.AddFeedTime = append(res.Data.Chart.AddFeedTime, addFeedTimeStr)
- res.Data.Chart.SprinkleTime = append(res.Data.Chart.SprinkleTime, sprinkleTimeStr)
- res.Data.Chart.StirTime = append(res.Data.Chart.StirTime, stirTimeStr)
- if execTime == nil {
- continue
- }
- tableList := map[string]string{
- "title": pastureName,
- "add_feed_time": "加料时间",
- "add_feed_time_max_value": execTime.AddFeedTime.MaxValue,
- "add_feed_time_up_middle_value": execTime.AddFeedTime.UpMiddleValue,
- "add_feed_time_middle_value": execTime.AddFeedTime.MiddleValue,
- "add_feed_time_down_middle_value": execTime.AddFeedTime.DownMiddleValue,
- "add_feed_time_min_value": execTime.AddFeedTime.MinValue,
- "sprinkle_time": "撒料时间",
- "sprinkle_time_max_value": execTime.SprinkleTime.MaxValue,
- "sprinkle_time_up_middle_value": execTime.SprinkleTime.UpMiddleValue,
- "sprinkle_time_middle_value": execTime.SprinkleTime.MiddleValue,
- "sprinkle_time_down_middle_value": execTime.SprinkleTime.DownMiddleValue,
- "sprinkle_time_min_value": execTime.SprinkleTime.MinValue,
- "stir_time": "搅拌延迟时间",
- "stir_time_max_value": execTime.StirTime.MaxValue,
- "stir_time_up_middle_value": execTime.StirTime.UpMiddleValue,
- "stir_time_middle_value": execTime.StirTime.MiddleValue,
- "stir_time_down_middle_value": execTime.StirTime.DownMiddleValue,
- "stir_time_min_value": execTime.StirTime.MinValue,
- }
- res.Data.TableList = append(res.Data.TableList, tableList)
- }
- return res, nil
- }
- func (s *StoreEntry) SprinkleFeedTime(ctx context.Context, req *operationPb.SprinkleFeedTimeRequest) (*model.SprinkleFeedTimeResponse, error) {
- res := &model.SprinkleFeedTimeResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &model.SprinkleFeedTimeData{
- Chart: &model.SprinkleFeedTimeChart{
- Title: make([]string, 0),
- SprinkleNumberList: make([][]int32, 0),
- },
- TableList: make([]*model.SprinkleFeedTimeTable, 0),
- },
- }
- pastureSprinkleDataList, err := s.PastureSprinkleFeedTime(ctx, req)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- tableList := make([]*model.SprinkleFeedTimeTable, 0)
- infoSprinkleNumber, errorSprinkleNumber := make([]int32, 0), make([]int32, 0)
- for pastureName, data := range pastureSprinkleDataList {
- sprinkleFeedTimeList := make(map[int32]map[int32][]int64, 0)
- for _, v := range data {
- tableList = append(tableList, &model.SprinkleFeedTimeTable{
- PastureName: pastureName,
- BarnName: v.FName,
- ClassNumber: fmt.Sprintf("%d", v.Times),
- RealitySprinkleFeedTime: tool.TimeSub(v.InTime, v.ProcessTime),
- })
- realityTime := tool.TimeSub(v.InTime, v.ProcessTime)
- realityTimeUnix, _ := time.Parse(model.LayoutTime, realityTime)
- if sprinkleFeedTimeList[v.FBarId] == nil {
- sprinkleFeedTimeList[v.FBarId] = make(map[int32][]int64, 0)
- }
- sprinkleFeedTimeList[v.FBarId][v.Times] = append(sprinkleFeedTimeList[v.FBarId][v.Times], realityTimeUnix.Unix())
- }
- res.Data.Chart.Title = append(res.Data.Chart.Title, pastureName)
- infoNumber, errNumber := sprinkleExecTimeAnalysis(sprinkleFeedTimeList)
- infoSprinkleNumber = append(infoSprinkleNumber, infoNumber)
- errorSprinkleNumber = append(errorSprinkleNumber, errNumber)
- }
- res.Data.Chart.SprinkleNumberList = append(res.Data.Chart.SprinkleNumberList, infoSprinkleNumber, errorSprinkleNumber)
- res.Data.TableList = tableList
- return res, nil
- }
- func sprinkleExecTimeAnalysis(sprinkleFeedTimeList map[int32]map[int32][]int64) (int32, int32) {
- var infoSprinkleNumber, errorSprinkleNumber int32 = 0, 0
- if len(sprinkleFeedTimeList) <= 0 {
- return infoSprinkleNumber, errorSprinkleNumber
- } else {
- for _, value := range sprinkleFeedTimeList {
- for _, execTimeList := range value {
- middleValue := tool.MedianInt64(execTimeList)
- for _, v := range execTimeList {
- if v >= middleValue-int64(compareTime) && v <= middleValue+int64(compareTime) {
- infoSprinkleNumber += 1
- } else {
- errorSprinkleNumber += 1
- }
- }
- }
- }
- }
- return infoSprinkleNumber, errorSprinkleNumber
- }
|