123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262 |
- package range_analysis
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/services/data"
- "eta/eta_api/utils"
- "fmt"
- "math"
- "sort"
- "strconv"
- "strings"
- "time"
- "github.com/shopspring/decimal"
- )
- // GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
- func GetAutoCalculateDateDataList(currentDate string, dataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newDataList []*data_manage.EdbDataList, err error) {
- currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
- switch req.DateRangeType {
- case 0:
- // 智能划分得到一个开始日期,和结束日期
- var startDateTime time.Time
- if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
- startDate := req.AutoDateConf.StartDate
- if startDate == "" {
- startDate = "2020-01-01"
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- } else {
- startConf := req.AutoDateConf.StartDateConf
- startDate := ""
- if startConf.BaseDateType == 0 { //
- startDate = currentDate
- } else if startConf.BaseDateType == 1 {
- startDate = time.Now().Format(utils.FormatDate)
- }
- if startConf.MoveForward > 0 {
- startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
- }
- if len(startConf.DateChange) > 0 {
- startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
- if err != nil {
- err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
- return
- }
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- }
- var calStartTime, calEndTime time.Time
- if currentDateTime.Before(startDateTime) {
- calStartTime = currentDateTime
- calEndTime = startDateTime
- } else {
- calStartTime = startDateTime
- calEndTime = currentDateTime
- }
- // 根据日期,获取数据
- for _, vv := range dataList {
- dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
- if (dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime)) ||
- dataTimeT.Equal(calStartTime) ||
- dataTimeT.Equal(calEndTime) {
- newDataList = append(newDataList, vv)
- }
- }
- }
- return
- }
- // HandleDataByCalculateType 根据计算公式处理数据
- func HandleDataByCalculateType(originList []*data_manage.ChartRangeAnalysisDateDataItem, originDataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newList []*data_manage.EdbDataList, err error) {
- if len(originList) == 0 {
- return
- }
- calculateType := req.CalculateType
- switch calculateType {
- case 0: //均值
- var sum float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- sum = 0
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for _, vv := range calDataList {
- sum += vv.Value
- }
- val := sum / float64(len(calDataList))
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- sum = 0
- for k, v := range item.DataList {
- sum += v.Value
- val := sum / float64(k+1)
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 1: //累计值
- var sum float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- sum = 0
- for _, v := range item.DataList {
- sum = 0
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for _, vv := range calDataList {
- sum += vv.Value
- }
- val := sum
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- sum = 0
- for _, v := range item.DataList {
- sum += v.Value
- val := sum
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 2: //涨幅
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- var baseVal float64
- //计算的数据返回需要重新确定
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- if len(calDataList) == 0 {
- continue
- }
- baseVal = calDataList[0].Value
- baseDate := calDataList[0].DataTime
- if baseVal == 0 {
- continue
- }
- if v.DataTime == baseDate {
- continue
- }
- val := (v.Value - baseVal) / baseVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- } else {
- for _, item := range originList {
- if len(item.DataList) == 0 {
- break
- }
- baseVal := item.DataList[0].Value
- baseDate := item.DataList[0].DataTime
- if baseVal == 0 {
- break
- }
- for _, v := range item.DataList {
- if v.DataTime == baseDate {
- continue
- }
- val := (v.Value - baseVal) / baseVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{
- DataTime: v.DataTime,
- Value: val,
- DataTimestamp: v.DataTimestamp,
- })
- }
- }
- }
- case 3: //复合增长率
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- var baseVal float64
- var baseDate string
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- if len(calDataList) == 0 {
- continue
- }
- baseVal = calDataList[0].Value
- baseDate = calDataList[0].DataTime
- if v.DataTime == baseDate {
- continue
- }
- if baseVal == 0 {
- continue
- }
- baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- // 计算两个日期相差的天数
- diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
- val := v.Value / baseVal
- val = math.Pow(val, 1/diff) - 1
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- if len(item.DataList) == 0 {
- break
- }
- baseVal := item.DataList[0].Value
- baseDate := item.DataList[0].DataTime
- if baseVal == 0 {
- break
- }
- for _, v := range item.DataList {
- if v.DataTime == baseDate {
- continue
- }
- baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("time.ParseInLocation err: %v", e)
- return
- }
- // 计算两个日期相差的天数
- diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
- val := v.Value / baseVal
- val = math.Pow(val, 1/diff) - 1
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- case 4: //最大值
- var maxVal float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for kk, vv := range calDataList {
- if kk == 0 {
- maxVal = vv.Value
- }
- if vv.Value > maxVal {
- maxVal = vv.Value
- }
- }
- val := maxVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- for k, v := range item.DataList {
- if k == 0 {
- maxVal = v.Value
- }
- if v.Value > maxVal {
- maxVal = v.Value
- }
- val := maxVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- case 5: //最小值
- var minVal float64
- if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
- for _, item := range originList {
- for _, v := range item.DataList {
- calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
- if e != nil {
- err = fmt.Errorf("获取区间数据失败:%s", e.Error())
- return
- }
- for kk, vv := range calDataList {
- if kk == 0 {
- minVal = vv.Value
- }
- if vv.Value < minVal {
- minVal = vv.Value
- }
- }
- val := minVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- } else {
- for _, item := range originList {
- for k, v := range item.DataList {
- if k == 0 {
- minVal = v.Value
- }
- if v.Value < minVal {
- minVal = v.Value
- }
- val := minVal
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
- }
- }
- }
- }
- return
- }
- // GetChartEdbInfoFormat 区间计算图表-获取指标信息
- func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingList []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- for _, edbInfoMapping := range edbInfoMappingList {
- if edbInfoMapping == nil {
- err = fmt.Errorf("指标信息有误")
- return
- }
- edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
- if edbInfoMapping.Unit == `无` {
- edbInfoMapping.Unit = ``
- }
- if chartInfoId <= 0 {
- edbInfoMapping.IsAxis = 1
- edbInfoMapping.LeadValue = 0
- edbInfoMapping.LeadUnit = ""
- edbInfoMapping.ChartEdbMappingId = 0
- edbInfoMapping.ChartInfoId = 0
- edbInfoMapping.IsOrder = false
- edbInfoMapping.EdbInfoType = 1
- edbInfoMapping.ChartStyle = ""
- edbInfoMapping.ChartColor = ""
- edbInfoMapping.ChartWidth = 0
- } else {
- edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
- }
- edbList = append(edbList, edbInfoMapping)
- }
- return
- }
- // GetChartDataByEdbInfoList 区间计算图表-根据指标信息获取x轴和y轴
- func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
- if chartInfoId > 0 && req.EdbInfoMode == 1 {
- edbList, xEdbIdValue, dataResp, err = GetChartDataByEdbInfoListBySeries(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, req)
- return
- }
- for _, edbInfoMapping := range edbInfoMappingList {
- edbInfoMapping, err = getChartDataByEdbInfo(edbInfoMapping, req)
- if err != nil {
- return
- }
- edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
- edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
- if req.CalculateType == 2 || req.CalculateType == 3 {
- edbInfoMapping.ConvertUnit = "无"
- edbInfoMapping.ConvertEnUnit = "无"
- }
- if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
- edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
- edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
- }
- if edbInfoMapping.ConvertUnit == "无" {
- edbInfoMapping.ConvertUnit = ""
- }
- if edbInfoMapping.ConvertEnUnit == "无" {
- edbInfoMapping.ConvertEnUnit = ""
- }
- dataList := edbInfoMapping.DataList.([]*data_manage.EdbDataList)
- // 处理上下限
- var maxData, minData float64
- if len(dataList) > 0 {
- maxData = dataList[0].Value
- minData = dataList[0].Value
- for _, v := range dataList {
- if v.Value > maxData {
- maxData = v.Value
- }
- if v.Value < minData {
- minData = v.Value
- }
- }
- }
- edbInfoMapping.MaxData = maxData
- edbInfoMapping.MinData = minData
- xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
- }
- //根据时间类型来筛选最终的数据
- var dateMax time.Time
- if dateType == utils.DateTypeNYears {
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*data_manage.EdbDataList)
- if len(dataList) > 0 {
- latestDate := dataList[len(dataList)-1].DataTime
- if latestDate != "" {
- lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
- if tErr != nil {
- err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
- return
- }
- if lastDateT.After(dateMax) {
- dateMax = lastDateT
- }
- }
- }
- }
- }
- startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
- if startDate != "" {
- for k, v := range edbInfoMappingList {
- var maxData, minData float64
- dataList := v.DataList.([]*data_manage.EdbDataList)
- newDataList := make([]*data_manage.EdbDataList, 0)
- if len(dataList) == 0 {
- newDataList = dataList
- } else {
- firstFlag := true
- for _, d := range dataList {
- if endDate != "" && d.DataTime > endDate {
- continue
- }
- if d.DataTime < startDate {
- continue
- }
- newDataList = append(newDataList, d)
- if firstFlag {
- maxData = d.Value
- minData = d.Value
- firstFlag = false
- } else {
- if d.Value > maxData {
- maxData = d.Value
- }
- if d.Value < minData {
- minData = d.Value
- }
- }
- }
- }
- edbInfoMappingList[k].DataList = newDataList
- edbInfoMappingList[k].MinData = minData
- edbInfoMappingList[k].MaxData = maxData
- }
- }
- dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
- if req.MultipleGraphConfigId > 0 {
- //判断MultipleGraphConfigId和图表关系是否正确
- multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- if len(multipleGraphConfigEdbMappingList) > 0 {
- // 查询指标是否已存在,如果已经被删除了,则返回0值
- edbIds := make([]int, 0)
- for _, v := range multipleGraphConfigEdbMappingList {
- edbIds = append(edbIds, v.EdbInfoId)
- }
- var condition string
- var pars []interface{}
- condition = " AND edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
- pars = append(pars, edbIds)
- count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
- if e != nil {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- if count > 0 {
- // 查询是否已经生成指标
- dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
- }
- }
- }
- edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
- if err != nil {
- err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
- return
- }
- return
- }
- func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
- //查询seriesId
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = fmt.Errorf("图表关联关系不存在")
- return
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- }
- //根据seriesId查询数据
- //并把数据放到dataList中
- for _, edbInfoMapping := range edbInfoMappingList {
- dataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
- dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
- if e != nil {
- err = e
- return
- }
- edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
- edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
- if req.CalculateType == 2 || req.CalculateType == 3 {
- edbInfoMapping.ConvertUnit = "无"
- edbInfoMapping.ConvertEnUnit = "无"
- }
- if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
- edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
- edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
- }
- if edbInfoMapping.ConvertUnit == "无" {
- edbInfoMapping.ConvertUnit = ""
- }
- if edbInfoMapping.ConvertEnUnit == "无" {
- edbInfoMapping.ConvertEnUnit = ""
- }
- edbInfoMapping.DataList = dataList
- // 处理上下限
- var maxData, minData float64
- if len(dataList) > 0 {
- maxData = dataList[0].Value
- minData = dataList[0].Value
- for _, v := range dataList {
- if v.Value > maxData {
- maxData = v.Value
- }
- if v.Value < minData {
- minData = v.Value
- }
- }
- }
- edbInfoMapping.MaxData = maxData
- edbInfoMapping.MinData = minData
- xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
- }
- var dateMax time.Time
- if dateType == utils.DateTypeNYears {
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*data_manage.EdbDataList)
- latestDate := dataList[len(dataList)-1].DataTime
- if latestDate != "" {
- lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
- if tErr != nil {
- err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
- return
- }
- if lastDateT.After(dateMax) {
- dateMax = lastDateT
- }
- }
- }
- }
- startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
- if startDate != "" {
- for k, v := range edbInfoMappingList {
- var maxData, minData float64
- dataList := v.DataList.([]*data_manage.EdbDataList)
- newDataList := make([]*data_manage.EdbDataList, 0)
- if len(dataList) == 0 {
- newDataList = dataList
- } else {
- firstFlag := true
- for _, d := range dataList {
- if endDate != "" && d.DataTime > endDate {
- continue
- }
- if d.DataTime < startDate {
- continue
- }
- newDataList = append(newDataList, d)
- if firstFlag {
- maxData = d.Value
- minData = d.Value
- firstFlag = false
- } else {
- if d.Value > maxData {
- maxData = d.Value
- }
- if d.Value < minData {
- minData = d.Value
- }
- }
- }
- }
- edbInfoMappingList[k].DataList = newDataList
- edbInfoMappingList[k].MinData = minData
- edbInfoMappingList[k].MaxData = maxData
- }
- }
- dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
- // 查询配置关联关系
- if req.MultipleGraphConfigId > 0 {
- multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- if len(multipleGraphConfigEdbMappingList) > 0 {
- // 查询指标是否已存在,如果已经被删除了,则返回0值
- edbIds := make([]int, 0)
- for _, v := range multipleGraphConfigEdbMappingList {
- edbIds = append(edbIds, v.EdbInfoId)
- }
- var condition string
- var pars []interface{}
- condition = " and edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
- pars = append(pars, edbIds)
- count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
- if e != nil {
- err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
- return
- }
- if count > 0 {
- // 查询是否已经生成指标
- dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
- }
- }
- }
- edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
- if err != nil {
- err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
- return
- }
- return
- }
- // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
- func getChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *data_manage.ChartEdbInfoMapping, err error) {
- newEdbInfoMapping = edbInfoMapping
- // 指标的开始日期和结束日期
- edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
- //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
- edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
- edbEndDate := edbEndDateTime.Format(utils.FormatDate)
- // 获取时间基准指标在时间区间内的值
- dataList := make([]*data_manage.EdbDataList, 0)
- switch edbInfoMapping.EdbInfoCategoryType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
- default:
- err = errors.New("指标base类型异常")
- return
- }
- /*var dataList data_manage.SortEdbDataList
- dataList = dataListTmp
- ascDataList := dataListTmp
- sort.Sort(dataList)*/
- dateList := make([]*data_manage.ChartRangeAnalysisDateDataItem, 0)
- switch req.DateRangeType {
- case 0:
- // 智能划分得到一个开始日期,和结束日期
- var startDateTime, endDateTime time.Time
- startDateTime = edbStartDateTime
- if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
- startDate := req.AutoDateConf.StartDate
- if startDate == "" {
- startDate = "2020-01-01"
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- } /*else {
- startConf := req.AutoDateConf.StartDateConf
- startDate := ""
- if startConf.BaseDateType == 0 { //
- startDate = edbEndDate
- } else if startConf.BaseDateType == 1 {
- startDate = time.Now().Format(utils.FormatDate)
- }
- if startConf.MoveForward > 0 {
- startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
- }
- if len(startConf.DateChange) > 0 {
- startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
- if err != nil {
- err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
- return
- }
- }
- startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- }*/
- if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
- endDate := req.AutoDateConf.EndDate
- if endDate == "" {
- err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
- return
- }
- // todo 如果截止日期比指标日期还要大,则用指标的最新日期
- endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- } else {
- endConf := req.AutoDateConf.EndDateConf
- endDate := edbEndDate
- if endConf.MoveForward > 0 {
- endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
- }
- if len(endConf.DateChange) > 0 {
- endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
- if err != nil {
- err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
- return
- }
- }
- endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- }
- dateList = append(dateList, &data_manage.ChartRangeAnalysisDateDataItem{
- StartDate: startDateTime,
- EndDate: endDateTime})
- case 1:
- // 手工划分得到多个开始日期和结束日期(已排序)
- for _, v := range req.ManualDateConf {
- startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
- endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- tmp := &data_manage.ChartRangeAnalysisDateDataItem{
- StartDate: startDateT,
- EndDate: endDateT,
- }
- dateList = append(dateList, tmp)
- }
- case 2:
- // 跨年划分得到多个开始日期和结束日期
- startYear := edbStartDateTime.Year()
- endYear := edbEndDateTime.Year()
- startDay := req.YearDateConf.StartDay
- endDay := req.YearDateConf.EndDay
- for year := startYear; year <= endYear; year++ {
- startDate := fmt.Sprintf("%d-%s", year, startDay)
- endDate := fmt.Sprintf("%d-%s", year+1, endDay)
- startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- if startDateTime.Before(edbStartDateTime) {
- break
- }
- tmp := &data_manage.ChartRangeAnalysisDateDataItem{
- StartDate: startDateTime,
- EndDate: endDateTime,
- }
- dateList = append(dateList, tmp)
- }
- }
- // 根据日期,获取数据
- for _, v := range dateList {
- for _, vv := range dataList {
- dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
- if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
- dataTimeT.Equal(v.StartDate) ||
- dataTimeT.Equal(v.EndDate) {
- v.DataList = append(v.DataList, vv)
- }
- }
- }
- // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
- newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
- if err != nil {
- return
- }
- if req.UnNormalDataDealType > 0 {
- switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
- case 1:
- dealDataList := make([]*data_manage.EdbDataList, 0)
- for _, v := range newDataList {
- if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
- dealDataList = append(dealDataList, v)
- }
- }
- newDataList = dealDataList
- case 2:
- for i, v := range newDataList {
- if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
- newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
- }
- }
- }
- }
- if req.DataConvertType > 0 {
- // 数据转换类型 0不转, 1乘 2除 3对数
- switch req.DataConvertType {
- case 1:
- for i, v := range newDataList {
- val := v.Value * req.DataConvertConf.Value
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = item.MaxData * v.ConvertValue
- //item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, v := range newDataList {
- val := v.Value / req.DataConvertConf.Value
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = item.MaxData / v.ConvertValue
- //item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, v := range newDataList {
- if v.Value <= 0 {
- err = errors.New("数据中含有负数或0,无法对数运算")
- return
- }
- val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
- val, _ = decimal.NewFromFloat(val).Round(4).Float64()
- newDataList[i].Value = val
- }
- //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- newEdbInfoMapping.DataList = newDataList
- //时间处理
- return
- }
- // RollingCorrelationChartDataResp 滚动区间计算图表数据
- type RollingCorrelationChartDataResp struct {
- MaxData float64
- MinData float64
- LatestDate string `description:"真实数据的最后日期"`
- EdbInfoCategoryType int
- ChartColor string
- ChartStyle string
- PredictChartColor string
- ChartType int
- ChartWidth int
- EdbName string
- EdbNameEn string
- Unit string
- UnitEn string
- IsAxis int
- DataList []data_manage.EdbDataList
- }
- // ChartInfoRefresh 图表刷新
- func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
- var errMsg string
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- var chartInfo *data_manage.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- } else {
- chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- }
- // 1.刷新图表关联的指标
- mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
- if e != nil {
- utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
- return
- }
- if len(mappings) == 0 {
- utils.FileLog.Info("图表无关联指标")
- return
- }
- var edbIds []int
- for _, v := range mappings {
- edbIds = append(edbIds, v.EdbInfoId)
- }
- if e, _ = data.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
- utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
- return
- }
- // todo 重新计算
- // 区间计算图表配置校验
- var extraConfig data_manage.ChartRangeAnalysisExtraConf
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- } else {
- _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
- if e != nil {
- err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
- return
- }
- }
- // 4.清除图表缓存
- key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
- _ = utils.Rc.Delete(key)
- return
- }
- // AddChartInfo 添加图表
- func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- errMsg = "请填写图表名称!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 区间计算图表配置校验
- var extraConfig data_manage.ChartRangeAnalysisExtraConf
- if req.ExtraConfig == `` {
- errMsg = "配置信息错误"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- if len(req.ChartEdbInfoList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if len(req.ChartEdbInfoList) > 100 {
- errMsg = "添加指标总数量不得超过100"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
- if err != nil {
- return
- }
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取分类信息失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var edbInfoIdArr []int
- for _, v := range req.ChartEdbInfoList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取指标信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- if edbInfo.EdbInfoId <= 0 {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- edbInfo.EdbNameSource = edbInfo.EdbName
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- var chartInfoId int
- // 判断图表是否存在
- {
- var condition string
- var pars []interface{}
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name=? AND source = ? "
- }
- pars = append(pars, req.ChartName, source)
- count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- disableVal := data.CheckIsDisableChart(edbInfoIdArr)
- chartInfo = new(data_manage.ChartInfo)
- chartInfo.ChartName = req.ChartName
- chartInfo.ChartNameEn = req.ChartName
- chartInfo.EdbInfoIds = edbInfoIdStr
- chartInfo.ChartClassifyId = req.ChartClassifyId
- chartInfo.SysUserId = sysUser.AdminId
- chartInfo.SysUserRealName = sysUser.RealName
- chartInfo.CreateTime = time.Now()
- chartInfo.ModifyTime = time.Now()
- chartInfo.IsSetName = 0
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
- chartInfo.ChartType = 1 // 区间计算图
- chartInfo.Calendar = "公历"
- chartInfo.DateType = req.DateType
- chartInfo.StartDate = req.StartDate
- chartInfo.EndDate = req.EndDate
- chartInfo.StartYear = req.StartYear
- chartInfo.SeasonStartDate = req.StartDate
- chartInfo.SeasonEndDate = req.EndDate
- chartInfo.LeftMin = req.LeftMin
- chartInfo.LeftMax = req.LeftMax
- chartInfo.RightMin = req.RightMin
- chartInfo.RightMax = req.RightMax
- chartInfo.Disabled = disableVal
- chartInfo.Source = source
- chartInfo.ChartThemeId = req.ChartThemeId
- chartInfo.SourcesFrom = req.SourcesFrom
- chartInfo.Instructions = req.Instructions
- chartInfo.MarkersLines = req.MarkersLines
- chartInfo.MarkersAreas = req.MarkersAreas
- if req.ExtraConfig != "" {
- chartInfo.ExtraConfig = req.ExtraConfig
- }
- // 指标信息
- mapList := make([]*data_manage.ChartEdbMapping, 0)
- for _, v := range req.ChartEdbInfoList {
- // todo 指标名称修改
- mapItem := new(data_manage.ChartEdbMapping)
- mapItem.EdbInfoId = v.EdbInfoId
- mapItem.EdbAliasName = v.EdbAliasName
- mapItem.IsAxis = v.IsAxis
- mapItem.CreateTime = time.Now()
- mapItem.ModifyTime = time.Now()
- edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + edbTimestamp + "_" + strconv.Itoa(v.EdbInfoId))
- mapItem.IsOrder = false
- mapItem.EdbInfoType = 1
- mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
- mapList = append(mapList, mapItem)
- }
- // 新增图表和指标mapping
- chartInfoId, e := data_manage.CreateRangeChartAndEdb(chartInfo, mapList)
- if e != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表失败, Err: " + e.Error())
- return
- }
- // 把系列和图表绑定
- if extraConfig.EdbInfoMode == 1 {
- // 新增指标系列
- err = AddSeries(edbInfoIdArr, chartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表失败, Err: " + err.Error())
- return
- }
- //todo 如果保存失败是否要删除
- }
- /*//添加配置信息
- multipleGraphConfig := &data_manage.MultipleGraphConfig{
- //MultipleGraphConfigId: 0,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表配置失败, Err: " + err.Error())
- return
- }*/
- if extraConfig.MultipleGraphConfigId > 0 {
- multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
- //Id: 0,
- MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
- ChartInfoId: chartInfo.ChartInfoId,
- Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
- return
- }
- }
- // 添加指标引用记录
- _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
- //添加es数据
- go data.EsAddOrEditChartInfo(chartInfoId)
- return
- }
- // EditChartInfo 编辑图表
- func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
- errMsg = "该图不是区间计算图表!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 区间计算图表配置校验
- var extraConfig data_manage.ChartRangeAnalysisExtraConf
- if req.ExtraConfig == `` {
- errMsg = "配置信息错误"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg)
- return
- }
- if len(req.ChartEdbInfoList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
- if len(req.ChartEdbInfoList) > 100 {
- errMsg = "添加指标总数量不得超过100"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
- if err != nil {
- return
- }
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取分类信息失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 图表操作权限
- ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
- if !ok {
- errMsg = "没有该图表的操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var edbInfoIdArr []int
- for _, v := range req.ChartEdbInfoList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "图表不存在!"
- err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取图表信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //判断图表是否存在
- {
- var condition string
- var pars []interface{}
- condition += " AND chart_info_id <> ? "
- pars = append(pars, req.ChartInfoId)
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name=? AND source = ? "
- }
- pars = append(pars, req.ChartName, chartItem.Source)
- count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- // 修改图表与指标mapping
- req.ChartType = 1
- dateType := req.DateType
- err = data_manage.EditRangeChartInfoAndMapping(&req, edbInfoIdStr, lang, "公历", dateType, 0, ``)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- addSeries := false
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartItem.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- addSeries = true
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- }
- if extraConfig.EdbInfoMode == 1 {
- if addSeries {
- // 新增指标系列
- err = AddSeries(edbInfoIdArr, chartItem.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表失败, Err: " + err.Error())
- return
- }
- } else {
- err = EditSeries(seriesMappingItem, edbInfoIdArr, extraConfig, req.ExtraConfig, true)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- }
- // todo 编辑失败处理
- } else if extraConfig.EdbInfoMode == 0 {
- if !addSeries {
- // 删除相关的系列
- factorSeriesOb := new(data_manage.FactorEdbSeries)
- err = factorSeriesOb.RemoveSeriesAndMappingByFactorEdbSeriesId(seriesMappingItem)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- }
- }
- resp := new(data_manage.AddChartInfoResp)
- resp.ChartInfoId = chartItem.ChartInfoId
- resp.UniqueCode = chartItem.UniqueCode
- resp.ChartType = req.ChartType
- // 添加指标引用记录
- _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
- //添加es数据
- go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
- //修改my eta es数据
- go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
- return
- }
- // CopyChartInfo 复制图表
- func CopyChartInfo(classifyId int, chartName string, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- var extraConfig data_manage.ChartRangeAnalysisExtraConf
- err = json.Unmarshal([]byte(oldChartInfo.ExtraConfig), &extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- //新增配置,并绑定图表
- multipleGraphConfig := &data_manage.MultipleGraphConfig{
- //MultipleGraphConfigId: 0,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
- if err != nil {
- errMsg = "新增区间计算图表配置失败"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- extraConfig.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
- newExtraConfigBytes, err := json.Marshal(extraConfig)
- if err != nil {
- errMsg = "配置信息错误"
- err = errors.New(errMsg + ", Err: " + err.Error())
- return
- }
- newExtraConfig := string(newExtraConfigBytes)
- chartInfo = &data_manage.ChartInfo{
- ChartInfoId: 0,
- ChartName: chartName,
- ChartClassifyId: classifyId,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- DateType: oldChartInfo.DateType,
- StartDate: oldChartInfo.StartDate,
- EndDate: oldChartInfo.EndDate,
- IsSetName: oldChartInfo.IsSetName,
- EdbInfoIds: oldChartInfo.EdbInfoIds,
- ChartType: oldChartInfo.ChartType,
- Calendar: oldChartInfo.Calendar,
- SeasonStartDate: oldChartInfo.SeasonStartDate,
- SeasonEndDate: oldChartInfo.SeasonEndDate,
- ChartImage: oldChartInfo.ChartImage,
- BarConfig: oldChartInfo.BarConfig,
- //Sort: sort,
- LeftMin: oldChartInfo.LeftMin,
- LeftMax: oldChartInfo.LeftMax,
- RightMin: oldChartInfo.RightMin,
- RightMax: oldChartInfo.RightMax,
- Right2Min: oldChartInfo.Right2Min,
- Right2Max: oldChartInfo.Right2Max,
- Disabled: oldChartInfo.Disabled,
- Source: oldChartInfo.Source,
- ExtraConfig: newExtraConfig,
- SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
- StartYear: oldChartInfo.StartYear,
- Unit: oldChartInfo.Unit,
- UnitEn: oldChartInfo.UnitEn,
- ChartThemeId: oldChartInfo.ChartThemeId,
- SourcesFrom: oldChartInfo.SourcesFrom,
- Instructions: oldChartInfo.Instructions,
- MarkersLines: oldChartInfo.MarkersLines,
- MarkersAreas: oldChartInfo.MarkersAreas,
- }
- newId, err := data_manage.AddChartInfo(chartInfo)
- if err != nil {
- err = fmt.Errorf("保存失败,Err:%s", err.Error())
- return
- }
- chartInfo.ChartInfoId = int(newId)
- edbInfoMappingList, err := data_manage.GetChartMappingList(oldChartInfo.ChartInfoId)
- if err != nil {
- err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
- return
- }
- // 添加图表与指标的关联关系
- edbInfoIdArr := make([]int, 0)
- {
- mapList := make([]*data_manage.ChartEdbMapping, 0)
- for _, v := range edbInfoMappingList {
- edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
- timestamp = strconv.FormatInt(time.Now().UnixNano(), 10)
- mapItem := &data_manage.ChartEdbMapping{
- //ChartEdbMappingId: 0,
- ChartInfoId: chartInfo.ChartInfoId,
- EdbInfoId: v.EdbInfoId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
- MaxData: v.MaxData,
- MinData: v.MinData,
- IsOrder: v.IsOrder,
- IsAxis: v.IsAxis,
- EdbInfoType: v.EdbInfoType,
- LeadValue: v.LeadValue,
- LeadUnit: v.LeadUnit,
- ChartStyle: v.ChartStyle,
- ChartColor: v.ChartColor,
- ChartWidth: v.ChartWidth,
- Source: v.Source,
- EdbAliasName: v.EdbAliasName,
- IsConvert: v.IsConvert,
- ConvertType: v.ConvertType,
- ConvertValue: v.ConvertValue,
- ConvertUnit: v.ConvertEnUnit,
- ConvertEnUnit: v.ConvertEnUnit,
- }
- mapList = append(mapList, mapItem)
- }
- err = data_manage.AddChartEdbMapping(mapList)
- if err != nil {
- err = fmt.Errorf("保存失败,Err:%s", err.Error())
- return
- }
- }
- // 添加系列和图表映射
- chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
- _, err = chartSeriesOb.GetItemByChartInfoId(oldChartInfo.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- } else {
- err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
- return
- }
- } else {
- // 新增指标系列
- // 区间计算图表配置校验
- err = AddSeries(edbInfoIdArr, chartInfo.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, chartInfo.ExtraConfig)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表失败, Err: " + err.Error())
- return
- }
- }
- multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
- //Id: 0,
- MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
- ChartInfoId: chartInfo.ChartInfoId,
- Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
- if err != nil {
- errMsg = "操作失败"
- err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
- return
- }
- //添加es数据
- go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
- return
- }
- /*
- // CalculateCorrelation 计算区间计算-获取x轴和y轴
- func CalculateCorrelation(leadValue int, leadUnit, frequencyA, frequencyB string, dataListA, dataListB []*data_manage.EdbDataList) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
- xData := make([]int, 0)
- yData := make([]float64, 0)
- if leadValue == 0 {
- xData = append(xData, 0)
- }
- if leadValue > 0 {
- leadMin := 0 - leadValue
- xLen := 2*leadValue + 1
- for i := 0; i < xLen; i++ {
- n := leadMin + i
- xData = append(xData, n)
- }
- }
- // 计算窗口,不包含第一天
- //startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- //startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
- //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
- //baseEdbInfo := edbInfoMappingB
- //changeEdbInfo := edbInfoMappingA
- // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
- //baseEdbInfo := edbInfoMappingA
- //changeEdbInfo := edbInfoMappingB
- // 获取时间基准指标在时间区间内的值
- //aDataList := make([]*data_manage.EdbDataList, 0)
- //switch baseEdbInfo.EdbInfoCategoryType {
- //case 0:
- // aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
- //case 1:
- // _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
- //default:
- // err = errors.New("指标base类型异常")
- // return
- //}
- //
- //// 获取变频指标所有日期的值, 插值法完善数据
- //bDataList := make([]*data_manage.EdbDataList, 0)
- //switch changeEdbInfo.EdbInfoCategoryType {
- //case 0:
- // bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
- //case 1:
- // _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
- //default:
- // err = errors.New("指标change类型异常")
- // return
- //}
- //changeDataMap := make(map[string]float64)
- //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
- //if e != nil {
- // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- // return
- //}
- // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
- baseDataList := make([]*data_manage.EdbDataList, 0)
- baseDataMap := make(map[string]float64)
- changeDataList := make([]*data_manage.EdbDataList, 0)
- changeDataMap := make(map[string]float64)
- // 先把低频指标升频为高频
- {
- frequencyIntMap := map[string]int{
- "日度": 1,
- "周度": 2,
- "旬度": 3,
- "月度": 4,
- "季度": 5,
- "年度": 6,
- }
- // 如果A指标是高频,那么就需要对B指标进行升频
- if frequencyIntMap[frequencyA] < frequencyIntMap[frequencyB] {
- tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListB, changeDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- changeDataList = tmpNewChangeDataList
- baseDataList = dataListA
- for _, v := range baseDataList {
- baseDataMap[v.DataTime] = v.Value
- }
- } else if frequencyIntMap[frequencyA] > frequencyIntMap[frequencyB] {
- // 如果B指标是高频,那么就需要对A指标进行升频
- tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListA, baseDataMap)
- if e != nil {
- err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
- return
- }
- baseDataList = tmpNewChangeDataList
- changeDataList = dataListB
- for _, v := range changeDataList {
- changeDataMap[v.DataTime] = v.Value
- }
- } else {
- baseDataList = dataListA
- for _, v := range baseDataList {
- baseDataMap[v.DataTime] = v.Value
- }
- changeDataList = dataListB
- for _, v := range changeDataList {
- changeDataMap[v.DataTime] = v.Value
- }
- }
- }
- // 计算不领先也不滞后时的相关系数
- baseCalculateData := make([]float64, 0)
- baseDataTimeArr := make([]string, 0)
- for i := range baseDataList {
- baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
- baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
- }
- //zeroBaseData := make([]float64, 0)
- //zeroCalculateData := make([]float64, 0)
- //for i := range baseDataTimeArr {
- // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
- // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
- // if ok1 && ok2 {
- // zeroBaseData = append(zeroBaseData, tmpBaseVal)
- // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
- // }
- //}
- //if len(zeroBaseData) != len(zeroCalculateData) {
- // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
- // return
- //}
- //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
- //if leadValue == 0 {
- // yData = append(yData, zeroRatio)
- //}
- // 计算领先/滞后N期
- if leadValue > 0 {
- // 平移变频指标领先/滞后的日期(单位天)
- moveUnitDays := utils.FrequencyDaysMap[leadUnit]
- for i := range xData {
- //if xData[i] == 0 {
- // yData = append(yData, zeroRatio)
- // continue
- //}
- xCalculateData := make([]float64, 0)
- yCalculateData := make([]float64, 0)
- // 平移指定天数
- mDays := int(moveUnitDays) * xData[i]
- _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
- // 取出对应的基准日期的值
- for i2 := range baseDataTimeArr {
- tmpDate := baseDataTimeArr[i2]
- if yVal, ok := dMap[tmpDate]; ok {
- xCalculateData = append(xCalculateData, baseCalculateData[i2])
- yCalculateData = append(yCalculateData, yVal)
- }
- }
- if len(yCalculateData) <= 0 {
- //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
- //return
- // 领先滞后后,没有可以计算的数据了
- continue
- }
- // 公式计算出领先/滞后频度对应点的区间计算系数
- ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
- yData = append(yData, ratio)
- }
- }
- xEdbIdValue = xData
- yDataList = make([]data_manage.YData, 0)
- yDate := "0000-00-00"
- yDataList = append(yDataList, data_manage.YData{
- Date: yDate,
- Value: yData,
- })
- return
- }
- // GetFactorChartDataByChartId 获取多因子区间计算图表数据
- func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
- if chartInfoId <= 0 {
- return
- }
- // 指标对应的图例
- extra := new(data_manage.CorrelationChartInfoExtraConfig)
- if extraConfig != "" {
- if e := json.Unmarshal([]byte(extraConfig), extra); e != nil {
- err = fmt.Errorf("解析图表额外配置失败, err: %v", e)
- return
- }
- }
- legends := make(map[string]*data_manage.CorrelationChartLegend)
- if extra != nil {
- for _, v := range extra.LegendConfig {
- s := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
- legends[s] = v
- }
- }
- // 获取图表引用到的系列指标
- chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
- cond := fmt.Sprintf(" AND %s = ? AND %s = 1", chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().EdbUsed)
- pars := make([]interface{}, 0)
- pars = append(pars, chartInfoId)
- chartMappings, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
- if e != nil {
- err = fmt.Errorf("获取图表引用系列指标失败")
- return
- }
- // 取出计算结果
- yDataList = make([]data_manage.YData, 0)
- yDate := "0000-00-00"
- for k, m := range chartMappings {
- var values []data_manage.FactorEdbSeriesCorrelationMatrixValues
- if m.CalculateData != "" {
- e = json.Unmarshal([]byte(m.CalculateData), &values)
- if e != nil {
- err = fmt.Errorf("系列指标计算数据有误, err: %v", e)
- return
- }
- }
- var y []float64
- for _, v := range values {
- if k == 0 {
- xEdbIdValue = append(xEdbIdValue, v.XData)
- }
- y = append(y, v.YData)
- }
- var yData data_manage.YData
- yData.Date = yDate
- yData.Value = y
- yData.SeriesEdb.SeriesId = m.FactorEdbSeriesId
- yData.SeriesEdb.EdbInfoId = m.EdbInfoId
- // 图例
- s := fmt.Sprintf("%d-%d", m.FactorEdbSeriesId, m.EdbInfoId)
- legend := legends[s]
- if legend != nil {
- yData.Name = legend.LegendName
- yData.Color = legend.Color
- }
- yDataList = append(yDataList, yData)
- }
- return
- }
- // FormatChartEdbInfoMappings 补充指标信息
- func FormatChartEdbInfoMappings(chartInfoId int, mappings []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- if len(mappings) == 0 {
- return
- }
- for _, v := range mappings {
- if chartInfoId <= 0 {
- v.IsAxis = 1
- v.LeadValue = 0
- v.LeadUnit = ""
- v.ChartEdbMappingId = 0
- v.ChartInfoId = 0
- v.IsOrder = false
- v.EdbInfoType = 1
- v.ChartStyle = ""
- v.ChartColor = ""
- v.ChartWidth = 0
- } else {
- v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
- v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
- }
- v.FrequencyEn = data.GetFrequencyEn(v.Frequency)
- if v.Unit == `无` {
- v.Unit = ``
- }
- edbList = append(edbList, v)
- }
- return
- }*/
- func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
- // 根据日期进行排序
- index := 0
- length := len(edbDataList)
- for i := length - 1; i >= 0; i-- {
- item := edbDataList[i]
- if item.DataTime == startDate {
- index += 1
- continue
- }
- if index >= moveForward {
- date = item.DataTime
- break
- }
- if index > 0 {
- index += 1
- date = item.DataTime
- }
- }
- return
- }
- // HandleEdbDateChange 处理日期变换
- func HandleEdbDateChange(date string, dateChange []*data_manage.EdbDataDateChangeConf) (newDate string, err error) {
- newDate = date
- if newDate != "" {
- if len(dateChange) > 0 {
- var dateTime time.Time
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- for _, v := range dateChange {
- if v.ChangeType == 1 {
- dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
- newDate = dateTime.Format(utils.FormatDate)
- } else if v.ChangeType == 2 {
- newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
- if err != nil {
- return
- }
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- }
- }
- }
- }
- return
- }
- // 添加指标系列和数据
- func AddSeries(edbInfoIds []int, chartInfoId, chartInfoSource int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string) (err error) {
- edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
- return
- }
- if len(edbArr) == 0 {
- err = fmt.Errorf("获取指标列表失败, 指标不存在")
- return
- }
- edbInfoType := edbArr[0].EdbInfoType
- // 新增指标系列
- seriesItem := new(data_manage.FactorEdbSeries)
- seriesItem.SeriesName = extraConf.SeriesName
- seriesItem.EdbInfoType = edbInfoType
- seriesItem.CreateTime = time.Now().Local()
- seriesItem.ModifyTime = time.Now().Local()
- seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
- seriesItem.CalculateStep = calculatesJson
- mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
- for _, v := range edbArr {
- mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
- EdbInfoId: v.EdbInfoId,
- EdbCode: v.EdbCode,
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- })
- }
- seriesId, e := seriesItem.CreateSeriesAndMapping(seriesItem, mappings)
- if e != nil {
- err = fmt.Errorf("新增因子指标系列失败, Err: " + e.Error())
- return
- }
- // 图表关联-此处添加的chart_info_id=0
- seriesChartMapping := new(data_manage.FactorEdbSeriesChartMapping)
- seriesChartMapping.CalculateType = data_manage.FactorEdbSeriesChartCalculateTypeRange
- //新增图表和指标的映射关系
- seriesChartMapping.CalculateData = ""
- seriesChartMapping.FactorEdbSeriesId = seriesId
- seriesChartMapping.ChartInfoId = chartInfoId
- seriesChartMapping.Source = chartInfoSource
- seriesChartMapping.CreateTime = time.Now().Local()
- seriesChartMapping.ModifyTime = time.Now().Local()
- if e = seriesChartMapping.Create(); e != nil {
- err = fmt.Errorf("新增图表关联失败, Err: " + e.Error())
- return
- }
- // todo 计算指标数据并存储
- _, e = FactorEdbStepCalculateRange(seriesId, edbInfoIds, extraConf, false)
- if e != nil {
- err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
- return
- }
- // 更新系列计算状态
- cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
- seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
- seriesItem.ModifyTime = time.Now().Local()
- if e = seriesItem.Update(cols); e != nil {
- err = fmt.Errorf("更新因子指标系列计算状态失败, Err: " + e.Error())
- return
- }
- return
- }
- func EditSeries(seriesMapping *data_manage.FactorEdbSeriesChartMapping, edbInfoIds []int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string, recalculate bool) (err error) {
- seriesOb := new(data_manage.FactorEdbSeries)
- seriesItem, e := seriesOb.GetItemById(seriesMapping.FactorEdbSeriesId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- err = fmt.Errorf("因子指标系列不存在, Err: " + e.Error())
- return
- }
- err = fmt.Errorf("获取因子指标系列失败, Err: " + e.Error())
- return
- }
- edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
- if e != nil {
- err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
- return
- }
- if len(edbArr) == 0 {
- err = fmt.Errorf("指标列表为空")
- return
- }
- var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
- calculateResp.SeriesId = seriesItem.FactorEdbSeriesId
- // 如果不需要进行重新计算(比如只改了系列名称)那么只更新指标系列
- seriesItem.SeriesName = extraConf.SeriesName
- seriesItem.EdbInfoType = edbArr[0].EdbInfoType
- seriesItem.ModifyTime = time.Now().Local()
- updateCols := []string{seriesOb.Cols().SeriesName, seriesOb.Cols().EdbInfoType, seriesOb.Cols().ModifyTime}
- if !recalculate {
- if e = seriesItem.Update(updateCols); e != nil {
- err = fmt.Errorf("更新因子指标系列失败, Err: " + e.Error())
- return
- }
- return
- }
- // 更新系列信息和指标关联
- seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
- seriesItem.CalculateStep = calculatesJson
- updateCols = append(updateCols, seriesOb.Cols().CalculateState, seriesOb.Cols().CalculateStep)
- mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
- for _, v := range edbArr {
- mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
- EdbInfoId: v.EdbInfoId,
- EdbCode: v.EdbCode,
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- })
- }
- if e = seriesItem.EditSeriesAndMapping(seriesItem, mappings, updateCols); e != nil {
- err = fmt.Errorf("更新因子指标系列信息失败, Err: %s", e.Error())
- return
- }
- // todo 重新计算
- _, e = FactorEdbStepCalculateRange(seriesItem.FactorEdbSeriesId, edbInfoIds, extraConf, false)
- if e != nil {
- err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
- return
- }
- // 更新系列计算状态
- cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
- seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
- seriesItem.ModifyTime = time.Now().Local()
- if e = seriesItem.Update(cols); e != nil {
- err = fmt.Errorf("更新因子指标系列计算状态失败, Err: %s", e.Error())
- return
- }
- return
- }
- // FactorEdbStepCalculateRange 因子指标-区间计算
- func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf data_manage.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
- // todo 如果指标已保存,则用指标数据还是图表指标数据?
- // 获取图表x轴y轴
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("StepCalculate计算失败, ErrMsg: %v", err)
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- }
- /*if len(calculateResp.Fail) > 0 {
- tips := "StepCalculate计算失败, ErrMsg: "
- for _, f := range calculateResp.Fail {
- tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
- }
- fmt.Println(tips)
- utils.FileLog.Info(tips)
- go alarm_msg.SendAlarmMsg(tips, 2)
- }*/
- }()
- edbInfoMappingList, e := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbArr)
- if e != nil {
- err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
- return
- }
- _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
- if err != nil {
- err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
- return
- }
- // 重新计算-先清除原数据
- calculateDataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
- cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
- pars := make([]interface{}, 0)
- pars = append(pars, seriesId)
- if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
- err = fmt.Errorf("清除原数据失败, err: %v", e)
- return
- }
- // 计算成功的保存结果
- dataArr := make([]*data_manage.FactorEdbSeriesCalculateDataQjjs, 0)
- for _, v := range edbInfoMappingList {
- dataList := v.DataList.([]*data_manage.EdbDataList)
- for _, dataItem := range dataList {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
- dataArr = append(dataArr, &data_manage.FactorEdbSeriesCalculateDataQjjs{
- FactorEdbSeriesId: seriesId,
- EdbInfoId: v.EdbInfoId,
- EdbCode: v.EdbCode,
- DataTime: dataTime,
- Value: dataItem.Value,
- CreateTime: time.Now().Local(),
- ModifyTime: time.Now().Local(),
- DataTimestamp: dataItem.DataTimestamp,
- })
- }
- }
- if len(dataArr) == 0 {
- err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
- return
- }
- if e = calculateDataOb.CreateMulti(dataArr); e != nil {
- err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
- return
- }
- return
- }
- func CheckChartRangeExtraConfig(extraConfig data_manage.ChartRangeAnalysisExtraConf) (err error, errMsg string, isSendEmail bool) {
- extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
- if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
- errMsg = "请输入指标系列名称"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
- errMsg = "计算方式参数错误"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- switch extraConfig.DateRangeType {
- case 0:
- case 1:
- if len(extraConfig.ManualDateConf) == 0 {
- errMsg = "请选择时间区间"
- err = errors.New(errMsg)
- return
- }
- // 先按开始时间排序
- sort.Sort(data_manage.ChartRangeAnalysisManualDateConfList(extraConfig.ManualDateConf))
- // 校验日期
- // 1.如果截止时间小于指标的截止日期,需要重置为指标的截止日期
- // 2.时间区间不能重叠
- for i := 1; i < len(extraConfig.ManualDateConf); i++ {
- start1, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i-1].EndDate)
- if e != nil {
- err = e
- errMsg = "截止日期格式有误"
- return
- }
- start2, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].EndDate)
- if e != nil {
- err = e
- errMsg = "截止日期格式有误"
- return
- }
- start3, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].StartDate)
- if e != nil {
- err = e
- errMsg = "截止日期格式有误"
- return
- }
- // 如果当前区间的开始时间小于等于前一个区间的结束时间,则存在重叠
- if !start2.After(start1) || start3.Before(start1) {
- errMsg = "日期区间存在重叠"
- return
- }
- }
- //如果截止时间大于指标的截止日期,需要重置为指标的截止日期
- case 2:
- if extraConfig.YearDateConf.StartDay == "" || extraConfig.YearDateConf.EndDay == "" {
- errMsg = "请选择时间区间"
- return
- }
- if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.StartDay); e != nil {
- errMsg = "开始日期格式有误"
- return
- }
- if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.EndDay); e != nil {
- errMsg = "结束日期格式有误"
- return
- }
- }
- return
- }
|