chart_info.go 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/alarm_msg"
  10. "eta/eta_api/services/data/data_manage_permission"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "github.com/shopspring/decimal"
  14. "math"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. func ChartInfoRefreshV1(chartInfoId int) (err error) {
  21. var errmsg string
  22. defer func() {
  23. if err != nil {
  24. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  25. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  26. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  27. }
  28. }()
  29. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
  30. if err != nil {
  31. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  32. return
  33. }
  34. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  35. baseMap := make(map[int]*data_manage.EdbInfo)
  36. for _, bv := range baseEdbInfoArr {
  37. // 如果不是普通指标,那么过滤
  38. if bv.EdbInfoType != 0 {
  39. continue
  40. }
  41. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  42. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  43. }
  44. baseMap[bv.EdbInfoId] = bv
  45. }
  46. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  47. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  48. calculateMap := make(map[int]*data_manage.EdbInfo)
  49. var calculateArr []int
  50. for _, bv := range calculateInfoArr {
  51. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  52. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  53. calculateArr = append(calculateArr, bv.EdbInfoId)
  54. }
  55. calculateMap[bv.EdbInfoId] = bv
  56. }
  57. sort.Ints(calculateArr)
  58. // 刷新相关基础指标
  59. for _, bv := range newBaseEdbInfoArr {
  60. err, errmsg = refreshBaseEdbInfo(bv)
  61. if err != nil {
  62. return
  63. }
  64. }
  65. // 刷新相关计算指标
  66. for _, v := range calculateArr {
  67. edbInfo := calculateMap[v]
  68. if edbInfo == nil {
  69. return err
  70. }
  71. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  72. if err != nil {
  73. return
  74. }
  75. }
  76. return err
  77. }
  78. // BatchChartInfoRefresh 图表批量刷新
  79. func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
  80. if len(chartInfoList) <= 0 {
  81. return
  82. }
  83. chartInfoIdSlice := make([]string, 0)
  84. for _, chartInfo := range chartInfoList {
  85. chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
  86. }
  87. var errmsg string
  88. defer func() {
  89. if err != nil {
  90. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  91. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  92. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  93. }
  94. }()
  95. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
  96. if err != nil {
  97. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  98. return
  99. }
  100. //基础指标去重
  101. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  102. baseMap := make(map[int]*data_manage.EdbInfo)
  103. for _, bv := range baseEdbInfoArr {
  104. fmt.Println(bv.UniqueCode)
  105. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  106. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  107. }
  108. baseMap[bv.EdbInfoId] = bv
  109. }
  110. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  111. //计算指标去重
  112. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  113. calculateMap := make(map[int]*data_manage.EdbInfo)
  114. var calculateArr []int
  115. for _, bv := range calculateInfoArr {
  116. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  117. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  118. calculateArr = append(calculateArr, bv.EdbInfoId)
  119. }
  120. calculateMap[bv.EdbInfoId] = bv
  121. }
  122. sort.Ints(calculateArr)
  123. // 刷新相关基础指标
  124. for _, bv := range newBaseEdbInfoArr {
  125. err, errmsg = refreshBaseEdbInfo(bv)
  126. if err != nil {
  127. return
  128. }
  129. }
  130. // 刷新相关计算指标
  131. for _, v := range calculateArr {
  132. edbInfo := calculateMap[v]
  133. if edbInfo == nil {
  134. return err
  135. }
  136. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  137. if err != nil {
  138. return
  139. }
  140. }
  141. return err
  142. }
  143. // refreshBaseEdbInfo 刷新基础指标
  144. func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
  145. source := bv.Source
  146. subSource := bv.SubSource
  147. edbInfoId := bv.EdbInfoId
  148. edbCode := bv.EdbCode
  149. //startDate := bv.StartDate
  150. frequency := bv.Frequency
  151. if bv.StartDate == "0000-00-00" {
  152. return
  153. }
  154. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  155. if err != nil {
  156. return
  157. }
  158. var limitDay int
  159. startDate := ""
  160. switch frequency {
  161. case "日度":
  162. limitDay = utils.DATA_START_REFRESH_LIMIT
  163. case "周度":
  164. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  165. case "月度":
  166. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  167. case "季度":
  168. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  169. case "年度":
  170. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  171. default:
  172. limitDay = utils.DATA_START_REFRESH_LIMIT
  173. }
  174. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  175. fmt.Println("source:", source)
  176. respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
  177. if err != nil {
  178. errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
  179. return
  180. }
  181. if respItem.Ret != 200 {
  182. errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  183. return
  184. }
  185. maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  186. if err != nil {
  187. return
  188. }
  189. if maxAndMinItem != nil {
  190. err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  191. if err != nil {
  192. return
  193. }
  194. }
  195. return
  196. }
  197. // refreshCalculateEdbInfo 刷新计算指标
  198. func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
  199. startDate := edbInfo.StartDate
  200. source := edbInfo.Source
  201. if source == utils.DATA_SOURCE_CALCULATE {
  202. startDate = edbInfo.StartDate
  203. sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
  204. if tmpErr != nil {
  205. err = tmpErr
  206. return
  207. }
  208. startDate = sTime.Format(utils.FormatDate)
  209. }
  210. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  211. if err != nil {
  212. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  213. errMsg = "RefreshEdbCalculateData Err:" + err.Error()
  214. return
  215. }
  216. if result.Ret != 200 {
  217. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  218. errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  219. err = errors.New("刷新失败")
  220. return
  221. }
  222. return
  223. }
  224. type ChartInfoReq struct {
  225. ChartInfoId int `description:"图表id,新增时传0"`
  226. }
  227. // DeleteChartInfoDataRedis 清除图表缓存
  228. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  229. var req ChartInfoReq
  230. err = json.Unmarshal(bodyByte, &req)
  231. if err != nil {
  232. return
  233. }
  234. if req.ChartInfoId > 0 {
  235. err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
  236. }
  237. return
  238. }
  239. // DeleteChartClassifyRedis 清除图表分类缓存
  240. func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
  241. err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
  242. return
  243. }
  244. // GetChartInfoDataKey 获取图表缓存的key
  245. func GetChartInfoDataKey(chartInfoId int) string {
  246. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  247. return key
  248. }
  249. // CheckOpChartPermission 判断图表操作权限
  250. func CheckOpChartPermission(sysUser *system.Admin, createUserId int, haveOperaAuth bool) (ok bool) {
  251. // 没有数据权限,那么直接返回
  252. if !haveOperaAuth {
  253. return
  254. }
  255. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  256. ok = true
  257. }
  258. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  259. if ok == false && createUserId == sysUser.AdminId {
  260. ok = true
  261. }
  262. // 如果图表权限id 是 1 ,那么允许编辑
  263. if ok == false && sysUser.ChartPermission == 1 {
  264. ok = true
  265. }
  266. return
  267. }
  268. // GetChartEdbData 获取图表的指标数据
  269. 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) {
  270. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  271. xEdbIdValue = make([]int, 0)
  272. yDataList = make([]data_manage.YData, 0)
  273. var extraConfig interface{}
  274. switch chartType {
  275. case 6:
  276. var tmpConfig data_manage.ChartTimeCombineExtraConf
  277. if extraConfigStr != `` {
  278. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  279. if err != nil {
  280. errMsg = "雷达图配置异常"
  281. err = errors.New(errMsg)
  282. return
  283. }
  284. }
  285. extraConfig = tmpConfig
  286. case 7: // 柱形图
  287. var barConfig data_manage.BarChartInfoReq
  288. if extraConfigStr == `` {
  289. errMsg = "柱方图未配置"
  290. err = errors.New(errMsg)
  291. return
  292. }
  293. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  294. if err != nil {
  295. errMsg = "柱方图配置异常"
  296. err = errors.New(errMsg)
  297. return
  298. }
  299. extraConfig = barConfig
  300. case 10: // 截面散点图
  301. var tmpExtraConfig data_manage.SectionScatterReq
  302. if extraConfigStr == `` {
  303. errMsg = "截面散点图未配置"
  304. err = errors.New(errMsg)
  305. return
  306. }
  307. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  308. if err != nil {
  309. errMsg = "截面散点配置异常"
  310. err = errors.New(errMsg)
  311. return
  312. }
  313. extraConfig = tmpExtraConfig
  314. case utils.CHART_TYPE_RADAR:
  315. var barConfig data_manage.RadarChartInfoReq
  316. if extraConfigStr == `` {
  317. errMsg = "雷达图未配置"
  318. err = errors.New(errMsg)
  319. return
  320. }
  321. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  322. if err != nil {
  323. errMsg = "雷达图配置异常"
  324. err = errors.New(errMsg)
  325. return
  326. }
  327. extraConfig = barConfig
  328. case utils.CHART_TYPE_SECTION_COMBINE:
  329. // 预览和详情都走这个接口
  330. var sectionExtraConfig data_manage.ChartSectionAllExtraConf
  331. if extraConfigStr == `` {
  332. errMsg = "截面组合图未配置"
  333. err = errors.New(errMsg)
  334. return
  335. }
  336. if chartInfoId > 0 {
  337. var sectionExtraConfigTmp data_manage.ChartSectionExtraConf
  338. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
  339. if err != nil {
  340. errMsg = "截面组合图配置异常"
  341. err = errors.New(errMsg)
  342. return
  343. }
  344. sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
  345. sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
  346. sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
  347. sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
  348. sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
  349. sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
  350. // 查询所有的seriesEdb
  351. seriesEdbList, e := data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
  352. if e != nil {
  353. errMsg = "查询seriesEdb失败"
  354. err = errors.New(errMsg + e.Error())
  355. return
  356. }
  357. // todo 是否有必要返回单位信息
  358. // 组装成map
  359. seriesEdbMap := make(map[int][]*data_manage.ChartSectionSeriesEdbConf)
  360. for _, v := range seriesEdbList {
  361. var dateConf *data_manage.ChartSectionSeriesDateConfItem
  362. if v.DateConf != "" {
  363. err = json.Unmarshal([]byte(v.DateConf), &dateConf)
  364. if err != nil {
  365. errMsg = "截面组合图配置异常"
  366. err = errors.New(errMsg + e.Error())
  367. return
  368. }
  369. }
  370. tmp := &data_manage.ChartSectionSeriesEdbConf{
  371. ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
  372. ChartSeriesId: v.ChartSeriesId,
  373. //ChartInfoId: v.ChartInfoId,
  374. EdbInfoId: v.EdbInfoId,
  375. DateConf: dateConf,
  376. EdbName: "",
  377. EdbNameEn: "",
  378. Unit: "",
  379. UnitEn: "",
  380. DateConfName: v.DateConfName,
  381. DateConfType: v.DateConfType,
  382. }
  383. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
  384. }
  385. //查询series
  386. seriesListTmp, e := data_manage.GetChartSeriesByChartInfoId(chartInfoId)
  387. if e != nil {
  388. errMsg = "查询series失败"
  389. err = errors.New(errMsg + e.Error())
  390. return
  391. }
  392. seriesList := make([]*data_manage.ChartSectionSeriesItem, 0)
  393. for _, v := range seriesListTmp {
  394. tmpSeries := &data_manage.ChartSectionSeriesItem{
  395. ChartSeriesId: v.ChartSeriesId,
  396. SeriesName: v.SeriesName,
  397. SeriesNameEn: v.SeriesNameEn,
  398. ChartStyle: v.ChartStyle,
  399. ChartColor: v.ChartColor,
  400. ChartWidth: v.ChartWidth,
  401. IsPoint: v.IsPoint,
  402. IsNumber: v.IsNumber,
  403. IsAxis: v.IsAxis,
  404. MaxData: v.MaxData,
  405. MinData: v.MinData,
  406. //IsOrder: false,
  407. EdbInfoList: nil,
  408. DataList: nil,
  409. }
  410. edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
  411. if ok {
  412. tmpSeries.EdbInfoList = edbInfoList
  413. }
  414. seriesList = append(seriesList, tmpSeries)
  415. }
  416. sectionExtraConfig.SeriesList = seriesList
  417. } else {
  418. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  419. if err != nil {
  420. errMsg = "截面组合图配置异常"
  421. err = errors.New(errMsg)
  422. return
  423. }
  424. }
  425. //校验引用日期名称是否重复
  426. dateNameMap := make(map[string]int)
  427. dateNameEnMap := make(map[string]int)
  428. for _, v := range sectionExtraConfig.DateConfList {
  429. if _, ok := dateNameMap[v.DateConfName]; ok {
  430. errMsg = "截面组合图引用日期名称设置重复"
  431. err = errors.New(errMsg + v.DateConfName)
  432. return
  433. }
  434. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  435. errMsg = "截面组合图引用日期名称设置重复"
  436. err = errors.New(errMsg + v.DateConfNameEn)
  437. return
  438. }
  439. if v.DateConfName != "" {
  440. dateNameMap[v.DateConfName] = 1
  441. }
  442. if v.DateConfNameEn != "" {
  443. dateNameEnMap[v.DateConfNameEn] = 1
  444. }
  445. }
  446. //检查系列名称是否重复
  447. seriesNameMap := make(map[string]int)
  448. seriesNameEnMap := make(map[string]int)
  449. for _, v := range sectionExtraConfig.SeriesList {
  450. if _, ok := seriesNameMap[v.SeriesName]; ok {
  451. errMsg = "截面组合图系列名称设置重复"
  452. err = errors.New(errMsg + v.SeriesName)
  453. return
  454. }
  455. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  456. errMsg = "截面组合图系列名称设置重复"
  457. err = errors.New(errMsg + v.SeriesNameEn)
  458. return
  459. }
  460. if v.SeriesName != "" {
  461. seriesNameMap[v.SeriesName] = 1
  462. }
  463. if v.SeriesNameEn != "" {
  464. seriesNameEnMap[v.SeriesNameEn] = 1
  465. }
  466. }
  467. // 检查基准系列是否设置
  468. if sectionExtraConfig.BaseChartSeriesName == "" {
  469. errMsg = "截面组合图基准系列名称未设置"
  470. err = errors.New(errMsg)
  471. return
  472. }
  473. //todo 如果是详情接口,应该要从其他表里查询配置
  474. extraConfig = sectionExtraConfig
  475. default:
  476. xEdbIdValue = make([]int, 0)
  477. yDataList = make([]data_manage.YData, 0)
  478. }
  479. // 指标对应的所有数据
  480. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  481. if err != nil {
  482. return
  483. }
  484. // 特殊图形数据处理
  485. switch chartType {
  486. case 6: //时序组合图
  487. //判断是否堆积
  488. timeConf := extraConfig.(data_manage.ChartTimeCombineExtraConf)
  489. if extraConfigStr == "" { //历史数据,默认开启堆积
  490. timeConf = data_manage.ChartTimeCombineExtraConf{
  491. IsHeap: 1,
  492. }
  493. }
  494. dataResp = data_manage.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
  495. case 2: // 季节性图
  496. if seasonExtraConfig != "" {
  497. // 季节性图计算不管图上数据时间,拿所有数据
  498. _, tempEdbList, e := getEdbDataMapListForSeason(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
  499. if e != nil {
  500. err = e
  501. return
  502. }
  503. dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
  504. } else {
  505. // 兼容无配置的老图
  506. dataResp = new(data_manage.SeasonChartResp)
  507. }
  508. case 7: // 柱形图
  509. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  510. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  511. for k := range yDataList {
  512. yDataList[k].Unit = barChartConf.Unit
  513. yDataList[k].UnitEn = barChartConf.UnitEn
  514. }
  515. for _, v := range edbList {
  516. // 指标别名
  517. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  518. for _, reqEdb := range barChartConf.EdbInfoIdList {
  519. if v.EdbInfoId == reqEdb.EdbInfoId {
  520. v.EdbAliasName = reqEdb.Name
  521. }
  522. }
  523. }
  524. }
  525. case 10: // 截面散点图
  526. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  527. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  528. var tmpExtraConfig data_manage.SectionScatterReq
  529. if extraConfigStr == `` {
  530. errMsg = "截面散点图未配置"
  531. err = errors.New(errMsg)
  532. return
  533. }
  534. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  535. if err != nil {
  536. errMsg = "截面散点配置异常"
  537. err = errors.New(errMsg)
  538. return
  539. }
  540. // 这个数据没有必要返回给前端
  541. for _, v := range edbList {
  542. v.DataList = nil
  543. }
  544. case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
  545. sectionConf := extraConfig.(data_manage.ChartSectionAllExtraConf)
  546. var chartInfo *data_manage.ChartInfo
  547. if chartInfoId > 0 {
  548. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  549. if err != nil {
  550. errMsg = "获取图表信息失败"
  551. return
  552. }
  553. }
  554. xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
  555. // 这个数据没有必要返回给前端
  556. for _, v := range edbList {
  557. v.DataList = nil
  558. }
  559. case utils.CHART_TYPE_RADAR: //雷达图
  560. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  561. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  562. }
  563. return
  564. }
  565. // GetEdbDataMapList 获取指标最后的基础数据
  566. 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) {
  567. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtra)
  568. return
  569. }
  570. // getEdbDataMapList 获取指标最后的基础数据
  571. 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) {
  572. // 指标对应的所有数据
  573. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  574. for _, v := range mappingList {
  575. //fmt.Println("v:", v.EdbInfoId)
  576. item := new(data_manage.ChartEdbInfoMapping)
  577. item.EdbInfoId = v.EdbInfoId
  578. item.SourceName = v.SourceName
  579. item.Source = v.Source
  580. item.EdbCode = v.EdbCode
  581. item.EdbName = v.EdbName
  582. item.EdbNameEn = v.EdbNameEn
  583. item.Frequency = v.Frequency
  584. item.EdbType = v.EdbType
  585. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  586. if v.Unit != `无` {
  587. item.Unit = v.Unit
  588. }
  589. item.UnitEn = v.UnitEn
  590. item.StartDate = v.StartDate
  591. item.EndDate = v.EndDate
  592. item.ModifyTime = v.ModifyTime
  593. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  594. item.PredictChartColor = v.PredictChartColor
  595. item.ClassifyId = v.ClassifyId
  596. if chartInfoId <= 0 {
  597. item.IsAxis = 1
  598. item.LeadValue = 0
  599. item.LeadUnit = ""
  600. item.ChartEdbMappingId = 0
  601. item.ChartInfoId = 0
  602. item.IsOrder = false
  603. item.EdbInfoType = 1
  604. item.ChartStyle = ""
  605. item.ChartColor = ""
  606. item.ChartWidth = 0
  607. item.MaxData = v.MaxValue
  608. item.MinData = v.MinValue
  609. } else {
  610. item.IsAxis = v.IsAxis
  611. item.EdbInfoType = v.EdbInfoType
  612. item.LeadValue = v.LeadValue
  613. item.LeadUnit = v.LeadUnit
  614. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  615. item.ChartEdbMappingId = v.ChartEdbMappingId
  616. item.ChartInfoId = v.ChartInfoId
  617. item.ChartStyle = v.ChartStyle
  618. item.ChartColor = v.ChartColor
  619. item.ChartWidth = v.ChartWidth
  620. item.IsOrder = v.IsOrder
  621. item.MaxData = v.MaxData
  622. item.MinData = v.MinData
  623. }
  624. item.LatestValue = v.LatestValue
  625. item.LatestDate = v.LatestDate
  626. item.UniqueCode = v.UniqueCode
  627. item.MoveLatestDate = v.LatestDate
  628. item.EdbAliasName = v.EdbAliasName
  629. item.IsConvert = v.IsConvert
  630. item.ConvertType = v.ConvertType
  631. item.ConvertValue = v.ConvertValue
  632. item.ConvertUnit = v.ConvertUnit
  633. item.ConvertEnUnit = v.ConvertEnUnit
  634. item.IsJoinPermission = v.IsJoinPermission
  635. var startDateReal string
  636. var diffSeconds int64
  637. if chartType == 2 { //季节性图
  638. startDateReal = startDate
  639. if len(mappingList) > 1 {
  640. item.IsAxis = v.IsAxis
  641. }
  642. } else {
  643. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  644. var startTimeRealTemp time.Time
  645. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  646. switch v.LeadUnit {
  647. case "天":
  648. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  649. case "月":
  650. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  651. case "季":
  652. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  653. case "周":
  654. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  655. case "年":
  656. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  657. }
  658. if startTimeRealTemp.Before(startDateParse) {
  659. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  660. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  661. } else {
  662. startDateReal = startDate
  663. diffSeconds = 0
  664. }
  665. // 预测指标的开始日期也要偏移
  666. {
  667. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  668. if tmpErr != nil {
  669. err = tmpErr
  670. return
  671. }
  672. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  673. if tmpErr != nil {
  674. err = tmpErr
  675. return
  676. }
  677. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  678. }
  679. } else {
  680. startDateReal = startDate
  681. }
  682. }
  683. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  684. calendarPreYear := 0
  685. if calendar == "农历" {
  686. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  687. if err != nil {
  688. fmt.Println("time.Parse:" + err.Error())
  689. }
  690. calendarPreYear = newStartDateReal.Year() - 1
  691. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  692. startDateReal = newStartDateReal.Format(utils.FormatDate)
  693. }
  694. dataList := make([]*data_manage.EdbDataList, 0)
  695. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  696. //fmt.Println("calendarPreYear:", calendarPreYear)
  697. //var newEdbInfo *data_manage.EdbInfo
  698. switch v.EdbInfoCategoryType {
  699. case 0:
  700. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  701. case 1:
  702. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  703. default:
  704. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  705. }
  706. if err != nil {
  707. return
  708. }
  709. if v.IsConvert == 1 {
  710. switch v.ConvertType {
  711. case 1:
  712. for i, data := range dataList {
  713. dataList[i].Value = data.Value * v.ConvertValue
  714. }
  715. //item.MaxData = item.MaxData * v.ConvertValue
  716. //item.MinData = item.MinData * v.ConvertValue
  717. case 2:
  718. for i, data := range dataList {
  719. dataList[i].Value = data.Value / v.ConvertValue
  720. }
  721. //item.MaxData = item.MaxData / v.ConvertValue
  722. //item.MinData = item.MinData / v.ConvertValue
  723. case 3:
  724. for i, data := range dataList {
  725. if data.Value <= 0 {
  726. err = errors.New("数据中含有负数或0,无法对数运算")
  727. return
  728. }
  729. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  730. }
  731. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  732. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  733. }
  734. }
  735. edbDataListMap[v.EdbInfoId] = dataList
  736. if diffSeconds != 0 && v.EdbInfoType == 0 {
  737. dataListLen := len(dataList)
  738. for i := 0; i < dataListLen; i++ {
  739. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  740. }
  741. }
  742. if chartType == 2 && item.IsAxis == 1 {
  743. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  744. if tmpErr != nil {
  745. //item.DataList = dataList
  746. item.IsNullData = true
  747. edbList = append(edbList, item)
  748. continue
  749. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  750. return
  751. }
  752. if calendar == "农历" {
  753. if len(dataList) <= 0 {
  754. result := new(data_manage.EdbDataResult)
  755. item.DataList = result
  756. } else {
  757. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  758. if tmpErr != nil {
  759. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  760. return
  761. }
  762. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  763. if tErr != nil {
  764. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  765. return
  766. }
  767. item.DataList = quarterDataList
  768. }
  769. } else {
  770. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  771. if tErr != nil {
  772. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  773. return
  774. }
  775. item.DataList = quarterDataList
  776. }
  777. } else if chartType == 2 && item.IsAxis == 0 {
  778. // 右轴数据处理
  779. xStartDate := "01-01"
  780. jumpYear := 0
  781. var seasonExtra data_manage.SeasonExtraItem
  782. if seasonExtraConfig != "" {
  783. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  784. if err != nil {
  785. return
  786. }
  787. }
  788. if seasonExtra.XStartDate != "" {
  789. xStartDate = seasonExtra.XStartDate
  790. jumpYear = seasonExtra.JumpYear
  791. }
  792. length := len(dataList)
  793. if length == 0 {
  794. return
  795. }
  796. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  797. if tmpErr != nil {
  798. //item.DataList = dataList
  799. item.IsNullData = true
  800. edbList = append(edbList, item)
  801. continue
  802. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  803. return
  804. }
  805. var rightAxisDate time.Time
  806. if jumpYear == 1 {
  807. latestDate = latestDate.AddDate(-1, 0, 0)
  808. }
  809. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
  810. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  811. if err != nil {
  812. return
  813. }
  814. newDataList := make([]*data_manage.EdbDataList, 0)
  815. for _, v := range dataList {
  816. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  817. if e != nil {
  818. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  819. return
  820. }
  821. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  822. newDataList = append(newDataList, v)
  823. }
  824. }
  825. item.DataList = newDataList
  826. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  827. //item.DataList = dataList
  828. } else {
  829. item.DataList = dataList
  830. }
  831. edbList = append(edbList, item)
  832. }
  833. return
  834. }
  835. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  836. func GetSeasonEdbInfoDataListByXDate(dataList []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
  837. xStartDate := "01-01"
  838. xEndDate := "12-31"
  839. jumpYear := 0
  840. legends := make([]data_manage.SeasonChartLegend, 0)
  841. var seasonExtra data_manage.SeasonExtraItem
  842. if seasonExtraConfig != "" {
  843. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  844. if err != nil {
  845. return
  846. }
  847. }
  848. if seasonExtra.XStartDate != "" {
  849. xStartDate = seasonExtra.XStartDate
  850. xEndDate = seasonExtra.XEndDate
  851. jumpYear = seasonExtra.JumpYear
  852. legends = seasonExtra.ChartLegend
  853. }
  854. length := len(dataList)
  855. if length == 0 {
  856. return
  857. }
  858. legendMap := make(map[string]string, 0)
  859. if len(legends) > 0 {
  860. for _, v := range legends {
  861. legendMap[v.Name] = v.Value
  862. }
  863. }
  864. latestDateStr := latestDate.Format(utils.FormatDate)
  865. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  866. //根据数据确定最早的年份,和最近年份
  867. //根据横轴的日期,汇总所有的年份
  868. startDate := dataList[0].DataTime
  869. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  870. if tmpErr != nil {
  871. err = tmpErr
  872. return
  873. }
  874. startYear := startDateT.Year()
  875. //获取数据的最新日期
  876. lastDate := dataList[length-1].DataTime
  877. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  878. if tmpErr != nil {
  879. err = tmpErr
  880. return
  881. }
  882. endYear := lastDateT.Year()
  883. nowYear := time.Now().Year()
  884. chartLegendMaxYear := 0
  885. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  886. quarterDataList := make([]*data_manage.QuarterData, 0)
  887. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  888. //整理出日期
  889. idx := 1
  890. chartLegendMap := make(map[string]int, 0)
  891. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  892. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  893. currentEndYear := currentStartYear
  894. if jumpYear == 1 {
  895. currentEndYear = currentStartYear + 1
  896. }
  897. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  898. name := fmt.Sprintf("%s_%s", startStr, endStr)
  899. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  900. startT, tEr := time.Parse(utils.FormatDate, startStr)
  901. if tEr != nil {
  902. err = tEr
  903. return
  904. }
  905. endT, tEr := time.Parse(utils.FormatDate, endStr)
  906. if tEr != nil {
  907. err = tEr
  908. return
  909. }
  910. if lastDateT.Before(startT) {
  911. //如果最新的日期在起始日之前,则跳出循环
  912. break
  913. }
  914. if endT.Year() > nowYear {
  915. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  916. nowYear = endT.Year()
  917. }
  918. item := data_manage.QuarterXDateItem{
  919. StartDate: startT,
  920. EndDate: endT,
  921. ShowName: showName,
  922. }
  923. chartLegendMaxYear = endT.Year()
  924. dataMap[name] = item
  925. chartLegendMap[name] = idx
  926. idx++
  927. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  928. if lastDateT.Before(endT) {
  929. //如果最新的日期在起始日之前,则跳出循环
  930. break
  931. }
  932. }
  933. lenYear := len(dataMap)
  934. if chartLegendMaxYear > endYear {
  935. chartLegendMaxYear = endYear
  936. }
  937. for k, v := range dataMap {
  938. if i, ok := chartLegendMap[k]; ok {
  939. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  940. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  941. }
  942. dataMap[k] = v
  943. }
  944. for _, v := range dataList {
  945. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  946. year := dataTimeT.Year()
  947. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  948. for k, dateItem := range dataMap {
  949. tmpVal := data_manage.EdbDataList{
  950. EdbDataId: v.EdbDataId,
  951. EdbInfoId: v.EdbInfoId,
  952. DataTime: v.DataTime,
  953. DataTimestamp: v.DataTimestamp,
  954. Value: v.Value,
  955. }
  956. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  957. if jumpYear == 1 {
  958. //计算前一年最大的日期, 只补齐数据到去年
  959. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  960. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  961. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  962. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  963. } else {
  964. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  965. }
  966. } else {
  967. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  968. }
  969. timestamp := newItemDate.UnixNano() / 1e6
  970. tmpVal.DataTimestamp = timestamp
  971. tmpV := &tmpVal
  972. if findVal, ok := quarterMap[k]; !ok {
  973. findVal = append(findVal, tmpV)
  974. quarterMap[k] = findVal
  975. } else {
  976. findVal = append(findVal, tmpV)
  977. quarterMap[k] = findVal
  978. }
  979. if v.DataTime == latestDateStr {
  980. dateItem.CuttingDataTimestamp = timestamp
  981. dataMap[k] = dateItem
  982. }
  983. //break
  984. }
  985. }
  986. }
  987. for k, v := range dataMap {
  988. itemList := quarterMap[k]
  989. /*if len(itemList) == 0 {
  990. continue
  991. }*/
  992. quarterItem := new(data_manage.QuarterData)
  993. quarterItem.Years = v.ShowName
  994. quarterItem.ChartLegend = v.ChartLegend
  995. if le, ok := legendMap[v.ShowName]; ok {
  996. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  997. quarterItem.ChartLegend = le
  998. }
  999. }
  1000. quarterItem.DataList = itemList
  1001. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  1002. //如果等于最后的实际日期,那么将切割时间戳记录
  1003. if quarterItem.CuttingDataTimestamp == 0 {
  1004. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1005. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  1006. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1007. }
  1008. }
  1009. quarterDataList = append(quarterDataList, quarterItem)
  1010. }
  1011. if len(quarterDataList) > 0 {
  1012. quarterDataListSort = quarterDataList
  1013. sort.Sort(quarterDataListSort)
  1014. }
  1015. return
  1016. }
  1017. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  1018. func GetSeasonEdbInfoDataListByXDateNong(result *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
  1019. xStartDate := "01-01"
  1020. xEndDate := "12-31"
  1021. jumpYear := 0
  1022. legends := make([]data_manage.SeasonChartLegend, 0)
  1023. var seasonExtra data_manage.SeasonExtraItem
  1024. if seasonExtraConfig != "" {
  1025. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  1026. if err != nil {
  1027. return
  1028. }
  1029. }
  1030. if seasonExtra.XStartDate != "" {
  1031. xStartDate = seasonExtra.XStartDate
  1032. xEndDate = seasonExtra.XEndDate
  1033. jumpYear = seasonExtra.JumpYear
  1034. legends = seasonExtra.ChartLegend
  1035. }
  1036. length := len(result.List)
  1037. if length == 0 {
  1038. return
  1039. }
  1040. legendMap := make(map[string]string, 0)
  1041. if len(legends) > 0 {
  1042. for _, v := range legends {
  1043. legendMap[v.Name] = v.Value
  1044. }
  1045. }
  1046. latestDateYear := latestDate.Year()
  1047. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  1048. //根据数据确定最早的年份,和最近年份
  1049. //根据横轴的日期,汇总所有的年份
  1050. startYear := result.List[0].Year
  1051. /*if jumpYear == 1 {
  1052. if startYear != calendarPreYear {
  1053. startYear = startYear - 1
  1054. }
  1055. }*/
  1056. itemLength := len(result.List[length-1].Items)
  1057. //获取数据的最新日期
  1058. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  1059. maxY := result.List[length-1].Year
  1060. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  1061. if tmpErr != nil {
  1062. err = tmpErr
  1063. return
  1064. }
  1065. endYear := lastDateT.Year()
  1066. nowYear := time.Now().Year()
  1067. chartLegendMaxYear := 0
  1068. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  1069. quarterDataList := make([]*data_manage.QuarterData, 0)
  1070. resultData := make([]*data_manage.QuarterData, 0)
  1071. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  1072. //整理出日期
  1073. var startTmpT, endTmpT time.Time
  1074. idx := 1
  1075. chartLegendMap := make(map[string]int, 0)
  1076. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  1077. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  1078. currentEndYear := currentStartYear
  1079. if jumpYear == 1 {
  1080. currentEndYear = currentStartYear + 1
  1081. }
  1082. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  1083. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  1084. startT, tEr := time.Parse(utils.FormatDate, startStr)
  1085. if tEr != nil {
  1086. err = tEr
  1087. return
  1088. }
  1089. endT, tEr := time.Parse(utils.FormatDate, endStr)
  1090. if tEr != nil {
  1091. err = tEr
  1092. return
  1093. }
  1094. if lastDateT.Before(startT) {
  1095. //如果最新的日期在起始日之前,则跳出循环
  1096. break
  1097. }
  1098. if endT.Year() > nowYear {
  1099. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  1100. nowYear = endT.Year()
  1101. }
  1102. item := data_manage.QuarterXDateItem{
  1103. StartDate: startT,
  1104. EndDate: endT,
  1105. ShowName: showName,
  1106. }
  1107. chartLegendMaxYear = endT.Year()
  1108. dataMap[showName] = item
  1109. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  1110. startTmpT = startT
  1111. endTmpT = endT
  1112. chartLegendMap[showName] = idx
  1113. idx++
  1114. if lastDateT.Before(endT) {
  1115. //如果最新的日期在起始日之前,则跳出循环
  1116. break
  1117. }
  1118. }
  1119. lenYear := len(dataMap)
  1120. if chartLegendMaxYear > endYear {
  1121. chartLegendMaxYear = endYear
  1122. }
  1123. for k, v := range dataMap {
  1124. if i, ok := chartLegendMap[k]; ok {
  1125. //v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  1126. v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
  1127. }
  1128. dataMap[k] = v
  1129. }
  1130. yearDataListMap := make(map[int]*data_manage.EdbDataItems, 0)
  1131. for _, lv := range result.List {
  1132. yearDataListMap[lv.Year] = lv
  1133. }
  1134. //判断哪些点应该落在同一条时间线上
  1135. /*maxY := lastDateT.Year()
  1136. changeFlag := false
  1137. if lastDateT.Month() >= 11 {
  1138. maxY = maxY + 1
  1139. }
  1140. if maxY < nowYear {
  1141. changeFlag = true
  1142. maxY = nowYear
  1143. }*/
  1144. /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
  1145. endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
  1146. minY := maxY
  1147. if jumpYear == 1 {
  1148. minY = maxY - 1
  1149. }
  1150. startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
  1151. startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
  1152. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  1153. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  1154. fmt.Printf("maxY 为%d \n", maxY)
  1155. for name, dateItem := range dataMap {
  1156. tY := dateItem.EndDate.Year()
  1157. if lastDateT.Month() >= 11 {
  1158. if maxY > endTmpT.Year() {
  1159. tY = tY + 1
  1160. }
  1161. }
  1162. lv, ok1 := yearDataListMap[tY]
  1163. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  1164. if !ok1 {
  1165. continue
  1166. }
  1167. for _, item := range lv.Items {
  1168. tmpVal := data_manage.EdbDataList{
  1169. EdbDataId: item.EdbDataId,
  1170. EdbInfoId: item.EdbInfoId,
  1171. DataTime: item.DataTime,
  1172. DataTimestamp: item.DataTimestamp,
  1173. Value: item.Value,
  1174. }
  1175. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  1176. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  1177. tmpV := &tmpVal
  1178. if findVal, ok := quarterMap[name]; !ok {
  1179. findVal = append(findVal, tmpV)
  1180. quarterMap[name] = findVal
  1181. } else {
  1182. findVal = append(findVal, tmpV)
  1183. quarterMap[name] = findVal
  1184. }
  1185. if lv.Year >= latestDateYear {
  1186. // 切割的日期时间字符串
  1187. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  1188. if item.DataTime == cuttingDataTimeStr {
  1189. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  1190. dataMap[name] = dateItem
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. for k, v := range dataMap {
  1197. itemList := quarterMap[k]
  1198. /*if len(itemList) == 0 {
  1199. continue
  1200. }*/
  1201. quarterItem := new(data_manage.QuarterData)
  1202. quarterItem.Years = v.ShowName
  1203. quarterItem.ChartLegend = v.ChartLegend
  1204. if le, ok := legendMap[v.ShowName]; ok {
  1205. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  1206. quarterItem.ChartLegend = le
  1207. }
  1208. }
  1209. quarterItem.DataList = itemList
  1210. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  1211. //如果等于最后的实际日期,那么将切割时间戳记录
  1212. if quarterItem.CuttingDataTimestamp == 0 {
  1213. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  1214. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  1215. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  1216. }
  1217. }
  1218. quarterDataList = append(quarterDataList, quarterItem)
  1219. }
  1220. if result.List[0].Year != calendarPreYear {
  1221. itemList := make([]*data_manage.EdbDataList, 0)
  1222. items := new(data_manage.QuarterData)
  1223. //items.Year = calendarPreYear
  1224. items.DataList = itemList
  1225. newResult := make([]*data_manage.QuarterData, 0)
  1226. newResult = append(newResult, items)
  1227. newResult = append(newResult, quarterDataList...)
  1228. resultData = newResult
  1229. } else {
  1230. resultData = quarterDataList
  1231. }
  1232. if len(quarterDataList) > 0 {
  1233. quarterDataListSort = resultData
  1234. sort.Sort(quarterDataListSort)
  1235. }
  1236. return
  1237. }
  1238. // BarChartData 柱方图的数据处理
  1239. 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) {
  1240. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1241. edbDataMap := make(map[int]map[string]float64)
  1242. for edbInfoId, edbDataList := range edbDataListMap {
  1243. edbDateData := make(map[string]float64)
  1244. for _, edbData := range edbDataList {
  1245. edbDateData[edbData.DataTime] = edbData.Value
  1246. }
  1247. edbDataMap[edbInfoId] = edbDateData
  1248. }
  1249. // edbIdList 指标展示顺序;x轴的指标顺序
  1250. edbIdList = make([]int, 0)
  1251. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1252. dateData := make(map[int]float64)
  1253. if barChartInfoSort.Sort == 0 {
  1254. for _, v := range mappingList {
  1255. edbIdList = append(edbIdList, v.EdbInfoId)
  1256. }
  1257. } else {
  1258. lenBarChartInfoDateList := len(barChartInfoDateList)
  1259. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  1260. err = errors.New("排序日期异常")
  1261. return
  1262. }
  1263. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  1264. // 日期配置
  1265. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  1266. for edbInfoId, dataList := range edbDataListMap {
  1267. if len(dataList) <= 0 {
  1268. // 没有数据的指标id
  1269. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1270. continue
  1271. }
  1272. findDate := barChartInfoDate.Date
  1273. switch barChartInfoDate.Type {
  1274. case 1: //最新值
  1275. findDate = dataList[len(dataList)-1].DataTime
  1276. case 2: //近期几天
  1277. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1278. if tmpErr != nil {
  1279. err = tmpErr
  1280. return
  1281. }
  1282. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1283. lenData := len(dataList) - 1
  1284. for i := lenData; i >= 0; i-- {
  1285. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1286. if tmpErr != nil {
  1287. err = tmpErr
  1288. return
  1289. }
  1290. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1291. findDate = dataList[i].DataTime
  1292. break
  1293. }
  1294. }
  1295. case 3: // 固定日期
  1296. //最早的日期
  1297. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1298. if tmpErr != nil {
  1299. err = tmpErr
  1300. return
  1301. }
  1302. //寻找固定日期的数据
  1303. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1304. if tmpErr != nil {
  1305. err = tmpErr
  1306. return
  1307. }
  1308. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1309. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1310. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1311. findDate = tmpDate
  1312. break
  1313. }
  1314. }
  1315. default:
  1316. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1317. return
  1318. }
  1319. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1320. dateData[edbInfoId] = tmpValue
  1321. } else {
  1322. // 没有数据的指标id
  1323. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1324. }
  1325. }
  1326. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1327. // 排序
  1328. dateDataSort := utils.NewMapSorter(dateData)
  1329. sort.Sort(dateDataSort)
  1330. if barChartInfoSort.Sort == 1 {
  1331. // 先将没有数据的指标id放在最前面
  1332. if len(notDataEdbIdList) > 0 {
  1333. edbIdList = append(edbIdList, notDataEdbIdList...)
  1334. }
  1335. for _, v := range dateDataSort {
  1336. edbIdList = append(edbIdList, v.Key)
  1337. }
  1338. } else {
  1339. for i := len(dateDataSort) - 1; i >= 0; i-- {
  1340. edbIdList = append(edbIdList, dateDataSort[i].Key)
  1341. }
  1342. // 再将没有数据的指标id放在最后面
  1343. if len(notDataEdbIdList) > 0 {
  1344. edbIdList = append(edbIdList, notDataEdbIdList...)
  1345. }
  1346. }
  1347. }
  1348. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  1349. for _, barChartInfoDate := range barChartInfoDateList {
  1350. var maxDate time.Time
  1351. findDataList := make([]float64, 0) // 当前日期的数据值
  1352. for _, edbInfoId := range edbIdList {
  1353. findDate := barChartInfoDate.Date //需要的日期值
  1354. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1355. if len(dataList) <= 0 {
  1356. // 没有数据的指标id
  1357. findDataList = append(findDataList, 0)
  1358. continue
  1359. }
  1360. switch barChartInfoDate.Type {
  1361. case 1: //最新值
  1362. dataList := edbDataListMap[edbInfoId]
  1363. findDate = dataList[len(dataList)-1].DataTime
  1364. case 2: //近期几天
  1365. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1366. if tmpErr != nil {
  1367. err = tmpErr
  1368. return
  1369. }
  1370. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1371. lenData := len(dataList) - 1
  1372. for i := lenData; i >= 0; i-- {
  1373. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1374. if tmpErr != nil {
  1375. err = tmpErr
  1376. return
  1377. }
  1378. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1379. findDate = dataList[i].DataTime
  1380. break
  1381. }
  1382. }
  1383. case 3: // 固定日期
  1384. //最早的日期
  1385. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1386. if tmpErr != nil {
  1387. err = tmpErr
  1388. return
  1389. }
  1390. //寻找固定日期的数据
  1391. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1392. if tmpErr != nil {
  1393. err = tmpErr
  1394. return
  1395. }
  1396. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1397. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1398. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1399. findDate = tmpDate
  1400. break
  1401. }
  1402. }
  1403. default:
  1404. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1405. return
  1406. }
  1407. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1408. if maxDate.IsZero() {
  1409. maxDate = findDateTime
  1410. } else {
  1411. if findDateTime.After(maxDate) {
  1412. maxDate = findDateTime
  1413. }
  1414. }
  1415. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1416. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1417. findDataList = append(findDataList, tmpValue)
  1418. } else {
  1419. findDataList = append(findDataList, 0)
  1420. }
  1421. }
  1422. yDate := "0000-00-00"
  1423. if !maxDate.IsZero() {
  1424. yDate = maxDate.Format(utils.FormatDate)
  1425. }
  1426. yDataList = append(yDataList, data_manage.YData{
  1427. Date: yDate,
  1428. Value: findDataList,
  1429. Color: barChartInfoDate.Color,
  1430. Name: barChartInfoDate.Name,
  1431. })
  1432. }
  1433. return
  1434. }
  1435. // ChartInfoRefreshV2 图表刷新
  1436. // @author Roc
  1437. // @datetime 2022-09-16 10:15:38
  1438. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1439. func ChartInfoRefreshV2(chartInfoId int) (err error, isAsync bool) {
  1440. var errmsg string
  1441. defer func() {
  1442. if err != nil {
  1443. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1444. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1445. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1446. }
  1447. }()
  1448. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1449. if err != nil {
  1450. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1451. return
  1452. }
  1453. edbIdList := make([]int, 0)
  1454. for _, v := range edbMappingList {
  1455. edbIdList = append(edbIdList, v.EdbInfoId)
  1456. }
  1457. // 批量刷新
  1458. err, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false, false)
  1459. if err != nil {
  1460. return
  1461. }
  1462. return
  1463. }
  1464. // BatchChartInfoRefreshV2 图表批量刷新
  1465. // @author Roc
  1466. // @datetime 2022-09-16 10:15:38
  1467. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1468. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo, redisKey string) (err error, isAsync bool) {
  1469. if len(chartInfoList) <= 0 {
  1470. return
  1471. }
  1472. chartInfoIdSlice := make([]int, 0)
  1473. for _, chartInfo := range chartInfoList {
  1474. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1475. }
  1476. var errMsg string
  1477. defer func() {
  1478. if err != nil {
  1479. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
  1480. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1481. fmt.Println("ChartInfoRefresh Err:" + errMsg)
  1482. }
  1483. }()
  1484. // 是否删除缓存
  1485. isDeleteCache := true
  1486. if redisKey != `` {
  1487. //设置最多10分钟缓存
  1488. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  1489. defer func() {
  1490. if isDeleteCache {
  1491. utils.Rc.Delete(redisKey)
  1492. }
  1493. }()
  1494. }
  1495. // 获取需要刷新的指标信息
  1496. edbIdList := make([]int, 0)
  1497. edbInfoIdMap := make(map[int]int)
  1498. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1499. for _, v := range edbMappingList {
  1500. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1501. edbIdList = append(edbIdList, v.EdbInfoId)
  1502. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1503. }
  1504. }
  1505. // 获取需要刷新的指标列表
  1506. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  1507. // 需要刷新的指标数量
  1508. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  1509. if totalEdbInfo > 20 { // 关联指标过多的时候,异步刷新
  1510. isAsync = true
  1511. isDeleteCache = false // 不删除缓存
  1512. //go refreshChartEdbInfo(chartInfoList, redisKey, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1513. // 加入队列刷新
  1514. var refreshItem EdbQueueRefreshReq
  1515. refreshItem.RefreshKey = redisKey
  1516. for _, v := range chartInfoList {
  1517. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1518. refreshItem.ItemRefreshKeys = append(refreshItem.ItemRefreshKeys, key)
  1519. }
  1520. refreshItem.BaseEdbInfoArr = newBaseEdbInfoArr
  1521. refreshItem.BasePredictEdbInfoArr = newBasePredictEdbInfoArr
  1522. refreshItem.CalculateMap = newCalculateMap
  1523. refreshItem.PredictCalculateMap = newPredictCalculateMap
  1524. refreshItem.CalculateArr = calculateArr
  1525. refreshItem.PredictCalculateArr = predictCalculateArr
  1526. go PushEdb2Refresh(refreshItem)
  1527. } else {
  1528. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1529. //清除图表缓存
  1530. for _, v := range chartInfoList {
  1531. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1532. _ = utils.Rc.Delete(key)
  1533. }
  1534. }
  1535. return
  1536. }
  1537. // refreshChartEdbInfo
  1538. // @Description: 刷新图表中的质保列表
  1539. // @author: Roc
  1540. // @datetime 2023-12-01 16:05:39
  1541. // @param chartInfoList []*data_manage.ChartInfo
  1542. // @param redisKey string
  1543. // @param newBaseEdbInfoArr []*data_manage.EdbInfo
  1544. // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
  1545. // @param newCalculateMap map[int]*data_manage.EdbInfo
  1546. // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
  1547. // @param calculateArr []int
  1548. // @param predictCalculateArr []int
  1549. func refreshChartEdbInfo(chartInfoList []*data_manage.ChartInfo, redisKey string, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  1550. edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1551. //清除图表缓存
  1552. for _, v := range chartInfoList {
  1553. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1554. _ = utils.Rc.Delete(key)
  1555. }
  1556. // 如果是有插入redisKey,那么刷新结束后就清除缓存
  1557. if redisKey != `` {
  1558. utils.Rc.Delete(redisKey)
  1559. }
  1560. }
  1561. // GetBatchChartRefreshKey
  1562. // @Description: 获取批量刷新key
  1563. // @author: Roc
  1564. // @datetime 2023-11-30 13:30:26
  1565. // @param source string
  1566. // @param primaryId int
  1567. // @param subId int
  1568. // @return string
  1569. func GetBatchChartRefreshKey(source string, primaryId, subId int) string {
  1570. if source == `` {
  1571. return ``
  1572. }
  1573. return fmt.Sprint("batch_chart_refresh:", source, ":", primaryId, ":", subId)
  1574. }
  1575. // CheckBatchChartRefreshResult
  1576. // @Description: 判断报告中的图表是否刷新完成
  1577. // @author: Roc
  1578. // @datetime 2023-11-30 13:30:26
  1579. // @param source string
  1580. // @param primaryId int
  1581. // @param subId int
  1582. // @return string
  1583. func CheckBatchChartRefreshResult(source string, primaryId, subId int) (refreshResult bool) {
  1584. refreshResult = true
  1585. redisKey := GetBatchChartRefreshKey(source, primaryId, subId)
  1586. if redisKey != `` {
  1587. // 如果找到了key,那么就是还在更新中
  1588. isOk := utils.Rc.IsExist(redisKey)
  1589. if isOk {
  1590. refreshResult = false
  1591. }
  1592. }
  1593. return
  1594. }
  1595. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  1596. // 相关性图表不判断指标
  1597. if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
  1598. return true
  1599. }
  1600. if chartNameEn != "" && len(edbList) <= 0 {
  1601. return true
  1602. }
  1603. if chartNameEn == "" {
  1604. return false
  1605. }
  1606. // 截面散点图的话,肯定是有英文配置的
  1607. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  1608. return true
  1609. }
  1610. for _, v := range edbList {
  1611. if v.EdbNameEn == "" {
  1612. return false
  1613. }
  1614. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  1615. return false
  1616. }
  1617. }
  1618. return true
  1619. }
  1620. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  1621. if edbNameEn == "" {
  1622. return false
  1623. }
  1624. if unit != "无" && unit != "" && unitEn == "" {
  1625. return false
  1626. }
  1627. return true
  1628. }
  1629. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  1630. // return int 0:启用,1:禁用
  1631. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  1632. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  1633. if err != nil {
  1634. return
  1635. }
  1636. //没有配置,立马返回
  1637. if crmConfig.ConfigValue == `` {
  1638. return
  1639. }
  1640. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  1641. for _, edbInfoId := range edbInfoIdArr {
  1642. for _, confEdbInfoId := range confEdbInfoIdList {
  1643. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  1644. isDisable = 1
  1645. return
  1646. }
  1647. }
  1648. }
  1649. return
  1650. }
  1651. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1652. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1653. switch chartType {
  1654. case 10: //截面散点
  1655. var extraConfig data_manage.SectionScatterReq
  1656. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1657. if err != nil {
  1658. return
  1659. }
  1660. // 判断是否有配置日期序列
  1661. if len(extraConfig.SeriesList) <= 0 {
  1662. errMsg = `请配置序列`
  1663. err = errors.New(errMsg)
  1664. return
  1665. }
  1666. // 判断是否有填写指标
  1667. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1668. errMsg = `请选择指标`
  1669. err = errors.New(errMsg)
  1670. return
  1671. }
  1672. // 遍历指标列表获取指标id
  1673. edbIdMap := make(map[int]int)
  1674. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1675. // X 轴的指标id
  1676. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1677. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1678. edbIdList = append(edbIdList, v.XEdbInfoId)
  1679. }
  1680. // Y 轴的指标id
  1681. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1682. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1683. edbIdList = append(edbIdList, v.YEdbInfoId)
  1684. }
  1685. }
  1686. case utils.CHART_TYPE_RADAR:
  1687. var extraConfig data_manage.RadarChartInfoReq
  1688. if extraConfigStr == `` {
  1689. errMsg = "雷达图未配置"
  1690. err = errors.New(errMsg)
  1691. return
  1692. }
  1693. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1694. if err != nil {
  1695. errMsg = "雷达图配置异常"
  1696. err = errors.New(errMsg)
  1697. return
  1698. }
  1699. case utils.CHART_TYPE_SECTION_COMBINE:
  1700. var extraConfig data_manage.ChartSectionAllExtraConf
  1701. if extraConfigStr == `` {
  1702. errMsg = "截面组合图未配置"
  1703. err = errors.New(errMsg)
  1704. return
  1705. }
  1706. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1707. if err != nil {
  1708. errMsg = "截面组合图配置异常"
  1709. err = errors.New(errMsg + err.Error())
  1710. return
  1711. }
  1712. //校验引用日期名称是否重复
  1713. dateNameMap := make(map[string]int)
  1714. dateNameEnMap := make(map[string]int)
  1715. for _, v := range extraConfig.DateConfList {
  1716. if _, ok := dateNameMap[v.DateConfName]; ok {
  1717. errMsg = "截面组合图引用日期名称设置重复"
  1718. err = errors.New(errMsg + v.DateConfName)
  1719. return
  1720. }
  1721. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  1722. errMsg = "截面组合图引用日期名称设置重复"
  1723. err = errors.New(errMsg + v.DateConfNameEn)
  1724. return
  1725. }
  1726. if v.DateConfName != "" {
  1727. dateNameMap[v.DateConfName] = 1
  1728. }
  1729. if v.DateConfNameEn != "" {
  1730. dateNameEnMap[v.DateConfNameEn] = 1
  1731. }
  1732. }
  1733. //检查系列名称是否重复
  1734. seriesNameMap := make(map[string]int)
  1735. seriesNameEnMap := make(map[string]int)
  1736. for _, v := range extraConfig.SeriesList {
  1737. if _, ok := seriesNameMap[v.SeriesName]; ok {
  1738. errMsg = "截面组合图系列名称设置重复"
  1739. err = errors.New(errMsg + v.SeriesName)
  1740. return
  1741. }
  1742. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  1743. errMsg = "截面组合图系列名称设置重复"
  1744. err = errors.New(errMsg + v.SeriesNameEn)
  1745. return
  1746. }
  1747. if v.SeriesName != "" {
  1748. seriesNameMap[v.SeriesName] = 1
  1749. }
  1750. if v.SeriesNameEn != "" {
  1751. seriesNameEnMap[v.SeriesNameEn] = 1
  1752. }
  1753. }
  1754. // 检查基准系列是否设置
  1755. if extraConfig.BaseChartSeriesName == "" {
  1756. errMsg = "截面组合图基准系列名称未设置"
  1757. err = errors.New(errMsg)
  1758. return
  1759. }
  1760. //todo 如果是详情接口,应该要从其他表里查询配置
  1761. // 遍历指标列表获取指标id
  1762. edbIdMap := make(map[int]int)
  1763. hasUsed := make(map[string]struct{})
  1764. for _, v := range extraConfig.SeriesList {
  1765. for _, item := range v.EdbInfoList {
  1766. if item.DateConfName != "" && item.DateConfType == 1 {
  1767. hasUsed[item.DateConfName] = struct{}{}
  1768. }
  1769. if _, ok := edbIdMap[item.EdbInfoId]; !ok {
  1770. edbIdMap[item.EdbInfoId] = item.EdbInfoId
  1771. edbIdList = append(edbIdList, item.EdbInfoId)
  1772. }
  1773. }
  1774. }
  1775. // todo 遍历引用日期获取引用日期里的指标ID, 如果引用日期里的指标没有被引用,是否加入映射
  1776. for _, v := range extraConfig.DateConfList {
  1777. if _, ok := hasUsed[v.DateConfName]; !ok {
  1778. continue
  1779. }
  1780. if _, ok := edbIdMap[v.EdbInfoId]; !ok && v.EdbInfoId > 0 {
  1781. edbIdMap[v.EdbInfoId] = v.EdbInfoId
  1782. edbIdList = append(edbIdList, v.EdbInfoId)
  1783. }
  1784. }
  1785. }
  1786. return
  1787. }
  1788. // GetSectionScatterChartData 截面散点图的数据处理
  1789. 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) {
  1790. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1791. edbDataMap := make(map[int]map[string]float64)
  1792. for edbInfoId, edbDataList := range edbDataListMap {
  1793. edbDateData := make(map[string]float64)
  1794. for _, edbData := range edbDataList {
  1795. edbDateData[edbData.DataTime] = edbData.Value
  1796. }
  1797. edbDataMap[edbInfoId] = edbDateData
  1798. }
  1799. // edbIdList 指标展示顺序;x轴的指标顺序
  1800. edbIdList = make([]int, 0)
  1801. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1802. for _, v := range mappingList {
  1803. edbIdList = append(edbIdList, v.EdbInfoId)
  1804. edbMappingMap[v.EdbInfoId] = v
  1805. }
  1806. //SectionScatterSeriesInfoResp
  1807. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1808. for _, seriesItem := range extraConfig.SeriesList {
  1809. var maxDate time.Time
  1810. // 系列中的指标数据
  1811. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1812. var minXVal, maxXVal, minYVal, maxYVal float64
  1813. for _, edbConf := range seriesItem.EdbInfoList {
  1814. tmpItem := data_manage.SectionScatterEdbItemResp{
  1815. IsShow: edbConf.IsShow,
  1816. Name: edbConf.Name,
  1817. NameEn: edbConf.NameEn,
  1818. } //单个坐标点的数据
  1819. //X轴的数据
  1820. {
  1821. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1822. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1823. if !ok {
  1824. continue
  1825. }
  1826. findDate := edbConf.XDate //需要的日期值
  1827. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1828. if len(dataList) <= 0 {
  1829. // 没有数据的指标id
  1830. //findDataList = append(findDataList, 0)
  1831. continue
  1832. }
  1833. tmpItem.XEdbInfoId = edbInfoId
  1834. tmpItem.XName = edbMappingInfo.EdbName
  1835. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1836. switch edbConf.XDateType {
  1837. case 1: //最新值
  1838. findDate = dataList[len(dataList)-1].DataTime
  1839. case 2: //近期几天
  1840. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1841. if tmpErr != nil {
  1842. err = tmpErr
  1843. return
  1844. }
  1845. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1846. lenData := len(dataList) - 1
  1847. for i := lenData; i >= 0; i-- {
  1848. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1849. if tmpErr != nil {
  1850. err = tmpErr
  1851. return
  1852. }
  1853. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1854. findDate = dataList[i].DataTime
  1855. break
  1856. }
  1857. }
  1858. case 3: // 固定日期
  1859. //最早的日期
  1860. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1861. if tmpErr != nil {
  1862. err = tmpErr
  1863. return
  1864. }
  1865. //寻找固定日期的数据
  1866. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1867. if tmpErr != nil {
  1868. err = tmpErr
  1869. return
  1870. }
  1871. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1872. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1873. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1874. findDate = tmpDate
  1875. break
  1876. }
  1877. }
  1878. default:
  1879. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1880. return
  1881. }
  1882. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1883. if maxDate.IsZero() {
  1884. maxDate = findDateTime
  1885. } else {
  1886. if findDateTime.After(maxDate) {
  1887. maxDate = findDateTime
  1888. }
  1889. }
  1890. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1891. tmpItem.XDate = findDate
  1892. tmpItem.XValue = tmpValue
  1893. } else {
  1894. continue
  1895. }
  1896. }
  1897. //Y轴的数据
  1898. {
  1899. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1900. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1901. if !ok {
  1902. continue
  1903. }
  1904. findDate := edbConf.YDate //需要的日期值
  1905. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1906. if len(dataList) <= 0 {
  1907. // 没有数据的指标id
  1908. //findDataList = append(findDataList, 0)
  1909. continue
  1910. }
  1911. tmpItem.YEdbInfoId = edbInfoId
  1912. tmpItem.YName = edbMappingInfo.EdbName
  1913. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1914. switch edbConf.YDateType {
  1915. case 1: //最新值
  1916. findDate = dataList[len(dataList)-1].DataTime
  1917. case 2: //近期几天
  1918. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1919. if tmpErr != nil {
  1920. err = tmpErr
  1921. return
  1922. }
  1923. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1924. lenData := len(dataList) - 1
  1925. for i := lenData; i >= 0; i-- {
  1926. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1927. if tmpErr != nil {
  1928. err = tmpErr
  1929. return
  1930. }
  1931. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1932. findDate = dataList[i].DataTime
  1933. break
  1934. }
  1935. }
  1936. case 3: // 固定日期
  1937. //最早的日期
  1938. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1939. if tmpErr != nil {
  1940. err = tmpErr
  1941. return
  1942. }
  1943. //寻找固定日期的数据
  1944. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1945. if tmpErr != nil {
  1946. err = tmpErr
  1947. return
  1948. }
  1949. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1950. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1951. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1952. findDate = tmpDate
  1953. break
  1954. }
  1955. }
  1956. default:
  1957. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1958. return
  1959. }
  1960. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1961. if maxDate.IsZero() {
  1962. maxDate = findDateTime
  1963. } else {
  1964. if findDateTime.After(maxDate) {
  1965. maxDate = findDateTime
  1966. }
  1967. }
  1968. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1969. tmpItem.YDate = findDate
  1970. tmpItem.YValue = tmpValue
  1971. } else {
  1972. continue
  1973. }
  1974. }
  1975. // 获取当前系列的X轴的最大最小值
  1976. {
  1977. if tmpItem.XValue < minXVal {
  1978. minXVal = tmpItem.XValue
  1979. }
  1980. if tmpItem.XValue > maxXVal {
  1981. maxXVal = tmpItem.XValue
  1982. }
  1983. if tmpItem.YValue < minYVal {
  1984. minYVal = tmpItem.YValue
  1985. }
  1986. if tmpItem.YValue > maxYVal {
  1987. maxYVal = tmpItem.YValue
  1988. }
  1989. }
  1990. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1991. }
  1992. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1993. var trendLine, rSquare string
  1994. // 生成线性方程式
  1995. var a, b float64
  1996. {
  1997. coordinateData := make([]utils.Coordinate, 0)
  1998. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1999. tmpCoordinate1 := utils.Coordinate{
  2000. X: tmpSeriesEdbInfo.XValue,
  2001. Y: tmpSeriesEdbInfo.YValue,
  2002. }
  2003. coordinateData = append(coordinateData, tmpCoordinate1)
  2004. }
  2005. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  2006. if len(coordinateData) >= 2 {
  2007. a, b = utils.GetLinearResult(coordinateData)
  2008. if !math.IsNaN(a) && !math.IsNaN(b) {
  2009. if b > 0 {
  2010. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  2011. } else {
  2012. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  2013. }
  2014. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  2015. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  2016. }
  2017. // 计算R平方
  2018. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  2019. }
  2020. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  2021. X: minXVal,
  2022. Y: minYVal,
  2023. }, data_manage.CoordinatePoint{
  2024. X: maxXVal,
  2025. Y: maxYVal,
  2026. })
  2027. }
  2028. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  2029. Name: seriesItem.Name,
  2030. NameEn: seriesItem.NameEn,
  2031. IsNameDefault: seriesItem.IsNameDefault,
  2032. Color: seriesItem.Color,
  2033. EdbInfoList: tmpSeriesEdbInfoList,
  2034. ShowTrendLine: seriesItem.ShowTrendLine,
  2035. ShowFitEquation: seriesItem.ShowFitEquation,
  2036. ShowRSquare: seriesItem.ShowRSquare,
  2037. TrendLine: trendLine,
  2038. RSquare: rSquare,
  2039. TrendLimitData: trendLimitData,
  2040. })
  2041. }
  2042. // 截面散点图点击详情时自动更新系列名
  2043. if len(extraConfig.SeriesList) > 0 {
  2044. // 默认名字的时候才自动更新
  2045. if extraConfig.SeriesList[0].IsNameDefault {
  2046. firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
  2047. needUpdate := false
  2048. if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
  2049. extraConfig.SeriesList[0].Name = v.LatestDate
  2050. extraConfig.SeriesList[0].NameEn = v.LatestDate
  2051. dataListResp[0].Name = v.LatestDate
  2052. dataListResp[0].NameEn = v.LatestDate
  2053. needUpdate = true
  2054. }
  2055. extraConfigByte, e := json.Marshal(extraConfig)
  2056. if e != nil {
  2057. errMsg := "截面散点系列更新异常"
  2058. err = errors.New(errMsg)
  2059. return
  2060. }
  2061. extraConfigStr := string(extraConfigByte)
  2062. if needUpdate {
  2063. err = data_manage.EditChartInfoExtraConfig(chartInfoId, extraConfigStr)
  2064. if err != nil {
  2065. errMsg := "截面散点系列更新异常"
  2066. err = errors.New(errMsg)
  2067. return
  2068. }
  2069. }
  2070. }
  2071. }
  2072. chartDataResp = data_manage.SectionScatterInfoResp{
  2073. XName: extraConfig.XName,
  2074. XNameEn: extraConfig.XNameEn,
  2075. XUnitName: extraConfig.XUnitName,
  2076. XUnitNameEn: extraConfig.XUnitNameEn,
  2077. YName: extraConfig.YName,
  2078. YNameEn: extraConfig.YNameEn,
  2079. YUnitName: extraConfig.YUnitName,
  2080. YUnitNameEn: extraConfig.YUnitNameEn,
  2081. XMinValue: extraConfig.XMinValue,
  2082. XMaxValue: extraConfig.XMaxValue,
  2083. YMinValue: extraConfig.YMinValue,
  2084. YMaxValue: extraConfig.YMaxValue,
  2085. DataList: dataListResp,
  2086. }
  2087. return
  2088. }
  2089. // AddChartInfo 添加图表
  2090. func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  2091. isSendEmail = true // 默认错误的时候要发送邮件
  2092. req.ChartName = strings.Trim(req.ChartName, " ")
  2093. if req.ChartName == "" {
  2094. errMsg = "请填写图表名称!"
  2095. err = errors.New(errMsg)
  2096. isSendEmail = false
  2097. return
  2098. }
  2099. if req.ChartClassifyId <= 0 {
  2100. errMsg = "分类参数错误!"
  2101. err = errors.New(errMsg)
  2102. isSendEmail = false
  2103. return
  2104. }
  2105. //if req.ChartThemeId <= 0 {
  2106. // errMsg = "请选择主题"
  2107. // err = errors.New(errMsg)
  2108. // isSendEmail = false
  2109. // return
  2110. //}
  2111. //
  2112. //// 判断主题是否存在
  2113. //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
  2114. //if err != nil {
  2115. // if err.Error() == utils.ErrNoRow() {
  2116. // errMsg = "主题不存在"
  2117. // err = errors.New(errMsg)
  2118. // return
  2119. // }
  2120. // errMsg = "获取主题信息失败"
  2121. // return
  2122. //}
  2123. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2124. if err != nil {
  2125. if err.Error() == utils.ErrNoRow() {
  2126. errMsg = "分类不存在"
  2127. err = errors.New(errMsg)
  2128. return
  2129. }
  2130. errMsg = "获取分类信息失败"
  2131. return
  2132. }
  2133. if chartClassify == nil {
  2134. errMsg = "分类不存在"
  2135. err = errors.New(errMsg)
  2136. return
  2137. }
  2138. chartType := req.ChartType
  2139. extraConfig := req.ExtraConfig
  2140. var extraConfigEdbInfoIdArr []int
  2141. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(chartType, extraConfig)
  2142. if err != nil {
  2143. err = errors.New("添加失败:" + err.Error())
  2144. return
  2145. }
  2146. // 季节性图表额外配置信息
  2147. var seasonExtraConfig string
  2148. // 关联指标
  2149. var edbInfoIdArr []int
  2150. chartEdbInfoList := req.ChartEdbInfoList
  2151. if len(chartEdbInfoList) > 0 {
  2152. for _, v := range chartEdbInfoList {
  2153. edbInfoId := v.EdbInfoId
  2154. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  2155. if tmpErr != nil {
  2156. if tmpErr.Error() == utils.ErrNoRow() {
  2157. errMsg = "指标不存在!"
  2158. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2159. return
  2160. } else {
  2161. errMsg = "获取指标信息失败!"
  2162. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  2163. return
  2164. }
  2165. }
  2166. if edbInfo == nil {
  2167. errMsg = "指标已被删除,请重新选择!"
  2168. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2169. return
  2170. } else {
  2171. if edbInfo.EdbInfoId <= 0 {
  2172. errMsg = "指标已被删除,请重新选择!"
  2173. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2174. return
  2175. }
  2176. }
  2177. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  2178. edbInfo.EdbNameSource = edbInfo.EdbName
  2179. //edbList = append(edbList, edbInfo)
  2180. //新增季节性图表数据计算
  2181. if chartType == 2 {
  2182. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  2183. if tmpErr != nil {
  2184. errMsg = "保存失败"
  2185. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  2186. return
  2187. }
  2188. if existCount <= 0 {
  2189. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  2190. }
  2191. // 处理季节性图表横轴配置
  2192. {
  2193. if req.SeasonExtraConfig.XEndDate != "" {
  2194. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  2195. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  2196. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  2197. return
  2198. }
  2199. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  2200. if tErr != nil {
  2201. errMsg = "季节性图表配置信息异常"
  2202. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  2203. return
  2204. }
  2205. seasonExtraConfig = string(seasonExtra)
  2206. }
  2207. }
  2208. }
  2209. }
  2210. } else {
  2211. if extraConfig == `` {
  2212. errMsg = "请选择指标!"
  2213. err = errors.New(errMsg)
  2214. return
  2215. }
  2216. edbInfoIdArr = extraConfigEdbInfoIdArr
  2217. lenEdbInfoIdArr := len(edbInfoIdArr)
  2218. if lenEdbInfoIdArr > 0 {
  2219. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  2220. if tmpErr != nil {
  2221. errMsg = "指标异常!"
  2222. err = errors.New("指标异常!Err:" + tmpErr.Error())
  2223. return
  2224. }
  2225. if len(tmpEdbList) != lenEdbInfoIdArr {
  2226. errMsg = "指标异常!"
  2227. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  2228. return
  2229. }
  2230. for _, v := range tmpEdbList {
  2231. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  2232. EdbInfoId: v.EdbInfoId,
  2233. MaxData: 0,
  2234. MinData: 0,
  2235. IsOrder: false,
  2236. IsAxis: 1,
  2237. EdbInfoType: 1,
  2238. LeadValue: 0,
  2239. LeadUnit: "",
  2240. ChartStyle: "",
  2241. ChartColor: "",
  2242. PredictChartColor: "",
  2243. ChartWidth: 0,
  2244. Source: utils.CHART_SOURCE_DEFAULT,
  2245. })
  2246. }
  2247. }
  2248. }
  2249. if len(edbInfoIdArr) <= 0 {
  2250. errMsg = "请选择指标!"
  2251. err = errors.New(errMsg)
  2252. return
  2253. }
  2254. // 图表额外配置
  2255. var allExtraConfig string
  2256. extraConfig, allExtraConfig, err, errMsg = HandleExtraConfig(0, chartType, extraConfig)
  2257. if err != nil {
  2258. if errMsg == `` {
  2259. errMsg = "指标异常!"
  2260. }
  2261. err = errors.New("指标异常!Err:" + err.Error())
  2262. return
  2263. }
  2264. sort.Ints(edbInfoIdArr)
  2265. var edbInfoIdArrStr []string
  2266. for _, v := range edbInfoIdArr {
  2267. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  2268. }
  2269. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  2270. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  2271. //if err != nil && err.Error() != utils.ErrNoRow() {
  2272. // br.Msg = "保存失败"
  2273. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2274. // return
  2275. //}
  2276. //if count > 0 {
  2277. // br.Msg = "所选指标已存在相同指标"
  2278. // return
  2279. //}
  2280. {
  2281. var count int // 同名指标数量
  2282. //判断图表是否存在
  2283. var condition string
  2284. var pars []interface{}
  2285. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  2286. //condition += " AND chart_classify_id=? "
  2287. //pars = append(pars, req.ChartClassifyId)
  2288. switch lang {
  2289. case utils.EnLangVersion:
  2290. condition += " AND chart_name_en = ? AND source = ? "
  2291. default:
  2292. condition += " AND chart_name = ? AND source = ? "
  2293. }
  2294. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2295. count, err = data_manage.GetChartInfoCountByCondition(condition, pars)
  2296. if err != nil {
  2297. errMsg = "判断图表名称是否存在失败"
  2298. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  2299. return
  2300. }
  2301. if count > 0 {
  2302. errMsg = "图表已存在,请重新填写"
  2303. err = errors.New(errMsg)
  2304. isSendEmail = false
  2305. return
  2306. }
  2307. }
  2308. barChartConf := `` //柱方图的配置信息
  2309. if chartType == 7 {
  2310. barChartConfReq := req.BarChartInfo
  2311. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  2312. errMsg = "请选择指标"
  2313. err = errors.New(errMsg)
  2314. isSendEmail = false
  2315. return
  2316. }
  2317. if len(barChartConfReq.DateList) <= 0 {
  2318. errMsg = "请选择日期"
  2319. err = errors.New(errMsg)
  2320. isSendEmail = false
  2321. return
  2322. }
  2323. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  2324. if tmpErr != nil {
  2325. errMsg = "柱方图信息异常"
  2326. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  2327. return
  2328. }
  2329. barChartConf = string(barChartConfByte)
  2330. }
  2331. disableVal := CheckIsDisableChart(edbInfoIdArr)
  2332. chartInfo = new(data_manage.ChartInfo)
  2333. chartInfo.ChartName = req.ChartName
  2334. chartInfo.ChartNameEn = req.ChartName
  2335. chartInfo.EdbInfoIds = edbInfoIdStr
  2336. chartInfo.ChartClassifyId = req.ChartClassifyId
  2337. chartInfo.SysUserId = sysUserId
  2338. chartInfo.SysUserRealName = sysUserRealName
  2339. chartInfo.CreateTime = time.Now()
  2340. chartInfo.ModifyTime = time.Now()
  2341. chartInfo.IsSetName = 0
  2342. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2343. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  2344. // todo 判断是否需要重新计算用户的start_date
  2345. if req.DateType > 0 {
  2346. chartInfo.DateType = req.DateType
  2347. } else {
  2348. chartInfo.DateType = 3
  2349. }
  2350. if chartType == 0 {
  2351. chartType = 1
  2352. }
  2353. chartInfo.ChartType = chartType
  2354. calendar := req.Calendar
  2355. if calendar == "" {
  2356. calendar = "公历"
  2357. }
  2358. chartInfo.Calendar = calendar
  2359. chartInfo.StartDate = req.StartDate
  2360. chartInfo.EndDate = req.EndDate
  2361. chartInfo.SeasonStartDate = req.StartDate
  2362. chartInfo.SeasonEndDate = req.EndDate
  2363. chartInfo.LeftMin = req.LeftMin
  2364. chartInfo.LeftMax = req.LeftMax
  2365. chartInfo.RightMin = req.RightMin
  2366. chartInfo.RightMax = req.RightMax
  2367. chartInfo.Right2Min = req.Right2Min
  2368. chartInfo.Right2Max = req.Right2Max
  2369. chartInfo.MinMaxSave = req.MinMaxSave
  2370. chartInfo.Disabled = disableVal
  2371. chartInfo.BarConfig = barChartConf
  2372. chartInfo.ExtraConfig = extraConfig
  2373. chartInfo.SeasonExtraConfig = seasonExtraConfig
  2374. chartInfo.StartYear = req.StartYear
  2375. chartInfo.Source = utils.CHART_SOURCE_DEFAULT
  2376. chartInfo.ChartThemeId = req.ChartThemeId
  2377. chartInfo.SourcesFrom = req.SourcesFrom
  2378. chartInfo.Instructions = req.Instructions
  2379. chartInfo.MarkersLines = req.MarkersLines
  2380. chartInfo.MarkersAreas = req.MarkersAreas
  2381. chartInfo.Unit = req.Unit
  2382. chartInfo.UnitEn = req.UnitEn
  2383. chartInfo.ChartAlias = req.ChartAlias
  2384. newId, err := data_manage.AddChartInfo(chartInfo)
  2385. if err != nil {
  2386. errMsg = `保存失败`
  2387. err = errors.New("保存失败,Err:" + err.Error())
  2388. return
  2389. }
  2390. chartInfo.ChartInfoId = int(newId)
  2391. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2392. for _, v := range chartEdbInfoList {
  2393. mapItem := new(data_manage.ChartEdbMapping)
  2394. mapItem.ChartInfoId = int(newId)
  2395. mapItem.EdbInfoId = v.EdbInfoId
  2396. mapItem.CreateTime = time.Now()
  2397. mapItem.ModifyTime = time.Now()
  2398. edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2399. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", edbTimestamp))
  2400. mapItem.MaxData = v.MaxData
  2401. mapItem.MinData = v.MinData
  2402. mapItem.IsOrder = v.IsOrder
  2403. mapItem.IsAxis = v.IsAxis
  2404. mapItem.EdbInfoType = v.EdbInfoType
  2405. mapItem.LeadValue = v.LeadValue
  2406. mapItem.LeadUnit = v.LeadUnit
  2407. mapItem.ChartStyle = v.ChartStyle
  2408. mapItem.ChartColor = v.ChartColor
  2409. mapItem.PredictChartColor = v.PredictChartColor
  2410. mapItem.ChartWidth = v.ChartWidth
  2411. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  2412. mapItem.EdbAliasName = v.EdbAliasName
  2413. mapItem.IsConvert = v.IsConvert
  2414. mapItem.ConvertType = v.ConvertType
  2415. mapItem.ConvertValue = v.ConvertValue
  2416. mapItem.ConvertUnit = v.ConvertUnit
  2417. mapItem.ConvertEnUnit = v.ConvertEnUnit
  2418. mapList = append(mapList, mapItem)
  2419. }
  2420. err = data_manage.AddChartEdbMapping(mapList)
  2421. if err != nil {
  2422. errMsg = `保存失败`
  2423. err = errors.New("保存失败,Err:" + err.Error())
  2424. return
  2425. }
  2426. if chartType == utils.CHART_TYPE_SECTION_COMBINE {
  2427. err = data_manage.AddChartSeriesAndEdbMapping(allExtraConfig, int(newId))
  2428. if err != nil {
  2429. errMsg = `保存失败`
  2430. err = errors.New("保存失败,Err:" + err.Error())
  2431. return
  2432. }
  2433. }
  2434. // 添加指标引用记录
  2435. _ = SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  2436. //添加es数据
  2437. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2438. return
  2439. }
  2440. // EditChartInfo 编辑图表
  2441. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  2442. isSendEmail = true // 默认错误的时候要发送邮件
  2443. req.ChartName = strings.Trim(req.ChartName, " ")
  2444. if req.ChartName == "" {
  2445. errMsg = "请填写图表名称!"
  2446. err = errors.New(errMsg)
  2447. isSendEmail = false
  2448. return
  2449. }
  2450. if req.ChartClassifyId <= 0 {
  2451. errMsg = "分类参数错误!"
  2452. err = errors.New(errMsg)
  2453. isSendEmail = false
  2454. return
  2455. }
  2456. //if req.ChartThemeId <= 0 {
  2457. // errMsg = "请选择主题!"
  2458. // err = errors.New(errMsg)
  2459. // isSendEmail = false
  2460. // return
  2461. //}
  2462. //
  2463. //// 判断主题是否存在
  2464. //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
  2465. //if err != nil {
  2466. // if err.Error() == utils.ErrNoRow() {
  2467. // errMsg = "主题不存在"
  2468. // err = errors.New(errMsg)
  2469. // return
  2470. // }
  2471. // errMsg = "获取主题信息失败"
  2472. // return
  2473. //}
  2474. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2475. if err != nil {
  2476. if err.Error() == utils.ErrNoRow() {
  2477. errMsg = "分类不存在"
  2478. err = errors.New(errMsg)
  2479. return
  2480. }
  2481. errMsg = "获取分类信息失败"
  2482. return
  2483. }
  2484. if chartClassify == nil {
  2485. errMsg = "分类不存在"
  2486. err = errors.New(errMsg)
  2487. return
  2488. }
  2489. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  2490. if err != nil {
  2491. if err.Error() == utils.ErrNoRow() {
  2492. errMsg = "图表已被删除,请刷新页面"
  2493. err = errors.New(errMsg)
  2494. isSendEmail = false
  2495. return
  2496. }
  2497. errMsg = "获取图表信息失败"
  2498. err = errors.New("获取图表信息失败,Err:" + err.Error())
  2499. return
  2500. }
  2501. var extraConfigEdbInfoIdArr []int
  2502. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  2503. if err != nil {
  2504. err = errors.New("添加失败:" + err.Error())
  2505. return
  2506. }
  2507. // 季节性图表额外配置信息
  2508. var seasonExtraConfig string
  2509. // 图表关联指标id
  2510. chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
  2511. // 关联指标
  2512. var edbInfoIdArr []int
  2513. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  2514. if chartItem.ChartType != 10 && chartItem.ChartType != utils.CHART_TYPE_SECTION_COMBINE {
  2515. //edbList := make([]*data_manage.EdbInfo, 0)
  2516. for _, v := range req.ChartEdbInfoList {
  2517. edbInfoId := v.EdbInfoId
  2518. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  2519. if tmpErr != nil {
  2520. if tmpErr.Error() == utils.ErrNoRow() {
  2521. errMsg = "指标不存在!"
  2522. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2523. return
  2524. } else {
  2525. errMsg = "获取指标信息失败!"
  2526. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  2527. return
  2528. }
  2529. }
  2530. if edbInfo == nil {
  2531. errMsg = "指标已被删除,请重新选择!"
  2532. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2533. return
  2534. } else {
  2535. if edbInfo.EdbInfoId <= 0 {
  2536. errMsg = "指标已被删除,请重新选择!"
  2537. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2538. return
  2539. }
  2540. }
  2541. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  2542. edbInfo.EdbNameSource = edbInfo.EdbName
  2543. //edbList = append(edbList, edbInfo)
  2544. //新增季节性图表数据计算
  2545. if req.ChartType == 2 {
  2546. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  2547. if tmpErr != nil {
  2548. errMsg = "保存失败"
  2549. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  2550. return
  2551. }
  2552. if existCount <= 0 {
  2553. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  2554. }
  2555. // 处理季节性图表横轴配置
  2556. {
  2557. if req.SeasonExtraConfig.XEndDate != "" {
  2558. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  2559. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  2560. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  2561. return
  2562. }
  2563. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  2564. if tErr != nil {
  2565. errMsg = "季节性图表配置信息异常"
  2566. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  2567. return
  2568. }
  2569. seasonExtraConfig = string(seasonExtra)
  2570. }
  2571. }
  2572. }
  2573. }
  2574. chartEdbInfoList = req.ChartEdbInfoList
  2575. } else {
  2576. if req.ExtraConfig == `` {
  2577. errMsg = "请选择指标!"
  2578. err = errors.New(errMsg)
  2579. isSendEmail = false
  2580. return
  2581. }
  2582. edbInfoIdArr = extraConfigEdbInfoIdArr
  2583. lenEdbInfoIdArr := len(edbInfoIdArr)
  2584. if lenEdbInfoIdArr > 0 {
  2585. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  2586. if tmpErr != nil {
  2587. errMsg = "指标异常!"
  2588. err = errors.New("指标异常!Err:" + tmpErr.Error())
  2589. return
  2590. }
  2591. if len(tmpEdbList) != lenEdbInfoIdArr {
  2592. errMsg = "指标异常!"
  2593. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  2594. return
  2595. }
  2596. for _, v := range tmpEdbList {
  2597. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  2598. EdbInfoId: v.EdbInfoId,
  2599. MaxData: 0,
  2600. MinData: 0,
  2601. IsOrder: false,
  2602. IsAxis: 1,
  2603. EdbInfoType: 1,
  2604. LeadValue: 0,
  2605. LeadUnit: "",
  2606. ChartStyle: "",
  2607. ChartColor: "",
  2608. PredictChartColor: "",
  2609. ChartWidth: 0,
  2610. Source: utils.CHART_SOURCE_DEFAULT,
  2611. })
  2612. }
  2613. }
  2614. }
  2615. if len(edbInfoIdArr) <= 0 {
  2616. errMsg = "请选择指标!"
  2617. err = errors.New(errMsg)
  2618. isSendEmail = false
  2619. return
  2620. }
  2621. // 图表额外配置
  2622. extraConfig, allExtraConfig, err, errMsg := HandleExtraConfig(req.ChartInfoId, req.ChartType, req.ExtraConfig)
  2623. if err != nil {
  2624. if errMsg == `` {
  2625. errMsg = "指标异常!"
  2626. }
  2627. err = errors.New("指标异常!Err:" + err.Error())
  2628. return
  2629. }
  2630. req.ExtraConfig = extraConfig
  2631. // 权限
  2632. var haveOperaAuth bool
  2633. {
  2634. // 已授权分类id
  2635. permissionChartIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, chartItem.ChartInfoId, chartItem.ChartClassifyId)
  2636. if tmpErr != nil {
  2637. if errMsg == `` {
  2638. errMsg = "获取失败!"
  2639. }
  2640. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  2641. return
  2642. }
  2643. haveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartItem.IsJoinPermission, chartClassify.IsJoinPermission, chartItem.ChartInfoId, chartItem.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2644. }
  2645. //图表操作权限
  2646. ok := CheckOpChartPermission(sysUser, chartItem.SysUserId, haveOperaAuth)
  2647. if !ok {
  2648. errMsg = "没有该图表的操作权限"
  2649. err = errors.New(errMsg)
  2650. isSendEmail = false
  2651. return
  2652. }
  2653. var calendar string
  2654. var dateType int
  2655. dateType = req.DateType
  2656. if req.ChartType != chartItem.ChartType {
  2657. if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
  2658. calendar = "公历"
  2659. }
  2660. if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
  2661. dateType = 3
  2662. }
  2663. }
  2664. sort.Ints(edbInfoIdArr)
  2665. var edbInfoIdArrStr []string
  2666. for _, v := range edbInfoIdArr {
  2667. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  2668. }
  2669. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  2670. //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
  2671. //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
  2672. //if err != nil && err.Error() != utils.ErrNoRow() {
  2673. // br.Msg = "保存失败"
  2674. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2675. // return
  2676. //}
  2677. //if count > 0 {
  2678. // br.Msg = "所选指标已存在相同指标"
  2679. // return
  2680. //}
  2681. // 判断图表是否存在
  2682. {
  2683. var count int
  2684. var condition string
  2685. var pars []interface{}
  2686. condition += " AND chart_info_id<>? "
  2687. pars = append(pars, req.ChartInfoId)
  2688. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  2689. //condition += " AND chart_classify_id=? "
  2690. //pars = append(pars, req.ChartClassifyId)
  2691. switch lang {
  2692. case utils.EnLangVersion:
  2693. condition += " AND chart_name_en = ? AND source = ? "
  2694. default:
  2695. condition += " AND chart_name = ? AND source = ? "
  2696. }
  2697. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2698. count, err = data_manage.GetChartInfoCountByCondition(condition, pars)
  2699. if err != nil {
  2700. errMsg = "判断图表名称是否存在失败"
  2701. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  2702. return
  2703. }
  2704. if count > 0 {
  2705. errMsg = "图表已存在,请重新填写"
  2706. err = errors.New(errMsg)
  2707. isSendEmail = false
  2708. return
  2709. }
  2710. }
  2711. barChartConf := `` //柱方图的配置信息
  2712. if req.ChartType == 7 {
  2713. barChartConfReq := req.BarChartInfo
  2714. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  2715. errMsg = "请选择指标"
  2716. err = errors.New(errMsg)
  2717. isSendEmail = false
  2718. return
  2719. }
  2720. if len(barChartConfReq.DateList) <= 0 {
  2721. errMsg = "请选择日期"
  2722. err = errors.New(errMsg)
  2723. isSendEmail = false
  2724. return
  2725. }
  2726. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  2727. if tmpErr != nil {
  2728. errMsg = "柱方图信息异常"
  2729. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  2730. return
  2731. }
  2732. barChartConf = string(barChartConfByte)
  2733. }
  2734. // 图表启用与否
  2735. disableVal := CheckIsDisableChart(edbInfoIdArr)
  2736. switch lang {
  2737. case utils.EnLangVersion:
  2738. req.ChartNameEn = req.ChartName
  2739. req.ChartName = chartItem.ChartName
  2740. if req.UnitEn == `` {
  2741. req.UnitEn = req.Unit
  2742. }
  2743. req.Unit = chartItem.Unit
  2744. default:
  2745. req.ChartNameEn = chartItem.ChartNameEn
  2746. req.UnitEn = chartItem.UnitEn
  2747. }
  2748. err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList, seasonExtraConfig)
  2749. if err != nil {
  2750. errMsg = "保存失败"
  2751. err = errors.New("保存失败,Err:" + err.Error())
  2752. return
  2753. }
  2754. resp := new(data_manage.AddChartInfoResp)
  2755. resp.ChartInfoId = chartItem.ChartInfoId
  2756. resp.UniqueCode = chartItem.UniqueCode
  2757. resp.ChartType = req.ChartType
  2758. if chartItem.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  2759. err = data_manage.EditChartSeriesAndEdbMapping(allExtraConfig, chartItem.ChartInfoId)
  2760. if err != nil {
  2761. errMsg = "保存失败"
  2762. err = errors.New("保存失败,Err:" + err.Error())
  2763. return
  2764. }
  2765. }
  2766. // todo 添加指标引用记录
  2767. _ = SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  2768. //添加es数据
  2769. go EsAddOrEditChartInfo(chartItem.ChartInfoId)
  2770. //修改my eta es数据
  2771. go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  2772. return
  2773. }
  2774. // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
  2775. func GetEdbSourceByEdbInfoIdList(chartEdbInfoMappingList []*data_manage.ChartEdbInfoMapping) (sourceNameList, sourceNameEnList []string) {
  2776. sourceNameList = make([]string, 0)
  2777. sourceNameEnList = make([]string, 0)
  2778. sourceMap := make(map[int]string)
  2779. for _, v := range chartEdbInfoMappingList {
  2780. // 指标类型:1:基础指标,2:计算指标
  2781. if v.EdbType == 2 || v.EdbInfoCategoryType == 1 {
  2782. //sourceMap[0] = "弘则研究"
  2783. baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  2784. for _, baseEdbInfo := range baseEdbInfoArr {
  2785. if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
  2786. sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
  2787. }
  2788. }
  2789. } else {
  2790. sourceMap[v.Source] = v.SourceName
  2791. }
  2792. }
  2793. for source, sourceName := range sourceMap {
  2794. if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
  2795. continue
  2796. }
  2797. sourceNameList = append(sourceNameList, sourceName)
  2798. sourceNameEn, ok := utils.DataSourceEnMap[source]
  2799. if !ok {
  2800. sourceNameEn = sourceName
  2801. }
  2802. sourceNameEnList = append(sourceNameEnList, sourceNameEn)
  2803. }
  2804. //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
  2805. //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
  2806. // 图表来源
  2807. conf, e := models.GetBusinessConf()
  2808. if e != nil {
  2809. return
  2810. }
  2811. if conf[models.BusinessConfCompanyName] != "" {
  2812. sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
  2813. sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
  2814. }
  2815. return
  2816. }
  2817. // RadarChartData 雷达图的数据处理
  2818. func RadarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
  2819. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  2820. edbDataMap := make(map[int]map[string]float64)
  2821. for edbInfoId, edbDataList := range edbDataListMap {
  2822. edbDateData := make(map[string]float64)
  2823. for _, edbData := range edbDataList {
  2824. edbDateData[edbData.DataTime] = edbData.Value
  2825. }
  2826. edbDataMap[edbInfoId] = edbDateData
  2827. }
  2828. // edbIdList 指标展示顺序;x轴的指标顺序
  2829. edbIdList = make([]int, 0)
  2830. for _, v := range mappingList {
  2831. edbIdList = append(edbIdList, v.EdbInfoId)
  2832. }
  2833. chartDateList := extraConfig.DateList
  2834. yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
  2835. for _, chartDate := range chartDateList {
  2836. var maxDate time.Time
  2837. findDataList := make([]float64, 0) // 当前日期的数据值
  2838. for _, edbInfoId := range edbIdList {
  2839. findDate := chartDate.Date //需要的日期值
  2840. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  2841. if len(dataList) <= 0 {
  2842. // 没有数据的指标id
  2843. findDataList = append(findDataList, 0)
  2844. continue
  2845. }
  2846. switch chartDate.Type {
  2847. case 1: //最新值
  2848. findDate = dataList[len(dataList)-1].DataTime
  2849. case 2: //近期几天
  2850. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  2851. if tmpErr != nil {
  2852. err = tmpErr
  2853. return
  2854. }
  2855. findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
  2856. lenData := len(dataList) - 1
  2857. for i := lenData; i >= 0; i-- {
  2858. currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  2859. if e != nil {
  2860. err = e
  2861. return
  2862. }
  2863. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  2864. findDate = dataList[i].DataTime
  2865. break
  2866. }
  2867. }
  2868. case 3: // 固定日期
  2869. //最早的日期
  2870. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  2871. if tmpErr != nil {
  2872. err = tmpErr
  2873. return
  2874. }
  2875. //寻找固定日期的数据
  2876. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
  2877. if tmpErr != nil {
  2878. err = tmpErr
  2879. return
  2880. }
  2881. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  2882. tmpDate := tmpDateTime.Format(utils.FormatDate)
  2883. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  2884. findDate = tmpDate
  2885. break
  2886. }
  2887. }
  2888. default:
  2889. err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
  2890. return
  2891. }
  2892. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  2893. if maxDate.IsZero() {
  2894. maxDate = findDateTime
  2895. } else {
  2896. if findDateTime.After(maxDate) {
  2897. maxDate = findDateTime
  2898. }
  2899. }
  2900. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  2901. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  2902. findDataList = append(findDataList, tmpValue)
  2903. } else {
  2904. findDataList = append(findDataList, 0)
  2905. }
  2906. }
  2907. yDate := "0000-00-00"
  2908. if !maxDate.IsZero() {
  2909. yDate = maxDate.Format(utils.FormatDate)
  2910. }
  2911. yDataList = append(yDataList, data_manage.RadarYData{
  2912. Date: yDate,
  2913. Value: findDataList,
  2914. Color: chartDate.Color,
  2915. Name: chartDate.Name,
  2916. })
  2917. }
  2918. chartDataResp = data_manage.RadarChartInfoResp{
  2919. YDataList: yDataList,
  2920. XEdbIdValue: edbIdList,
  2921. }
  2922. return
  2923. }
  2924. // GetChartConvertEdbData 获取图表数据转换的指标数据
  2925. 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) {
  2926. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  2927. xEdbIdValue = make([]int, 0)
  2928. yDataList = make([]data_manage.YData, 0)
  2929. var extraConfig interface{}
  2930. switch chartType {
  2931. case 7: // 柱形图
  2932. var barConfig data_manage.BarChartInfoReq
  2933. if extraConfigStr == `` {
  2934. errMsg = "柱方图未配置"
  2935. err = errors.New(errMsg)
  2936. return
  2937. }
  2938. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  2939. if err != nil {
  2940. errMsg = "柱方图配置异常"
  2941. err = errors.New(errMsg)
  2942. return
  2943. }
  2944. extraConfig = barConfig
  2945. case 10: // 截面散点图
  2946. var tmpExtraConfig data_manage.SectionScatterReq
  2947. if extraConfigStr == `` {
  2948. errMsg = "截面散点图未配置"
  2949. err = errors.New(errMsg)
  2950. return
  2951. }
  2952. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  2953. if err != nil {
  2954. errMsg = "截面散点配置异常"
  2955. err = errors.New(errMsg)
  2956. return
  2957. }
  2958. extraConfig = tmpExtraConfig
  2959. case utils.CHART_TYPE_RADAR:
  2960. var barConfig data_manage.RadarChartInfoReq
  2961. if extraConfigStr == `` {
  2962. errMsg = "雷达图未配置"
  2963. err = errors.New(errMsg)
  2964. return
  2965. }
  2966. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  2967. if err != nil {
  2968. errMsg = "雷达图配置异常"
  2969. err = errors.New(errMsg)
  2970. return
  2971. }
  2972. extraConfig = barConfig
  2973. default:
  2974. xEdbIdValue = make([]int, 0)
  2975. yDataList = make([]data_manage.YData, 0)
  2976. }
  2977. // 指标对应的所有数据
  2978. edbDataListMap, edbList, err := getEdbConvertDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig, isAxis)
  2979. if err != nil {
  2980. return
  2981. }
  2982. // 特殊图形数据处理
  2983. switch chartType {
  2984. case 2: // 季节性图
  2985. if seasonExtraConfig != "" {
  2986. // 季节性图计算不管图上数据时间,拿所有数据
  2987. _, tempEdbList, e := getEdbDataMapList(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
  2988. if e != nil {
  2989. err = e
  2990. return
  2991. }
  2992. dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
  2993. } else {
  2994. // 兼容无配置的老图
  2995. dataResp = new(data_manage.SeasonChartResp)
  2996. }
  2997. case 7: // 柱形图
  2998. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  2999. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  3000. for k := range yDataList {
  3001. yDataList[k].Unit = barChartConf.Unit
  3002. yDataList[k].UnitEn = barChartConf.UnitEn
  3003. }
  3004. for _, v := range edbList {
  3005. // 指标别名
  3006. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  3007. for _, reqEdb := range barChartConf.EdbInfoIdList {
  3008. if v.EdbInfoId == reqEdb.EdbInfoId {
  3009. v.EdbAliasName = reqEdb.Name
  3010. }
  3011. }
  3012. }
  3013. }
  3014. case 10: // 截面散点图
  3015. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  3016. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  3017. var tmpExtraConfig data_manage.SectionScatterReq
  3018. if extraConfigStr == `` {
  3019. errMsg = "截面散点图未配置"
  3020. err = errors.New(errMsg)
  3021. return
  3022. }
  3023. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  3024. if err != nil {
  3025. errMsg = "截面散点配置异常"
  3026. err = errors.New(errMsg)
  3027. return
  3028. }
  3029. // 这个数据没有必要返回给前端
  3030. for _, v := range edbList {
  3031. v.DataList = nil
  3032. }
  3033. case utils.CHART_TYPE_RADAR: //雷达图
  3034. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  3035. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  3036. }
  3037. return
  3038. }
  3039. 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) {
  3040. // 指标对应的所有数据
  3041. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  3042. for _, v := range mappingList {
  3043. //fmt.Println("v:", v.EdbInfoId)
  3044. item := new(data_manage.ChartEdbInfoMapping)
  3045. item.EdbInfoId = v.EdbInfoId
  3046. item.SourceName = v.SourceName
  3047. item.Source = v.Source
  3048. item.EdbCode = v.EdbCode
  3049. item.EdbName = v.EdbName
  3050. item.EdbNameEn = v.EdbNameEn
  3051. item.Frequency = v.Frequency
  3052. item.EdbType = v.EdbType
  3053. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  3054. if v.Unit != `无` {
  3055. item.Unit = v.Unit
  3056. }
  3057. item.UnitEn = v.UnitEn
  3058. item.StartDate = v.StartDate
  3059. item.EndDate = v.EndDate
  3060. item.ModifyTime = v.ModifyTime
  3061. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  3062. item.PredictChartColor = v.PredictChartColor
  3063. item.ClassifyId = v.ClassifyId
  3064. if chartInfoId <= 0 {
  3065. item.IsAxis = 1
  3066. item.LeadValue = 0
  3067. item.LeadUnit = ""
  3068. item.ChartEdbMappingId = 0
  3069. item.ChartInfoId = 0
  3070. item.IsOrder = false
  3071. item.EdbInfoType = 1
  3072. item.ChartStyle = ""
  3073. item.ChartColor = ""
  3074. item.ChartWidth = 0
  3075. item.MaxData = v.MaxValue
  3076. item.MinData = v.MinValue
  3077. } else {
  3078. item.IsAxis = v.IsAxis
  3079. item.EdbInfoType = v.EdbInfoType
  3080. item.LeadValue = v.LeadValue
  3081. item.LeadUnit = v.LeadUnit
  3082. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  3083. item.ChartEdbMappingId = v.ChartEdbMappingId
  3084. item.ChartInfoId = v.ChartInfoId
  3085. item.ChartStyle = v.ChartStyle
  3086. item.ChartColor = v.ChartColor
  3087. item.ChartWidth = v.ChartWidth
  3088. item.IsOrder = v.IsOrder
  3089. item.MaxData = v.MaxData
  3090. item.MinData = v.MinData
  3091. }
  3092. item.LatestValue = v.LatestValue
  3093. item.LatestDate = v.LatestDate
  3094. item.UniqueCode = v.UniqueCode
  3095. item.MoveLatestDate = v.LatestDate
  3096. item.EdbAliasName = v.EdbAliasName
  3097. item.IsConvert = v.IsConvert
  3098. item.ConvertType = v.ConvertType
  3099. item.ConvertValue = v.ConvertValue
  3100. item.ConvertUnit = v.ConvertUnit
  3101. item.ConvertEnUnit = v.ConvertEnUnit
  3102. var startDateReal string
  3103. var diffSeconds int64
  3104. if chartType == 2 { //季节性图
  3105. startDateReal = startDate
  3106. if len(mappingList) > 1 {
  3107. item.IsAxis = v.IsAxis
  3108. }
  3109. } else {
  3110. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  3111. var startTimeRealTemp time.Time
  3112. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  3113. switch v.LeadUnit {
  3114. case "天":
  3115. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  3116. case "月":
  3117. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  3118. case "季":
  3119. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  3120. case "周":
  3121. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  3122. case "年":
  3123. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  3124. }
  3125. if startTimeRealTemp.Before(startDateParse) {
  3126. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  3127. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  3128. } else {
  3129. startDateReal = startDate
  3130. diffSeconds = 0
  3131. }
  3132. // 预测指标的开始日期也要偏移
  3133. {
  3134. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  3135. if tmpErr != nil {
  3136. err = tmpErr
  3137. return
  3138. }
  3139. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  3140. if tmpErr != nil {
  3141. err = tmpErr
  3142. return
  3143. }
  3144. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  3145. }
  3146. } else {
  3147. startDateReal = startDate
  3148. }
  3149. }
  3150. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  3151. calendarPreYear := 0
  3152. if calendar == "农历" {
  3153. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  3154. if err != nil {
  3155. fmt.Println("time.Parse:" + err.Error())
  3156. }
  3157. calendarPreYear = newStartDateReal.Year() - 1
  3158. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  3159. startDateReal = newStartDateReal.Format(utils.FormatDate)
  3160. }
  3161. dataList := make([]*data_manage.EdbDataList, 0)
  3162. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  3163. //fmt.Println("calendarPreYear:", calendarPreYear)
  3164. //var newEdbInfo *data_manage.EdbInfo
  3165. switch v.EdbInfoCategoryType {
  3166. case 0:
  3167. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  3168. case 1:
  3169. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  3170. default:
  3171. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  3172. }
  3173. if err != nil {
  3174. return
  3175. }
  3176. if v.IsConvert == 1 {
  3177. switch v.ConvertType {
  3178. case 1:
  3179. for i, data := range dataList {
  3180. dataList[i].Value = data.Value * v.ConvertValue
  3181. }
  3182. item.MaxData = item.MaxData * v.ConvertValue
  3183. item.MinData = item.MinData * v.ConvertValue
  3184. case 2:
  3185. for i, data := range dataList {
  3186. dataList[i].Value = data.Value / v.ConvertValue
  3187. }
  3188. item.MaxData = item.MaxData / v.ConvertValue
  3189. item.MinData = item.MinData / v.ConvertValue
  3190. case 3:
  3191. for i, data := range dataList {
  3192. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  3193. }
  3194. item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  3195. item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  3196. }
  3197. }
  3198. edbDataListMap[v.EdbInfoId] = dataList
  3199. if diffSeconds != 0 && v.EdbInfoType == 0 {
  3200. dataListLen := len(dataList)
  3201. for i := 0; i < dataListLen; i++ {
  3202. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  3203. }
  3204. }
  3205. if chartType == 2 && isAxis == 1 {
  3206. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  3207. if tmpErr != nil {
  3208. //item.DataList = dataList
  3209. item.IsNullData = true
  3210. edbList = append(edbList, item)
  3211. continue
  3212. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  3213. return
  3214. }
  3215. if calendar == "农历" {
  3216. if len(dataList) <= 0 {
  3217. result := new(data_manage.EdbDataResult)
  3218. item.DataList = result
  3219. } else {
  3220. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  3221. if tmpErr != nil {
  3222. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  3223. return
  3224. }
  3225. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  3226. if tErr != nil {
  3227. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  3228. return
  3229. }
  3230. item.DataList = quarterDataList
  3231. }
  3232. } else {
  3233. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  3234. if tErr != nil {
  3235. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  3236. return
  3237. }
  3238. item.DataList = quarterDataList
  3239. }
  3240. } else if chartType == 2 && isAxis == 0 {
  3241. // 右轴数据处理,只要最新一年
  3242. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  3243. if tmpErr != nil {
  3244. //item.DataList = dataList
  3245. item.IsNullData = true
  3246. edbList = append(edbList, item)
  3247. continue
  3248. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  3249. return
  3250. }
  3251. newDataList := make([]*data_manage.EdbDataList, 0)
  3252. for _, v := range dataList {
  3253. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  3254. if e != nil {
  3255. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  3256. return
  3257. }
  3258. if dataTime.Year() == latestDate.Year() {
  3259. newDataList = append(newDataList, v)
  3260. }
  3261. }
  3262. item.DataList = newDataList
  3263. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  3264. //item.DataList = dataList
  3265. } else {
  3266. item.DataList = dataList
  3267. }
  3268. edbList = append(edbList, item)
  3269. }
  3270. return
  3271. }
  3272. // GetChartEdbDataV2 获取图表的指标数据
  3273. 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) {
  3274. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  3275. xEdbIdValue = make([]int, 0)
  3276. yDataList = make([]data_manage.YData, 0)
  3277. var extraConfig interface{}
  3278. switch chartType {
  3279. case 6:
  3280. var tmpConfig data_manage.ChartTimeCombineExtraConf
  3281. if extraConfigStr != `` {
  3282. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  3283. if err != nil {
  3284. errMsg = "雷达图配置异常"
  3285. err = errors.New(errMsg)
  3286. return
  3287. }
  3288. }
  3289. extraConfig = tmpConfig
  3290. case 7: // 柱形图
  3291. var barConfig data_manage.BarChartInfoReq
  3292. if extraConfigStr == `` {
  3293. errMsg = "柱方图未配置"
  3294. err = errors.New(errMsg)
  3295. return
  3296. }
  3297. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  3298. if err != nil {
  3299. errMsg = "柱方图配置异常"
  3300. err = errors.New(errMsg)
  3301. return
  3302. }
  3303. extraConfig = barConfig
  3304. case 10: // 截面散点图
  3305. var tmpExtraConfig data_manage.SectionScatterReq
  3306. if extraConfigStr == `` {
  3307. errMsg = "截面散点图未配置"
  3308. err = errors.New(errMsg)
  3309. return
  3310. }
  3311. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  3312. if err != nil {
  3313. errMsg = "截面散点配置异常"
  3314. err = errors.New(errMsg)
  3315. return
  3316. }
  3317. extraConfig = tmpExtraConfig
  3318. case utils.CHART_TYPE_RADAR:
  3319. var barConfig data_manage.RadarChartInfoReq
  3320. if extraConfigStr == `` {
  3321. errMsg = "雷达图未配置"
  3322. err = errors.New(errMsg)
  3323. return
  3324. }
  3325. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  3326. if err != nil {
  3327. errMsg = "雷达图配置异常"
  3328. err = errors.New(errMsg)
  3329. return
  3330. }
  3331. extraConfig = barConfig
  3332. case utils.CHART_TYPE_SECTION_COMBINE:
  3333. // 预览和详情都走这个接口
  3334. var sectionExtraConfig data_manage.ChartSectionAllExtraConf
  3335. if extraConfigStr == `` {
  3336. errMsg = "截面组合图未配置"
  3337. err = errors.New(errMsg)
  3338. return
  3339. }
  3340. if chartInfoId > 0 {
  3341. var sectionExtraConfigTmp data_manage.ChartSectionExtraConf
  3342. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
  3343. if err != nil {
  3344. errMsg = "截面组合图配置异常"
  3345. err = errors.New(errMsg)
  3346. return
  3347. }
  3348. sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
  3349. sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
  3350. sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
  3351. sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
  3352. sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
  3353. sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
  3354. // 查询所有的seriesEdb
  3355. seriesEdbList, e := data_manage.GetChartSeriesEdbByChartInfoId(chartInfoId)
  3356. if e != nil {
  3357. errMsg = "查询seriesEdb失败"
  3358. err = errors.New(errMsg + e.Error())
  3359. return
  3360. }
  3361. // todo 是否有必要返回单位信息
  3362. // 组装成map
  3363. seriesEdbMap := make(map[int][]*data_manage.ChartSectionSeriesEdbConf)
  3364. for _, v := range seriesEdbList {
  3365. var dateConf *data_manage.ChartSectionSeriesDateConfItem
  3366. if v.DateConf != "" {
  3367. err = json.Unmarshal([]byte(v.DateConf), &dateConf)
  3368. if err != nil {
  3369. errMsg = "截面组合图配置异常"
  3370. err = errors.New(errMsg + e.Error())
  3371. return
  3372. }
  3373. }
  3374. tmp := &data_manage.ChartSectionSeriesEdbConf{
  3375. ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
  3376. ChartSeriesId: v.ChartSeriesId,
  3377. //ChartInfoId: v.ChartInfoId,
  3378. EdbInfoId: v.EdbInfoId,
  3379. DateConf: dateConf,
  3380. EdbName: "",
  3381. EdbNameEn: "",
  3382. Unit: "",
  3383. UnitEn: "",
  3384. DateConfName: v.DateConfName,
  3385. DateConfType: v.DateConfType,
  3386. }
  3387. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
  3388. }
  3389. //查询series
  3390. seriesListTmp, e := data_manage.GetChartSeriesByChartInfoId(chartInfoId)
  3391. if e != nil {
  3392. errMsg = "查询series失败"
  3393. err = errors.New(errMsg + e.Error())
  3394. return
  3395. }
  3396. seriesList := make([]*data_manage.ChartSectionSeriesItem, 0)
  3397. for _, v := range seriesListTmp {
  3398. tmpSeries := &data_manage.ChartSectionSeriesItem{
  3399. ChartSeriesId: v.ChartSeriesId,
  3400. SeriesName: v.SeriesName,
  3401. SeriesNameEn: v.SeriesNameEn,
  3402. ChartStyle: v.ChartStyle,
  3403. ChartColor: v.ChartColor,
  3404. ChartWidth: v.ChartWidth,
  3405. IsPoint: v.IsPoint,
  3406. IsNumber: v.IsNumber,
  3407. IsAxis: v.IsAxis,
  3408. MaxData: v.MaxData,
  3409. MinData: v.MinData,
  3410. //IsOrder: false,
  3411. EdbInfoList: nil,
  3412. DataList: nil,
  3413. }
  3414. edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
  3415. if ok {
  3416. tmpSeries.EdbInfoList = edbInfoList
  3417. }
  3418. seriesList = append(seriesList, tmpSeries)
  3419. }
  3420. sectionExtraConfig.SeriesList = seriesList
  3421. } else {
  3422. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  3423. if err != nil {
  3424. errMsg = "截面组合图配置异常"
  3425. err = errors.New(errMsg)
  3426. return
  3427. }
  3428. }
  3429. //校验引用日期名称是否重复
  3430. dateNameMap := make(map[string]int)
  3431. dateNameEnMap := make(map[string]int)
  3432. for _, v := range sectionExtraConfig.DateConfList {
  3433. if _, ok := dateNameMap[v.DateConfName]; ok {
  3434. errMsg = "截面组合图引用日期名称设置重复"
  3435. err = errors.New(errMsg + v.DateConfName)
  3436. return
  3437. }
  3438. if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
  3439. errMsg = "截面组合图引用日期名称设置重复"
  3440. err = errors.New(errMsg + v.DateConfNameEn)
  3441. return
  3442. }
  3443. if v.DateConfName != "" {
  3444. dateNameMap[v.DateConfName] = 1
  3445. }
  3446. if v.DateConfNameEn != "" {
  3447. dateNameEnMap[v.DateConfNameEn] = 1
  3448. }
  3449. }
  3450. //检查系列名称是否重复
  3451. seriesNameMap := make(map[string]int)
  3452. seriesNameEnMap := make(map[string]int)
  3453. for _, v := range sectionExtraConfig.SeriesList {
  3454. if _, ok := seriesNameMap[v.SeriesName]; ok {
  3455. errMsg = "截面组合图系列名称设置重复"
  3456. err = errors.New(errMsg + v.SeriesName)
  3457. return
  3458. }
  3459. if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
  3460. errMsg = "截面组合图系列名称设置重复"
  3461. err = errors.New(errMsg + v.SeriesNameEn)
  3462. return
  3463. }
  3464. if v.SeriesName != "" {
  3465. seriesNameMap[v.SeriesName] = 1
  3466. }
  3467. if v.SeriesNameEn != "" {
  3468. seriesNameEnMap[v.SeriesNameEn] = 1
  3469. }
  3470. }
  3471. // 检查基准系列是否设置
  3472. if sectionExtraConfig.BaseChartSeriesName == "" {
  3473. errMsg = "截面组合图基准系列名称未设置"
  3474. err = errors.New(errMsg)
  3475. return
  3476. }
  3477. //todo 如果是详情接口,应该要从其他表里查询配置
  3478. extraConfig = sectionExtraConfig
  3479. default:
  3480. xEdbIdValue = make([]int, 0)
  3481. yDataList = make([]data_manage.YData, 0)
  3482. }
  3483. // 指标对应的所有数据
  3484. edbDataListMap, edbList, err := chartInfoData.GetEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  3485. if err != nil {
  3486. return
  3487. }
  3488. // 特殊图形数据处理
  3489. switch chartType {
  3490. case 6: //时序组合图
  3491. //判断是否堆积
  3492. timeConf := extraConfig.(data_manage.ChartTimeCombineExtraConf)
  3493. if extraConfigStr == "" { //历史数据,默认开启堆积
  3494. timeConf = data_manage.ChartTimeCombineExtraConf{
  3495. IsHeap: 1,
  3496. }
  3497. }
  3498. dataResp = data_manage.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
  3499. case 2: // 季节性图
  3500. if seasonExtraConfig != "" {
  3501. // 季节性图计算不管图上数据时间,拿所有数据
  3502. _, tempEdbList, e := getEdbDataMapListForSeason(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
  3503. if e != nil {
  3504. err = e
  3505. return
  3506. }
  3507. dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
  3508. } else {
  3509. // 兼容无配置的老图
  3510. dataResp = new(data_manage.SeasonChartResp)
  3511. }
  3512. case 7: // 柱形图
  3513. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  3514. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  3515. for k := range yDataList {
  3516. yDataList[k].Unit = barChartConf.Unit
  3517. yDataList[k].UnitEn = barChartConf.UnitEn
  3518. }
  3519. for _, v := range edbList {
  3520. // 指标别名
  3521. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  3522. for _, reqEdb := range barChartConf.EdbInfoIdList {
  3523. if v.EdbInfoId == reqEdb.EdbInfoId {
  3524. v.EdbAliasName = reqEdb.Name
  3525. }
  3526. }
  3527. }
  3528. }
  3529. case 10: // 截面散点图
  3530. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  3531. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  3532. var tmpExtraConfig data_manage.SectionScatterReq
  3533. if extraConfigStr == `` {
  3534. errMsg = "截面散点图未配置"
  3535. err = errors.New(errMsg)
  3536. return
  3537. }
  3538. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  3539. if err != nil {
  3540. errMsg = "截面散点配置异常"
  3541. err = errors.New(errMsg)
  3542. return
  3543. }
  3544. // 这个数据没有必要返回给前端
  3545. for _, v := range edbList {
  3546. v.DataList = nil
  3547. }
  3548. case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
  3549. sectionConf := extraConfig.(data_manage.ChartSectionAllExtraConf)
  3550. var chartInfo *data_manage.ChartInfo
  3551. if chartInfoId > 0 {
  3552. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  3553. if err != nil {
  3554. errMsg = "获取图表信息失败"
  3555. return
  3556. }
  3557. }
  3558. xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)
  3559. // 这个数据没有必要返回给前端
  3560. for _, v := range edbList {
  3561. v.DataList = nil
  3562. }
  3563. case utils.CHART_TYPE_RADAR: //雷达图
  3564. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  3565. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  3566. }
  3567. return
  3568. }
  3569. // SeasonChartData 季节性图的数据处理
  3570. func SeasonChartData(dataList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (dataResp data_manage.SeasonChartResp, err error) {
  3571. var seasonConfig data_manage.SeasonExtraItem
  3572. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonConfig)
  3573. if err != nil {
  3574. err = errors.New("季节性图配置异常, Err:" + err.Error())
  3575. return
  3576. }
  3577. for _, mappingItem := range dataList {
  3578. if mappingItem.IsAxis == 0 {
  3579. continue
  3580. }
  3581. quarterDataList, ok := mappingItem.DataList.(data_manage.QuarterDataList)
  3582. if !ok {
  3583. continue
  3584. }
  3585. // 上下限区间
  3586. if seasonConfig.MaxMinLimits.Year > 0 {
  3587. startYear := time.Now().AddDate(-seasonConfig.MaxMinLimits.Year, 0, 0).Year()
  3588. dataResp.MaxMinLimits.List = make([]*data_manage.MaxMinLimitsData, 0)
  3589. dataTimeMap := make(map[time.Time]time.Time)
  3590. dataTimeList := make([]string, 0)
  3591. maxValueMap := make(map[time.Time]float64)
  3592. minValueMap := make(map[time.Time]float64)
  3593. maxMinDataList := make([]*data_manage.MaxMinLimitsData, 0)
  3594. var startDateStr string
  3595. var endDateStr string
  3596. var newDate time.Time
  3597. startDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XStartDate)
  3598. endDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XEndDate)
  3599. startDate, e := time.Parse(utils.FormatDate, startDateStr)
  3600. if e != nil {
  3601. err = e
  3602. return
  3603. }
  3604. endDate, e := time.Parse(utils.FormatDate, endDateStr)
  3605. if e != nil {
  3606. err = e
  3607. return
  3608. }
  3609. // 日度 周度插值
  3610. for _, v := range quarterDataList {
  3611. if mappingItem.Frequency == "日度" || mappingItem.Frequency == "周度" {
  3612. handleDataMap := make(map[string]float64)
  3613. dataTimeList, _, err = HandleDataByLinearRegressionToList(v.DataList, handleDataMap)
  3614. if err != nil {
  3615. err = errors.New("插值处理数据异常, Err:" + err.Error())
  3616. return
  3617. }
  3618. // 不包含当年
  3619. if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
  3620. continue
  3621. }
  3622. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  3623. chartLegend,_ := strconv.Atoi(v.ChartLegend)
  3624. if chartLegend < startYear {
  3625. continue
  3626. }
  3627. for _, date := range dataTimeList {
  3628. dateTime, e := time.Parse(utils.FormatDate, date)
  3629. if e != nil {
  3630. err = errors.New("时间格式化异常, Err:" + e.Error())
  3631. return
  3632. }
  3633. if dateTime.Year() < startYear {
  3634. continue
  3635. }
  3636. // 不包含2月29号
  3637. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  3638. continue
  3639. }
  3640. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  3641. if seasonConfig.JumpYear == 1 {
  3642. if startDate.After(endDate) {
  3643. // 如果跨年且不到一年
  3644. // 全年截取一部分
  3645. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  3646. continue
  3647. }
  3648. if newDate.After(startDate.AddDate(0, 0, -1)) {
  3649. // 减一年
  3650. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  3651. }
  3652. // 处理上下限列表
  3653. if value, ok := maxValueMap[newDate]; ok {
  3654. if value < handleDataMap[date] {
  3655. maxValueMap[newDate] = handleDataMap[date]
  3656. }
  3657. } else {
  3658. maxValueMap[newDate] = handleDataMap[date]
  3659. }
  3660. if value, ok := minValueMap[newDate]; ok {
  3661. if value > handleDataMap[date] {
  3662. minValueMap[newDate] = handleDataMap[date]
  3663. }
  3664. } else {
  3665. minValueMap[newDate] = handleDataMap[date]
  3666. }
  3667. dataTimeMap[newDate] = newDate
  3668. } else {
  3669. // 如果跨年且大于等于一年
  3670. // double后截取
  3671. if newDate.After(startDate) {
  3672. // 处理上下限列表
  3673. if value, ok := maxValueMap[newDate]; ok {
  3674. if value < handleDataMap[date] {
  3675. maxValueMap[newDate] = handleDataMap[date]
  3676. }
  3677. } else {
  3678. maxValueMap[newDate] = handleDataMap[date]
  3679. }
  3680. if value, ok := minValueMap[newDate]; ok {
  3681. if value > handleDataMap[date] {
  3682. minValueMap[newDate] = handleDataMap[date]
  3683. }
  3684. } else {
  3685. minValueMap[newDate] = handleDataMap[date]
  3686. }
  3687. dataTimeMap[newDate] = newDate
  3688. }
  3689. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  3690. newEndDate := endDate.AddDate(1, 0, 0)
  3691. if newDate.Before(newEndDate) {
  3692. // 处理上下限列表
  3693. if value, ok := maxValueMap[newDate]; ok {
  3694. if value < handleDataMap[date] {
  3695. maxValueMap[newDate] = handleDataMap[date]
  3696. }
  3697. } else {
  3698. maxValueMap[newDate] = handleDataMap[date]
  3699. }
  3700. if value, ok := minValueMap[newDate]; ok {
  3701. if value > handleDataMap[date] {
  3702. minValueMap[newDate] = handleDataMap[date]
  3703. }
  3704. } else {
  3705. minValueMap[newDate] = handleDataMap[date]
  3706. }
  3707. dataTimeMap[newDate] = newDate
  3708. }
  3709. }
  3710. } else {
  3711. // 如果不跨年 正常获取
  3712. // 获取当前日期所在区间
  3713. // 处理上下限列表
  3714. if value, ok := maxValueMap[newDate]; ok {
  3715. if value < handleDataMap[date] {
  3716. maxValueMap[newDate] = handleDataMap[date]
  3717. }
  3718. } else {
  3719. maxValueMap[newDate] = handleDataMap[date]
  3720. }
  3721. if value, ok := minValueMap[newDate]; ok {
  3722. if value > handleDataMap[date] {
  3723. minValueMap[newDate] = handleDataMap[date]
  3724. }
  3725. } else {
  3726. minValueMap[newDate] = handleDataMap[date]
  3727. }
  3728. dataTimeMap[newDate] = newDate
  3729. }
  3730. }
  3731. } else {
  3732. // 旬度、月度、季度、半年度 不插值,需要先把日期列表和数据map取出来
  3733. for _, vv := range v.DataList {
  3734. dateTime, e := time.Parse(utils.FormatDate, vv.DataTime)
  3735. if e != nil {
  3736. err = errors.New("时间格式化异常, Err:" + e.Error())
  3737. return
  3738. }
  3739. // 不包含当年
  3740. if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
  3741. continue
  3742. }
  3743. if dateTime.Year() < startYear {
  3744. continue
  3745. }
  3746. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  3747. chartLegend,_ := strconv.Atoi(v.ChartLegend)
  3748. if chartLegend < startYear {
  3749. continue
  3750. }
  3751. // 月度的2月29号日期改为2月28日
  3752. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  3753. dateTime = dateTime.AddDate(0, 0, -1)
  3754. }
  3755. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  3756. if seasonConfig.JumpYear == 1 {
  3757. if startDate.After(endDate) {
  3758. // 如果跨年且不到一年
  3759. // 全年截取一部分
  3760. if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
  3761. continue
  3762. }
  3763. if newDate.After(startDate.AddDate(0, 0, -1)) {
  3764. // 减一年
  3765. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
  3766. }
  3767. // 处理上下限列表
  3768. if value, ok := maxValueMap[newDate]; ok {
  3769. if value < vv.Value {
  3770. maxValueMap[newDate] = vv.Value
  3771. }
  3772. } else {
  3773. maxValueMap[newDate] = vv.Value
  3774. }
  3775. if value, ok := minValueMap[newDate]; ok {
  3776. if value > vv.Value {
  3777. minValueMap[newDate] = vv.Value
  3778. }
  3779. } else {
  3780. minValueMap[newDate] = vv.Value
  3781. }
  3782. dataTimeMap[newDate] = newDate
  3783. } else {
  3784. // 如果跨年且大于等于一年
  3785. // double后截取
  3786. if newDate.After(startDate) {
  3787. // 处理上下限列表
  3788. if value, ok := maxValueMap[newDate]; ok {
  3789. if value < vv.Value {
  3790. maxValueMap[newDate] = vv.Value
  3791. }
  3792. } else {
  3793. maxValueMap[newDate] = vv.Value
  3794. }
  3795. if value, ok := minValueMap[newDate]; ok {
  3796. if value > vv.Value {
  3797. minValueMap[newDate] = vv.Value
  3798. }
  3799. } else {
  3800. minValueMap[newDate] = vv.Value
  3801. }
  3802. dataTimeMap[newDate] = newDate
  3803. }
  3804. newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
  3805. newEndDate := endDate.AddDate(1, 0, 0)
  3806. if newDate.Before(newEndDate) {
  3807. // 处理上下限列表
  3808. if value, ok := maxValueMap[newDate]; ok {
  3809. if value < vv.Value {
  3810. maxValueMap[newDate] = vv.Value
  3811. }
  3812. } else {
  3813. maxValueMap[newDate] = vv.Value
  3814. }
  3815. if value, ok := minValueMap[newDate]; ok {
  3816. if value > vv.Value {
  3817. minValueMap[newDate] = vv.Value
  3818. }
  3819. } else {
  3820. minValueMap[newDate] = vv.Value
  3821. }
  3822. dataTimeMap[newDate] = newDate
  3823. }
  3824. }
  3825. } else {
  3826. // 如果不跨年 正常获取
  3827. // 获取当前日期所在区间
  3828. // 处理上下限列表
  3829. if value, ok := maxValueMap[newDate]; ok {
  3830. if value < vv.Value {
  3831. maxValueMap[newDate] = vv.Value
  3832. }
  3833. } else {
  3834. maxValueMap[newDate] = vv.Value
  3835. }
  3836. if value, ok := minValueMap[newDate]; ok {
  3837. if value > vv.Value {
  3838. minValueMap[newDate] = vv.Value
  3839. }
  3840. } else {
  3841. minValueMap[newDate] = vv.Value
  3842. }
  3843. dataTimeMap[newDate] = newDate
  3844. }
  3845. }
  3846. }
  3847. }
  3848. for _, v := range dataTimeMap {
  3849. maxMinItem := &data_manage.MaxMinLimitsData{}
  3850. if maxValue, ok := maxValueMap[v]; ok {
  3851. maxMinItem.MaxValue = maxValue
  3852. } else {
  3853. maxMinItem.MaxValue = minValueMap[v]
  3854. }
  3855. if minValue, ok := minValueMap[v]; ok {
  3856. maxMinItem.MinValue = minValue
  3857. } else {
  3858. maxMinItem.MinValue = maxValueMap[v]
  3859. }
  3860. maxMinItem.DataTime = v.Format(utils.FormatDate)
  3861. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  3862. maxMinDataList = append(maxMinDataList, maxMinItem)
  3863. }
  3864. // 排序
  3865. sort.Slice(maxMinDataList, func(i, j int) bool {
  3866. return maxMinDataList[i].DataTime < maxMinDataList[j].DataTime
  3867. })
  3868. dataResp.MaxMinLimits.List = maxMinDataList
  3869. dataResp.MaxMinLimits.Color = seasonConfig.MaxMinLimits.Color
  3870. dataResp.MaxMinLimits.Legend = seasonConfig.MaxMinLimits.Legend
  3871. dataResp.MaxMinLimits.IsShow = seasonConfig.MaxMinLimits.IsShow
  3872. dataResp.MaxMinLimits.IsAdd = seasonConfig.MaxMinLimits.IsAdd
  3873. dataResp.MaxMinLimits.Year = seasonConfig.MaxMinLimits.Year
  3874. }
  3875. // 自定义同期均线
  3876. if seasonConfig.SamePeriodAverage.Year > 0 {
  3877. handleDataMap := make(map[string]float64)
  3878. dataTimeMap := make(map[time.Time]time.Time)
  3879. dataTimeList := make([]string, 0)
  3880. valueMap := make(map[time.Time]float64)
  3881. averageDataList := make([]*data_manage.SamePeriodAverageData, 0)
  3882. dateNumMap := make(map[time.Time]float64)
  3883. for i := len(quarterDataList) - 1; i >= 0; i-- {
  3884. // 插值成日度
  3885. dataTimeList, _, err = HandleDataByLinearRegressionToList(quarterDataList[i].DataList, handleDataMap)
  3886. if err != nil {
  3887. err = errors.New("插值处理数据异常, Err:" + err.Error())
  3888. return
  3889. }
  3890. for _, date := range dataTimeList {
  3891. dateTime, e := time.Parse(utils.FormatDate, date)
  3892. if e != nil {
  3893. err = errors.New("时间格式化异常, Err:" + e.Error())
  3894. return
  3895. }
  3896. startYear := time.Now().AddDate(-seasonConfig.SamePeriodAverage.Year, 0, 0).Year()
  3897. if dateTime.Year() < startYear {
  3898. continue
  3899. }
  3900. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  3901. chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
  3902. if chartLegend < startYear {
  3903. continue
  3904. }
  3905. // 不包含2月29号
  3906. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  3907. continue
  3908. }
  3909. // 不包含当年
  3910. if quarterDataList[i].ChartLegend == strconv.Itoa(time.Now().Year()) {
  3911. continue
  3912. }
  3913. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  3914. // 处理均值
  3915. if value, ok := valueMap[newDate]; ok {
  3916. valueMap[newDate] = handleDataMap[date] + value
  3917. } else {
  3918. valueMap[newDate] = handleDataMap[date]
  3919. }
  3920. dateNumMap[newDate] += 1
  3921. dataTimeMap[newDate] = newDate
  3922. }
  3923. }
  3924. //year := float64(seasonConfig.SamePeriodAverage.Year)
  3925. for _, v := range dataTimeMap {
  3926. averageItem := &data_manage.SamePeriodAverageData{}
  3927. year := dateNumMap[v]
  3928. if value, ok := valueMap[v]; ok {
  3929. if year > 0 {
  3930. averageItem.Value = value / year
  3931. } else {
  3932. averageItem.Value = value
  3933. }
  3934. }
  3935. averageItem.DataTime = v.Format(utils.FormatDate)
  3936. averageItem.DataTimestamp = v.UnixNano() / 1e6
  3937. averageDataList = append(averageDataList, averageItem)
  3938. }
  3939. // 排序
  3940. sort.Slice(averageDataList, func(i, j int) bool {
  3941. return averageDataList[i].DataTime < averageDataList[j].DataTime
  3942. })
  3943. dataResp.SamePeriodAverage.List = averageDataList
  3944. dataResp.SamePeriodAverage.Year = seasonConfig.SamePeriodAverage.Year
  3945. dataResp.SamePeriodAverage.Color = seasonConfig.SamePeriodAverage.Color
  3946. dataResp.SamePeriodAverage.Legend = seasonConfig.SamePeriodAverage.Legend
  3947. dataResp.SamePeriodAverage.IsShow = seasonConfig.SamePeriodAverage.IsShow
  3948. dataResp.SamePeriodAverage.LineType = seasonConfig.SamePeriodAverage.LineType
  3949. dataResp.SamePeriodAverage.LineWidth = seasonConfig.SamePeriodAverage.LineWidth
  3950. dataResp.SamePeriodAverage.IsAdd = seasonConfig.SamePeriodAverage.IsAdd
  3951. }
  3952. // 自定义同期标准差
  3953. if seasonConfig.SamePeriodStandardDeviation.Year > 1 && seasonConfig.SamePeriodStandardDeviation.Multiple > 0 {
  3954. startYear := time.Now().AddDate(-seasonConfig.SamePeriodStandardDeviation.Year, 0, 0).Year()
  3955. // 先算均值,再算标准差
  3956. handleDataMap := make(map[string]float64)
  3957. dataTimeMap := make(map[time.Time]time.Time)
  3958. dataTimeValueMap := make(map[time.Time][]float64)
  3959. dataTimeList := make([]string, 0)
  3960. valueMap := make(map[time.Time]float64)
  3961. samePeriodStandardDeviationList := make([]*data_manage.MaxMinLimitsData, 0)
  3962. for i := len(quarterDataList) - 1; i > len(quarterDataList)-seasonConfig.SamePeriodAverage.Year-1 && i > 0; i-- {
  3963. // 插值成日度
  3964. dataTimeList, _, err = HandleDataByLinearRegressionToListV2(quarterDataList[i].DataList, handleDataMap)
  3965. if err != nil {
  3966. err = errors.New("插值处理数据异常, Err:" + err.Error())
  3967. return
  3968. }
  3969. for _, date := range dataTimeList {
  3970. dateTime, e := time.Parse(utils.FormatDate, date)
  3971. if e != nil {
  3972. err = errors.New("时间格式化异常, Err:" + e.Error())
  3973. return
  3974. }
  3975. if dateTime.Year() < startYear {
  3976. continue
  3977. }
  3978. // 农历时dataTimeList的年份为最新年份,图例时间的年份才是真实年份
  3979. chartLegend,_ := strconv.Atoi(quarterDataList[i].ChartLegend)
  3980. if chartLegend < startYear {
  3981. continue
  3982. }
  3983. // 不包含2月29号
  3984. if dateTime.Month() == 2 && dateTime.Day() == 29 {
  3985. continue
  3986. }
  3987. newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
  3988. // 处理均值
  3989. if value, ok := valueMap[newDate]; ok {
  3990. valueMap[newDate] = (handleDataMap[date] + value) / 2
  3991. } else {
  3992. valueMap[newDate] = handleDataMap[date]
  3993. }
  3994. dataTimeMap[newDate] = newDate
  3995. valueList := dataTimeValueMap[newDate]
  3996. valueList = append(valueList, handleDataMap[date])
  3997. dataTimeValueMap[newDate] = valueList
  3998. }
  3999. }
  4000. for _, v := range dataTimeMap {
  4001. valueList := dataTimeValueMap[v]
  4002. stdev := utils.CalculateStandardDeviation(valueList)
  4003. stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
  4004. maxMinItem := &data_manage.MaxMinLimitsData{}
  4005. if value, ok := valueMap[v]; ok {
  4006. maxMinItem.MaxValue = value + stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  4007. maxMinItem.MinValue = value - stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
  4008. }
  4009. maxMinItem.DataTime = v.Format(utils.FormatDate)
  4010. maxMinItem.DataTimestamp = v.UnixNano() / 1e6
  4011. samePeriodStandardDeviationList = append(samePeriodStandardDeviationList, maxMinItem)
  4012. }
  4013. // 排序
  4014. sort.Slice(samePeriodStandardDeviationList, func(i, j int) bool {
  4015. return samePeriodStandardDeviationList[i].DataTime < samePeriodStandardDeviationList[j].DataTime
  4016. })
  4017. dataResp.SamePeriodStandardDeviation.List = samePeriodStandardDeviationList
  4018. dataResp.SamePeriodStandardDeviation.Color = seasonConfig.SamePeriodStandardDeviation.Color
  4019. dataResp.SamePeriodStandardDeviation.Legend = seasonConfig.SamePeriodStandardDeviation.Legend
  4020. dataResp.SamePeriodStandardDeviation.IsShow = seasonConfig.SamePeriodStandardDeviation.IsShow
  4021. dataResp.SamePeriodStandardDeviation.Multiple = seasonConfig.SamePeriodStandardDeviation.Multiple
  4022. dataResp.SamePeriodStandardDeviation.Year = seasonConfig.SamePeriodStandardDeviation.Year
  4023. dataResp.SamePeriodStandardDeviation.IsAdd = seasonConfig.SamePeriodStandardDeviation.IsAdd
  4024. }
  4025. // 自定义右轴
  4026. if seasonConfig.RightAxis.IndicatorType != 0 {
  4027. if seasonConfig.RightAxis.IndicatorType == 1 {
  4028. startTime, _ := time.Parse(utils.FormatDate, mappingItem.StartDate)
  4029. for i := len(quarterDataList) - 1; i > len(quarterDataList)-2 && i > 0; i-- {
  4030. var rightMappingItem data_manage.ChartEdbInfoMapping
  4031. rightMappingItem = *mappingItem
  4032. // 计算同比值
  4033. tbzDataList, minValue, maxValue, e := GetEdbDataTbzForSeason(mappingItem.Frequency, quarterDataList[i].DataList, startTime)
  4034. if e != nil {
  4035. err = errors.New("计算同比值失败, Err:" + e.Error())
  4036. return
  4037. }
  4038. rightMappingItem.DataList = tbzDataList
  4039. rightMappingItem.MaxData = maxValue
  4040. rightMappingItem.MinData = minValue
  4041. dataResp.RightAxis.EdbInfoList = append(dataResp.RightAxis.EdbInfoList, &rightMappingItem)
  4042. }
  4043. }
  4044. dataResp.RightAxis.SeasonRightAxis = seasonConfig.RightAxis
  4045. }
  4046. }
  4047. return
  4048. }
  4049. // 计算百分位对应的值
  4050. func PercentileAlgorithm(x float64, data []float64) float64 {
  4051. N := float64(len(data))
  4052. n := (x / 100) * (N - 1)
  4053. // 如果n是整数,则直接返回第n个数值
  4054. if n == float64(int(n)) {
  4055. return data[int(n)-1]
  4056. }
  4057. // 如果n是小数,执行插值
  4058. nInt := int(n)
  4059. e := n - float64(nInt)
  4060. // 取出相邻的两个数值
  4061. Sn := data[nInt-1]
  4062. Sn1 := data[nInt]
  4063. // 计算插值结果
  4064. y := Sn + e*(Sn1-Sn)
  4065. return y
  4066. }
  4067. func MarkerLineCalculate(markerLine data_manage.MarkersLine, dataList interface{}, chartInfo *data_manage.ChartInfoView) (value string, err error) {
  4068. if markerLine.Calculation == 1 {
  4069. // 区间均值
  4070. averge := 0.0
  4071. length := 0
  4072. // 计算左轴
  4073. if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
  4074. //季节性图结构体不一样
  4075. quarterDataList := dataList.(data_manage.QuarterDataList)
  4076. for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
  4077. for _, vv := range quarterData.DataList {
  4078. if markerLine.TimeIntervalType == 1 {
  4079. startDate := markerLine.StartDate.Date
  4080. endDate := time.Now().Format(utils.FormatDate)
  4081. if markerLine.StartDate.TimeType == 2 {
  4082. // 动态
  4083. if markerLine.StartDate.Conf.BaseDate == 1 {
  4084. // 指标最新日期
  4085. startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4086. } else {
  4087. // 系统日期
  4088. startDate = time.Now().Format(utils.FormatDate)
  4089. }
  4090. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4091. }
  4092. if markerLine.EndDate.TimeType == 1 {
  4093. // 固定
  4094. endDate = markerLine.EndDate.Date
  4095. } else if markerLine.EndDate.TimeType == 2 {
  4096. // 动态
  4097. if markerLine.StartDate.Conf.BaseDate == 1 {
  4098. // 指标最新日期
  4099. endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4100. } else {
  4101. // 系统日期
  4102. endDate = time.Now().Format(utils.FormatDate)
  4103. }
  4104. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4105. }
  4106. if vv.DataTime >= startDate && vv.DataTime <= endDate {
  4107. averge += vv.Value
  4108. length += 1
  4109. }
  4110. } else {
  4111. averge += vv.Value
  4112. length += 1
  4113. }
  4114. }
  4115. averge = averge / float64(length)
  4116. value = fmt.Sprintf("%.2f", averge)
  4117. }
  4118. } else {
  4119. dataList := dataList.([]*data_manage.EdbDataList)
  4120. for _, dataItem := range dataList {
  4121. if markerLine.TimeIntervalType == 1 {
  4122. startDate := markerLine.StartDate.Date
  4123. endDate := time.Now().Format(utils.FormatDate)
  4124. if markerLine.StartDate.TimeType == 2 {
  4125. // 动态
  4126. if markerLine.StartDate.Conf.BaseDate == 1 {
  4127. // 指标最新日期
  4128. startDate = dataList[len(dataList)-1].DataTime
  4129. } else {
  4130. // 系统日期
  4131. startDate = time.Now().Format(utils.FormatDate)
  4132. }
  4133. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4134. }
  4135. if markerLine.EndDate.TimeType == 1 {
  4136. // 固定
  4137. endDate = markerLine.EndDate.Date
  4138. } else if markerLine.EndDate.TimeType == 2 {
  4139. // 动态
  4140. if markerLine.StartDate.Conf.BaseDate == 1 {
  4141. // 指标最新日期
  4142. endDate = dataList[len(dataList)-1].DataTime
  4143. } else {
  4144. // 系统日期
  4145. endDate = time.Now().Format(utils.FormatDate)
  4146. }
  4147. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4148. }
  4149. if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
  4150. averge += dataItem.Value
  4151. length += 1
  4152. }
  4153. } else {
  4154. averge += dataItem.Value
  4155. length += 1
  4156. }
  4157. }
  4158. averge = averge / float64(length)
  4159. value = fmt.Sprintf("%.2f", averge)
  4160. }
  4161. } else if markerLine.Calculation == 2 {
  4162. // 区间均值加N倍标准差
  4163. averge := 0.0
  4164. length := 0
  4165. // 计算左轴
  4166. if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
  4167. //季节性图结构体不一样
  4168. faloatList := make([]float64, 0)
  4169. quarterDataList := dataList.(data_manage.QuarterDataList)
  4170. for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
  4171. for _, vv := range quarterData.DataList {
  4172. if markerLine.TimeIntervalType == 1 {
  4173. startDate := markerLine.StartDate.Date
  4174. endDate := time.Now().Format(utils.FormatDate)
  4175. if markerLine.StartDate.TimeType == 2 {
  4176. // 动态
  4177. if markerLine.StartDate.Conf.BaseDate == 1 {
  4178. // 指标最新日期
  4179. startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4180. } else {
  4181. // 系统日期
  4182. startDate = time.Now().Format(utils.FormatDate)
  4183. }
  4184. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4185. }
  4186. if markerLine.EndDate.TimeType == 1 {
  4187. // 固定
  4188. endDate = markerLine.EndDate.Date
  4189. } else if markerLine.EndDate.TimeType == 2 {
  4190. // 动态
  4191. if markerLine.StartDate.Conf.BaseDate == 1 {
  4192. // 指标最新日期
  4193. endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4194. } else {
  4195. // 系统日期
  4196. endDate = time.Now().Format(utils.FormatDate)
  4197. }
  4198. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4199. }
  4200. if vv.DataTime >= startDate && vv.DataTime <= endDate {
  4201. faloatList = append(faloatList, vv.Value)
  4202. averge += vv.Value
  4203. length += 1
  4204. }
  4205. } else {
  4206. faloatList = append(faloatList, vv.Value)
  4207. averge += vv.Value
  4208. length += 1
  4209. }
  4210. }
  4211. averge = averge / float64(length)
  4212. stdev := utils.CalculateStandardDeviation(faloatList)
  4213. stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
  4214. value = fmt.Sprintf("%.2f", averge+stdev*markerLine.CalculationValue)
  4215. }
  4216. } else {
  4217. dataList := dataList.([]*data_manage.EdbDataList)
  4218. floatList := make([]float64, 0)
  4219. for _, dataItem := range dataList {
  4220. if markerLine.TimeIntervalType == 1 {
  4221. startDate := markerLine.StartDate.Date
  4222. endDate := time.Now().Format(utils.FormatDate)
  4223. if markerLine.StartDate.TimeType == 2 {
  4224. // 动态
  4225. if markerLine.StartDate.Conf.BaseDate == 1 {
  4226. // 指标最新日期
  4227. startDate = dataList[len(dataList)-1].DataTime
  4228. } else {
  4229. // 系统日期
  4230. startDate = time.Now().Format(utils.FormatDate)
  4231. }
  4232. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4233. }
  4234. if markerLine.EndDate.TimeType == 1 {
  4235. // 固定
  4236. endDate = markerLine.EndDate.Date
  4237. } else if markerLine.EndDate.TimeType == 2 {
  4238. // 动态
  4239. if markerLine.StartDate.Conf.BaseDate == 1 {
  4240. // 指标最新日期
  4241. endDate = dataList[len(dataList)-1].DataTime
  4242. } else {
  4243. // 系统日期
  4244. endDate = time.Now().Format(utils.FormatDate)
  4245. }
  4246. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4247. }
  4248. if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
  4249. floatList = append(floatList, dataItem.Value)
  4250. averge += dataItem.Value
  4251. length += 1
  4252. }
  4253. } else {
  4254. floatList = append(floatList, dataItem.Value)
  4255. averge += dataItem.Value
  4256. length += 1
  4257. }
  4258. }
  4259. averge = averge / float64(length)
  4260. stdev := utils.CalculateStandardDeviation(floatList)
  4261. stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()
  4262. value = fmt.Sprintf("%.2f", averge+stdev*markerLine.CalculationValue)
  4263. }
  4264. } else if markerLine.Calculation == 3 {
  4265. // 区间个数分位
  4266. markerLineValue := 0.0
  4267. if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
  4268. //季节性图结构体不一样
  4269. faloatList := make([]float64, 0)
  4270. quarterDataList := dataList.(data_manage.QuarterDataList)
  4271. for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
  4272. for _, vv := range quarterData.DataList {
  4273. if markerLine.TimeIntervalType == 1 {
  4274. startDate := markerLine.StartDate.Date
  4275. endDate := time.Now().Format(utils.FormatDate)
  4276. if markerLine.StartDate.TimeType == 2 {
  4277. // 动态
  4278. if markerLine.StartDate.Conf.BaseDate == 1 {
  4279. // 指标最新日期
  4280. startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4281. } else {
  4282. // 系统日期
  4283. startDate = time.Now().Format(utils.FormatDate)
  4284. }
  4285. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4286. }
  4287. if markerLine.EndDate.TimeType == 1 {
  4288. // 固定
  4289. endDate = markerLine.EndDate.Date
  4290. } else if markerLine.EndDate.TimeType == 2 {
  4291. // 动态
  4292. if markerLine.StartDate.Conf.BaseDate == 1 {
  4293. // 指标最新日期
  4294. endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4295. } else {
  4296. // 系统日期
  4297. endDate = time.Now().Format(utils.FormatDate)
  4298. }
  4299. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4300. }
  4301. if vv.DataTime >= startDate && vv.DataTime <= endDate {
  4302. faloatList = append(faloatList, vv.Value)
  4303. }
  4304. } else {
  4305. faloatList = append(faloatList, vv.Value)
  4306. }
  4307. }
  4308. sort.Float64s(faloatList)
  4309. markerLineValue = PercentileAlgorithm(markerLine.CalculationValue, faloatList)
  4310. value = fmt.Sprintf("%.2f", markerLineValue)
  4311. }
  4312. } else {
  4313. dataList := dataList.([]*data_manage.EdbDataList)
  4314. floatList := make([]float64, 0)
  4315. for _, dataItem := range dataList {
  4316. if markerLine.TimeIntervalType == 1 {
  4317. startDate := markerLine.StartDate.Date
  4318. endDate := time.Now().Format(utils.FormatDate)
  4319. if markerLine.StartDate.TimeType == 2 {
  4320. // 动态
  4321. if markerLine.StartDate.Conf.BaseDate == 1 {
  4322. // 指标最新日期
  4323. startDate = dataList[len(dataList)-1].DataTime
  4324. } else {
  4325. // 系统日期
  4326. startDate = time.Now().Format(utils.FormatDate)
  4327. }
  4328. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4329. }
  4330. if markerLine.EndDate.TimeType == 1 {
  4331. // 固定
  4332. endDate = markerLine.EndDate.Date
  4333. } else if markerLine.EndDate.TimeType == 2 {
  4334. // 动态
  4335. if markerLine.StartDate.Conf.BaseDate == 1 {
  4336. // 指标最新日期
  4337. endDate = dataList[len(dataList)-1].DataTime
  4338. } else {
  4339. // 系统日期
  4340. endDate = time.Now().Format(utils.FormatDate)
  4341. }
  4342. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4343. }
  4344. if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
  4345. floatList = append(floatList, dataItem.Value)
  4346. }
  4347. } else {
  4348. floatList = append(floatList, dataItem.Value)
  4349. }
  4350. }
  4351. sort.Float64s(floatList)
  4352. markerLineValue = PercentileAlgorithm(markerLine.CalculationValue, floatList)
  4353. value = fmt.Sprintf("%.2f", markerLineValue)
  4354. }
  4355. } else if markerLine.Calculation == 4 {
  4356. // 数值分位
  4357. markerLineValue := 0.0
  4358. maxValue := 0.0
  4359. minValue := 0.0
  4360. if chartInfo.ChartType == 2 && markerLine.EdbType == 0 {
  4361. //季节性图结构体不一样
  4362. quarterDataList := dataList.(data_manage.QuarterDataList)
  4363. for _, quarterData := range quarterDataList[len(quarterDataList)-1:] {
  4364. for _, vv := range quarterData.DataList {
  4365. if markerLine.TimeIntervalType == 1 {
  4366. startDate := markerLine.StartDate.Date
  4367. endDate := time.Now().Format(utils.FormatDate)
  4368. if markerLine.StartDate.TimeType == 2 {
  4369. // 动态
  4370. if markerLine.StartDate.Conf.BaseDate == 1 {
  4371. // 指标最新日期
  4372. startDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4373. } else {
  4374. // 系统日期
  4375. startDate = time.Now().Format(utils.FormatDate)
  4376. }
  4377. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4378. }
  4379. if markerLine.EndDate.TimeType == 1 {
  4380. // 固定
  4381. endDate = markerLine.EndDate.Date
  4382. } else if markerLine.EndDate.TimeType == 2 {
  4383. // 动态
  4384. if markerLine.StartDate.Conf.BaseDate == 1 {
  4385. // 指标最新日期
  4386. endDate = quarterData.DataList[len(quarterData.DataList)-1].DataTime
  4387. } else {
  4388. // 系统日期
  4389. endDate = time.Now().Format(utils.FormatDate)
  4390. }
  4391. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4392. }
  4393. if vv.DataTime >= startDate && vv.DataTime <= endDate {
  4394. if maxValue < vv.Value {
  4395. maxValue = vv.Value
  4396. }
  4397. if minValue > vv.Value {
  4398. minValue = vv.Value
  4399. }
  4400. }
  4401. } else {
  4402. if maxValue < vv.Value {
  4403. maxValue = vv.Value
  4404. }
  4405. if minValue > vv.Value {
  4406. minValue = vv.Value
  4407. }
  4408. }
  4409. }
  4410. markerLineValue = CalculatePercentile(markerLine.CalculationValue, minValue, maxValue)
  4411. value = fmt.Sprintf("%.2f", markerLineValue)
  4412. }
  4413. } else {
  4414. dataList := dataList.([]*data_manage.EdbDataList)
  4415. for _, dataItem := range dataList {
  4416. if markerLine.TimeIntervalType == 1 {
  4417. startDate := markerLine.StartDate.Date
  4418. endDate := time.Now().Format(utils.FormatDate)
  4419. if markerLine.StartDate.TimeType == 2 {
  4420. // 动态
  4421. if markerLine.StartDate.Conf.BaseDate == 1 {
  4422. // 指标最新日期
  4423. startDate = dataList[len(dataList)-1].DataTime
  4424. } else {
  4425. // 系统日期
  4426. startDate = time.Now().Format(utils.FormatDate)
  4427. }
  4428. startDate, err = HandleDateChange(startDate, markerLine.StartDate.Conf)
  4429. }
  4430. if markerLine.EndDate.TimeType == 1 {
  4431. // 固定
  4432. endDate = markerLine.EndDate.Date
  4433. } else if markerLine.EndDate.TimeType == 2 {
  4434. // 动态
  4435. if markerLine.StartDate.Conf.BaseDate == 1 {
  4436. // 指标最新日期
  4437. endDate = dataList[len(dataList)-1].DataTime
  4438. } else {
  4439. // 系统日期
  4440. endDate = time.Now().Format(utils.FormatDate)
  4441. }
  4442. endDate, err = HandleDateChange(endDate, markerLine.StartDate.Conf)
  4443. }
  4444. if dataItem.DataTime >= startDate && dataItem.DataTime <= endDate {
  4445. if maxValue < dataItem.Value {
  4446. maxValue = dataItem.Value
  4447. }
  4448. if minValue > dataItem.Value {
  4449. minValue = dataItem.Value
  4450. }
  4451. }
  4452. } else {
  4453. if maxValue < dataItem.Value {
  4454. maxValue = dataItem.Value
  4455. }
  4456. if minValue > dataItem.Value {
  4457. minValue = dataItem.Value
  4458. }
  4459. }
  4460. }
  4461. markerLineValue = CalculatePercentile(markerLine.CalculationValue, minValue, maxValue)
  4462. value = fmt.Sprintf("%.2f", markerLineValue)
  4463. }
  4464. }
  4465. return
  4466. }
  4467. // HandleDateChange 处理日期变换
  4468. func HandleDateChange(date string, edbDateConf data_manage.EdbDateChangeConf) (newDate string, err error) {
  4469. newDate = date
  4470. if newDate != "" {
  4471. if len(edbDateConf.DateChange) > 0 {
  4472. var dateTime time.Time
  4473. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  4474. if err != nil {
  4475. err = fmt.Errorf("日期解析失败: %s", err.Error())
  4476. return
  4477. }
  4478. for _, v := range edbDateConf.DateChange {
  4479. if v.ChangeType == 1 {
  4480. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  4481. newDate = dateTime.Format(utils.FormatDate)
  4482. } else if v.ChangeType == 2 {
  4483. newDate, err, _ = handleSystemAppointDateT2(dateTime, v.FrequencyDay, v.Frequency)
  4484. if err != nil {
  4485. return
  4486. }
  4487. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  4488. if err != nil {
  4489. err = fmt.Errorf("日期解析失败: %s", err.Error())
  4490. return
  4491. }
  4492. }
  4493. }
  4494. }
  4495. }
  4496. return
  4497. }
  4498. // handleSystemAppointDateT
  4499. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  4500. // @author: Roc
  4501. // @datetime2023-10-27 09:31:35
  4502. // @param Frequency string
  4503. // @param Day string
  4504. // @return date string
  4505. // @return err error
  4506. // @return errMsg string
  4507. func handleSystemAppointDateT2(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  4508. //currDate := time.Now()
  4509. switch frequency {
  4510. case "本周":
  4511. day := int(currDate.Weekday())
  4512. if day == 0 { // 周日
  4513. day = 7
  4514. }
  4515. num := 0
  4516. switch appointDay {
  4517. case "周一":
  4518. num = 1
  4519. case "周二":
  4520. num = 2
  4521. case "周三":
  4522. num = 3
  4523. case "周四":
  4524. num = 4
  4525. case "周五":
  4526. num = 5
  4527. case "周六":
  4528. num = 6
  4529. case "周日":
  4530. num = 7
  4531. }
  4532. day = num - day
  4533. date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
  4534. case "本旬":
  4535. day := currDate.Day()
  4536. var tmpDate time.Time
  4537. switch appointDay {
  4538. case "第一天":
  4539. if day <= 10 {
  4540. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  4541. } else if day <= 20 {
  4542. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  4543. } else {
  4544. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  4545. }
  4546. case "最后一天":
  4547. if day <= 10 {
  4548. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  4549. } else if day <= 20 {
  4550. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  4551. } else {
  4552. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  4553. }
  4554. }
  4555. date = tmpDate.Format(utils.FormatDate)
  4556. case "本月":
  4557. var tmpDate time.Time
  4558. switch appointDay {
  4559. case "第一天":
  4560. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  4561. case "最后一天":
  4562. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  4563. }
  4564. date = tmpDate.Format(utils.FormatDate)
  4565. case "本季":
  4566. month := currDate.Month()
  4567. var tmpDate time.Time
  4568. switch appointDay {
  4569. case "第一天":
  4570. if month <= 3 {
  4571. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  4572. } else if month <= 6 {
  4573. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  4574. } else if month <= 9 {
  4575. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  4576. } else {
  4577. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  4578. }
  4579. case "最后一天":
  4580. if month <= 3 {
  4581. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  4582. } else if month <= 6 {
  4583. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  4584. } else if month <= 9 {
  4585. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  4586. } else {
  4587. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  4588. }
  4589. }
  4590. date = tmpDate.Format(utils.FormatDate)
  4591. case "本半年":
  4592. month := currDate.Month()
  4593. var tmpDate time.Time
  4594. switch appointDay {
  4595. case "第一天":
  4596. if month <= 6 {
  4597. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  4598. } else {
  4599. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  4600. }
  4601. case "最后一天":
  4602. if month <= 6 {
  4603. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  4604. } else {
  4605. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  4606. }
  4607. }
  4608. date = tmpDate.Format(utils.FormatDate)
  4609. case "本年":
  4610. var tmpDate time.Time
  4611. switch appointDay {
  4612. case "第一天":
  4613. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  4614. case "最后一天":
  4615. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  4616. }
  4617. date = tmpDate.Format(utils.FormatDate)
  4618. default:
  4619. errMsg = "错误的日期频度:" + frequency
  4620. err = errors.New(errMsg)
  4621. return
  4622. }
  4623. return
  4624. }
  4625. // CalculatePercentile 计算数值分位
  4626. func CalculatePercentile(x float64, min float64, max float64) float64 {
  4627. return (x/100)*(max-min) + min
  4628. }
  4629. // getEdbDataMapListForSeason 获取指标最后的基础数据-插值补充好数据-季节性图用
  4630. 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) {
  4631. // 指标对应的所有数据
  4632. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  4633. for _, v := range mappingList {
  4634. //fmt.Println("v:", v.EdbInfoId)
  4635. item := new(data_manage.ChartEdbInfoMapping)
  4636. item.EdbInfoId = v.EdbInfoId
  4637. item.SourceName = v.SourceName
  4638. item.Source = v.Source
  4639. item.EdbCode = v.EdbCode
  4640. item.EdbName = v.EdbName
  4641. item.EdbNameEn = v.EdbNameEn
  4642. item.Frequency = v.Frequency
  4643. item.EdbType = v.EdbType
  4644. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  4645. if v.Unit != `无` {
  4646. item.Unit = v.Unit
  4647. }
  4648. item.UnitEn = v.UnitEn
  4649. item.StartDate = v.StartDate
  4650. item.EndDate = v.EndDate
  4651. item.ModifyTime = v.ModifyTime
  4652. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  4653. item.PredictChartColor = v.PredictChartColor
  4654. item.ClassifyId = v.ClassifyId
  4655. if chartInfoId <= 0 {
  4656. item.IsAxis = 1
  4657. item.LeadValue = 0
  4658. item.LeadUnit = ""
  4659. item.ChartEdbMappingId = 0
  4660. item.ChartInfoId = 0
  4661. item.IsOrder = false
  4662. item.EdbInfoType = 1
  4663. item.ChartStyle = ""
  4664. item.ChartColor = ""
  4665. item.ChartWidth = 0
  4666. item.MaxData = v.MaxValue
  4667. item.MinData = v.MinValue
  4668. } else {
  4669. item.IsAxis = v.IsAxis
  4670. item.EdbInfoType = v.EdbInfoType
  4671. item.LeadValue = v.LeadValue
  4672. item.LeadUnit = v.LeadUnit
  4673. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  4674. item.ChartEdbMappingId = v.ChartEdbMappingId
  4675. item.ChartInfoId = v.ChartInfoId
  4676. item.ChartStyle = v.ChartStyle
  4677. item.ChartColor = v.ChartColor
  4678. item.ChartWidth = v.ChartWidth
  4679. item.IsOrder = v.IsOrder
  4680. item.MaxData = v.MaxData
  4681. item.MinData = v.MinData
  4682. }
  4683. item.LatestValue = v.LatestValue
  4684. item.LatestDate = v.LatestDate
  4685. item.UniqueCode = v.UniqueCode
  4686. item.MoveLatestDate = v.LatestDate
  4687. item.EdbAliasName = v.EdbAliasName
  4688. item.IsConvert = v.IsConvert
  4689. item.ConvertType = v.ConvertType
  4690. item.ConvertValue = v.ConvertValue
  4691. item.ConvertUnit = v.ConvertUnit
  4692. item.ConvertEnUnit = v.ConvertEnUnit
  4693. item.IsJoinPermission = v.IsJoinPermission
  4694. var startDateReal string
  4695. var diffSeconds int64
  4696. if chartType == 2 { //季节性图
  4697. startDateReal = startDate
  4698. if len(mappingList) > 1 {
  4699. item.IsAxis = v.IsAxis
  4700. }
  4701. } else {
  4702. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  4703. var startTimeRealTemp time.Time
  4704. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  4705. switch v.LeadUnit {
  4706. case "天":
  4707. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  4708. case "月":
  4709. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  4710. case "季":
  4711. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  4712. case "周":
  4713. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  4714. case "年":
  4715. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  4716. }
  4717. if startTimeRealTemp.Before(startDateParse) {
  4718. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  4719. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  4720. } else {
  4721. startDateReal = startDate
  4722. diffSeconds = 0
  4723. }
  4724. // 预测指标的开始日期也要偏移
  4725. {
  4726. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  4727. if tmpErr != nil {
  4728. err = tmpErr
  4729. return
  4730. }
  4731. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  4732. if tmpErr != nil {
  4733. err = tmpErr
  4734. return
  4735. }
  4736. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  4737. }
  4738. } else {
  4739. startDateReal = startDate
  4740. }
  4741. }
  4742. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  4743. calendarPreYear := 0
  4744. if calendar == "农历" {
  4745. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  4746. if err != nil {
  4747. fmt.Println("time.Parse:" + err.Error())
  4748. }
  4749. calendarPreYear = newStartDateReal.Year() - 1
  4750. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  4751. startDateReal = newStartDateReal.Format(utils.FormatDate)
  4752. }
  4753. dataList := make([]*data_manage.EdbDataList, 0)
  4754. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  4755. //fmt.Println("calendarPreYear:", calendarPreYear)
  4756. //var newEdbInfo *data_manage.EdbInfo
  4757. switch v.EdbInfoCategoryType {
  4758. case 0:
  4759. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  4760. case 1:
  4761. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  4762. default:
  4763. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  4764. }
  4765. if err != nil {
  4766. return
  4767. }
  4768. handleDataMap := make(map[string]float64)
  4769. newDataList, _, _, e := HandleDataByLinearRegressionToListV3(dataList, handleDataMap)
  4770. if e != nil {
  4771. err = e
  4772. return
  4773. }
  4774. if v.IsConvert == 1 {
  4775. switch v.ConvertType {
  4776. case 1:
  4777. for i, data := range newDataList {
  4778. newDataList[i].Value = data.Value * v.ConvertValue
  4779. }
  4780. //item.MaxData = item.MaxData * v.ConvertValue
  4781. //item.MinData = item.MinData * v.ConvertValue
  4782. case 2:
  4783. for i, data := range newDataList {
  4784. newDataList[i].Value = data.Value / v.ConvertValue
  4785. }
  4786. //item.MaxData = item.MaxData / v.ConvertValue
  4787. //item.MinData = item.MinData / v.ConvertValue
  4788. case 3:
  4789. for i, data := range newDataList {
  4790. if data.Value <= 0 {
  4791. err = errors.New("数据中含有负数或0,无法对数运算")
  4792. return
  4793. }
  4794. newDataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  4795. }
  4796. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  4797. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  4798. }
  4799. }
  4800. edbDataListMap[v.EdbInfoId] = newDataList
  4801. if diffSeconds != 0 && v.EdbInfoType == 0 {
  4802. newDataListLen := len(newDataList)
  4803. for i := 0; i < newDataListLen; i++ {
  4804. newDataList[i].DataTimestamp = newDataList[i].DataTimestamp - diffSeconds
  4805. }
  4806. }
  4807. if chartType == 2 && item.IsAxis == 1 {
  4808. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  4809. if tmpErr != nil {
  4810. //item.DataList = newDataList
  4811. item.IsNullData = true
  4812. edbList = append(edbList, item)
  4813. continue
  4814. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  4815. return
  4816. }
  4817. if calendar == "农历" {
  4818. if len(newDataList) <= 0 {
  4819. result := new(data_manage.EdbDataResult)
  4820. item.DataList = result
  4821. } else {
  4822. result, tmpErr := data_manage.AddCalculateQuarterV6(newDataList)
  4823. if tmpErr != nil {
  4824. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  4825. return
  4826. }
  4827. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  4828. if tErr != nil {
  4829. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  4830. return
  4831. }
  4832. item.DataList = quarterDataList
  4833. }
  4834. } else {
  4835. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(newDataList, latestDate, seasonExtraConfig)
  4836. if tErr != nil {
  4837. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  4838. return
  4839. }
  4840. item.DataList = quarterDataList
  4841. }
  4842. } else if chartType == 2 && item.IsAxis == 0 {
  4843. // 右轴数据处理
  4844. xStartDate := "01-01"
  4845. jumpYear := 0
  4846. var seasonExtra data_manage.SeasonExtraItem
  4847. if seasonExtraConfig != "" {
  4848. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  4849. if err != nil {
  4850. return
  4851. }
  4852. }
  4853. if seasonExtra.XStartDate != "" {
  4854. xStartDate = seasonExtra.XStartDate
  4855. jumpYear = seasonExtra.JumpYear
  4856. }
  4857. length := len(newDataList)
  4858. if length == 0 {
  4859. return
  4860. }
  4861. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  4862. if tmpErr != nil {
  4863. //item.DataList = newDataList
  4864. item.IsNullData = true
  4865. edbList = append(edbList, item)
  4866. continue
  4867. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  4868. return
  4869. }
  4870. var rightAxisDate time.Time
  4871. if jumpYear == 1 {
  4872. latestDate = latestDate.AddDate(-1, 0, 0)
  4873. }
  4874. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
  4875. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  4876. if err != nil {
  4877. return
  4878. }
  4879. newList := make([]*data_manage.EdbDataList, 0)
  4880. for _, v := range newDataList {
  4881. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  4882. if e != nil {
  4883. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  4884. return
  4885. }
  4886. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  4887. newList = append(newList, v)
  4888. }
  4889. }
  4890. item.DataList = newList
  4891. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  4892. //item.DataList = newDataList
  4893. } else {
  4894. item.DataList = newDataList
  4895. }
  4896. edbList = append(edbList, item)
  4897. }
  4898. return
  4899. }