123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_chart_lib/models"
- "eta/eta_chart_lib/models/data_manage"
- "eta/eta_chart_lib/services/alarm_msg"
- "eta/eta_chart_lib/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "sort"
- "strconv"
- "time"
- )
- // GetFrequencyEn 获取频度的英文版
- func GetFrequencyEn(frequency string) (frequencyEn string) {
- switch frequency {
- case "日度":
- frequencyEn = "day"
- return
- case "周度":
- frequencyEn = "week"
- return
- case "旬度":
- frequencyEn = "ten days"
- return
- case "月度":
- frequencyEn = "month"
- return
- case "季度":
- frequencyEn = "quarter"
- return
- case "年度":
- frequencyEn = "year"
- return
- }
- return
- }
- func GetLeadUnitEn(unit string) (unitEn string) {
- switch unit {
- case "天":
- unitEn = "day"
- return
- case "周":
- unitEn = "week"
- return
- case "月":
- unitEn = "month"
- return
- case "季":
- unitEn = "quarter"
- return
- case "年":
- unitEn = "year"
- return
- }
- return
- }
- // GetChartEdbData 获取图表的指标数据
- func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, extraConfigStr, seasonExtraConfig string) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []models.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*models.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]models.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 6:
- var tmpConfig models.ChartTimeCombineExtraConf
- if extraConfigStr != `` {
- err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- extraConfig = tmpConfig
- case 7: // 柱形图
- var barConfig data_manage.BarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "柱方图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "柱方图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case 10: // 截面散点图
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = tmpExtraConfig
- case utils.CHART_TYPE_RADAR:
- var barConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case utils.CHART_TYPE_SECTION_COMBINE:
- // 预览和详情都走这个接口
- var sectionExtraConfig models.ChartSectionAllExtraConf
- if extraConfigStr == `` {
- errMsg = "截面组合图未配置"
- err = errors.New(errMsg)
- return
- }
- if chartInfoId > 0 {
- var sectionExtraConfigTmp models.ChartSectionExtraConf
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfigTmp)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg)
- return
- }
- sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
- sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
- sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
- sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
- sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
- sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
- // 查询所有的seriesEdb
- seriesEdbList, e := models.GetChartSeriesEdbByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询seriesEdb失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- // todo 是否有必要返回单位信息
- // 组装成map
- seriesEdbMap := make(map[int][]*models.ChartSectionSeriesEdbConf)
- for _, v := range seriesEdbList {
- var dateConf *models.ChartSectionSeriesDateConfItem
- if v.DateConf != "" {
- err = json.Unmarshal([]byte(v.DateConf), &dateConf)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg + err.Error())
- return
- }
- }
- tmp := &models.ChartSectionSeriesEdbConf{
- ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
- ChartSeriesId: v.ChartSeriesId,
- //ChartInfoId: v.ChartInfoId,
- EdbInfoId: v.EdbInfoId,
- DateConf: dateConf,
- EdbName: "",
- EdbNameEn: "",
- Unit: "",
- UnitEn: "",
- DateConfName: v.DateConfName,
- DateConfType: v.DateConfType,
- }
- seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
- }
- //查询series
- seriesListTmp, e := models.GetChartSeriesByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询series失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- seriesList := make([]*models.ChartSectionSeriesItem, 0)
- for _, v := range seriesListTmp {
- tmpSeries := &models.ChartSectionSeriesItem{
- ChartSeriesId: v.ChartSeriesId,
- SeriesName: v.SeriesName,
- SeriesNameEn: v.SeriesNameEn,
- ChartStyle: v.ChartStyle,
- ChartColor: v.ChartColor,
- ChartWidth: v.ChartWidth,
- IsPoint: v.IsPoint,
- IsNumber: v.IsNumber,
- IsAxis: v.IsAxis,
- MaxData: v.MaxData,
- MinData: v.MinData,
- //IsOrder: false,
- EdbInfoList: nil,
- DataList: nil,
- }
- edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
- if ok {
- tmpSeries.EdbInfoList = edbInfoList
- }
- seriesList = append(seriesList, tmpSeries)
- }
- sectionExtraConfig.SeriesList = seriesList
- } else {
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfig)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- //校验引用日期名称是否重复
- dateNameMap := make(map[string]int)
- dateNameEnMap := make(map[string]int)
- for _, v := range sectionExtraConfig.DateConfList {
- if _, ok := dateNameMap[v.DateConfName]; ok {
- errMsg = "截面组合图引用日期名称设置重复"
- err = errors.New(errMsg + v.DateConfName)
- return
- }
- if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
- errMsg = "截面组合图引用日期名称设置重复"
- err = errors.New(errMsg + v.DateConfNameEn)
- return
- }
- if v.DateConfName != "" {
- dateNameMap[v.DateConfName] = 1
- }
- if v.DateConfNameEn != "" {
- dateNameEnMap[v.DateConfNameEn] = 1
- }
- }
- //检查系列名称是否重复
- seriesNameMap := make(map[string]int)
- seriesNameEnMap := make(map[string]int)
- for _, v := range sectionExtraConfig.SeriesList {
- if _, ok := seriesNameMap[v.SeriesName]; ok {
- errMsg = "截面组合图系列名称设置重复"
- err = errors.New(errMsg + v.SeriesName)
- return
- }
- if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
- errMsg = "截面组合图系列名称设置重复"
- err = errors.New(errMsg + v.SeriesNameEn)
- return
- }
- if v.SeriesName != "" {
- seriesNameMap[v.SeriesName] = 1
- }
- if v.SeriesNameEn != "" {
- seriesNameEnMap[v.SeriesNameEn] = 1
- }
- }
- // 检查基准系列是否设置
- if sectionExtraConfig.BaseChartSeriesName == "" {
- errMsg = "截面组合图基准系列名称未设置"
- err = errors.New(errMsg)
- return
- }
- //todo 如果是详情接口,应该要从其他表里查询配置
- extraConfig = sectionExtraConfig
- default:
- xEdbIdValue = make([]int, 0)
- yDataList = make([]models.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 2: // 季节性图
- if seasonExtraConfig != "" {
- // 季节性图计算不管图上数据时间,拿所有数据
- _, tempEdbList, e := getEdbDataMapListForSeason(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
- if e != nil {
- err = e
- return
- }
- dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
- } else {
- // 兼容无配置的老图
- dataResp = new(models.SeasonChartResp)
- }
- case 6: //时序组合图
- //判断是否堆积
- timeConf := extraConfig.(models.ChartTimeCombineExtraConf)
- if extraConfigStr == "" { //历史数据,默认开启堆积
- timeConf = models.ChartTimeCombineExtraConf{
- IsHeap: 1,
- }
- }
- dataResp = models.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for _, v := range edbList {
- // 指标别名
- if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
- for _, reqEdb := range barChartConf.EdbInfoIdList {
- if v.EdbInfoId == reqEdb.EdbInfoId {
- v.EdbAliasName = reqEdb.Name
- }
- }
- }
- }
- case 10: // 截面散点图
- sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
- xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
- case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
- sectionConf := extraConfig.(models.ChartSectionAllExtraConf)
- var chartInfo *models.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = models.GetChartInfoById(chartInfoId)
- if err != nil {
- errMsg = "获取图表信息失败"
- return
- }
- }
- xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_RADAR: //雷达图
- radarConf := extraConfig.(data_manage.RadarChartInfoReq)
- xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
- }
- return
- }
- // GetEdbDataMapList 获取指标最后的基础数据
- func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
- edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- return
- }
- // getEdbDataMapList 获取指标最后的基础数据
- func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*models.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(models.ChartEdbInfoMapping)
- item.EdbInfoId = v.EdbInfoId
- item.SourceName = v.SourceName
- item.Source = v.Source
- item.EdbCode = v.EdbCode
- item.EdbName = v.EdbName
- item.EdbType = v.EdbType
- item.EdbNameEn = v.EdbNameEn
- item.Frequency = v.Frequency
- item.FrequencyEn = GetFrequencyEn(v.Frequency)
- if v.Unit != `无` {
- item.Unit = v.Unit
- }
- item.UnitEn = v.UnitEn
- item.StartDate = v.StartDate
- item.EndDate = v.EndDate
- item.ModifyTime = v.ModifyTime
- item.EdbInfoCategoryType = v.EdbInfoCategoryType
- item.PredictChartColor = v.PredictChartColor
- item.EdbAliasName = v.EdbAliasName
- if chartInfoId <= 0 {
- item.IsAxis = 1
- item.LeadValue = 0
- item.LeadUnit = ""
- //item.ChartEdbMappingId = 0
- item.ChartInfoId = 0
- item.IsOrder = false
- item.EdbInfoType = 1
- item.ChartStyle = ""
- item.ChartColor = ""
- item.ChartWidth = 0
- item.MaxData = v.MaxValue
- item.MinData = v.MinValue
- } else {
- item.IsAxis = v.IsAxis
- item.EdbInfoType = v.EdbInfoType
- item.LeadValue = v.LeadValue
- item.LeadUnit = v.LeadUnit
- item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
- //item.ChartEdbMappingId = v.ChartEdbMappingId
- item.ChartInfoId = v.ChartInfoId
- item.ChartStyle = v.ChartStyle
- item.ChartColor = v.ChartColor
- item.ChartWidth = v.ChartWidth
- item.IsOrder = v.IsOrder
- item.MaxData = v.MaxData
- item.MinData = v.MinData
- }
- item.LatestValue = v.LatestValue
- item.LatestDate = v.LatestDate
- item.UniqueCode = v.UniqueCode
- item.MoveLatestDate = v.LatestDate
- item.IsConvert = v.IsConvert
- item.ConvertType = v.ConvertType
- item.ConvertValue = v.ConvertValue
- item.ConvertUnit = v.ConvertUnit
- item.ConvertEnUnit = v.ConvertEnUnit
- var startDateReal string
- var diffSeconds int64
- if chartType == 2 { //季节性图
- startDateReal = startDate
- if len(mappingList) > 1 {
- item.IsAxis = v.IsAxis
- }
- } else {
- if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
- var startTimeRealTemp time.Time
- startDateParse, _ := time.Parse(utils.FormatDate, startDate)
- switch v.LeadUnit {
- case "天":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
- case "月":
- startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
- case "季":
- startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
- case "周":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
- case "年":
- startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
- }
- if startTimeRealTemp.Before(startDateParse) {
- startDateReal = startTimeRealTemp.Format(utils.FormatDate)
- diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
- } else {
- startDateReal = startDate
- diffSeconds = 0
- }
- // 预测指标的开始日期也要偏移
- {
- day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
- }
- } else {
- startDateReal = startDate
- }
- }
- //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- calendarPreYear := 0
- if calendar == "农历" {
- newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- calendarPreYear = newStartDateReal.Year() - 1
- newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
- startDateReal = newStartDateReal.Format(utils.FormatDate)
- }
- dataList := make([]*models.EdbDataList, 0)
- //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- switch v.EdbInfoCategoryType {
- case 0:
- dataList, err = models.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
- case 1:
- _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
- }
- if err != nil {
- return
- }
- if v.IsConvert == 1 {
- switch v.ConvertType {
- case 1:
- for i, data := range dataList {
- dataList[i].Value = data.Value * v.ConvertValue
- }
- //item.MaxData = item.MaxData * v.ConvertValue
- //item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, data := range dataList {
- dataList[i].Value = data.Value / v.ConvertValue
- }
- //item.MaxData = item.MaxData / v.ConvertValue
- //item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, data := range dataList {
- dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
- }
- //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- edbDataListMap[v.EdbInfoId] = dataList
- if diffSeconds != 0 && v.EdbInfoType == 0 {
- dataListLen := len(dataList)
- for i := 0; i < dataListLen; i++ {
- dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
- }
- }
- if chartType == 2 && item.IsAxis == 1 {
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = dataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- if calendar == "农历" {
- if len(dataList) <= 0 {
- result := new(models.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := models.AddCalculateQuarterV6(dataList)
- if tmpErr != nil {
- err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
- return
- }
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else {
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else if chartType == 2 && item.IsAxis == 0 {
- // 右轴数据处理
- xStartDate := "01-01"
- jumpYear := 0
- var seasonExtra models.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- jumpYear = seasonExtra.JumpYear
- }
- length := len(dataList)
- if length == 0 {
- return
- }
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = dataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- var rightAxisDate time.Time
- if jumpYear == 1 {
- latestDate = latestDate.AddDate(-1, 0, 0)
- }
- latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
- rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
- if err != nil {
- return
- }
- newDataList := make([]*models.EdbDataList, 0)
- for _, v := range dataList {
- dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
- if e != nil {
- err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
- return
- }
- if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
- newDataList = append(newDataList, v)
- }
- }
- item.DataList = newDataList
- } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
- //item.DataList = dataList
- } else {
- item.DataList = dataList
- }
- edbList = append(edbList, item)
- }
- return
- }
- // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
- func GetSeasonEdbInfoDataListByXDate(dataList []*models.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort models.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]models.SeasonChartLegend, 0)
- var seasonExtra models.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- xEndDate = seasonExtra.XEndDate
- jumpYear = seasonExtra.JumpYear
- legends = seasonExtra.ChartLegend
- }
- length := len(dataList)
- if length == 0 {
- return
- }
- legendMap := make(map[string]string, 0)
- if len(legends) > 0 {
- for _, v := range legends {
- legendMap[v.Name] = v.Value
- }
- }
- latestDateStr := latestDate.Format(utils.FormatDate)
- //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
- //根据数据确定最早的年份,和最近年份
- //根据横轴的日期,汇总所有的年份
- startDate := dataList[0].DataTime
- startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startYear := startDateT.Year()
- //获取数据的最新日期
- lastDate := dataList[length-1].DataTime
- lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- endYear := lastDateT.Year()
- nowYear := time.Now().Year()
- chartLegendMaxYear := 0
- dataMap := make(map[string]models.QuarterXDateItem, 0)
- quarterDataList := make([]*models.QuarterData, 0)
- quarterMap := make(map[string][]*models.EdbDataList, 0)
- //整理出日期
- idx := 1
- chartLegendMap := make(map[string]int, 0)
- for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
- startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
- currentEndYear := currentStartYear
- if jumpYear == 1 {
- currentEndYear = currentStartYear + 1
- }
- endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
- name := fmt.Sprintf("%s_%s", startStr, endStr)
- showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
- startT, tEr := time.Parse(utils.FormatDate, startStr)
- if tEr != nil {
- err = tEr
- return
- }
- endT, tEr := time.Parse(utils.FormatDate, endStr)
- if tEr != nil {
- err = tEr
- return
- }
- if lastDateT.Before(startT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- if endT.Year() > nowYear {
- //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
- nowYear = endT.Year()
- }
- item := models.QuarterXDateItem{
- StartDate: startT,
- EndDate: endT,
- ShowName: showName,
- }
- chartLegendMaxYear = endT.Year()
- dataMap[name] = item
- chartLegendMap[name] = idx
- idx++
- fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
- if lastDateT.Before(endT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- }
- lenYear := len(dataMap)
- if chartLegendMaxYear > endYear {
- chartLegendMaxYear = endYear
- }
- for k, v := range dataMap {
- if i, ok := chartLegendMap[k]; ok {
- //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
- v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
- }
- dataMap[k] = v
- }
- for _, v := range dataList {
- dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
- year := dataTimeT.Year()
- newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
- for k, dateItem := range dataMap {
- tmpVal := models.EdbDataList{
- EdbDataId: v.EdbDataId,
- EdbInfoId: v.EdbInfoId,
- DataTime: v.DataTime,
- DataTimestamp: v.DataTimestamp,
- Value: v.Value,
- }
- if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
- if jumpYear == 1 {
- //计算前一年最大的日期, 只补齐数据到去年
- beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
- beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
- if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
- newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
- } else {
- newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
- }
- } else {
- newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
- }
- timestamp := newItemDate.UnixNano() / 1e6
- tmpVal.DataTimestamp = timestamp
- tmpV := &tmpVal
- if findVal, ok := quarterMap[k]; !ok {
- findVal = append(findVal, tmpV)
- quarterMap[k] = findVal
- } else {
- findVal = append(findVal, tmpV)
- quarterMap[k] = findVal
- }
- if v.DataTime == latestDateStr {
- dateItem.CuttingDataTimestamp = timestamp
- dataMap[k] = dateItem
- }
- //break
- }
- }
- }
- for k, v := range dataMap {
- itemList := quarterMap[k]
- quarterItem := new(models.QuarterData)
- quarterItem.Years = v.ShowName
- quarterItem.Year = v.ChartLegend
- quarterItem.ChartLegend = v.ChartLegend
- if le, ok := legendMap[v.ShowName]; ok {
- if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
- quarterItem.ChartLegend = le
- }
- }
- quarterItem.DataList = itemList
- quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
- //如果等于最后的实际日期,那么将切割时间戳记录
- if quarterItem.CuttingDataTimestamp == 0 {
- //如果大于最后的实际日期,那么第一个点就是切割的时间戳
- if latestDate.Before(v.StartDate) && len(itemList) > 0 {
- quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
- }
- }
- quarterDataList = append(quarterDataList, quarterItem)
- }
- if len(quarterDataList) > 0 {
- quarterDataListSort = quarterDataList
- sort.Sort(quarterDataListSort)
- }
- return
- }
- // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
- func GetSeasonEdbInfoDataListByXDateNong(result *models.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort models.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]models.SeasonChartLegend, 0)
- var seasonExtra models.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- xEndDate = seasonExtra.XEndDate
- jumpYear = seasonExtra.JumpYear
- legends = seasonExtra.ChartLegend
- }
- length := len(result.List)
- if length == 0 {
- return
- }
- legendMap := make(map[string]string, 0)
- if len(legends) > 0 {
- for _, v := range legends {
- legendMap[v.Name] = v.Value
- }
- }
- latestDateYear := latestDate.Year()
- //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
- //根据数据确定最早的年份,和最近年份
- //根据横轴的日期,汇总所有的年份
- startYear := result.List[0].Year
- /*if jumpYear == 1 {
- if startYear != calendarPreYear {
- startYear = startYear - 1
- }
- }*/
- itemLength := len(result.List[length-1].Items)
- //获取数据的最新日期
- lastDate := result.List[length-1].Items[itemLength-1].DataTime
- maxY := result.List[length-1].Year
- lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- endYear := lastDateT.Year()
- nowYear := time.Now().Year()
- chartLegendMaxYear := 0
- dataMap := make(map[string]models.QuarterXDateItem, 0)
- quarterDataList := make([]*models.QuarterData, 0)
- resultData := make([]*models.QuarterData, 0)
- quarterMap := make(map[string][]*models.EdbDataList, 0)
- //整理出日期
- var startTmpT, endTmpT time.Time
- idx := 1
- chartLegendMap := make(map[string]int, 0)
- for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
- startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
- currentEndYear := currentStartYear
- if jumpYear == 1 {
- currentEndYear = currentStartYear + 1
- }
- endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
- showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
- startT, tEr := time.Parse(utils.FormatDate, startStr)
- if tEr != nil {
- err = tEr
- return
- }
- endT, tEr := time.Parse(utils.FormatDate, endStr)
- if tEr != nil {
- err = tEr
- return
- }
- if lastDateT.Before(startT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- if endT.Year() > nowYear {
- //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
- nowYear = endT.Year()
- }
- item := models.QuarterXDateItem{
- StartDate: startT,
- EndDate: endT,
- ShowName: showName,
- }
- chartLegendMaxYear = endT.Year()
- dataMap[showName] = item
- fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
- startTmpT = startT
- endTmpT = endT
- chartLegendMap[showName] = idx
- idx++
- if lastDateT.Before(endT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- }
- lenYear := len(dataMap)
- if chartLegendMaxYear > endYear {
- chartLegendMaxYear = endYear
- }
- for k, v := range dataMap {
- if i, ok := chartLegendMap[k]; ok {
- //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
- v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
- }
- dataMap[k] = v
- }
- yearDataListMap := make(map[int]*models.EdbDataItems, 0)
- for _, lv := range result.List {
- yearDataListMap[lv.Year] = lv
- }
- //判断哪些点应该落在同一条时间线上
- /*maxY := lastDateT.Year()
- changeFlag := false
- if lastDateT.Month() >= 11 {
- maxY = maxY + 1
- }
- if maxY < nowYear {
- changeFlag = true
- maxY = nowYear
- }*/
- /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
- endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
- minY := maxY
- if jumpYear == 1 {
- minY = maxY - 1
- }
- startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
- startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
- fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
- fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
- fmt.Printf("maxY 为%d \n", maxY)
- for name, dateItem := range dataMap {
- tY := dateItem.EndDate.Year()
- if lastDateT.Month() >= 11 {
- if maxY > endTmpT.Year() {
- tY = tY + 1
- }
- }
- lv, ok1 := yearDataListMap[tY]
- fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
- if !ok1 {
- continue
- }
- for _, item := range lv.Items {
- tmpVal := models.EdbDataList{
- EdbDataId: item.EdbDataId,
- EdbInfoId: item.EdbInfoId,
- DataTime: item.DataTime,
- DataTimestamp: item.DataTimestamp,
- Value: item.Value,
- }
- dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
- if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
- tmpV := &tmpVal
- if findVal, ok := quarterMap[name]; !ok {
- findVal = append(findVal, tmpV)
- quarterMap[name] = findVal
- } else {
- findVal = append(findVal, tmpV)
- quarterMap[name] = findVal
- }
- if lv.Year >= latestDateYear {
- // 切割的日期时间字符串
- cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
- if item.DataTime == cuttingDataTimeStr {
- dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
- dataMap[name] = dateItem
- }
- }
- }
- }
- }
- for k, v := range dataMap {
- itemList := quarterMap[k]
- quarterItem := new(models.QuarterData)
- quarterItem.Years = v.ShowName
- quarterItem.Year = v.ChartLegend
- quarterItem.ChartLegend = v.ChartLegend
- if le, ok := legendMap[v.ShowName]; ok {
- if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
- quarterItem.ChartLegend = le
- }
- }
- quarterItem.DataList = itemList
- quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
- //如果等于最后的实际日期,那么将切割时间戳记录
- if quarterItem.CuttingDataTimestamp == 0 {
- //如果大于最后的实际日期,那么第一个点就是切割的时间戳
- if latestDate.Before(v.StartDate) && len(itemList) > 0 {
- quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
- }
- }
- quarterDataList = append(quarterDataList, quarterItem)
- }
- if result.List[0].Year != calendarPreYear {
- itemList := make([]*models.EdbDataList, 0)
- items := new(models.QuarterData)
- //items.Year = calendarPreYear
- items.DataList = itemList
- newResult := make([]*models.QuarterData, 0)
- newResult = append(newResult, items)
- newResult = append(newResult, quarterDataList...)
- resultData = newResult
- } else {
- resultData = quarterDataList
- }
- if len(quarterDataList) > 0 {
- quarterDataListSort = resultData
- sort.Sort(quarterDataListSort)
- }
- return
- }
- // BarChartData 柱方图的数据处理
- func BarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []models.YData, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
- dateData := make(map[int]float64)
- if barChartInfoSort.Sort == 0 {
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- } else {
- lenBarChartInfoDateList := len(barChartInfoDateList)
- if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
- err = errors.New("排序日期异常")
- return
- }
- notDataEdbIdList := make([]int, 0) //没有数据的指标id
- // 日期配置
- barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
- for edbInfoId, dataList := range edbDataListMap {
- if len(dataList) <= 0 {
- // 没有数据的指标id
- notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
- continue
- }
- findDate := barChartInfoDate.Date
- switch barChartInfoDate.Type {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
- return
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- dateData[edbInfoId] = tmpValue
- } else {
- // 没有数据的指标id
- notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
- }
- }
- //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
- // 排序
- dateDataSort := utils.NewMapSorter(dateData)
- sort.Sort(dateDataSort)
- if barChartInfoSort.Sort == 1 {
- // 先将没有数据的指标id放在最前面
- if len(notDataEdbIdList) > 0 {
- edbIdList = append(edbIdList, notDataEdbIdList...)
- }
- for _, v := range dateDataSort {
- edbIdList = append(edbIdList, v.Key)
- }
- } else {
- for i := len(dateDataSort) - 1; i >= 0; i-- {
- edbIdList = append(edbIdList, dateDataSort[i].Key)
- }
- // 再将没有数据的指标id放在最后面
- if len(notDataEdbIdList) > 0 {
- edbIdList = append(edbIdList, notDataEdbIdList...)
- }
- }
- }
- yDataList = make([]models.YData, 0) //y轴的数据列表
- for _, barChartInfoDate := range barChartInfoDateList {
- var maxDate time.Time
- findDataList := make([]float64, 0) // 当前日期的数据值
- for _, edbInfoId := range edbIdList {
- findDate := barChartInfoDate.Date //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- findDataList = append(findDataList, 0)
- continue
- }
- switch barChartInfoDate.Type {
- case 1: //最新值
- dataList := edbDataListMap[edbInfoId]
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- findDataList = append(findDataList, tmpValue)
- } else {
- findDataList = append(findDataList, 0)
- }
- }
- yDate := "0000-00-00"
- if !maxDate.IsZero() {
- yDate = maxDate.Format(utils.FormatDate)
- }
- yDataList = append(yDataList, models.YData{
- Date: yDate,
- Value: findDataList,
- Color: barChartInfoDate.Color,
- Name: barChartInfoDate.Name,
- })
- }
- return
- }
- // ChartInfoRefreshV2 图表刷新
- // @author Roc
- // @datetime 2022-09-16 10:15:38
- // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
- func ChartInfoRefreshV2(chartInfoId int) (err error) {
- var errmsg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
- fmt.Println("ChartInfoRefresh Err:" + errmsg)
- }
- }()
- edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- edbIdList := make([]int, 0)
- for _, v := range edbMappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- // 批量刷新
- err, errmsg = EdbInfoRefreshAllFromBase(edbIdList, false)
- if err != nil {
- return
- }
- return
- }
- // GetSectionScatterChartData 柱方图的数据处理
- func GetSectionScatterChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- edbMappingMap := make(map[int]*models.ChartEdbInfoMapping)
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- edbMappingMap[v.EdbInfoId] = v
- }
- //SectionScatterSeriesInfoResp
- dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
- for _, seriesItem := range extraConfig.SeriesList {
- var maxDate time.Time
- // 系列中的指标数据
- tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
- var minXVal, maxXVal, minYVal, maxYVal float64
- for _, edbConf := range seriesItem.EdbInfoList {
- tmpItem := data_manage.SectionScatterEdbItemResp{
- IsShow: edbConf.IsShow,
- Name: edbConf.Name,
- NameEn: edbConf.NameEn,
- } //单个坐标点的数据
- //X轴的数据
- {
- edbInfoId := edbConf.XEdbInfoId //X轴的指标
- edbMappingInfo, ok := edbMappingMap[edbInfoId]
- if !ok {
- continue
- }
- findDate := edbConf.XDate //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- //findDataList = append(findDataList, 0)
- continue
- }
- tmpItem.XEdbInfoId = edbInfoId
- tmpItem.XName = edbMappingInfo.EdbName
- tmpItem.XNameEn = edbMappingInfo.EdbNameEn
- switch edbConf.XDateType {
- case 1: //最新值
- dataList := edbDataListMap[edbInfoId]
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpItem.XDate = findDate
- tmpItem.XValue = tmpValue
- } else {
- continue
- }
- }
- //Y轴的数据
- {
- edbInfoId := edbConf.YEdbInfoId //Y轴的指标
- edbMappingInfo, ok := edbMappingMap[edbInfoId]
- if !ok {
- continue
- }
- findDate := edbConf.YDate //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- //findDataList = append(findDataList, 0)
- continue
- }
- tmpItem.YEdbInfoId = edbInfoId
- tmpItem.YName = edbMappingInfo.EdbName
- tmpItem.YNameEn = edbMappingInfo.EdbNameEn
- switch edbConf.YDateType {
- case 1: //最新值
- dataList := edbDataListMap[edbInfoId]
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpItem.YDate = findDate
- tmpItem.YValue = tmpValue
- } else {
- continue
- }
- }
- // 获取当前系列的X轴的最大最小值
- {
- if tmpItem.XValue < minXVal {
- minXVal = tmpItem.XValue
- }
- if tmpItem.XValue > maxXVal {
- maxXVal = tmpItem.XValue
- }
- if tmpItem.YValue < minYVal {
- minYVal = tmpItem.YValue
- }
- if tmpItem.YValue > maxYVal {
- maxYVal = tmpItem.YValue
- }
- }
- tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
- }
- trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
- var trendLine, rSquare string
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
- tmpCoordinate1 := utils.Coordinate{
- X: tmpSeriesEdbInfo.XValue,
- Y: tmpSeriesEdbInfo.YValue,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- }
- // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
- if len(coordinateData) >= 2 {
- a, b = utils.GetLinearResult(coordinateData)
- if !math.IsNaN(a) || !math.IsNaN(b) {
- if b > 0 {
- trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
- } else {
- trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
- }
- minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
- maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
- }
- // 计算R平方
- rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
- }
- trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
- X: minXVal,
- Y: minYVal,
- }, data_manage.CoordinatePoint{
- X: maxXVal,
- Y: maxYVal,
- })
- }
- dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
- Name: seriesItem.Name,
- NameEn: seriesItem.NameEn,
- Color: seriesItem.Color,
- EdbInfoList: tmpSeriesEdbInfoList,
- ShowTrendLine: seriesItem.ShowTrendLine,
- ShowFitEquation: seriesItem.ShowFitEquation,
- ShowRSquare: seriesItem.ShowRSquare,
- TrendLine: trendLine,
- RSquare: rSquare,
- TrendLimitData: trendLimitData,
- })
- }
- chartDataResp = data_manage.SectionScatterInfoResp{
- XName: extraConfig.XName,
- XNameEn: extraConfig.XNameEn,
- XUnitName: extraConfig.XUnitName,
- XUnitNameEn: extraConfig.XUnitNameEn,
- YName: extraConfig.YName,
- YNameEn: extraConfig.YNameEn,
- YUnitName: extraConfig.YUnitName,
- YUnitNameEn: extraConfig.YUnitNameEn,
- XMinValue: extraConfig.XMinValue,
- XMaxValue: extraConfig.XMaxValue,
- YMinValue: extraConfig.YMinValue,
- YMaxValue: extraConfig.YMaxValue,
- DataList: dataListResp,
- }
- return
- }
- // RadarChartData 雷达图的数据处理
- func RadarChartData(mappingList []*models.ChartEdbInfoMapping, edbDataListMap map[int][]*models.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- chartDateList := extraConfig.DateList
- yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
- for _, chartDate := range chartDateList {
- var maxDate time.Time
- findDataList := make([]float64, 0) // 当前日期的数据值
- for _, edbInfoId := range edbIdList {
- findDate := chartDate.Date //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- findDataList = append(findDataList, 0)
- continue
- }
- switch chartDate.Type {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if e != nil {
- err = e
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
- findDataList = append(findDataList, tmpValue)
- } else {
- findDataList = append(findDataList, 0)
- }
- }
- yDate := "0000-00-00"
- if !maxDate.IsZero() {
- yDate = maxDate.Format(utils.FormatDate)
- }
- yDataList = append(yDataList, data_manage.RadarYData{
- Date: yDate,
- Value: findDataList,
- Color: chartDate.Color,
- Name: chartDate.Name,
- })
- }
- chartDataResp = data_manage.RadarChartInfoResp{
- YDataList: yDataList,
- XEdbIdValue: edbIdList,
- }
- return
- }
- // GetChartEdbDataV2 获取图表的指标数据
- func GetChartEdbDataV2(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, extraConfigStr, seasonExtraConfig string, chartInfoData ChartInfoDataShow) (edbList []*models.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []models.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*models.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]models.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 6:
- var tmpConfig models.ChartTimeCombineExtraConf
- if extraConfigStr != `` {
- err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- extraConfig = tmpConfig
- case 7: // 柱形图
- var barConfig data_manage.BarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "柱方图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "柱方图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case 10: // 截面散点图
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = tmpExtraConfig
- case utils.CHART_TYPE_RADAR:
- var barConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case utils.CHART_TYPE_SECTION_COMBINE:
- // 预览和详情都走这个接口
- var sectionExtraConfig models.ChartSectionAllExtraConf
- if extraConfigStr == `` {
- errMsg = "截面组合图未配置"
- err = errors.New(errMsg)
- return
- }
- if chartInfoId > 0 {
- var sectionExtraConfigTmp models.ChartSectionExtraConf
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfigTmp)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg)
- return
- }
- sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
- sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
- sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
- sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
- sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
- sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
- // 查询所有的seriesEdb
- seriesEdbList, e := models.GetChartSeriesEdbByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询seriesEdb失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- // todo 是否有必要返回单位信息
- // 组装成map
- seriesEdbMap := make(map[int][]*models.ChartSectionSeriesEdbConf)
- for _, v := range seriesEdbList {
- var dateConf *models.ChartSectionSeriesDateConfItem
- if v.DateConf != "" {
- err = json.Unmarshal([]byte(v.DateConf), &dateConf)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg + err.Error())
- return
- }
- }
- tmp := &models.ChartSectionSeriesEdbConf{
- ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
- ChartSeriesId: v.ChartSeriesId,
- //ChartInfoId: v.ChartInfoId,
- EdbInfoId: v.EdbInfoId,
- DateConf: dateConf,
- EdbName: "",
- EdbNameEn: "",
- Unit: "",
- UnitEn: "",
- DateConfName: v.DateConfName,
- DateConfType: v.DateConfType,
- }
- seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
- }
- //查询series
- seriesListTmp, e := models.GetChartSeriesByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询series失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- seriesList := make([]*models.ChartSectionSeriesItem, 0)
- for _, v := range seriesListTmp {
- tmpSeries := &models.ChartSectionSeriesItem{
- ChartSeriesId: v.ChartSeriesId,
- SeriesName: v.SeriesName,
- SeriesNameEn: v.SeriesNameEn,
- ChartStyle: v.ChartStyle,
- ChartColor: v.ChartColor,
- ChartWidth: v.ChartWidth,
- IsPoint: v.IsPoint,
- IsNumber: v.IsNumber,
- IsAxis: v.IsAxis,
- MaxData: v.MaxData,
- MinData: v.MinData,
- //IsOrder: false,
- EdbInfoList: nil,
- DataList: nil,
- }
- edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
- if ok {
- tmpSeries.EdbInfoList = edbInfoList
- }
- seriesList = append(seriesList, tmpSeries)
- }
- sectionExtraConfig.SeriesList = seriesList
- } else {
- err = json.Unmarshal([]byte(extraConfigStr), §ionExtraConfig)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- //校验引用日期名称是否重复
- dateNameMap := make(map[string]int)
- dateNameEnMap := make(map[string]int)
- for _, v := range sectionExtraConfig.DateConfList {
- if _, ok := dateNameMap[v.DateConfName]; ok {
- errMsg = "截面组合图引用日期名称设置重复"
- err = errors.New(errMsg + v.DateConfName)
- return
- }
- if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
- errMsg = "截面组合图引用日期名称设置重复"
- err = errors.New(errMsg + v.DateConfNameEn)
- return
- }
- if v.DateConfName != "" {
- dateNameMap[v.DateConfName] = 1
- }
- if v.DateConfNameEn != "" {
- dateNameEnMap[v.DateConfNameEn] = 1
- }
- }
- //检查系列名称是否重复
- seriesNameMap := make(map[string]int)
- seriesNameEnMap := make(map[string]int)
- for _, v := range sectionExtraConfig.SeriesList {
- if _, ok := seriesNameMap[v.SeriesName]; ok {
- errMsg = "截面组合图系列名称设置重复"
- err = errors.New(errMsg + v.SeriesName)
- return
- }
- if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
- errMsg = "截面组合图系列名称设置重复"
- err = errors.New(errMsg + v.SeriesNameEn)
- return
- }
- if v.SeriesName != "" {
- seriesNameMap[v.SeriesName] = 1
- }
- if v.SeriesNameEn != "" {
- seriesNameEnMap[v.SeriesNameEn] = 1
- }
- }
- // 检查基准系列是否设置
- if sectionExtraConfig.BaseChartSeriesName == "" {
- errMsg = "截面组合图基准系列名称未设置"
- err = errors.New(errMsg)
- return
- }
- //todo 如果是详情接口,应该要从其他表里查询配置
- extraConfig = sectionExtraConfig
- default:
- xEdbIdValue = make([]int, 0)
- yDataList = make([]models.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := chartInfoData.GetEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 6: //时序组合图
- //判断是否堆积
- timeConf := extraConfig.(models.ChartTimeCombineExtraConf)
- if extraConfigStr == "" { //历史数据,默认开启堆积
- timeConf = models.ChartTimeCombineExtraConf{
- IsHeap: 1,
- }
- }
- dataResp = models.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for _, v := range edbList {
- // 指标别名
- if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
- for _, reqEdb := range barChartConf.EdbInfoIdList {
- if v.EdbInfoId == reqEdb.EdbInfoId {
- v.EdbAliasName = reqEdb.Name
- }
- }
- }
- }
- case 10: // 截面散点图
- sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
- xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
- case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
- sectionConf := extraConfig.(models.ChartSectionAllExtraConf)
- var chartInfo *models.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = models.GetChartInfoById(chartInfoId)
- if err != nil {
- errMsg = "获取图表信息失败"
- return
- }
- }
- xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_RADAR: //雷达图
- radarConf := extraConfig.(data_manage.RadarChartInfoReq)
- xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
- }
- return
- }
- // SeasonChartData 季节性图的数据处理
- func SeasonChartData(dataList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (dataResp models.SeasonChartResp, err error) {
- var seasonConfig models.SeasonExtraItem
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonConfig)
- if err != nil {
- err = errors.New("季节性图配置异常, Err:" + err.Error())
- return
- }
- for _, mappingItem := range dataList {
- if mappingItem.IsAxis == 0 {
- continue
- }
- quarterDataList, ok := mappingItem.DataList.(models.QuarterDataList)
- if !ok {
- continue
- }
- // 上下限区间
- if seasonConfig.MaxMinLimits.Year > 0 {
- startYear := time.Now().AddDate(-seasonConfig.MaxMinLimits.Year, 0, 0).Year()
- dataResp.MaxMinLimits.List = make([]*models.MaxMinLimitsData, 0)
- dataTimeMap := make(map[time.Time]time.Time)
- dataTimeList := make([]string, 0)
- maxValueMap := make(map[time.Time]float64)
- minValueMap := make(map[time.Time]float64)
- maxMinDataList := make([]*models.MaxMinLimitsData, 0)
- var startDateStr string
- var endDateStr string
- var newDate time.Time
- startDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XStartDate)
- endDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XEndDate)
- startDate, e := time.Parse(utils.FormatDate, startDateStr)
- if e != nil {
- err = e
- return
- }
- endDate, e := time.Parse(utils.FormatDate, endDateStr)
- if e != nil {
- err = e
- return
- }
- // 日度 周度插值
- for _, v := range quarterDataList {
- if mappingItem.Frequency == "日度" || mappingItem.Frequency == "周度" {
- handleDataMap := make(map[string]float64)
- dataTimeList, _, err = HandleDataByLinearRegressionToList(v.DataList, handleDataMap)
- if err != nil {
- err = errors.New("插值处理数据异常, Err:" + err.Error())
- return
- }
- // 不包含当年
- if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
- continue
- }
- // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
- chartLegend,_ := strconv.Atoi(v.ChartLegend)
- if chartLegend < startYear {
- continue
- }
- for _, date := range dataTimeList {
- dateTime, e := time.Parse(utils.FormatDate, date)
- if e != nil {
- err = errors.New("时间格式化异常, Err:" + e.Error())
- return
- }
- if dateTime.Year() < startYear {
- continue
- }
- // 不包含2月29号
- if dateTime.Month() == 2 && dateTime.Day() == 29 {
- continue
- }
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
- if seasonConfig.JumpYear == 1 {
- if startDate.After(endDate) {
- // 如果跨年且不到一年
- // 全年截取一部分
- if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
- continue
- }
- if newDate.After(startDate.AddDate(0, 0, -1)) {
- // 减一年
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
- }
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < handleDataMap[date] {
- maxValueMap[newDate] = handleDataMap[date]
- }
- } else {
- maxValueMap[newDate] = handleDataMap[date]
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > handleDataMap[date] {
- minValueMap[newDate] = handleDataMap[date]
- }
- } else {
- minValueMap[newDate] = handleDataMap[date]
- }
- dataTimeMap[newDate] = newDate
- } else {
- // 如果跨年且大于等于一年
- // double后截取
- if newDate.After(startDate) {
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < handleDataMap[date] {
- maxValueMap[newDate] = handleDataMap[date]
- }
- } else {
- maxValueMap[newDate] = handleDataMap[date]
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > handleDataMap[date] {
- minValueMap[newDate] = handleDataMap[date]
- }
- } else {
- minValueMap[newDate] = handleDataMap[date]
- }
- dataTimeMap[newDate] = newDate
- }
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
- newEndDate := endDate.AddDate(1, 0, 0)
- if newDate.Before(newEndDate) {
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < handleDataMap[date] {
- maxValueMap[newDate] = handleDataMap[date]
- }
- } else {
- maxValueMap[newDate] = handleDataMap[date]
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > handleDataMap[date] {
- minValueMap[newDate] = handleDataMap[date]
- }
- } else {
- minValueMap[newDate] = handleDataMap[date]
- }
- dataTimeMap[newDate] = newDate
- }
- }
- } else {
- // 如果不跨年 正常获取
- // 获取当前日期所在区间
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < handleDataMap[date] {
- maxValueMap[newDate] = handleDataMap[date]
- }
- } else {
- maxValueMap[newDate] = handleDataMap[date]
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > handleDataMap[date] {
- minValueMap[newDate] = handleDataMap[date]
- }
- } else {
- minValueMap[newDate] = handleDataMap[date]
- }
- dataTimeMap[newDate] = newDate
- }
- }
- } else {
- // 旬度、月度、季度、半年度 不插值,需要先把日期列表和数据map取出来
- for _, vv := range v.DataList {
- dateTime, e := time.Parse(utils.FormatDate, vv.DataTime)
- if e != nil {
- err = errors.New("时间格式化异常, Err:" + e.Error())
- return
- }
- // 不包含当年
- if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
- continue
- }
- if dateTime.Year() < startYear {
- continue
- }
- // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
- chartLegend,_ := strconv.Atoi(v.ChartLegend)
- if chartLegend < startYear {
- continue
- }
- // 月度的2月29号日期改为2月28日
- if dateTime.Month() == 2 && dateTime.Day() == 29 {
- dateTime = dateTime.AddDate(0, 0, -1)
- }
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
- if seasonConfig.JumpYear == 1 {
- if startDate.After(endDate) {
- // 如果跨年且不到一年
- // 全年截取一部分
- if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
- continue
- }
- if newDate.After(startDate.AddDate(0, 0, -1)) {
- // 减一年
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
- }
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < vv.Value {
- maxValueMap[newDate] = vv.Value
- }
- } else {
- maxValueMap[newDate] = vv.Value
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > vv.Value {
- minValueMap[newDate] = vv.Value
- }
- } else {
- minValueMap[newDate] = vv.Value
- }
- dataTimeMap[newDate] = newDate
- } else {
- // 如果跨年且大于等于一年
- // double后截取
- if newDate.After(startDate) {
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < vv.Value {
- maxValueMap[newDate] = vv.Value
- }
- } else {
- maxValueMap[newDate] = vv.Value
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > vv.Value {
- minValueMap[newDate] = vv.Value
- }
- } else {
- minValueMap[newDate] = vv.Value
- }
- dataTimeMap[newDate] = newDate
- }
- newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
- newEndDate := endDate.AddDate(1, 0, 0)
- if newDate.Before(newEndDate) {
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < vv.Value {
- maxValueMap[newDate] = vv.Value
- }
- } else {
- maxValueMap[newDate] = vv.Value
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > vv.Value {
- minValueMap[newDate] = vv.Value
- }
- } else {
- minValueMap[newDate] = vv.Value
- }
- dataTimeMap[newDate] = newDate
- }
- }
- } else {
- // 如果不跨年 正常获取
- // 获取当前日期所在区间
- // 处理上下限列表
- if value, ok := maxValueMap[newDate]; ok {
- if value < vv.Value {
- maxValueMap[newDate] = vv.Value
- }
- } else {
- maxValueMap[newDate] = vv.Value
- }
- if value, ok := minValueMap[newDate]; ok {
- if value > vv.Value {
- minValueMap[newDate] = vv.Value
- }
- } else {
- minValueMap[newDate] = vv.Value
- }
- dataTimeMap[newDate] = newDate
- }
- }
- }
- }
- for _, v := range dataTimeMap {
- maxMinItem := &models.MaxMinLimitsData{}
- if maxValue, ok := maxValueMap[v]; ok {
- maxMinItem.MaxValue = maxValue
- } else {
- maxMinItem.MaxValue = minValueMap[v]
- }
- if minValue, ok := minValueMap[v]; ok {
- maxMinItem.MinValue = minValue
- } else {
- maxMinItem.MinValue = maxValueMap[v]
- }
- maxMinItem.DataTime = v.Format(utils.FormatDate)
- maxMinItem.DataTimestamp = v.UnixNano() / 1e6
- maxMinDataList = append(maxMinDataList, maxMinItem)
- }
- // 排序
- sort.Slice(maxMinDataList, func(i, j int) bool {
- return maxMinDataList[i].DataTime < maxMinDataList[j].DataTime
- })
- dataResp.MaxMinLimits.List = maxMinDataList
- dataResp.MaxMinLimits.Color = seasonConfig.MaxMinLimits.Color
- dataResp.MaxMinLimits.Legend = seasonConfig.MaxMinLimits.Legend
- dataResp.MaxMinLimits.IsShow = seasonConfig.MaxMinLimits.IsShow
- dataResp.MaxMinLimits.IsAdd = seasonConfig.MaxMinLimits.IsAdd
- dataResp.MaxMinLimits.Year = seasonConfig.MaxMinLimits.Year
- }
- // 自定义同期均线
- if seasonConfig.SamePeriodAverage.Year > 0 {
- handleDataMap := make(map[string]float64)
- dataTimeMap := make(map[time.Time]time.Time)
- dataTimeList := make([]string, 0)
- valueMap := make(map[time.Time]float64)
- averageDataList := make([]*models.SamePeriodAverageData, 0)
- dateNumMap := make(map[time.Time]float64)
- for i := len(quarterDataList) - 1; i >= 0; i-- {
- // 插值成日度
- dataTimeList, _, err = HandleDataByLinearRegressionToList(quarterDataList[i].DataList, handleDataMap)
- if err != nil {
- err = errors.New("插值处理数据异常, Err:" + err.Error())
- return
- }
- for _, date := range dataTimeList {
- dateTime, e := time.Parse(utils.FormatDate, date)
- if e != nil {
- err = errors.New("时间格式化异常, Err:" + e.Error())
- return
- }
- startYear := time.Now().AddDate(-seasonConfig.SamePeriodAverage.Year, 0, 0).Year()
- if dateTime.Year() < startYear {
- continue
- }
- // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
- chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
- if chartLegend < startYear {
- continue
- }
- // 不包含2月29号
- if dateTime.Month() == 2 && dateTime.Day() == 29 {
- continue
- }
- // 不包含当年
- if quarterDataList[i].ChartLegend == strconv.Itoa(time.Now().Year()) {
- continue
- }
- newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
- // 处理均值
- if value, ok := valueMap[newDate]; ok {
- valueMap[newDate] = handleDataMap[date] + value
- } else {
- valueMap[newDate] = handleDataMap[date]
- }
- dateNumMap[newDate] += 1
- dataTimeMap[newDate] = newDate
- }
- }
- //year := float64(seasonConfig.SamePeriodAverage.Year)
- for _, v := range dataTimeMap {
- averageItem := &models.SamePeriodAverageData{}
- year := dateNumMap[v]
- if value, ok := valueMap[v]; ok {
- if year > 0 {
- averageItem.Value = value / year
- } else {
- averageItem.Value = value
- }
- }
- averageItem.DataTime = v.Format(utils.FormatDate)
- averageItem.DataTimestamp = v.UnixNano() / 1e6
- averageDataList = append(averageDataList, averageItem)
- }
- // 排序
- sort.Slice(averageDataList, func(i, j int) bool {
- return averageDataList[i].DataTime < averageDataList[j].DataTime
- })
- dataResp.SamePeriodAverage.List = averageDataList
- dataResp.SamePeriodAverage.Year = seasonConfig.SamePeriodAverage.Year
- dataResp.SamePeriodAverage.Color = seasonConfig.SamePeriodAverage.Color
- dataResp.SamePeriodAverage.Legend = seasonConfig.SamePeriodAverage.Legend
- dataResp.SamePeriodAverage.IsShow = seasonConfig.SamePeriodAverage.IsShow
- dataResp.SamePeriodAverage.LineType = seasonConfig.SamePeriodAverage.LineType
- dataResp.SamePeriodAverage.LineWidth = seasonConfig.SamePeriodAverage.LineWidth
- dataResp.SamePeriodAverage.IsAdd = seasonConfig.SamePeriodAverage.IsAdd
- }
- // 自定义同期标准差
- if seasonConfig.SamePeriodStandardDeviation.Year > 1 && seasonConfig.SamePeriodStandardDeviation.Multiple > 0 {
- startYear := time.Now().AddDate(-seasonConfig.SamePeriodStandardDeviation.Year, 0, 0).Year()
- // 先算均值,再算标准差
- handleDataMap := make(map[string]float64)
- dataTimeMap := make(map[time.Time]time.Time)
- dataTimeValueMap := make(map[time.Time][]float64)
- dataTimeList := make([]string, 0)
- valueMap := make(map[time.Time]float64)
- samePeriodStandardDeviationList := make([]*models.MaxMinLimitsData, 0)
- for i := len(quarterDataList) - 1; i > len(quarterDataList)-seasonConfig.SamePeriodAverage.Year-1 && i > 0; i-- {
- // 插值成日度
- dataTimeList, _, err = HandleDataByLinearRegressionToListV2(quarterDataList[i].DataList, handleDataMap)
- if err != nil {
- err = errors.New("插值处理数据异常, Err:" + err.Error())
- return
- }
- for _, date := range dataTimeList {
- dateTime, e := time.Parse(utils.FormatDate, date)
- if e != nil {
- err = errors.New("时间格式化异常, Err:" + e.Error())
- return
- }
- if dateTime.Year() < startYear {
- continue
- }
- // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
- chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
- if chartLegend < startYear {
- continue
- }
- // 不包含2月29号
- if dateTime.Month() == 2 && dateTime.Day() == 29 {
- continue
- }
- newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
- // 处理均值
- if value, ok := valueMap[newDate]; ok {
- valueMap[newDate] = (handleDataMap[date] + value) / 2
- } else {
- valueMap[newDate] = handleDataMap[date]
- }
- dataTimeMap[newDate] = newDate
- valueList := dataTimeValueMap[newDate]
- valueList = append(valueList, handleDataMap[date])
- dataTimeValueMap[newDate] = valueList
- }
- }
- for _, v := range dataTimeMap {
- valueList := dataTimeValueMap[v]
- stdev := utils.CalculateStandardDeviation(valueList)
- stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
- maxMinItem := &models.MaxMinLimitsData{}
- if value, ok := valueMap[v]; ok {
- maxMinItem.MaxValue = value + stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
- maxMinItem.MinValue = value - stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
- }
- maxMinItem.DataTime = v.Format(utils.FormatDate)
- maxMinItem.DataTimestamp = v.UnixNano() / 1e6
- samePeriodStandardDeviationList = append(samePeriodStandardDeviationList, maxMinItem)
- }
- // 排序
- sort.Slice(samePeriodStandardDeviationList, func(i, j int) bool {
- return samePeriodStandardDeviationList[i].DataTime < samePeriodStandardDeviationList[j].DataTime
- })
- dataResp.SamePeriodStandardDeviation.List = samePeriodStandardDeviationList
- dataResp.SamePeriodStandardDeviation.Color = seasonConfig.SamePeriodStandardDeviation.Color
- dataResp.SamePeriodStandardDeviation.Legend = seasonConfig.SamePeriodStandardDeviation.Legend
- dataResp.SamePeriodStandardDeviation.IsShow = seasonConfig.SamePeriodStandardDeviation.IsShow
- dataResp.SamePeriodStandardDeviation.Multiple = seasonConfig.SamePeriodStandardDeviation.Multiple
- dataResp.SamePeriodStandardDeviation.Year = seasonConfig.SamePeriodStandardDeviation.Year
- dataResp.SamePeriodStandardDeviation.IsAdd = seasonConfig.SamePeriodStandardDeviation.IsAdd
- }
- // 自定义右轴
- if seasonConfig.RightAxis.IndicatorType != 0 {
- if seasonConfig.RightAxis.IndicatorType == 1 {
- startTime, _ := time.Parse(utils.FormatDate, mappingItem.StartDate)
- for i := len(quarterDataList) - 1; i > len(quarterDataList)-2 && i > 0; i-- {
- var rightMappingItem models.ChartEdbInfoMapping
- rightMappingItem = *mappingItem
- // 计算同比值
- tbzDataList, minValue, maxValue, e := GetEdbDataTbzForSeason(mappingItem.Frequency, quarterDataList[i].DataList, startTime)
- if e != nil {
- err = errors.New("计算同比值失败, Err:" + e.Error())
- return
- }
- rightMappingItem.DataList = tbzDataList
- rightMappingItem.MaxData = maxValue
- rightMappingItem.MinData = minValue
- dataResp.RightAxis.EdbInfoList = append(dataResp.RightAxis.EdbInfoList, &rightMappingItem)
- }
- }
- dataResp.RightAxis.SeasonRightAxis = seasonConfig.RightAxis
- }
- }
- return
- }
- // getEdbDataMapListForSeason 获取指标最后的基础数据-插值补充好数据-季节性图用
- func getEdbDataMapListForSeason(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*models.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(models.ChartEdbInfoMapping)
- item.EdbInfoId = v.EdbInfoId
- item.SourceName = v.SourceName
- item.Source = v.Source
- item.EdbCode = v.EdbCode
- item.EdbName = v.EdbName
- item.EdbNameEn = v.EdbNameEn
- item.Frequency = v.Frequency
- item.EdbType = v.EdbType
- item.FrequencyEn = GetFrequencyEn(v.Frequency)
- if v.Unit != `无` {
- item.Unit = v.Unit
- }
- item.UnitEn = v.UnitEn
- item.StartDate = v.StartDate
- item.EndDate = v.EndDate
- item.ModifyTime = v.ModifyTime
- item.EdbInfoCategoryType = v.EdbInfoCategoryType
- item.PredictChartColor = v.PredictChartColor
- item.ClassifyId = v.ClassifyId
- if chartInfoId <= 0 {
- item.IsAxis = 1
- item.LeadValue = 0
- item.LeadUnit = ""
- item.ChartEdbMappingId = 0
- item.ChartInfoId = 0
- item.IsOrder = false
- item.EdbInfoType = 1
- item.ChartStyle = ""
- item.ChartColor = ""
- item.ChartWidth = 0
- item.MaxData = v.MaxValue
- item.MinData = v.MinValue
- } else {
- item.IsAxis = v.IsAxis
- item.EdbInfoType = v.EdbInfoType
- item.LeadValue = v.LeadValue
- item.LeadUnit = v.LeadUnit
- item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
- item.ChartEdbMappingId = v.ChartEdbMappingId
- item.ChartInfoId = v.ChartInfoId
- item.ChartStyle = v.ChartStyle
- item.ChartColor = v.ChartColor
- item.ChartWidth = v.ChartWidth
- item.IsOrder = v.IsOrder
- item.MaxData = v.MaxData
- item.MinData = v.MinData
- }
- item.LatestValue = v.LatestValue
- item.LatestDate = v.LatestDate
- item.UniqueCode = v.UniqueCode
- item.MoveLatestDate = v.LatestDate
- item.EdbAliasName = v.EdbAliasName
- item.IsConvert = v.IsConvert
- item.ConvertType = v.ConvertType
- item.ConvertValue = v.ConvertValue
- item.ConvertUnit = v.ConvertUnit
- item.ConvertEnUnit = v.ConvertEnUnit
- item.IsJoinPermission = v.IsJoinPermission
- var startDateReal string
- var diffSeconds int64
- if chartType == 2 { //季节性图
- startDateReal = startDate
- if len(mappingList) > 1 {
- item.IsAxis = v.IsAxis
- }
- } else {
- if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
- var startTimeRealTemp time.Time
- startDateParse, _ := time.Parse(utils.FormatDate, startDate)
- switch v.LeadUnit {
- case "天":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
- case "月":
- startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
- case "季":
- startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
- case "周":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
- case "年":
- startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
- }
- if startTimeRealTemp.Before(startDateParse) {
- startDateReal = startTimeRealTemp.Format(utils.FormatDate)
- diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
- } else {
- startDateReal = startDate
- diffSeconds = 0
- }
- // 预测指标的开始日期也要偏移
- {
- day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
- }
- } else {
- startDateReal = startDate
- }
- }
- //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- calendarPreYear := 0
- if calendar == "农历" {
- newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- calendarPreYear = newStartDateReal.Year() - 1
- newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
- startDateReal = newStartDateReal.Format(utils.FormatDate)
- }
- dataList := make([]*models.EdbDataList, 0)
- //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- //fmt.Println("calendarPreYear:", calendarPreYear)
- //var newEdbInfo *data_manage.EdbInfo
- switch v.EdbInfoCategoryType {
- case 0:
- dataList, err = models.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
- case 1:
- _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
- }
- if err != nil {
- return
- }
- handleDataMap := make(map[string]float64)
- newDataList, _, _, e := HandleDataByLinearRegressionToListV3(dataList, handleDataMap)
- if e != nil {
- err = e
- return
- }
- if v.IsConvert == 1 {
- switch v.ConvertType {
- case 1:
- for i, data := range newDataList {
- newDataList[i].Value = data.Value * v.ConvertValue
- }
- //item.MaxData = item.MaxData * v.ConvertValue
- //item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, data := range newDataList {
- newDataList[i].Value = data.Value / v.ConvertValue
- }
- //item.MaxData = item.MaxData / v.ConvertValue
- //item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, data := range newDataList {
- if data.Value <= 0 {
- err = errors.New("数据中含有负数或0,无法对数运算")
- return
- }
- newDataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
- }
- //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- edbDataListMap[v.EdbInfoId] = newDataList
- if diffSeconds != 0 && v.EdbInfoType == 0 {
- newDataListLen := len(newDataList)
- for i := 0; i < newDataListLen; i++ {
- newDataList[i].DataTimestamp = newDataList[i].DataTimestamp - diffSeconds
- }
- }
- if chartType == 2 && item.IsAxis == 1 {
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = newDataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- if calendar == "农历" {
- if len(newDataList) <= 0 {
- result := new(models.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := models.AddCalculateQuarterV6(newDataList)
- if tmpErr != nil {
- err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
- return
- }
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else {
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(newDataList, latestDate, seasonExtraConfig)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else if chartType == 2 && item.IsAxis == 0 {
- // 右轴数据处理
- xStartDate := "01-01"
- jumpYear := 0
- var seasonExtra models.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- jumpYear = seasonExtra.JumpYear
- }
- length := len(newDataList)
- if length == 0 {
- return
- }
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = newDataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- var rightAxisDate time.Time
- if jumpYear == 1 {
- latestDate = latestDate.AddDate(-1, 0, 0)
- }
- latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
- rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
- if err != nil {
- return
- }
- newList := make([]*models.EdbDataList, 0)
- for _, v := range newDataList {
- dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
- if e != nil {
- err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
- return
- }
- if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
- newList = append(newList, v)
- }
- }
- item.DataList = newList
- } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
- //item.DataList = newDataList
- } else {
- item.DataList = newDataList
- }
- edbList = append(edbList, item)
- }
- return
- }
|