123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package data
- import (
- "errors"
- "hongze/hongze_chart_lib/models"
- "hongze/hongze_chart_lib/models/data_manage"
- "hongze/hongze_chart_lib/utils"
- "time"
- )
- 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,
- })
- }
- return
- }
- func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
-
- 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 "周度":
-
- 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
- }
- 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)
- }
- 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) {
-
- if edbInfo.EdbType != 1 {
- if !isTimeBetween {
- endDate = ``
- }
- return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
- }
-
- 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
- }
- func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
- dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, startDate, endDate)
- return
- }
|