chart_info.go 139 KB

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