package group import ( "fmt" "sort" "tmr-watch/models" "tmr-watch/module" "tmr-watch/pkg/logger/zaplog" "tmr-watch/pkg/util" operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation" "go.uber.org/zap" ) // DistributeFeedFormulaService 饲料配方下发牧场端 func DistributeFeedFormulaService(req *operationPb.DistributeDataRequest) error { zaplog.Info("DistributeFeedFormulaService", zap.Any("request", req)) feedTemplateList := make([]*models.FeedTemplate, 0) for _, b := range req.FeedFormulaList { feedTemplateList = append(feedTemplateList, models.NewFeedTemplateByGroup(int64(req.PastureId), b)) } return module.DistributeFeedFormula(feedTemplateList) } // CancelDistributeFeedFormulaService 取消饲料配方下发牧场端 func CancelDistributeFeedFormulaService(req *models.CancelDistributeFeedFormulaRequest) error { if len(req.PastureDataId) <= 0 { return nil } return module.CancelDistributeFeedFormula(req.PastureId, req.PastureDataId) } func FeedFormulaIsModifyService(req *models.PastureFeedFormulaIsModifyRequest) error { return module.FeedFormulaIsModify(req) } // FeedFormulaList 配方列表数据 func FeedFormulaList(req *models.FeedListRequest) (*models.FeedFormulaData, error) { res := &models.FeedFormulaData{ Total: 0, Page: req.Page, PageSize: req.PageSize, List: make([]*models.FeedTemplate, 0), } dataList, total, err := module.FeedTemplateList(req) if err != nil { zaplog.Error("FeedFormulaList", zap.Any("dataList", dataList), zap.Any("err", err)) return nil, err } res.Total = int32(total) res.List = dataList return res, nil } // FeedList 饲料列表数据 func FeedList(req *models.FeedListRequest) (*models.FeedData, error) { res := &models.FeedData{ Total: 0, Page: req.Page, PageSize: req.PageSize, List: make([]*models.Feed, 0), } dataList, total, err := module.FeedList(req) if err != nil { zaplog.Error("FeedList", zap.Any("dataList", dataList), zap.Any("err", err)) return nil, err } res.Total = int32(total) res.List = dataList return res, nil } // FeedTemplateDetailList 饲料配方详情列表数据 func FeedTemplateDetailList(req *models.FeedListRequest) (*models.FeedDetailData, error) { res := &models.FeedDetailData{ Total: 0, Page: req.Page, PageSize: req.PageSize, List: make([]*models.FeedTemplateDetail, 0), } dataList, total, err := module.FeedTemplateDetailList(req) if err != nil { zaplog.Error("FeedTemplateDetailList", zap.Any("dataList", dataList), zap.Any("err", err)) return nil, err } res.Total = int32(total) res.List = dataList return res, nil } // AnalysisAccuracyService 首页准确率分析 func AnalysisAccuracyService(req *models.AnalysisAccuracyRequest) (*models.AnalysisAccuracyResponse, error) { response := &models.AnalysisAccuracyResponse{ MixedFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0), MixedFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0), SprinkleFodderAccurateRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0), SprinkleFodderCorrectRatio: make([]*models.PastureAnalysisAccuracyDataValue, 0), } // 混料准确率和正确率 mixedFodderDataList, err := module.MixedFodderData(req) if err != nil { return nil, err } // 撒料准确率和撒料正确率 sprinkleFodderDataList, err := module.SprinkleFodderData(req) if err != nil { return nil, err } timeList := util.TimeBetween(req.StartDate, req.EndDate) for _, dayTime := range timeList { var mixedInfo, sprinkleInfo bool for _, mixed := range mixedFodderDataList { myDate := mixed.PlanTime.Format(util.LayoutDateFormat) if myDate != dayTime { continue } // 混料准确率 mixedAccurateValue := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: mixed.MixedFodderAccurateRatio, } // 混料正确率 mixedCorrectValue := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: mixed.MixedFodderCorrectRatio, } response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, mixedAccurateValue) response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, mixedCorrectValue) mixedInfo = true } if !mixedInfo { noInfo := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: 0, } response.MixedFodderAccurateRatio = append(response.MixedFodderAccurateRatio, noInfo) response.MixedFodderCorrectRatio = append(response.MixedFodderCorrectRatio, noInfo) } for _, sprinkle := range sprinkleFodderDataList { smyDate := sprinkle.PlanTime.Format(util.LayoutDateFormat) if smyDate != dayTime { continue } // 撒料准确率 sprinkleAccurateValue := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: sprinkle.SprinkleFodderAccurateRatio, } // 撒料正确率 sprinkleCorrectValue := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: sprinkle.SprinkleFodderCorrectRatio, } response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, sprinkleCorrectValue) response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, sprinkleAccurateValue) sprinkleInfo = true } if !sprinkleInfo { noInfo := &models.PastureAnalysisAccuracyDataValue{ DayTime: dayTime, Ratio: 0, } response.SprinkleFodderAccurateRatio = append(response.SprinkleFodderAccurateRatio, noInfo) response.SprinkleFodderCorrectRatio = append(response.SprinkleFodderCorrectRatio, noInfo) } } return response, nil } // ProcessAnalysisService 过程分析-执行时间 func ProcessAnalysisService(req *models.AnalysisAccuracyRequest) (*models.ProcessAnalysisResponse, error) { response := &models.ProcessAnalysisResponse{ AddFeedTime: &models.ProcessAnalysisDataValue{}, SprinkleTime: &models.ProcessAnalysisDataValue{}, StirTime: &models.ProcessAnalysisDataValue{}, } // 获取指定时间段内tmr设备列表 tmrList, err := module.TMRList(req) if err != nil { return nil, err } addFeedList := make([]int, 0) sprinkleFeedList := make([]int, 0) stirDelayList := make([]int, 0) for _, v := range tmrList { mixedProcessTimeList, err := module.MixedProcessTimeList(req.PastureId, v.Id) if err != nil { zaplog.Error("ProcessAnalysisService", zap.Any("MixedProcessTimeList", err)) } else { tmrMixAllTime := 0 for _, t := range mixedProcessTimeList { tmrMixAllTime += util.TimeParseToMinutes(t.ProcessTime) } addFeedList = append(addFeedList, tmrMixAllTime) } sprinkleProcessTimeList, err := module.SprinkleProcessTimeList(req.PastureId, v.Id) if err != nil { zaplog.Error("ProcessAnalysisService", zap.Any("SprinkleProcessTimeList", err)) } else { tmrSprinkleAllTime := 0 for _, s := range sprinkleProcessTimeList { tmrSprinkleAllTime += util.TimeParseToMinutes(s.ProcessTime) } sprinkleFeedList = append(sprinkleFeedList, tmrSprinkleAllTime) } } if len(addFeedList) > 0 { sort.Ints(addFeedList) response.AddFeedTime.MaxValue = fmt.Sprintf("%.2f", float64(addFeedList[len(addFeedList)-1])/60) response.AddFeedTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[len(addFeedList)/2+1:])/60) response.AddFeedTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList)/60) response.AddFeedTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(addFeedList[0:len(addFeedList)/2])/60) response.AddFeedTime.MinValue = fmt.Sprintf("%.2f", float64(addFeedList[0])/60) } if len(sprinkleFeedList) > 0 { sort.Ints(sprinkleFeedList) response.SprinkleTime.MaxValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[len(sprinkleFeedList)-1])/60) response.SprinkleTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[len(sprinkleFeedList)/2+1:])/60) response.SprinkleTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList)/60) response.SprinkleTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(sprinkleFeedList[0:len(sprinkleFeedList)/2])/60) response.SprinkleTime.MinValue = fmt.Sprintf("%.2f", float64(sprinkleFeedList[0])/60) } if len(stirDelayList) > 0 { sort.Ints(stirDelayList) response.StirTime.MaxValue = fmt.Sprintf("%d", stirDelayList[len(stirDelayList)-1]) response.StirTime.UpMiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList[len(stirDelayList)/2+1:])) response.StirTime.MiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList)) response.StirTime.DownMiddleValue = fmt.Sprintf("%.2f", util.Median(stirDelayList[0:len(stirDelayList)/2])) response.StirTime.MinValue = fmt.Sprintf("%d", stirDelayList[0]) } return response, nil } // SprinkleStatisticsService 撒料统计 func SprinkleStatisticsService(req *models.SprinkleStatisticsRequest) ([]*models.SprinkleStatisticsDataList, error) { sprinkleDataList, err := module.SprinkleStatistics(req) if err != nil { return nil, err } return sprinkleDataList, nil } func AccountDistributionService(req *models.AccountDistributionRequest) error { return module.AccountDistribution(req) } func ForageCategoryDistributeService(req *models.CategoryRequest) error { feedClass := &models.FeedClass{ PastureId: int64(req.PastureId), FCCode: req.Number, FCName: req.Name, Enable: req.IsShow, BigFeedClassId: req.ParentId, BigFeedClassName: req.ParentName, GroupId: int64(req.GroupId), } return module.ForageCategoryDistribute(feedClass) } func CattleCategoryDistributeService(req *models.CategoryRequest) error { cowClass := &models.CowClass{ PastureId: int64(req.PastureId), ClassCode: req.Number, ClassName: req.Name, Enable: req.IsShow, ParentId: req.ParentId, ParentName: req.ParentName, GroupId: int64(req.GroupId), } return module.CattleCategoryDistribute(cowClass) } func CategoryDeleteService(keyWord string, req *models.CategoryDeleteRequest) error { switch keyWord { case "feed_delete": return module.ForageCategoryDelete(int64(req.PastureId), int64(req.GroupId)) case "cow_delete": return module.CowCategoryDelete(int64(req.PastureId), int64(req.GroupId)) } return nil } func FeedUsageService(req *models.FeedFormulaUsageRequest) (*models.FeedFormulaUsageResponse, error) { return module.FeedTemplateUsageDetail(req) } func FeedFormulaVersionService(pastureId int64, req *models.FeedFormulaUpdateVersionRequest) error { return module.FeedFormulaVersion(pastureId, req) }