chart_info.go 138 KB

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