chart_info.go 138 KB

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