chart_info.go 168 KB

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