chart_info.go 124 KB

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