123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038 |
- package models
- import (
- "errors"
- "fmt"
- "github.com/nosixtools/solarlunar"
- "github.com/shopspring/decimal"
- "hongze/hongze_edb_lib/utils"
- "math"
- "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
-
- 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 {
- tmpHistoryVal := decimal.NewFromFloat(0)
- tmpHistoryValNum := 0
- tmpLenAllDataList := len(allDataList)
- tmpK := tmpLenAllDataList - 1
- lastDayStr := allDataList[tmpK].DataTime
- lastDayVal := allDataList[tmpK].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 := 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
- }
- }
- }
-
- tmpHistoryLastDate := lastDay.AddDate(year-lastDay.Year(), 0, moveDay)
- for i := 0; i <= 35; i++ {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, i)
- if val, ok := existMap[tmpDate.Format(utils.FormatDate)]; ok {
- tmpHistoryLastVal = val
- isFindHistoryLast = true
- break
- } else {
- tmpDate := tmpHistoryLastDate.AddDate(0, 0, -i)
- if val, ok := existMap[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
-
- 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
- }
|