package data import ( "errors" "hongze/hongze_chart_lib/models" "hongze/hongze_chart_lib/models/data_manage" "hongze/hongze_chart_lib/utils" "time" ) // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据 func GetChartPredictEdbInfoDataList(predictEdbConf data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*models.EdbDataList, err error) { endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local) if err != nil { return } latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local) if err != nil { return } // 开始预测数据的时间 startDate := latestDate // 如果有筛选时间的话 if filtrateStartDateStr != `` { filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local) if tmpErr != nil { err = tmpErr return } //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间 if filtrateStartDate.After(latestDate) { startDate = filtrateStartDate.AddDate(0, 0, -1) } } dataValue := lastDataValue if predictEdbConf.RuleType == 2 { dataValue = predictEdbConf.FixedValue } //获取后面的预测数据 dayList := getPredictEdbDayList(startDate, endDate, frequency) predictEdbInfoData = make([]*models.EdbDataList, 0) for k, v := range dayList { predictEdbInfoData = append(predictEdbInfoData, &models.EdbDataList{ EdbDataId: predictEdbConf.PredictEdbInfoId + 10000000000 + k, EdbInfoId: predictEdbConf.PredictEdbInfoId, DataTime: v.Format(utils.FormatDate), Value: dataValue, DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适 }) } return } // GetPredictEdbDayList 获取预测指标日期列表 func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) { //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency) switch frequency { case "日度": for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) { //周六、日排除 if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday { continue } dayList = append(dayList, currDate) } case "周度": //nextDate := startDate.AddDate(0, 0, 7) for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) { dayList = append(dayList, currDate) } case "月度": for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); { currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 2, -1) if !currDate.After(endDate) { dayList = append(dayList, currDate) } } } return } // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据 func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) { edbInfo, err := data_manage.GetEdbInfoById(edbInfoId) if err != nil { errMsg = `获取预测指标信息失败` return } return GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween) } // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据 func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) { // 查找该预测指标配置 predictEdbConf, err = data_manage.GetPredictEdbConfById(edbInfo.EdbInfoId) if err != nil && err.Error() != utils.ErrNoRow() { errMsg = "获取预测指标配置信息失败" return } if predictEdbConf == nil { errMsg = "获取预测指标配置信息失败" err = errors.New(errMsg) return } // 来源指标 sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { errMsg = "找不到来源指标信息" err = errors.New(errMsg) } return } //获取指标数据(实际已生成) dataList, err = models.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate) if err != nil { return } // 获取预测指标未来的数据 predictDataList := make([]*models.EdbDataList, 0) endDateStr := edbInfo.EndDate //预测指标的结束日期 if isTimeBetween { //如果是时间区间,那么 reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local) endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local) // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天 if reqEndDateTime.Before(endDateTime) { endDateStr = endDate } } predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency) if err != nil { return } dataList = append(dataList, predictDataList...) if len(predictDataList) > 0 { tmpValue := predictDataList[0] // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回 if edbInfo.MaxValue < tmpValue.Value { edbInfo.MaxValue = tmpValue.Value } // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回 if edbInfo.MinValue > tmpValue.Value { edbInfo.MinValue = tmpValue.Value } } return }