123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497 |
- package chart
- import (
- "errors"
- "fmt"
- "github.com/nosixtools/solarlunar"
- "github.com/shopspring/decimal"
- edbDataModel "hongze/hongze_yb/models/tables/edb_data"
- edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
- predictEdbRuleDataModel "hongze/hongze_yb/models/tables/predict_edb_rule_data"
- "hongze/hongze_yb/utils"
- "math"
- "strings"
- "time"
- )
- func GetChartPredictEdbInfoDataListByRule1(edbInfoId int, dataValue float64, startDate, endDate time.Time, frequency string, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList) {
- newPredictEdbInfoData = predictEdbInfoData
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
- for k, v := range dayList {
- newPredictEdbInfoData = append(newPredictEdbInfoData, &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + k,
- EdbInfoId: edbInfoId,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- DataTimestamp: (v.UnixNano() / 1e6) + 1000,
- })
- existMap[v.Format(utils.FormatDate)] = dataValue
- }
- return
- }
- func GetChartPredictEdbInfoDataListByRuleTb(edbInfoId int, tbValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
- for k, currentDate := range dayList {
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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 = TbzDiv(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 = TbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = TbzDiv(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 = TbzDiv(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 = TbzDiv(preValue, tbValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = TbzDiv(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 TbzDiv(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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 0)
- allDataList = append(allDataList, realPredictEdbInfoData...)
- allDataList = append(allDataList, predictEdbInfoData...)
- newPredictEdbInfoData = predictEdbInfoData
- index := len(allDataList)
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
- for k, currentDate := range dayList {
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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 = TczDiv(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 = TczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = TczDiv(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 = TczDiv(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 = TczDiv(preValue, tcValue)
- calculateStatus = true
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- val = TczDiv(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 TczDiv(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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 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 := HbzDiv(allDataList[tmpK].Value, hbValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + index + 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 HbzDiv(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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 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 := HczDiv(allDataList[tmpK].Value, hcValue)
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + index + 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 HczDiv(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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 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 := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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 GetChartPredictEdbInfoDataListByRuleNLinearRegression(edbInfoId int, nValue int, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
-
-
-
-
-
-
- allDataList := make([]*edbDataModel.EdbDataList, 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
- }
-
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- for k, currentDate := range dayList {
- tmpK := nValue + k + 1
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(tmpK))
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).RoundCeil(4).Float64()
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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
- }
- 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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 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 := predictEdbRuleDataModel.GetPredictEdbRuleDataList(edbInfoId, configId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
- if err != nil {
- return
- }
- for _, v := range tmpPredictEdbRuleDataList {
- hcDataMap[v.DataTime.Format(utils.FormatDate)] = 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 := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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 GetChartPredictEdbInfoDataListByRuleFinalValueHc(edbInfoId int, finalValue float64, startDate, endDate time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.EdbDataList, 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([]*edbDataModel.EdbDataList, 0)
- lastK := lenDay - 1
- for k, currentDate := range dayList {
- tmpK := index + k - 1
- var val float64
-
- if k == lastK {
- val = finalValue
- } else {
- val = HczDiv(allDataList[tmpK].Value, hcValue)
- }
- currentDateStr := currentDate.Format(utils.FormatDate)
- tmpData := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + index + 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
- }
- 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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*edbDataModel.EdbDataList, 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([]*edbDataModel.EdbDataList, 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 := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + index + k,
- EdbInfoId: edbInfoId,
- DataTime: currentDateStr,
- Value: val,
- DataTimestamp: (currentDate.UnixNano() / 1e6) + 1000,
- }
- newPredictEdbInfoData = append(newPredictEdbInfoData, tmpData)
- allDataList = append(allDataList, tmpData)
- existMap[currentDateStr] = val
-
- err = HandleDataByLinearRegression([]*edbDataModel.EdbDataList{
- 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 []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*edbDataModel.EdbDataList, 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 := &edbDataModel.EdbDataList{
- EdbDataId: edbInfoId + 10000000000 + 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 GetChartPredictEdbInfoDataListByRuleTbzscz(edbInfoId int, tbEndValue float64, dayList []time.Time, frequency string, realPredictEdbInfoData, predictEdbInfoData []*edbDataModel.EdbDataList, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64) {
- allDataList := make([]*edbDataModel.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)).Sub(decimal.NewFromFloat(preValue)).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)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).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)).Sub(decimal.NewFromFloat(preValue)).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)).Sub(decimal.NewFromFloat(preValue)).Div(decimal.NewFromFloat(preValue))
- break
- } else {
- preDateDayStr := preDateDay.Format(utils.FormatDate)
- if preValue, ok := existMap[preDateDayStr]; ok {
- lastTb = (decimal.NewFromFloat(lastData.Value)).Sub(decimal.NewFromFloat(preValue)).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)))
- predictEdbInfoData = make([]*edbDataModel.EdbDataList, 0)
- for k, currentDate := range dayList {
- var tbValue decimal.Decimal
- if k == lenDay-1 {
- tbValue = tbEndValueDecimal.Add(decimal.NewFromInt(1))
- } else {
- tbValue = lastTb.Add(avgTbVal.Mul(decimal.NewFromInt(int64(k + 1)))).Add(decimal.NewFromInt(1))
- }
- tmpData := &edbDataModel.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 []*edbDataModel.EdbDataList, newNhccDataMap, existMap map[string]float64) (newPredictEdbInfoData []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
- allDataList := make([]*edbDataModel.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 := &edbDataModel.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 []*edbDataModel.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 := edbInfoModel.GetEdbInfoById(firstEdbInfoId)
- if err != nil {
- return
- }
-
- aDataList := make([]edbDataModel.EdbDataList, 0)
- aDataMap := make(map[string]float64)
- {
-
- var firstDataList []*edbDataModel.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- firstDataList, err = edbDataModel.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([]edbDataModel.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 []*edbDataModel.EdbDataList, moveDay int) (newDataList []edbDataModel.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 := edbDataModel.EdbDataList{
-
- DataTime: currDate.Format(utils.FormatDate),
- Value: tmpValue,
- }
- dateDataMap[tmpData.DataTime] = tmpData.Value
- newDataList = append(newDataList, tmpData)
- }
- return
- }
|