123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package chart
- import (
- "errors"
- "github.com/shopspring/decimal"
- edbDataModel "hongze/hongze_yb/models/tables/edb_data"
- edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
- predictEdbConfModel "hongze/hongze_yb/models/tables/predict_edb_conf"
- "hongze/hongze_yb/utils"
- "strconv"
- "time"
- )
- // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据
- func GetChartPredictEdbInfoDataList(predictEdbConf predictEdbConfModel.PredictEdbConf, filtrateStartDateStr, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*edbDataModel.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([]*edbDataModel.EdbDataList, 0)
- for k, v := range dayList {
- predictEdbInfoData = append(predictEdbInfoData, &edbDataModel.EdbDataList{
- EdbDataId: int(predictEdbConf.PredictEdbInfoID) + 10000000000 + k,
- EdbInfoId: int(predictEdbConf.PredictEdbInfoID),
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- DataTimestamp: (v.UnixNano() / 1e6) + 1000, //前端需要让加1s,说是2022-09-01 00:00:00 这样的整点不合适
- })
- }
- return
- }
- // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
- func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*predictEdbConfModel.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency string, realPredictEdbInfoData []*edbDataModel.EdbDataList) (predictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, 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)
- }
- }
- //var dateArr []string
- // 对应日期的值
- existMap := make(map[string]float64)
- for _, v := range realPredictEdbInfoData {
- //dateArr = append(dateArr, v.DataTime)
- existMap[v.DataTime] = v.Value
- }
- predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
- //dataValue := lastDataValue
- //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
- for _, predictEdbConf := range predictEdbConfList {
- dataEndTime := endDate
- if predictEdbConf.EndDate.Before(dataEndTime) {
- dataEndTime = predictEdbConf.EndDate
- }
- var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
- switch predictEdbConf.RuleType {
- case 1: //1:最新
- var lastDataValue float64 //最新值
- tmpAllData := make([]*edbDataModel.EdbDataList, 0)
- tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
- tmpAllData = append(tmpAllData, predictEdbInfoData...)
- lenTmpAllData := len(tmpAllData)
- if lenTmpAllData > 0 {
- lastDataValue = tmpAllData[lenTmpAllData-1].Value
- }
- predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), lastDataValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- tmpMaxValue = lastDataValue
- tmpMinValue = lastDataValue
- case 2: //2:固定值
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- dataValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(int(predictEdbConf.PredictEdbInfoID), dataValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- tmpMaxValue = dataValue
- tmpMinValue = dataValue
- case 3: //3:同比
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tbValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(int(predictEdbConf.PredictEdbInfoID), tbValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- case 4: //4:同差
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tcValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(int(predictEdbConf.PredictEdbInfoID), tcValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- case 5: //5:环比
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- hbValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(int(predictEdbConf.PredictEdbInfoID), hbValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- case 6: //6:环差
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- hcValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(int(predictEdbConf.PredictEdbInfoID), hcValue, startDate, dataEndTime, frequency, predictEdbInfoData, existMap)
- case 7: //7:N期移动均值
- nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(int(predictEdbConf.PredictEdbInfoID), nValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 8: //8:N期段线性外推值
- nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNLinearRegression(int(predictEdbConf.PredictEdbInfoID), nValue, startDate, dataEndTime, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- }
- //startDate = dataEndTime.AddDate(0, 0, 1)
- startDate = dataEndTime
- if tmpMinValue < minValue {
- minValue = tmpMinValue
- }
- if tmpMaxValue < maxValue {
- maxValue = tmpMaxValue
- }
- }
- 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 []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
- edbInfo, err := edbInfoModel.GetEdbInfoById(edbInfoId)
- if err != nil {
- errMsg = `获取预测指标信息失败`
- return
- }
- return GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
- }
- // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
- func GetPredictDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
- // 非计算指标,直接从表里获取数据
- if edbInfo.EdbType != 1 {
- if !isTimeBetween {
- endDate = ``
- }
- return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
- }
- // 查找该预测指标配置
- predictEdbConfList, err := predictEdbConfModel.GetPredictEdbConfListById(edbInfo.EdbInfoId)
- if err != nil {
- errMsg = "获取预测指标配置信息失败"
- return
- }
- if len(predictEdbConfList) == 0 {
- errMsg = "获取预测指标配置信息失败"
- err = errors.New(errMsg)
- return
- }
- predictEdbConf = predictEdbConfList[0]
- // 来源指标
- sourceEdbInfoItem, err = edbInfoModel.GetEdbInfoById(int(predictEdbConf.SourceEdbInfoID))
- if err != nil {
- if err == utils.ErrNoRow {
- errMsg = "找不到来源指标信息"
- err = errors.New(errMsg)
- }
- return
- }
- allDataList := make([]*edbDataModel.EdbDataList, 0)
- //获取指标数据(实际已生成)
- dataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
- if err != nil {
- return
- }
- // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
- if startDate != `` {
- allDataList, err = edbDataModel.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
- if err != nil {
- return
- }
- } else {
- allDataList = dataList
- }
- // 获取预测指标未来的数据
- predictDataList := make([]*edbDataModel.EdbDataList, 0)
- endDateStr := edbInfo.EndDate.Format(utils.FormatDate) //预测指标的结束日期
- if isTimeBetween { //如果是时间区间,那么
- reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
- if reqEndDateTime.Before(edbInfo.EndDate) {
- endDateStr = endDate
- }
- }
- //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
- var predictMinValue, predictMaxValue float64
- predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate.Format(utils.FormatDate), endDateStr, edbInfo.Frequency, allDataList)
- if err != nil {
- return
- }
- dataList = append(dataList, predictDataList...)
- if len(predictDataList) > 0 {
- // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
- if edbInfo.MinValue > predictMinValue {
- edbInfo.MinValue = predictMinValue
- }
- // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
- if edbInfo.MaxValue < predictMaxValue {
- edbInfo.MaxValue = predictMaxValue
- }
- }
- return
- }
- // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
- func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *edbInfoModel.EdbInfo, startDate, endDate string) (dataList []*edbDataModel.EdbDataList, sourceEdbInfoItem *edbInfoModel.EdbInfo, predictEdbConf *predictEdbConfModel.PredictEdbConf, err error, errMsg string) {
- dataList, err = edbDataModel.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
- return
- }
|