1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/services/data/data_manage_permission"
- "eta/eta_api/utils"
- "fmt"
- "math"
- "sort"
- "strconv"
- "strings"
- "time"
- "github.com/shopspring/decimal"
- )
- func ChartInfoRefreshV1(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)
- }
- }()
- baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- baseMap := make(map[int]*data_manage.EdbInfo)
- for _, bv := range baseEdbInfoArr {
- // 如果不是普通指标,那么过滤
- if bv.EdbInfoType != 0 {
- continue
- }
- if _, ok := baseMap[bv.EdbInfoId]; !ok {
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
- }
- baseMap[bv.EdbInfoId] = bv
- }
- fmt.Println("calculateInfoArr:", len(calculateInfoArr))
- newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
- calculateMap := make(map[int]*data_manage.EdbInfo)
- var calculateArr []int
- for _, bv := range calculateInfoArr {
- if _, ok := calculateMap[bv.EdbInfoId]; !ok {
- newCalculateInfoArr = append(newCalculateInfoArr, bv)
- calculateArr = append(calculateArr, bv.EdbInfoId)
- }
- calculateMap[bv.EdbInfoId] = bv
- }
- sort.Ints(calculateArr)
- // 刷新相关基础指标
- for _, bv := range newBaseEdbInfoArr {
- err, errmsg = refreshBaseEdbInfo(bv)
- if err != nil {
- return
- }
- }
- // 刷新相关计算指标
- for _, v := range calculateArr {
- edbInfo := calculateMap[v]
- if edbInfo == nil {
- return err
- }
- err, errmsg = refreshCalculateEdbInfo(edbInfo)
- if err != nil {
- return
- }
- }
- return err
- }
- // BatchChartInfoRefresh 图表批量刷新
- func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
- if len(chartInfoList) <= 0 {
- return
- }
- chartInfoIdSlice := make([]string, 0)
- for _, chartInfo := range chartInfoList {
- chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
- }
- 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)
- }
- }()
- baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- //基础指标去重
- newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- baseMap := make(map[int]*data_manage.EdbInfo)
- for _, bv := range baseEdbInfoArr {
- fmt.Println(bv.UniqueCode)
- if _, ok := baseMap[bv.EdbInfoId]; !ok {
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
- }
- baseMap[bv.EdbInfoId] = bv
- }
- fmt.Println("calculateInfoArr:", len(calculateInfoArr))
- //计算指标去重
- newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
- calculateMap := make(map[int]*data_manage.EdbInfo)
- var calculateArr []int
- for _, bv := range calculateInfoArr {
- if _, ok := calculateMap[bv.EdbInfoId]; !ok {
- newCalculateInfoArr = append(newCalculateInfoArr, bv)
- calculateArr = append(calculateArr, bv.EdbInfoId)
- }
- calculateMap[bv.EdbInfoId] = bv
- }
- sort.Ints(calculateArr)
- // 刷新相关基础指标
- for _, bv := range newBaseEdbInfoArr {
- err, errmsg = refreshBaseEdbInfo(bv)
- if err != nil {
- return
- }
- }
- // 刷新相关计算指标
- for _, v := range calculateArr {
- edbInfo := calculateMap[v]
- if edbInfo == nil {
- return err
- }
- err, errmsg = refreshCalculateEdbInfo(edbInfo)
- if err != nil {
- return
- }
- }
- return err
- }
- // refreshBaseEdbInfo 刷新基础指标
- func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
- source := bv.Source
- subSource := bv.SubSource
- edbInfoId := bv.EdbInfoId
- edbCode := bv.EdbCode
- //startDate := bv.StartDate
- frequency := bv.Frequency
- if bv.StartDate == "0000-00-00" {
- return
- }
- sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
- if err != nil {
- return
- }
- var limitDay int
- startDate := ""
- switch frequency {
- case "日度":
- limitDay = utils.DATA_START_REFRESH_LIMIT
- case "周度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 7
- case "月度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 30
- case "季度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 90
- case "年度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 365
- default:
- limitDay = utils.DATA_START_REFRESH_LIMIT
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- fmt.Println("source:", source)
- respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
- if err != nil {
- errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
- return
- }
- if respItem.Ret != 200 {
- errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
- return
- }
- maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
- if err != nil {
- return
- }
- if maxAndMinItem != nil {
- err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- if err != nil {
- return
- }
- }
- return
- }
- // refreshCalculateEdbInfo 刷新计算指标
- func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
- startDate := edbInfo.StartDate
- source := edbInfo.Source
- if source == utils.DATA_SOURCE_CALCULATE {
- startDate = edbInfo.StartDate
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startDate = sTime.Format(utils.FormatDate)
- }
- result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if err != nil {
- fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
- errMsg = "RefreshEdbCalculateData Err:" + err.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = errors.New("刷新失败")
- return
- }
- return
- }
- type ChartInfoReq struct {
- ChartInfoId int `description:"图表id,新增时传0"`
- }
- // DeleteChartInfoDataRedis 清除图表缓存
- func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
- var req ChartInfoReq
- err = json.Unmarshal(bodyByte, &req)
- if err != nil {
- return
- }
- if req.ChartInfoId > 0 {
- err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
- }
- return
- }
- // DeleteChartClassifyRedis 清除图表分类缓存
- func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
- err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
- return
- }
- // GetChartInfoDataKey 获取图表缓存的key
- func GetChartInfoDataKey(chartInfoId int) string {
- key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
- return key
- }
- // CheckOpChartPermission 判断图表操作权限
- func CheckOpChartPermission(sysUser *system.Admin, createUserId int, haveOperaAuth bool) (ok bool) {
- // 没有数据权限,那么直接返回
- if !haveOperaAuth {
- return
- }
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
- ok = true
- }
- // 如果图表创建人与当前操作人相同的话,那么就是允许操作
- if ok == false && createUserId == sysUser.AdminId {
- ok = true
- }
- // 如果图表权限id 是 1 ,那么允许编辑
- if ok == false && sysUser.ChartPermission == 1 {
- ok = true
- }
- return
- }
- // GetChartEdbData 获取图表的指标数据
- func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 6:
- var tmpConfig data_manage.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 data_manage.ChartSectionAllExtraConf
- if extraConfigStr == `` {
- errMsg = "截面组合图未配置"
- err = errors.New(errMsg)
- return
- }
- if chartInfoId > 0 {
- var sectionExtraConfigTmp data_manage.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 := data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询seriesEdb失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- // todo 是否有必要返回单位信息
- // 组装成map
- seriesEdbMap := make(map[int][]*data_manage.ChartSectionSeriesEdbConf)
- for _, v := range seriesEdbList {
- var dateConf *data_manage.ChartSectionSeriesDateConfItem
- if v.DateConf != "" {
- err = json.Unmarshal([]byte(v.DateConf), &dateConf)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg + e.Error())
- return
- }
- }
- tmp := &data_manage.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 := data_manage.GetChartSeriesByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询series失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- seriesList := make([]*data_manage.ChartSectionSeriesItem, 0)
- for _, v := range seriesListTmp {
- tmpSeries := &data_manage.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([]data_manage.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 6: //时序组合图
- //判断是否堆积
- timeConf := extraConfig.(data_manage.ChartTimeCombineExtraConf)
- if extraConfigStr == "" { //历史数据,默认开启堆积
- timeConf = data_manage.ChartTimeCombineExtraConf{
- IsHeap: 1,
- }
- }
- dataResp = data_manage.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
- 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(data_manage.SeasonChartResp)
- }
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for k := range yDataList {
- yDataList[k].Unit = barChartConf.Unit
- yDataList[k].UnitEn = barChartConf.UnitEn
- }
- 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(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
- 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
- }
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
- sectionConf := extraConfig.(data_manage.ChartSectionAllExtraConf)
- var chartInfo *data_manage.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = data_manage.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 []*data_manage.ChartEdbInfoMapping, seasonExtra string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtra)
- return
- }
- // getEdbDataMapList 获取指标最后的基础数据
- func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*data_manage.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(data_manage.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([]*data_manage.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 = data_manage.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
- }
- 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 {
- if data.Value <= 0 {
- err = errors.New("数据中含有负数或0,无法对数运算")
- return
- }
- 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(data_manage.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := data_manage.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 data_manage.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([]*data_manage.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 []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]data_manage.SeasonChartLegend, 0)
- var seasonExtra data_manage.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]data_manage.QuarterXDateItem, 0)
- quarterDataList := make([]*data_manage.QuarterData, 0)
- quarterMap := make(map[string][]*data_manage.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 := data_manage.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 := data_manage.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]
- /*if len(itemList) == 0 {
- continue
- }*/
- quarterItem := new(data_manage.QuarterData)
- quarterItem.Years = v.ShowName
- 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 *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]data_manage.SeasonChartLegend, 0)
- var seasonExtra data_manage.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]data_manage.QuarterXDateItem, 0)
- quarterDataList := make([]*data_manage.QuarterData, 0)
- resultData := make([]*data_manage.QuarterData, 0)
- quarterMap := make(map[string][]*data_manage.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 := data_manage.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]*data_manage.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 := data_manage.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]
- /*if len(itemList) == 0 {
- continue
- }*/
- quarterItem := new(data_manage.QuarterData)
- quarterItem.Years = v.ShowName
- 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([]*data_manage.EdbDataList, 0)
- items := new(data_manage.QuarterData)
- //items.Year = calendarPreYear
- items.DataList = itemList
- newResult := make([]*data_manage.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 []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []data_manage.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([]data_manage.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 {
- 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.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, isAsync bool) {
- 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, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false, false)
- if err != nil {
- return
- }
- return
- }
- // BatchChartInfoRefreshV2 图表批量刷新
- // @author Roc
- // @datetime 2022-09-16 10:15:38
- // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
- func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo, redisKey string) (err error, isAsync bool) {
- if len(chartInfoList) <= 0 {
- return
- }
- chartInfoIdSlice := make([]int, 0)
- for _, chartInfo := range chartInfoList {
- chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
- }
- 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)
- }
- }()
- // 是否删除缓存
- isDeleteCache := true
- if redisKey != `` {
- //设置最多10分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Minute*10)
- defer func() {
- if isDeleteCache {
- utils.Rc.Delete(redisKey)
- }
- }()
- }
- // 获取需要刷新的指标信息
- edbIdList := make([]int, 0)
- edbInfoIdMap := make(map[int]int)
- edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
- for _, v := range edbMappingList {
- if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
- edbIdList = append(edbIdList, v.EdbInfoId)
- edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
- }
- }
- // 获取需要刷新的指标列表
- newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
- // 需要刷新的指标数量
- totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
- if totalEdbInfo > 20 { // 关联指标过多的时候,异步刷新
- isAsync = true
- isDeleteCache = false // 不删除缓存
- //go refreshChartEdbInfo(chartInfoList, redisKey, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- // 加入队列刷新
- var refreshItem EdbQueueRefreshReq
- refreshItem.RefreshKey = redisKey
- for _, v := range chartInfoList {
- key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
- refreshItem.ItemRefreshKeys = append(refreshItem.ItemRefreshKeys, key)
- // eta平台的缓存,用于Ppt等地方的展示
- refreshItem.ItemRefreshKeys = append(refreshItem.ItemRefreshKeys, GetChartInfoDataKey(v.ChartInfoId))
- }
- refreshItem.BaseEdbInfoArr = newBaseEdbInfoArr
- refreshItem.BasePredictEdbInfoArr = newBasePredictEdbInfoArr
- refreshItem.CalculateMap = newCalculateMap
- refreshItem.PredictCalculateMap = newPredictCalculateMap
- refreshItem.CalculateArr = calculateArr
- refreshItem.PredictCalculateArr = predictCalculateArr
- go PushEdb2Refresh(refreshItem)
- } else {
- err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //清除图表缓存
- for _, v := range chartInfoList {
- key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
- _ = utils.Rc.Delete(key)
- // eta平台的缓存,用于Ppt等地方的展示
- _ = utils.Rc.Delete(GetChartInfoDataKey(v.ChartInfoId))
- }
- }
- return
- }
- // refreshChartEdbInfo
- // @Description: 刷新图表中的质保列表
- // @author: Roc
- // @datetime 2023-12-01 16:05:39
- // @param chartInfoList []*data_manage.ChartInfo
- // @param redisKey string
- // @param newBaseEdbInfoArr []*data_manage.EdbInfo
- // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
- // @param newCalculateMap map[int]*data_manage.EdbInfo
- // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
- // @param calculateArr []int
- // @param predictCalculateArr []int
- func refreshChartEdbInfo(chartInfoList []*data_manage.ChartInfo, redisKey string, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
- edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //清除图表缓存
- for _, v := range chartInfoList {
- key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
- _ = utils.Rc.Delete(key)
- }
- // 如果是有插入redisKey,那么刷新结束后就清除缓存
- if redisKey != `` {
- utils.Rc.Delete(redisKey)
- }
- }
- // GetBatchChartRefreshKey
- // @Description: 获取批量刷新key
- // @author: Roc
- // @datetime 2023-11-30 13:30:26
- // @param source string
- // @param primaryId int
- // @param subId int
- // @return string
- func GetBatchChartRefreshKey(source string, primaryId, subId int) string {
- if source == `` {
- return ``
- }
- return fmt.Sprint("batch_chart_refresh:", source, ":", primaryId, ":", subId)
- }
- // CheckBatchChartRefreshResult
- // @Description: 判断报告中的图表是否刷新完成
- // @author: Roc
- // @datetime 2023-11-30 13:30:26
- // @param source string
- // @param primaryId int
- // @param subId int
- // @return string
- func CheckBatchChartRefreshResult(source string, primaryId, subId int) (refreshResult bool) {
- refreshResult = true
- redisKey := GetBatchChartRefreshKey(source, primaryId, subId)
- if redisKey != `` {
- // 如果找到了key,那么就是还在更新中
- isOk := utils.Rc.IsExist(redisKey)
- if isOk {
- refreshResult = false
- }
- }
- return
- }
- func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
- // 相关性图表不判断指标
- if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
- return true
- }
- if chartNameEn != "" && len(edbList) <= 0 {
- return true
- }
- if chartNameEn == "" {
- return false
- }
- // 截面散点图的话,肯定是有英文配置的
- if chartType == utils.CHART_TYPE_SECTION_SCATTER {
- return true
- }
- for _, v := range edbList {
- if v.EdbNameEn == "" {
- return false
- }
- if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
- return false
- }
- }
- return true
- }
- func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
- if edbNameEn == "" {
- return false
- }
- if unit != "无" && unit != "" && unitEn == "" {
- return false
- }
- return true
- }
- // CheckIsDisableChart 根据指标Id判断是否是禁用图表
- // return int 0:启用,1:禁用
- func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
- crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
- if err != nil {
- return
- }
- //没有配置,立马返回
- if crmConfig.ConfigValue == `` {
- return
- }
- confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
- for _, edbInfoId := range edbInfoIdArr {
- for _, confEdbInfoId := range confEdbInfoIdList {
- if strconv.Itoa(edbInfoId) == confEdbInfoId {
- isDisable = 1
- return
- }
- }
- }
- return
- }
- // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
- func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
- switch chartType {
- case 10: //截面散点
- var extraConfig data_manage.SectionScatterReq
- err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
- if err != nil {
- return
- }
- // 判断是否有配置日期序列
- if len(extraConfig.SeriesList) <= 0 {
- errMsg = `请配置序列`
- err = errors.New(errMsg)
- return
- }
- // 判断是否有填写指标
- if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
- errMsg = `请选择指标`
- err = errors.New(errMsg)
- return
- }
- // 遍历指标列表获取指标id
- edbIdMap := make(map[int]int)
- for _, v := range extraConfig.SeriesList[0].EdbInfoList {
- // X 轴的指标id
- if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
- edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
- edbIdList = append(edbIdList, v.XEdbInfoId)
- }
- // Y 轴的指标id
- if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
- edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
- edbIdList = append(edbIdList, v.YEdbInfoId)
- }
- }
- case utils.CHART_TYPE_RADAR:
- var extraConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- case utils.CHART_TYPE_SECTION_COMBINE:
- var extraConfig data_manage.ChartSectionAllExtraConf
- if extraConfigStr == `` {
- errMsg = "截面组合图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg + err.Error())
- return
- }
- //校验引用日期名称是否重复
- dateNameMap := make(map[string]int)
- dateNameEnMap := make(map[string]int)
- for _, v := range extraConfig.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 extraConfig.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 extraConfig.BaseChartSeriesName == "" {
- errMsg = "截面组合图基准系列名称未设置"
- err = errors.New(errMsg)
- return
- }
- //todo 如果是详情接口,应该要从其他表里查询配置
- // 遍历指标列表获取指标id
- edbIdMap := make(map[int]int)
- hasUsed := make(map[string]struct{})
- for _, v := range extraConfig.SeriesList {
- for _, item := range v.EdbInfoList {
- if item.DateConfName != "" && item.DateConfType == 1 {
- hasUsed[item.DateConfName] = struct{}{}
- }
- if _, ok := edbIdMap[item.EdbInfoId]; !ok {
- edbIdMap[item.EdbInfoId] = item.EdbInfoId
- edbIdList = append(edbIdList, item.EdbInfoId)
- }
- }
- }
- // todo 遍历引用日期获取引用日期里的指标ID, 如果引用日期里的指标没有被引用,是否加入映射
- for _, v := range extraConfig.DateConfList {
- if _, ok := hasUsed[v.DateConfName]; !ok {
- continue
- }
- if _, ok := edbIdMap[v.EdbInfoId]; !ok && v.EdbInfoId > 0 {
- edbIdMap[v.EdbInfoId] = v.EdbInfoId
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- }
- }
- return
- }
- // GetSectionScatterChartData 截面散点图的数据处理
- func GetSectionScatterChartData(chartInfoId int, mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.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]*data_manage.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: //最新值
- 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: //最新值
- 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,
- IsNameDefault: seriesItem.IsNameDefault,
- Color: seriesItem.Color,
- EdbInfoList: tmpSeriesEdbInfoList,
- ShowTrendLine: seriesItem.ShowTrendLine,
- ShowFitEquation: seriesItem.ShowFitEquation,
- ShowRSquare: seriesItem.ShowRSquare,
- TrendLine: trendLine,
- RSquare: rSquare,
- TrendLimitData: trendLimitData,
- })
- }
- // 截面散点图点击详情时自动更新系列名
- if len(extraConfig.SeriesList) > 0 {
- // 默认名字的时候才自动更新
- if extraConfig.SeriesList[0].IsNameDefault {
- firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
- needUpdate := false
- if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
- extraConfig.SeriesList[0].Name = v.LatestDate
- extraConfig.SeriesList[0].NameEn = v.LatestDate
- dataListResp[0].Name = v.LatestDate
- dataListResp[0].NameEn = v.LatestDate
- needUpdate = true
- }
- extraConfigByte, e := json.Marshal(extraConfig)
- if e != nil {
- errMsg := "截面散点系列更新异常"
- err = errors.New(errMsg)
- return
- }
- extraConfigStr := string(extraConfigByte)
- if needUpdate {
- err = data_manage.EditChartInfoExtraConfig(chartInfoId, extraConfigStr)
- if err != nil {
- errMsg := "截面散点系列更新异常"
- err = errors.New(errMsg)
- return
- }
- }
- }
- }
- 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
- }
- // AddChartInfo 添加图表
- func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName, 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
- }
- //if req.ChartThemeId <= 0 {
- // errMsg = "请选择主题"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //
- //// 判断主题是否存在
- //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() {
- // errMsg = "主题不存在"
- // err = errors.New(errMsg)
- // return
- // }
- // errMsg = "获取主题信息失败"
- // return
- //}
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取分类信息失败"
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- chartType := req.ChartType
- extraConfig := req.ExtraConfig
- var extraConfigEdbInfoIdArr []int
- extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(chartType, extraConfig)
- if err != nil {
- err = errors.New("添加失败:" + err.Error())
- return
- }
- // 季节性图表额外配置信息
- var seasonExtraConfig string
- // 关联指标
- var edbInfoIdArr []int
- chartEdbInfoList := req.ChartEdbInfoList
- if len(chartEdbInfoList) > 0 {
- for _, v := range 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("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- if edbInfo.EdbInfoId <= 0 {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- edbInfo.EdbNameSource = edbInfo.EdbName
- //edbList = append(edbList, edbInfo)
- //新增季节性图表数据计算
- if chartType == 2 {
- existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
- if tmpErr != nil {
- errMsg = "保存失败"
- err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if existCount <= 0 {
- go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
- }
- // 处理季节性图表横轴配置
- {
- if req.SeasonExtraConfig.XEndDate != "" {
- if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
- errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
- err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
- return
- }
- seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
- if tErr != nil {
- errMsg = "季节性图表配置信息异常"
- err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
- return
- }
- seasonExtraConfig = string(seasonExtra)
- }
- }
- }
- }
- } else {
- if extraConfig == `` {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- return
- }
- edbInfoIdArr = extraConfigEdbInfoIdArr
- lenEdbInfoIdArr := len(edbInfoIdArr)
- if lenEdbInfoIdArr > 0 {
- tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
- if tmpErr != nil {
- errMsg = "指标异常!"
- err = errors.New("指标异常!Err:" + tmpErr.Error())
- return
- }
- if len(tmpEdbList) != lenEdbInfoIdArr {
- errMsg = "指标异常!"
- err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
- return
- }
- for _, v := range tmpEdbList {
- chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
- EdbInfoId: v.EdbInfoId,
- MaxData: 0,
- MinData: 0,
- IsOrder: false,
- IsAxis: 1,
- EdbInfoType: 1,
- LeadValue: 0,
- LeadUnit: "",
- ChartStyle: "",
- ChartColor: "",
- PredictChartColor: "",
- ChartWidth: 0,
- Source: utils.CHART_SOURCE_DEFAULT,
- })
- }
- }
- }
- if len(edbInfoIdArr) <= 0 {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- return
- }
- // 图表额外配置
- var allExtraConfig string
- extraConfig, allExtraConfig, err, errMsg = HandleExtraConfig(0, chartType, extraConfig)
- if err != nil {
- if errMsg == `` {
- errMsg = "指标异常!"
- }
- err = errors.New("指标异常!Err:" + err.Error())
- return
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "保存失败"
- // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "所选指标已存在相同指标"
- // return
- //}
- {
- var count int // 同名指标数量
- //判断图表是否存在
- var condition string
- var pars []interface{}
- // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
- //condition += " AND chart_classify_id=? "
- //pars = append(pars, req.ChartClassifyId)
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name = ? AND source = ? "
- }
- pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
- count, err = data_manage.GetChartInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- barChartConf := `` //柱方图的配置信息
- if chartType == 7 {
- barChartConfReq := req.BarChartInfo
- if len(barChartConfReq.EdbInfoIdList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if len(barChartConfReq.DateList) <= 0 {
- errMsg = "请选择日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
- if tmpErr != nil {
- errMsg = "柱方图信息异常"
- err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
- return
- }
- barChartConf = string(barChartConfByte)
- }
- disableVal := CheckIsDisableChart(edbInfoIdArr)
- chartInfo = new(data_manage.ChartInfo)
- chartInfo.ChartName = req.ChartName
- chartInfo.ChartNameEn = req.ChartName
- chartInfo.EdbInfoIds = edbInfoIdStr
- chartInfo.ChartClassifyId = req.ChartClassifyId
- chartInfo.SysUserId = sysUserId
- chartInfo.SysUserRealName = sysUserRealName
- 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)
- // todo 判断是否需要重新计算用户的start_date
- if req.DateType > 0 {
- chartInfo.DateType = req.DateType
- } else {
- chartInfo.DateType = 3
- }
- if chartType == 0 {
- chartType = 1
- }
- chartInfo.ChartType = chartType
- calendar := req.Calendar
- if calendar == "" {
- calendar = "公历"
- }
- chartInfo.Calendar = calendar
- chartInfo.StartDate = req.StartDate
- chartInfo.EndDate = req.EndDate
- 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.Right2Min = req.Right2Min
- chartInfo.Right2Max = req.Right2Max
- chartInfo.MinMaxSave = req.MinMaxSave
- chartInfo.Disabled = disableVal
- chartInfo.BarConfig = barChartConf
- chartInfo.ExtraConfig = extraConfig
- chartInfo.SeasonExtraConfig = seasonExtraConfig
- chartInfo.StartYear = req.StartYear
- chartInfo.Source = utils.CHART_SOURCE_DEFAULT
- chartInfo.ChartThemeId = req.ChartThemeId
- chartInfo.SourcesFrom = req.SourcesFrom
- chartInfo.Instructions = req.Instructions
- chartInfo.MarkersLines = req.MarkersLines
- chartInfo.MarkersAreas = req.MarkersAreas
- chartInfo.Unit = req.Unit
- chartInfo.UnitEn = req.UnitEn
- chartInfo.ChartAlias = req.ChartAlias
- newId, err := data_manage.AddChartInfo(chartInfo)
- if err != nil {
- errMsg = `保存失败`
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- chartInfo.ChartInfoId = int(newId)
- mapList := make([]*data_manage.ChartEdbMapping, 0)
- for _, v := range chartEdbInfoList {
- mapItem := new(data_manage.ChartEdbMapping)
- mapItem.ChartInfoId = int(newId)
- mapItem.EdbInfoId = v.EdbInfoId
- mapItem.CreateTime = time.Now()
- mapItem.ModifyTime = time.Now()
- edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", edbTimestamp))
- mapItem.MaxData = v.MaxData
- mapItem.MinData = v.MinData
- mapItem.IsOrder = v.IsOrder
- mapItem.IsAxis = v.IsAxis
- mapItem.EdbInfoType = v.EdbInfoType
- mapItem.LeadValue = v.LeadValue
- mapItem.LeadUnit = v.LeadUnit
- mapItem.ChartStyle = v.ChartStyle
- mapItem.ChartColor = v.ChartColor
- mapItem.PredictChartColor = v.PredictChartColor
- mapItem.ChartWidth = v.ChartWidth
- mapItem.Source = utils.CHART_SOURCE_DEFAULT
- mapItem.EdbAliasName = v.EdbAliasName
- mapItem.IsConvert = v.IsConvert
- mapItem.ConvertType = v.ConvertType
- mapItem.ConvertValue = v.ConvertValue
- mapItem.ConvertUnit = v.ConvertUnit
- mapItem.ConvertEnUnit = v.ConvertEnUnit
- mapList = append(mapList, mapItem)
- }
- err = data_manage.AddChartEdbMapping(mapList)
- if err != nil {
- errMsg = `保存失败`
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- if chartType == utils.CHART_TYPE_SECTION_COMBINE {
- err = data_manage.AddChartSeriesAndEdbMapping(allExtraConfig, int(newId))
- if err != nil {
- errMsg = `保存失败`
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- }
- // 添加指标引用记录
- _ = SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
- //添加es数据
- go EsAddOrEditChartInfo(chartInfo.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 // 默认错误的时候要发送邮件
- 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
- }
- //if req.ChartThemeId <= 0 {
- // errMsg = "请选择主题!"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //
- //// 判断主题是否存在
- //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() {
- // errMsg = "主题不存在"
- // err = errors.New(errMsg)
- // return
- // }
- // errMsg = "获取主题信息失败"
- // return
- //}
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取分类信息失败"
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- 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
- }
- var extraConfigEdbInfoIdArr []int
- extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
- if err != nil {
- err = errors.New("添加失败:" + err.Error())
- return
- }
- // 季节性图表额外配置信息
- var seasonExtraConfig string
- // 图表关联指标id
- chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
- // 关联指标
- var edbInfoIdArr []int
- // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
- if chartItem.ChartType != 10 && chartItem.ChartType != utils.CHART_TYPE_SECTION_COMBINE {
- //edbList := make([]*data_manage.EdbInfo, 0)
- 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
- //edbList = append(edbList, edbInfo)
- //新增季节性图表数据计算
- if req.ChartType == 2 {
- existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
- if tmpErr != nil {
- errMsg = "保存失败"
- err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if existCount <= 0 {
- go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
- }
- // 处理季节性图表横轴配置
- {
- if req.SeasonExtraConfig.XEndDate != "" {
- if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
- errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
- err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
- return
- }
- seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
- if tErr != nil {
- errMsg = "季节性图表配置信息异常"
- err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
- return
- }
- seasonExtraConfig = string(seasonExtra)
- }
- }
- }
- }
- chartEdbInfoList = req.ChartEdbInfoList
- } else {
- if req.ExtraConfig == `` {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- edbInfoIdArr = extraConfigEdbInfoIdArr
- lenEdbInfoIdArr := len(edbInfoIdArr)
- if lenEdbInfoIdArr > 0 {
- tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
- if tmpErr != nil {
- errMsg = "指标异常!"
- err = errors.New("指标异常!Err:" + tmpErr.Error())
- return
- }
- if len(tmpEdbList) != lenEdbInfoIdArr {
- errMsg = "指标异常!"
- err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
- return
- }
- for _, v := range tmpEdbList {
- chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
- EdbInfoId: v.EdbInfoId,
- MaxData: 0,
- MinData: 0,
- IsOrder: false,
- IsAxis: 1,
- EdbInfoType: 1,
- LeadValue: 0,
- LeadUnit: "",
- ChartStyle: "",
- ChartColor: "",
- PredictChartColor: "",
- ChartWidth: 0,
- Source: utils.CHART_SOURCE_DEFAULT,
- })
- }
- }
- }
- if len(edbInfoIdArr) <= 0 {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 图表额外配置
- extraConfig, allExtraConfig, err, errMsg := HandleExtraConfig(req.ChartInfoId, req.ChartType, req.ExtraConfig)
- if err != nil {
- if errMsg == `` {
- errMsg = "指标异常!"
- }
- err = errors.New("指标异常!Err:" + err.Error())
- return
- }
- req.ExtraConfig = extraConfig
- // 权限
- var haveOperaAuth bool
- {
- // 已授权分类id
- permissionChartIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, chartItem.ChartInfoId, chartItem.ChartClassifyId)
- if tmpErr != nil {
- if errMsg == `` {
- errMsg = "获取失败!"
- }
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- haveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartItem.IsJoinPermission, chartClassify.IsJoinPermission, chartItem.ChartInfoId, chartItem.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
- }
- //图表操作权限
- ok := CheckOpChartPermission(sysUser, chartItem.SysUserId, haveOperaAuth)
- if !ok {
- errMsg = "没有该图表的操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var calendar string
- var dateType int
- dateType = req.DateType
- if req.ChartType != chartItem.ChartType {
- if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
- calendar = "公历"
- }
- if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
- dateType = 3
- }
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
- //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "保存失败"
- // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "所选指标已存在相同指标"
- // return
- //}
- // 判断图表是否存在
- {
- var count int
- var condition string
- var pars []interface{}
- condition += " AND chart_info_id<>? "
- pars = append(pars, req.ChartInfoId)
- // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
- //condition += " AND chart_classify_id=? "
- //pars = append(pars, req.ChartClassifyId)
- switch lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? AND source = ? "
- default:
- condition += " AND chart_name = ? AND source = ? "
- }
- pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
- count, err = data_manage.GetChartInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- barChartConf := `` //柱方图的配置信息
- if req.ChartType == 7 {
- barChartConfReq := req.BarChartInfo
- if len(barChartConfReq.EdbInfoIdList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if len(barChartConfReq.DateList) <= 0 {
- errMsg = "请选择日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
- if tmpErr != nil {
- errMsg = "柱方图信息异常"
- err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
- return
- }
- barChartConf = string(barChartConfByte)
- }
- // 图表启用与否
- disableVal := CheckIsDisableChart(edbInfoIdArr)
- switch lang {
- case utils.EnLangVersion:
- req.ChartNameEn = req.ChartName
- req.ChartName = chartItem.ChartName
- if req.UnitEn == `` {
- req.UnitEn = req.Unit
- }
- req.Unit = chartItem.Unit
- default:
- req.ChartNameEn = chartItem.ChartNameEn
- req.UnitEn = chartItem.UnitEn
- }
- err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList, seasonExtraConfig)
- 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
- if chartItem.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
- err = data_manage.EditChartSeriesAndEdbMapping(allExtraConfig, chartItem.ChartInfoId)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- }
- // todo 添加指标引用记录
- _ = SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
- //添加es数据
- go EsAddOrEditChartInfo(chartItem.ChartInfoId)
- //修改my eta es数据
- go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
- return
- }
- // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
- func GetEdbSourceByEdbInfoIdList(chartEdbInfoMappingList []*data_manage.ChartEdbInfoMapping) (sourceNameList, sourceNameEnList []string) {
- sourceNameList = make([]string, 0)
- sourceNameEnList = make([]string, 0)
- sourceMap := make(map[int]string)
- for _, v := range chartEdbInfoMappingList {
- // 指标类型:1:基础指标,2:计算指标
- if v.EdbType == 2 || v.EdbInfoCategoryType == 1 {
- //sourceMap[0] = "弘则研究"
- baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- for _, baseEdbInfo := range baseEdbInfoArr {
- if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
- sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
- }
- }
- } else {
- sourceMap[v.Source] = v.SourceName
- }
- }
- for source, sourceName := range sourceMap {
- if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
- continue
- }
- sourceNameList = append(sourceNameList, sourceName)
- sourceNameEn, ok := utils.DataSourceEnMap[source]
- if !ok {
- sourceNameEn = sourceName
- }
- sourceNameEnList = append(sourceNameEnList, sourceNameEn)
- }
- //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
- //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
- // 图表来源
- conf, e := models.GetBusinessConf()
- if e != nil {
- return
- }
- if conf[models.BusinessConfCompanyName] != "" {
- sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
- sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
- }
- return
- }
- // RadarChartData 雷达图的数据处理
- func RadarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.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
- }
- // GetChartConvertEdbData 获取图表数据转换的指标数据
- func GetChartConvertEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string, isAxis int) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- var extraConfig interface{}
- switch chartType {
- 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
- default:
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := getEdbConvertDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig, isAxis)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 2: // 季节性图
- if seasonExtraConfig != "" {
- // 季节性图计算不管图上数据时间,拿所有数据
- _, tempEdbList, e := getEdbDataMapList(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
- if e != nil {
- err = e
- return
- }
- dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
- } else {
- // 兼容无配置的老图
- dataResp = new(data_manage.SeasonChartResp)
- }
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for k := range yDataList {
- yDataList[k].Unit = barChartConf.Unit
- yDataList[k].UnitEn = barChartConf.UnitEn
- }
- 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(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
- 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
- }
- // 这个数据没有必要返回给前端
- 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
- }
- func getEdbConvertDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string, isAxis int) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*data_manage.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(data_manage.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
- 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([]*data_manage.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 = data_manage.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
- }
- 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 && 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(data_manage.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := data_manage.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 && isAxis == 0 {
- // 右轴数据处理,只要最新一年
- 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
- }
- newDataList := make([]*data_manage.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.Year() == latestDate.Year() {
- 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
- }
- // GetChartEdbDataV2 获取图表的指标数据
- func GetChartEdbDataV2(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string, chartInfoData ChartInfoDataShow) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 6:
- var tmpConfig data_manage.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 data_manage.ChartSectionAllExtraConf
- if extraConfigStr == `` {
- errMsg = "截面组合图未配置"
- err = errors.New(errMsg)
- return
- }
- if chartInfoId > 0 {
- var sectionExtraConfigTmp data_manage.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 := data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询seriesEdb失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- // todo 是否有必要返回单位信息
- // 组装成map
- seriesEdbMap := make(map[int][]*data_manage.ChartSectionSeriesEdbConf)
- for _, v := range seriesEdbList {
- var dateConf *data_manage.ChartSectionSeriesDateConfItem
- if v.DateConf != "" {
- err = json.Unmarshal([]byte(v.DateConf), &dateConf)
- if err != nil {
- errMsg = "截面组合图配置异常"
- err = errors.New(errMsg + e.Error())
- return
- }
- }
- tmp := &data_manage.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 := data_manage.GetChartSeriesByChartInfoId(chartInfoId)
- if e != nil {
- errMsg = "查询series失败"
- err = errors.New(errMsg + e.Error())
- return
- }
- seriesList := make([]*data_manage.ChartSectionSeriesItem, 0)
- for _, v := range seriesListTmp {
- tmpSeries := &data_manage.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([]data_manage.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := chartInfoData.GetEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 6: //时序组合图
- //判断是否堆积
- timeConf := extraConfig.(data_manage.ChartTimeCombineExtraConf)
- if extraConfigStr == "" { //历史数据,默认开启堆积
- timeConf = data_manage.ChartTimeCombineExtraConf{
- IsHeap: 1,
- }
- }
- dataResp = data_manage.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
- 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(data_manage.SeasonChartResp)
- }
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for k := range yDataList {
- yDataList[k].Unit = barChartConf.Unit
- yDataList[k].UnitEn = barChartConf.UnitEn
- }
- 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(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
- 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
- }
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
- sectionConf := extraConfig.(data_manage.ChartSectionAllExtraConf)
- var chartInfo *data_manage.ChartInfo
- if chartInfoId > 0 {
- chartInfo, err = data_manage.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 []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (dataResp data_manage.SeasonChartResp, err error) {
- var seasonConfig data_manage.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.(data_manage.QuarterDataList)
- if !ok {
- continue
- }
- // 上下限区间
- if seasonConfig.MaxMinLimits.Year > 0 {
- startYear := time.Now().AddDate(-seasonConfig.MaxMinLimits.Year, 0, 0).Year()
- dataResp.MaxMinLimits.List = make([]*data_manage.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([]*data_manage.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 := &data_manage.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([]*data_manage.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 := &data_manage.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([]*data_manage.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 := &data_manage.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 data_manage.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
- }
- // 计算百分位对应的值
- func PercentileAlgorithm(x float64, data []float64) float64 {
- N := float64(len(data))
- n := (x / 100) * (N - 1)
- // 如果n是整数,则直接返回第n个数值
- if n == float64(int(n)) {
- return data[int(n)-1]
- }
- // 如果n是小数,执行插值
- nInt := int(n)
- e := n - float64(nInt)
- // 取出相邻的两个数值
- Sn := data[nInt-1]
- Sn1 := data[nInt]
- // 计算插值结果
- y := Sn + e*(Sn1-Sn)
- return y
- }
- func MarkerLineCalculate(markerLine data_manage.MarkersLine, dataList interface{}, chartInfo *data_manage.ChartInfoView) (value string, err error) {
- if markerLine.Calculation == 1 {
- // 区间均值
- averge := 0.0
- length := 0
- // 计算左轴
- if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
- //季节性图结构体不一样
- quarterDataList := dataList.(data_manage.QuarterDataList)
- for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
- for _, vv := range quarterData.DataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if vv.DataTime >= startDate && vv.DataTime <= endDate {
- averge += vv.Value
- length += 1
- }
- } else {
- averge += vv.Value
- length += 1
- }
- }
- averge = averge / float64(length)
- value = fmt.Sprintf("%.2f", averge)
- }
- } else {
- dataList := dataList.([]*data_manage.EdbDataList)
- for _, dataItem := range dataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
- averge += dataItem.Value
- length += 1
- }
- } else {
- averge += dataItem.Value
- length += 1
- }
- }
- averge = averge / float64(length)
- value = fmt.Sprintf("%.2f", averge)
- }
- } else if markerLine.Calculation == 2 {
- // 区间均值加N倍标准差
- averge := 0.0
- length := 0
- // 计算左轴
- if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
- //季节性图结构体不一样
- faloatList := make([]float64, 0)
- quarterDataList := dataList.(data_manage.QuarterDataList)
- for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
- for _, vv := range quarterData.DataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if vv.DataTime >= startDate && vv.DataTime <= endDate {
- faloatList = append(faloatList, vv.Value)
- averge += vv.Value
- length += 1
- }
- } else {
- faloatList = append(faloatList, vv.Value)
- averge += vv.Value
- length += 1
- }
- }
- averge = averge / float64(length)
- stdev := utils.CalculateStandardDeviation(faloatList)
- stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
- value = fmt.Sprintf("%.2f", averge+stdev*markerLine.CalculationValue)
- }
- } else {
- dataList := dataList.([]*data_manage.EdbDataList)
- floatList := make([]float64, 0)
- for _, dataItem := range dataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
- floatList = append(floatList, dataItem.Value)
- averge += dataItem.Value
- length += 1
- }
- } else {
- floatList = append(floatList, dataItem.Value)
- averge += dataItem.Value
- length += 1
- }
- }
- averge = averge / float64(length)
- stdev := utils.CalculateStandardDeviation(floatList)
- stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
- value = fmt.Sprintf("%.2f", averge+stdev*markerLine.CalculationValue)
- }
- } else if markerLine.Calculation == 3 {
- // 区间个数分位
- markerLineValue := 0.0
- if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
- //季节性图结构体不一样
- faloatList := make([]float64, 0)
- quarterDataList := dataList.(data_manage.QuarterDataList)
- for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
- for _, vv := range quarterData.DataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if vv.DataTime >= startDate && vv.DataTime <= endDate {
- faloatList = append(faloatList, vv.Value)
- }
- } else {
- faloatList = append(faloatList, vv.Value)
- }
- }
- sort.Float64s(faloatList)
- markerLineValue = PercentileAlgorithm(markerLine.CalculationValue, faloatList)
- value = fmt.Sprintf("%.2f", markerLineValue)
- }
- } else {
- dataList := dataList.([]*data_manage.EdbDataList)
- floatList := make([]float64, 0)
- for _, dataItem := range dataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
- floatList = append(floatList, dataItem.Value)
- }
- } else {
- floatList = append(floatList, dataItem.Value)
- }
- }
- sort.Float64s(floatList)
- markerLineValue = PercentileAlgorithm(markerLine.CalculationValue, floatList)
- value = fmt.Sprintf("%.2f", markerLineValue)
- }
- } else if markerLine.Calculation == 4 {
- // 数值分位
- markerLineValue := 0.0
- maxValue := 0.0
- minValue := 0.0
- if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
- //季节性图结构体不一样
- quarterDataList := dataList.(data_manage.QuarterDataList)
- for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
- for _, vv := range quarterData.DataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if vv.DataTime >= startDate && vv.DataTime <= endDate {
- if maxValue < vv.Value {
- maxValue = vv.Value
- }
- if minValue > vv.Value {
- minValue = vv.Value
- }
- }
- } else {
- if maxValue < vv.Value {
- maxValue = vv.Value
- }
- if minValue > vv.Value {
- minValue = vv.Value
- }
- }
- }
- markerLineValue = CalculatePercentile(markerLine.CalculationValue, minValue, maxValue)
- value = fmt.Sprintf("%.2f", markerLineValue)
- }
- } else {
- dataList := dataList.([]*data_manage.EdbDataList)
- for _, dataItem := range dataList {
- if markerLine.TimeIntervalType == 1 {
- startDate := markerLine.StartDate.Date
- endDate := time.Now().Format(utils.FormatDate)
- if markerLine.StartDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- startDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- startDate = time.Now().Format(utils.FormatDate)
- }
- startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
- }
- if markerLine.EndDate.TimeType == 1 {
- // 固定
- endDate = markerLine.EndDate.Date
- } else if markerLine.EndDate.TimeType == 2 {
- // 动态
- if markerLine.StartDate.Conf.BaseDate == 1 {
- // 指标最新日期
- endDate = dataList[len(dataList)-1].DataTime
- } else {
- // 系统日期
- endDate = time.Now().Format(utils.FormatDate)
- }
- endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
- }
- if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
- if maxValue < dataItem.Value {
- maxValue = dataItem.Value
- }
- if minValue > dataItem.Value {
- minValue = dataItem.Value
- }
- }
- } else {
- if maxValue < dataItem.Value {
- maxValue = dataItem.Value
- }
- if minValue > dataItem.Value {
- minValue = dataItem.Value
- }
- }
- }
- markerLineValue = CalculatePercentile(markerLine.CalculationValue, minValue, maxValue)
- value = fmt.Sprintf("%.2f", markerLineValue)
- }
- }
- return
- }
- // HandleDateChange 处理日期变换
- func HandleDateChange(date string, edbDateConf data_manage.EdbDateChangeConf) (newDate string, err error) {
- newDate = date
- if newDate != "" {
- if len(edbDateConf.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 edbDateConf.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, _ = handleSystemAppointDateT2(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
- }
- // handleSystemAppointDateT
- // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
- // @author: Roc
- // @datetime2023-10-27 09:31:35
- // @param Frequency string
- // @param Day string
- // @return date string
- // @return err error
- // @return errMsg string
- func handleSystemAppointDateT2(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
- //currDate := time.Now()
- switch frequency {
- case "本周":
- day := int(currDate.Weekday())
- if day == 0 { // 周日
- day = 7
- }
- num := 0
- switch appointDay {
- case "周一":
- num = 1
- case "周二":
- num = 2
- case "周三":
- num = 3
- case "周四":
- num = 4
- case "周五":
- num = 5
- case "周六":
- num = 6
- case "周日":
- num = 7
- }
- day = num - day
- date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
- case "本旬":
- day := currDate.Day()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if day <= 10 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
- } else if day <= 20 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if day <= 10 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
- } else if day <= 20 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本月":
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
- case "最后一天":
- tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本季":
- month := currDate.Month()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if month <= 3 {
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- } else if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
- } else if month <= 9 {
- tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if month <= 3 {
- tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
- } else if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
- } else if month <= 9 {
- tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本半年":
- month := currDate.Month()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本年":
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- case "最后一天":
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- date = tmpDate.Format(utils.FormatDate)
- default:
- errMsg = "错误的日期频度:" + frequency
- err = errors.New(errMsg)
- return
- }
- return
- }
- // CalculatePercentile 计算数值分位
- func CalculatePercentile(x float64, min float64, max float64) float64 {
- return (x/100)*(max-min) + min
- }
- // getEdbDataMapListForSeason 获取指标最后的基础数据-插值补充好数据-季节性图用
- func getEdbDataMapListForSeason(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*data_manage.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(data_manage.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([]*data_manage.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 = data_manage.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(data_manage.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := data_manage.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 data_manage.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([]*data_manage.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
- }
|