1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063 |
- package models
- import (
- "errors"
- "fmt"
- "github.com/nosixtools/solarlunar"
- "github.com/shopspring/decimal"
- "hongze/hongze_edb_lib/utils"
- "math"
- "strings"
- "time"
- )
- func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData) {
- newPredictEdbInfoData = predictEdbInfoData
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, v := range dayList {
- newPredictEdbInfoData = append(newPredictEdbInfoData, &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + k,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- })
- existMap[v.Format(utils.FormatDate)] = dataValue
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, currentDate := range dayList {
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDate.Format(utils.FormatDate),
-
- }
- var val float64
- var calculateStatus bool
-
-
- preDate := currentDate.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateStr]; ok {
- val = PredictTbzDiv(preValue, tbValue)
- 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 = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok {
- val = PredictTbzDiv(preValue, tbValue)
- 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 = PredictTbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = PredictTbzDiv(preValue, tbValue)
- 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
- }
- func PredictTbzDiv(a, b float64) (result float64) {
- if b != 0 {
-
- af := decimal.NewFromFloat(a)
-
- bf := decimal.NewFromFloat(b)
-
- cf := decimal.NewFromFloat(1)
-
- val := bf.Add(cf)
-
- result, _ = val.Mul(af).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleTc(edbInfoId int, tcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, currentDate := range dayList {
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDate.Format(utils.FormatDate),
-
- }
- var val float64
- var calculateStatus bool
-
-
- preDate := currentDate.AddDate(-1, 0, 0)
- preDateStr := preDate.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateStr]; ok {
- val = PredictTczDiv(preValue, tcValue)
- 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 = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- }
- }
- nextDateDay = nextDateDay.AddDate(0, 0, 1)
- preDateDay = preDateDay.AddDate(0, 0, -1)
- }
- case "季度", "年度":
- if preValue, ok := existMap[preDateStr]; ok {
- val = PredictTczDiv(preValue, tcValue)
- 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 = PredictTczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = PredictTczDiv(preValue, tcValue)
- 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
- }
- func PredictTczDiv(a, b float64) (result float64) {
- if b != 0 {
-
- af := decimal.NewFromFloat(a)
-
- bf := decimal.NewFromFloat(b)
-
- result, _ = af.Add(bf).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleHb(edbInfoId int, hbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := index + k - 1
-
- val := PredictHbzDiv(allDataList[tmpK].Value, hbValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- func PredictHbzDiv(a, b float64) (result float64) {
- if b != 0 {
-
- af := decimal.NewFromFloat(a)
-
- bf := decimal.NewFromFloat(b)
-
- cf := decimal.NewFromFloat(1)
-
- val := bf.Add(cf)
-
- result, _ = val.Mul(af).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleHc(edbInfoId int, hcValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := index + k - 1
-
- val := PredictHczDiv(allDataList[tmpK].Value, hcValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- func PredictHczDiv(a, b float64) (result float64) {
- if b != 0 {
-
- af := decimal.NewFromFloat(a)
-
- bf := decimal.NewFromFloat(b)
-
- result, _ = af.Add(bf).RoundCeil(4).Float64()
- } else {
- result = 0
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData < nValue || lenAllData <= 0 {
- return
- }
- if nValue <= 0 {
- return
- }
-
- decimalN := decimal.NewFromInt(int64(nValue))
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpIndex := lenAllData + k - 1
-
- tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
- for tmpK := 2; tmpK <= nValue; tmpK++ {
- tmpIndex2 := tmpIndex - tmpK
- tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
- tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
- }
-
- val, _ := tmpDecimalVal.Div(decimalN).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
-
-
-
-
-
-
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData < nValue || lenAllData <= 0 {
- return
- }
- if nValue <= 1 {
- return
- }
-
-
- coordinateData := make([]Coordinate, 0)
- for tmpK := nValue; tmpK > 0; tmpK-- {
- tmpIndex2 := lenAllData - tmpK
- tmpCoordinate := Coordinate{
- X: float64(nValue - tmpK + 1),
- Y: allDataList[tmpIndex2].Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate)
- }
- a, b := getLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
-
- aDecimal := decimal.NewFromFloat(a)
- bDecimal := decimal.NewFromFloat(b)
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := nValue + k + 1
- xDecimal := decimal.NewFromInt(int64(tmpK))
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
- DataTime: currentDateStr,
- Value: val,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- type Series []Coordinate
- type Coordinate struct {
- X, Y float64
- }
- func getLinearResult(s []Coordinate) (gradient, intercept float64) {
- if len(s) <= 1 {
- return
- }
-
- var sum [5]float64
-
- i := 0
- for ; i < len(s); i++ {
- sum[0] += s[i].X
- sum[1] += s[i].Y
- sum[2] += s[i].X * s[i].X
- sum[3] += s[i].X * s[i].Y
- sum[4] += s[i].Y * s[i].Y
- }
-
- f := float64(i)
- gradient = (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
- intercept = (sum[1] / f) - (gradient * sum[0] / f)
-
-
-
-
-
-
-
-
- return
- }
- func GetChartPredictEdbInfoDataListByRuleTrendsHC(edbInfoId, configId int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData <= 0 {
- return
- }
- hcDataMap := make(map[string]float64)
- tmpPredictEdbRuleDataList, err := GetPredictEdbRuleDataItemList(edbInfoId, configId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
- if err != nil {
- return
- }
- for _, v := range tmpPredictEdbRuleDataList {
- hcDataMap[v.DataTime] = v.Value
- }
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
-
- tmpLenAllDataList := len(allDataList)
- lastValue := allDataList[tmpLenAllDataList-1].Value
-
- currentDateStr := currentDate.Format(utils.FormatDate)
- hcVal, ok := hcDataMap[currentDateStr]
- if !ok {
- continue
- }
- lastValueDecimal := decimal.NewFromFloat(lastValue)
- hcValDecimal := decimal.NewFromFloat(hcVal)
- val, _ := lastValueDecimal.Add(hcValDecimal).RoundCeil(4).Float64()
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
-
- DataTime: currentDateStr,
- Value: val,
-
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- lenDay := len(dayList)
- if lenDay <= 0 {
- return
- }
- var hcValue float64
- lastValueDeciamal := decimal.NewFromFloat(allDataList[index-1].Value)
- finalValueDeciamal := decimal.NewFromFloat(finalValue)
- dayDecimal := decimal.NewFromInt(int64(lenDay))
- hcValue, _ = finalValueDeciamal.Sub(lastValueDeciamal).Div(dayDecimal).Float64()
-
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- lastK := lenDay - 1
- for k, currentDate := range dayList {
- tmpK := index + k - 1
- var val float64
-
- if k == lastK {
- val = finalValue
- } else {
- val = PredictHczDiv(allDataList[tmpK].Value, hcValue)
- }
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
-
- DataTime: currentDateStr,
- Value: val,
-
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- type SeasonConf struct {
- Calendar string `description:"公历、农历"`
- YearType int `description:"选择方式,1:连续N年;2:指定年份"`
- NValue int `description:"连续N年"`
- YearList []int `description:"指定年份列表"`
- }
- func GetChartPredictEdbInfoDataListByRuleSeason(edbInfoId int, yearsList []int, calendar string, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
-
- handleDataMap := make(map[string]float64)
- err = HandleDataByLinearRegression(allDataList, handleDataMap)
- if err != nil {
- return
- }
-
- moveDayMap := make(map[int]int, 0)
- {
- if calendar == "公历" {
- for _, year := range yearsList {
- moveDayMap[year] = 0
- }
- } else {
- currentDay := time.Now()
- if currentDay.Month() >= 11 {
- currentDay = currentDay.AddDate(1, 0, 0)
- }
- currentYear := currentDay.Year()
- currentYearCjnl := fmt.Sprintf("%d-01-01", currentYear)
- currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false)
- currentYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, currentYearCjgl, time.Local)
- if tmpErr != nil {
- err = errors.New("当前春节公历日期转换失败:" + tmpErr.Error())
- return
- }
-
- for _, year := range yearsList {
- tmpYearCjnl := fmt.Sprintf("%d-01-01", year)
- tmpYearCjgl := solarlunar.LunarToSolar(tmpYearCjnl, false)
-
- tmpYearCjglTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpYearCjgl, time.Local)
- if tmpErr != nil {
- err = errors.New(fmt.Sprintf("%d公历日期转换失败:%s", year, tmpErr.Error()))
- return
- }
- tmpCurrentYearCjglTime := currentYearCjglTime.AddDate(year-currentYear, 0, 0)
- moveDay := utils.GetTimeSubDay(tmpYearCjglTime, tmpCurrentYearCjglTime)
- moveDayMap[year] = moveDay
- }
- }
- }
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
-
- predictEdbInfoData = make([]*EdbInfoSearchData, 0)
- for k, currentDate := range dayList {
-
- if strings.Contains(currentDate.Format(utils.FormatDate), "02-29") {
- continue
- }
- tmpHistoryVal := decimal.NewFromFloat(0)
- tmpHistoryValNum := 0
- tmpLenAllDataList := len(allDataList)
- tmpK := tmpLenAllDataList - 1
- lastDayData := allDataList[tmpK]
- lastDayStr := lastDayData.DataTime
- lastDayVal := lastDayData.Value
- lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, lastDayStr, time.Local)
- if tmpErr != nil {
- err = errors.New("获取上期日期转换失败:" + tmpErr.Error())
- }
- for _, year := range yearsList {
- moveDay := moveDayMap[year]
- var tmpHistoryCurrentVal, tmpHistoryLastVal float64
- var isFindHistoryCurrent, isFindHistoryLast bool
-
- tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, -moveDay)
- for i := 0; i <= 35; i++ {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- } else {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- }
- }
- }
-
- tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, -moveDay)
- for i := 0; i <= 35; i++ {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryLastVal = val
- isFindHistoryLast = true
- break
- } else {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
- if val, ok := handleDataMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryLastVal = val
- isFindHistoryLast = true
- break
- }
- }
- }
-
- if isFindHistoryCurrent && isFindHistoryLast {
- af := decimal.NewFromFloat(tmpHistoryCurrentVal)
- bf := decimal.NewFromFloat(tmpHistoryLastVal)
- tmpHistoryVal = tmpHistoryVal.Add(af.Sub(bf))
- tmpHistoryValNum++
- }
- }
-
- if tmpHistoryValNum != len(yearsList) {
- continue
- }
- lastDayValDec := decimal.NewFromFloat(lastDayVal)
- val, _ := tmpHistoryVal.Div(decimal.NewFromInt(int64(tmpHistoryValNum))).Add(lastDayValDec).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + index + k,
-
- DataTime: currentDateStr,
- Value: val,
-
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- err = HandleDataByLinearRegression([]*EdbInfoSearchData{
- lastDayData, tmpData,
- }, handleDataMap)
- if err != nil {
- return
- }
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
- type MoveAverageConf struct {
- Year int `description:"指定年份"`
- NValue int `description:"N期的数据"`
- }
- func GetChartPredictEdbInfoDataListByRuleMoveAverageTb(edbInfoId int, nValue, year int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*EdbInfoSearchData, existMap map[string]float64) (newPredictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
- allDataList := make([]*EdbInfoSearchData, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- lenAllData := len(allDataList)
- if lenAllData < nValue || lenAllData <= 0 {
- return
- }
- if nValue <= 0 {
- return
- }
-
- decimalN := decimal.NewFromInt(int64(nValue))
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpLenAllDataList := len(allDataList)
- tmpIndex := tmpLenAllDataList - 1
- averageDateList := make([]string, 0)
-
- tmpDecimalVal := decimal.NewFromFloat(allDataList[tmpIndex].Value)
- averageDateList = append(averageDateList, allDataList[tmpIndex].DataTime)
- for tmpK := 2; tmpK <= nValue; tmpK++ {
- tmpIndex2 := tmpIndex - tmpK
- tmpDecimalVal2 := decimal.NewFromFloat(allDataList[tmpIndex2].Value)
- tmpDecimalVal = tmpDecimalVal.Add(tmpDecimalVal2)
- averageDateList = append(averageDateList, allDataList[tmpIndex2].DataTime)
- }
-
- tmpAverageVal := tmpDecimalVal.Div(decimalN)
- var tmpHistoryCurrentVal float64
- var isFindHistoryCurrent, isFindHistoryLast bool
- tmpHistoryDecimalVal := decimal.NewFromFloat(0)
- {
-
- tmpHistoryValNum := 0
- {
-
- tmpHistoryCurrentDate := currentDate.AddDate(year-currentDate.Year(), 0, 0)
- for i := 0; i <= 35; i++ {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- } else {
- tmpDate := tmpHistoryCurrentDate.AddDate(0, 0, -i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryCurrentVal = val
- isFindHistoryCurrent = true
- break
- }
- }
- }
- }
- for _, averageDate := range averageDateList {
- lastDay, tmpErr := time.ParseInLocation(utils.FormatDate, averageDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, 0)
- for i := 0; i <= 35; i++ {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpDecimalVal2 := decimal.NewFromFloat(val)
- tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
- tmpHistoryValNum++
- break
- } else {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpDecimalVal2 := decimal.NewFromFloat(val)
- tmpHistoryDecimalVal = tmpHistoryDecimalVal.Add(tmpDecimalVal2)
- tmpHistoryValNum++
- break
- }
- }
- }
- }
-
- if tmpHistoryValNum == nValue {
- isFindHistoryLast = true
- }
- }
-
- if !isFindHistoryLast || !isFindHistoryCurrent {
- continue
- }
-
- tbVal := tmpAverageVal.Div(tmpHistoryDecimalVal)
-
- val, _ := decimal.NewFromFloat(tmpHistoryCurrentVal).Mul(tbVal).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &EdbInfoSearchData{
- EdbDataId: edbInfoId + 10000000000 + lenAllData + k,
-
- DataTime: currentDateStr,
- Value: val,
-
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- if val < minValue {
- minValue = val
- }
- if val > maxValue {
- maxValue = val
- }
- }
- return
- }
|