|
@@ -8,6 +8,7 @@ import (
|
|
"hongze/hongze_chart_lib/models"
|
|
"hongze/hongze_chart_lib/models"
|
|
"hongze/hongze_chart_lib/models/data_manage"
|
|
"hongze/hongze_chart_lib/models/data_manage"
|
|
"hongze/hongze_chart_lib/utils"
|
|
"hongze/hongze_chart_lib/utils"
|
|
|
|
+ "math"
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
@@ -1073,3 +1074,423 @@ func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, ye
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func GetChartPredictEdbInfoDataListByRuleTbzscz(edbInfoId int, tbEndValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64) {
|
|
|
|
+ allDataList := make([]*models.EdbDataList, 0)
|
|
|
|
+ allDataList = append(allDataList, realPredictEdbInfoData...)
|
|
|
|
+ allDataList = append(allDataList, predictEdbInfoData...)
|
|
|
|
+ newPredictEdbInfoData = predictEdbInfoData
|
|
|
|
+
|
|
|
|
+ index := len(allDataList)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if index <= 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ lastData := allDataList[index-1]
|
|
|
|
+ lastDayTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
|
|
|
|
+
|
|
|
|
+ var lastTb decimal.Decimal
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ preDate := lastDayTime.AddDate(-1, 0, 0)
|
|
|
|
+ preDateStr := preDate.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ } else {
|
|
|
|
+ switch frequency {
|
|
|
|
+ case "月度":
|
|
|
|
+
|
|
|
|
+ nextDateDay := preDate
|
|
|
|
+ preDateDay := preDate
|
|
|
|
+ for i := 0; i <= 35; i++ {
|
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[nextDateDayStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateDayStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ nextDateDay = nextDateDay.AddDate(0, 0, 1)
|
|
|
|
+ preDateDay = preDateDay.AddDate(0, 0, -1)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ case "季度", "年度":
|
|
|
|
+ if preValue, ok := existMap[preDateStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ default:
|
|
|
|
+ nextDateDay := preDate
|
|
|
|
+ preDateDay := preDate
|
|
|
|
+
|
|
|
|
+ for i := 0; i < 35; i++ {
|
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[nextDateDayStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateDayStr]; ok {
|
|
|
|
+ lastTb = decimal.NewFromFloat(lastData.Value).Div(decimal.NewFromFloat(preValue))
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ nextDateDay = nextDateDay.AddDate(0, 0, 1)
|
|
|
|
+ preDateDay = preDateDay.AddDate(0, 0, -1)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ lenDay := len(dayList)
|
|
|
|
+ tbEndValueDecimal := decimal.NewFromFloat(tbEndValue)
|
|
|
|
+ avgTbVal := tbEndValueDecimal.Sub(lastTb).Div(decimal.NewFromInt(int64(lenDay)))
|
|
|
|
+
|
|
|
|
+ fmt.Println(lastTb.Float64())
|
|
|
|
+ fmt.Println(decimal.NewFromFloat(tbEndValue).Sub(lastTb))
|
|
|
|
+ fmt.Println(avgTbVal.Float64())
|
|
|
|
+
|
|
|
|
+ predictEdbInfoData = make([]*models.EdbDataList, 0)
|
|
|
|
+ for k, currentDate := range dayList {
|
|
|
|
+ var tbValue decimal.Decimal
|
|
|
|
+ if k == lenDay-1 {
|
|
|
|
+ tbValue = tbEndValueDecimal
|
|
|
|
+ } else {
|
|
|
|
+ tbValue = lastTb.Add(avgTbVal.Mul(decimal.NewFromInt(int64(k + 1))))
|
|
|
|
+ }
|
|
|
|
+ tmpData := &models.EdbDataList{
|
|
|
|
+ EdbDataId: edbInfoId + 100000 + index + k,
|
|
|
|
+ EdbInfoId: edbInfoId,
|
|
|
|
+ DataTime: currentDate.Format(utils.FormatDate),
|
|
|
|
+
|
|
|
|
+ DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000,
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var val float64
|
|
|
|
+ var calculateStatus bool
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ preDate := currentDate.AddDate(-1, 0, 0)
|
|
|
|
+ preDateStr := preDate.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ } else {
|
|
|
|
+ switch frequency {
|
|
|
|
+ case "月度":
|
|
|
|
+
|
|
|
|
+ nextDateDay := preDate
|
|
|
|
+ preDateDay := preDate
|
|
|
|
+ for i := 0; i <= 35; i++ {
|
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[nextDateDayStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateDayStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ nextDateDay = nextDateDay.AddDate(0, 0, 1)
|
|
|
|
+ preDateDay = preDateDay.AddDate(0, 0, -1)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ case "季度", "年度":
|
|
|
|
+ if preValue, ok := existMap[preDateStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ default:
|
|
|
|
+ nextDateDay := preDate
|
|
|
|
+ preDateDay := preDate
|
|
|
|
+
|
|
|
|
+ for i := 0; i < 35; i++ {
|
|
|
|
+ nextDateDayStr := nextDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[nextDateDayStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+ preDateDayStr := preDateDay.Format(utils.FormatDate)
|
|
|
|
+ if preValue, ok := existMap[preDateDayStr]; ok {
|
|
|
|
+ val, _ = decimal.NewFromFloat(preValue).Mul(tbValue).RoundCeil(4).Float64()
|
|
|
|
+ calculateStatus = true
|
|
|
|
+ break
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ nextDateDay = nextDateDay.AddDate(0, 0, 1)
|
|
|
|
+ preDateDay = preDateDay.AddDate(0, 0, -1)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if calculateStatus {
|
|
|
|
+ tmpData.Value = val
|
|
|
|
+ newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
|
|
|
|
+ allDataList = append(allDataList, tmpData)
|
|
|
|
+ existMap[tmpData.DataTime] = val
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if val < minValue {
|
|
|
|
+ minValue = val
|
|
|
|
+ }
|
|
|
|
+ if val > maxValue {
|
|
|
|
+ maxValue = val
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+type RuleLineNhConf struct {
|
|
|
|
+ StartDate string `description:"开始日期"`
|
|
|
|
+ EndDate string `description:"结束日期"`
|
|
|
|
+ MoveDay int `description:"移动天数"`
|
|
|
|
+ EdbInfoId int `description:"指标id"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func GetChartPredictEdbInfoDataListByRuleLineNh(edbInfoId int, dayList []time.Time, realPredictEdbInfoData, predictEdbInfoData []*models.EdbDataList, newNhccDataMap, existMap map[string]float64) (newPredictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
|
|
|
|
+ allDataList := make([]*models.EdbDataList, 0)
|
|
|
|
+ allDataList = append(allDataList, realPredictEdbInfoData...)
|
|
|
|
+ allDataList = append(allDataList, predictEdbInfoData...)
|
|
|
|
+ newPredictEdbInfoData = predictEdbInfoData
|
|
|
|
+
|
|
|
|
+ lenAllData := len(allDataList)
|
|
|
|
+ if lenAllData <= 0 {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for k, currentDate := range dayList {
|
|
|
|
+
|
|
|
|
+ currentDateStr := currentDate.Format(utils.FormatDate)
|
|
|
|
+ val, ok := newNhccDataMap[currentDateStr]
|
|
|
|
+ if !ok {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ tmpData := &models.EdbDataList{
|
|
|
|
+ EdbDataId: edbInfoId + 100000 + lenAllData + k,
|
|
|
|
+ EdbInfoId: edbInfoId,
|
|
|
|
+ DataTime: currentDateStr,
|
|
|
|
+ Value: val,
|
|
|
|
+ DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000,
|
|
|
|
+ }
|
|
|
|
+ newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
|
|
|
|
+ allDataList = append(allDataList, tmpData)
|
|
|
|
+ existMap[currentDateStr] = val
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if val < minValue {
|
|
|
|
+ minValue = val
|
|
|
|
+ }
|
|
|
|
+ if val > maxValue {
|
|
|
|
+ maxValue = val
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func getCalculateNhccData(secondDataList []*models.EdbDataList, ruleConf RuleLineNhConf) (newBDataMap map[string]float64, err error) {
|
|
|
|
+ firstEdbInfoId := ruleConf.EdbInfoId
|
|
|
|
+ moveDay := ruleConf.MoveDay
|
|
|
|
+ startDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.StartDate, time.Local)
|
|
|
|
+ endDate, _ := time.ParseInLocation(utils.FormatDate, ruleConf.EndDate, time.Local)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ edbInfo, err := data_manage.GetEdbInfoById(firstEdbInfoId)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ aDataList := make([]models.EdbDataList, 0)
|
|
|
|
+ aDataMap := make(map[string]float64)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ var firstDataList []*models.EdbDataList
|
|
|
|
+ switch edbInfo.EdbInfoType {
|
|
|
|
+ case 0:
|
|
|
|
+ firstDataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
|
|
|
|
+ case 1:
|
|
|
|
+ _, firstDataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
|
|
|
|
+ default:
|
|
|
|
+ err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
|
|
|
|
+ }
|
|
|
|
+ if err != nil {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ aDataList, aDataMap = handleNhccData(firstDataList, moveDay)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ bDataList := make([]models.EdbDataList, 0)
|
|
|
|
+ bDataMap := make(map[string]float64)
|
|
|
|
+ {
|
|
|
|
+ bDataList, bDataMap = handleNhccData(secondDataList, 0)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(aDataList) <= 0 {
|
|
|
|
+ err = errors.New("指标A没有数据")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if len(bDataList) <= 0 {
|
|
|
|
+ err = errors.New("指标B没有数据")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ {
|
|
|
|
+ endAData := aDataList[len(aDataList)-1]
|
|
|
|
+ tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, endAData.DataTime, time.Local)
|
|
|
|
+ if tmpErr != nil {
|
|
|
|
+ err = tmpErr
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if tmpEndDate.Before(endDate) {
|
|
|
|
+ endDate = tmpEndDate
|
|
|
|
+ }
|
|
|
|
+ endBData := bDataList[len(bDataList)-1]
|
|
|
|
+ tmpEndDate, tmpErr = time.ParseInLocation(utils.FormatDate, endBData.DataTime, time.Local)
|
|
|
|
+ if tmpErr != nil {
|
|
|
|
+ err = tmpErr
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if tmpEndDate.Before(endDate) {
|
|
|
|
+ endDate = tmpEndDate
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ var a, b float64
|
|
|
|
+ {
|
|
|
|
+ coordinateData := make([]utils.Coordinate, 0)
|
|
|
|
+ for i := startDate; i.Before(endDate) || i.Equal(endDate); i = i.AddDate(0, 0, 1) {
|
|
|
|
+ dateStr := i.Format(utils.FormatDate)
|
|
|
|
+ xValue, ok := aDataMap[dateStr]
|
|
|
|
+ if !ok {
|
|
|
|
+ err = errors.New("指标A日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ yValue, ok := bDataMap[dateStr]
|
|
|
|
+ if !ok {
|
|
|
|
+ err = errors.New("指标B日期:" + dateStr + "数据异常,导致计算线性方程公式失败")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ tmpCoordinate := utils.Coordinate{
|
|
|
|
+ X: xValue,
|
|
|
|
+ Y: yValue,
|
|
|
|
+ }
|
|
|
|
+ coordinateData = append(coordinateData, tmpCoordinate)
|
|
|
|
+ }
|
|
|
|
+ a, b = utils.GetLinearResult(coordinateData)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if math.IsNaN(a) || math.IsNaN(b) {
|
|
|
|
+ err = errors.New("线性方程公式生成失败")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ newBDataMap = make(map[string]float64)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ aDecimal := decimal.NewFromFloat(a)
|
|
|
|
+ bDecimal := decimal.NewFromFloat(b)
|
|
|
|
+ for _, aData := range aDataList {
|
|
|
|
+ xDecimal := decimal.NewFromFloat(aData.Value)
|
|
|
|
+ val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
|
|
|
|
+ newBDataMap[aData.DataTime] = val
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+func handleNhccData(dataList []*models.EdbDataList, moveDay int) (newDataList []models.EdbDataList, dateDataMap map[string]float64) {
|
|
|
|
+ dateMap := make(map[time.Time]float64)
|
|
|
|
+ var minDate, maxDate time.Time
|
|
|
|
+ dateDataMap = make(map[string]float64)
|
|
|
|
+
|
|
|
|
+ for _, v := range dataList {
|
|
|
|
+ currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
|
|
|
|
+ if minDate.IsZero() || currDate.Before(minDate) {
|
|
|
|
+ minDate = currDate
|
|
|
|
+ }
|
|
|
|
+ if maxDate.IsZero() || currDate.After(maxDate) {
|
|
|
|
+ maxDate = currDate
|
|
|
|
+ }
|
|
|
|
+ dateMap[currDate] = v.Value
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ newDateMap := make(map[time.Time]float64)
|
|
|
|
+ for currDate, value := range dateMap {
|
|
|
|
+ newDate := currDate.AddDate(0, 0, moveDay)
|
|
|
|
+ newDateMap[newDate] = value
|
|
|
|
+ }
|
|
|
|
+ minDate = minDate.AddDate(0, 0, moveDay)
|
|
|
|
+ maxDate = maxDate.AddDate(0, 0, moveDay)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ dayNum := utils.GetTimeSubDay(minDate, maxDate)
|
|
|
|
+
|
|
|
|
+ for i := 0; i <= dayNum; i++ {
|
|
|
|
+ currDate := minDate.AddDate(0, 0, i)
|
|
|
|
+ tmpValue, ok := newDateMap[currDate]
|
|
|
|
+ if !ok {
|
|
|
|
+
|
|
|
|
+ if len(newDataList) <= 0 {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tmpValue = newDataList[len(newDataList)-1].Value
|
|
|
|
+ }
|
|
|
|
+ tmpData := models.EdbDataList{
|
|
|
|
+
|
|
|
|
+ DataTime: currDate.Format(utils.FormatDate),
|
|
|
|
+ Value: tmpValue,
|
|
|
|
+ }
|
|
|
|
+ dateDataMap[tmpData.DataTime] = tmpData.Value
|
|
|
|
+ newDataList = append(newDataList, tmpData)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return
|
|
|
|
+}
|