chart_info.go 155 KB

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