chart_info.go 156 KB

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