chart_info.go 126 KB

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