chart_info.go 134 KB

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