chart_info.go 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306
  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.ChartInfoMore
  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.ChartInfo
  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. tmp.SearchText = v.SearchText
  2129. if tmp.SearchText == "" {
  2130. tmp.SearchText = v.ChartName
  2131. }
  2132. finalList = append(finalList, tmp)
  2133. }
  2134. }
  2135. //新增搜索词记录
  2136. {
  2137. searchKeyword := new(data_manage.SearchKeyword)
  2138. searchKeyword.KeyWord = keyword
  2139. searchKeyword.CreateTime = time.Now()
  2140. go data_manage.AddSearchKeyword(searchKeyword)
  2141. }
  2142. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2143. resp := data_manage.ChartInfoListByEsResp{
  2144. Paging: page,
  2145. List: finalList,
  2146. }
  2147. br.Ret = 200
  2148. br.Success = true
  2149. br.Msg = "获取成功"
  2150. br.Data = resp
  2151. }
  2152. // @Title 图表刷新接口
  2153. // @Description 图表刷新接口
  2154. // @Param ChartInfoId query int true "图表id"
  2155. // @Param UniqueCode query string true "唯一code"
  2156. // @Success Ret=200 刷新成功
  2157. // @router /chart_info/refresh [get]
  2158. func (this *EdbInfoController) ChartInfoRefresh() {
  2159. br := new(models.BaseResponse).Init()
  2160. defer func() {
  2161. this.Data["json"] = br
  2162. this.ServeJSON()
  2163. }()
  2164. sysUser := this.SysUser
  2165. if sysUser == nil {
  2166. br.Msg = "请登录"
  2167. br.ErrMsg = "请登录,SysUser Is Empty"
  2168. br.Ret = 408
  2169. return
  2170. }
  2171. chartInfoId, _ := this.GetInt("ChartInfoId")
  2172. uniqueCode := this.GetString("UniqueCode")
  2173. if chartInfoId <= 0 && uniqueCode == `` {
  2174. br.Msg = "参数错误"
  2175. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  2176. return
  2177. }
  2178. var chartInfo *data_manage.ChartInfo
  2179. var err error
  2180. if chartInfoId > 0 {
  2181. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  2182. } else {
  2183. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  2184. }
  2185. if err != nil {
  2186. if err.Error() == utils.ErrNoRow() {
  2187. br.Msg = "图表已被删除,无需刷新"
  2188. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  2189. return
  2190. }
  2191. br.Msg = "刷新失败"
  2192. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2193. return
  2194. }
  2195. err, isAsync := data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  2196. if err != nil {
  2197. br.Msg = "刷新失败"
  2198. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2199. return
  2200. }
  2201. /*
  2202. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  2203. //if err != nil {
  2204. // br.Msg = "刷新失败"
  2205. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2206. // return
  2207. //}
  2208. */
  2209. //清除图表缓存
  2210. {
  2211. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  2212. _ = utils.Rc.Delete(key)
  2213. }
  2214. br.Ret = 200
  2215. br.Success = true
  2216. br.Msg = "刷新成功"
  2217. if isAsync {
  2218. br.Msg = "图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2219. }
  2220. }
  2221. // @Title 设置图表图片
  2222. // @Description 设置图表图片接口
  2223. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  2224. // @Success Ret=200 保存成功
  2225. // @router /chart_info/image/set [post]
  2226. func (this *ChartInfoController) ChartInfoImageSet() {
  2227. br := new(models.BaseResponse).Init()
  2228. defer func() {
  2229. this.Data["json"] = br
  2230. this.ServeJSON()
  2231. }()
  2232. sysUser := this.SysUser
  2233. if sysUser == nil {
  2234. br.Msg = "请登录"
  2235. br.ErrMsg = "请登录,SysUser Is Empty"
  2236. br.Ret = 408
  2237. return
  2238. }
  2239. var req data_manage.SetChartInfoImageReq
  2240. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2241. if err != nil {
  2242. br.Msg = "参数解析异常!"
  2243. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2244. return
  2245. }
  2246. if req.ChartInfoId > 0 && req.ImageUrl != "" {
  2247. err = data_manage.EditChartInfoImage(&req)
  2248. if err != nil {
  2249. br.Msg = "保存失败"
  2250. br.ErrMsg = "保存失败,Err:" + err.Error()
  2251. return
  2252. }
  2253. //修改es数据
  2254. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  2255. //修改my eta es数据
  2256. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  2257. }
  2258. br.Ret = 200
  2259. br.Success = true
  2260. br.Msg = "保存成功"
  2261. }
  2262. // ChartInfoDetailFromUniqueCode
  2263. // @Title 根据编码获取图表详情
  2264. // @Description 根据编码获取图表详情接口
  2265. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  2266. // @Param IsCache query bool true "是否走缓存,默认false"
  2267. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  2268. // @router /chart_info/detail/from_unique_code [get]
  2269. func (this *ChartInfoController) ChartInfoDetailFromUniqueCode() {
  2270. br := new(models.BaseResponse).Init()
  2271. defer func() {
  2272. this.Data["json"] = br
  2273. this.ServeJSON()
  2274. }()
  2275. sysUser := this.SysUser
  2276. if sysUser == nil {
  2277. br.Msg = "请登录"
  2278. br.ErrMsg = "请登录,SysUser Is Empty"
  2279. br.Ret = 408
  2280. return
  2281. }
  2282. uniqueCode := this.GetString("UniqueCode")
  2283. if uniqueCode == "" {
  2284. br.Msg = "参数错误"
  2285. br.ErrMsg = "参数错误,uniqueCode is empty"
  2286. return
  2287. }
  2288. //是否走缓存
  2289. isCache, _ := this.GetBool("IsCache")
  2290. status := true
  2291. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  2292. if err != nil {
  2293. if err.Error() == utils.ErrNoRow() {
  2294. status = false
  2295. } else {
  2296. br.Msg = "获取失败"
  2297. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2298. return
  2299. }
  2300. }
  2301. if chartInfo == nil {
  2302. status = false
  2303. }
  2304. if !status {
  2305. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2306. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  2307. resp.EdbInfoList = endInfoList
  2308. resp.ChartInfo = chartInfo
  2309. resp.Status = false
  2310. return
  2311. }
  2312. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  2313. if !isOk {
  2314. br.Msg = msg
  2315. br.ErrMsg = errMsg
  2316. return
  2317. }
  2318. br.Ret = 200
  2319. br.Success = true
  2320. msg = "获取成功"
  2321. br.Data = resp
  2322. }
  2323. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  2324. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2325. msg = `获取失败`
  2326. adminId := sysUser.AdminId
  2327. // 图表数据权限
  2328. // 图表分类
  2329. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  2330. if err != nil {
  2331. errMsg = "获取图表分类信息失败,Err:" + err.Error()
  2332. return
  2333. }
  2334. // 已授权分类id
  2335. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  2336. if err != nil {
  2337. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  2338. return
  2339. }
  2340. // 指标数据map
  2341. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  2342. defer func() {
  2343. if isOk {
  2344. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  2345. {
  2346. //判断是否加入我的图库
  2347. var myChartCondition string
  2348. var myChartPars []interface{}
  2349. myChartCondition += ` AND a.admin_id=? `
  2350. myChartPars = append(myChartPars, adminId)
  2351. myChartCondition += ` AND a.chart_info_id=? `
  2352. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  2353. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  2354. if err != nil && err.Error() != utils.ErrNoRow() {
  2355. msg = "获取失败"
  2356. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  2357. return
  2358. }
  2359. if myChartList != nil && len(myChartList) > 0 {
  2360. chartInfo.IsAdd = true
  2361. chartInfo.MyChartId = myChartList[0].MyChartId
  2362. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  2363. }
  2364. }
  2365. // 图表权限校验
  2366. {
  2367. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2368. //图表操作权限
  2369. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  2370. chartInfo.Button = data_manage.ChartViewButton{
  2371. IsEdit: chartInfo.IsEdit,
  2372. IsEnChart: chartInfo.IsEnChart,
  2373. IsAdd: chartInfo.IsAdd,
  2374. IsCopy: true,
  2375. IsSetName: chartInfo.IsSetName,
  2376. }
  2377. //if !resp.ChartInfo.HaveOperaAuth {
  2378. // for _, v := range resp.EdbInfoList {
  2379. // v.DataList = nil
  2380. // }
  2381. // resp.DataResp = nil
  2382. // resp.XEdbIdValue = []int{}
  2383. // resp.YDataList = []data_manage.YData{}
  2384. //}
  2385. }
  2386. // 指标权限
  2387. {
  2388. classifyMap := make(map[int]*data_manage.EdbClassify)
  2389. // 分类
  2390. {
  2391. classifyIdList := make([]int, 0)
  2392. for _, v := range resp.EdbInfoList {
  2393. classifyIdList = append(classifyIdList, v.ClassifyId)
  2394. }
  2395. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2396. if tmpErr != nil {
  2397. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  2398. return
  2399. }
  2400. for _, v := range classifyList {
  2401. classifyMap[v.ClassifyId] = v
  2402. }
  2403. }
  2404. // 指标
  2405. if len(edbClassifyPermissionMap) < 0 {
  2406. edbInfoIdList := make([]int, 0)
  2407. for _, v := range resp.EdbInfoList {
  2408. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  2409. }
  2410. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2411. if tmpErr != nil {
  2412. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  2413. return
  2414. }
  2415. for _, v := range edbInfoList {
  2416. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2417. ClassifyId: v.ClassifyId,
  2418. IsJoinPermission: v.IsJoinPermission,
  2419. EdbInfoId: v.EdbInfoId,
  2420. }
  2421. }
  2422. }
  2423. // 获取所有有权限的指标和分类
  2424. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2425. if err != nil {
  2426. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2427. return
  2428. }
  2429. for _, v := range resp.EdbInfoList {
  2430. // 数据权限
  2431. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  2432. if !ok {
  2433. continue
  2434. }
  2435. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  2436. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2437. }
  2438. }
  2439. }
  2440. }
  2441. }()
  2442. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  2443. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  2444. if utils.Re == nil && isCache {
  2445. if utils.Re == nil && utils.Rc.IsExist(key) {
  2446. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  2447. err := json.Unmarshal(redisData, &resp)
  2448. if err != nil || resp == nil {
  2449. return
  2450. }
  2451. isOk = true
  2452. fmt.Println("source redis")
  2453. return
  2454. }
  2455. }
  2456. }
  2457. // 获取主题样式
  2458. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  2459. if err != nil {
  2460. msg = "获取失败"
  2461. errMsg = "获取主题信息失败,Err:" + err.Error()
  2462. return
  2463. }
  2464. chartInfo.ChartThemeStyle = chartTheme.Config
  2465. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  2466. chartInfoId := chartInfo.ChartInfoId
  2467. dateType := chartInfo.DateType
  2468. startDate := chartInfo.StartDate
  2469. endDate := chartInfo.EndDate
  2470. startYear := chartInfo.StartYear
  2471. calendar := chartInfo.Calendar
  2472. chartType := chartInfo.ChartType
  2473. if calendar == "" {
  2474. calendar = "公历"
  2475. }
  2476. fmt.Println("a dateType:", dateType)
  2477. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  2478. if err != nil {
  2479. msg = "获取失败"
  2480. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2481. return
  2482. }
  2483. yearMax := 0
  2484. if dateType == utils.DateTypeNYears {
  2485. for _, v := range mappingList {
  2486. if v.LatestDate != "" {
  2487. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  2488. if tErr != nil {
  2489. msg = "获取失败"
  2490. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  2491. return
  2492. }
  2493. if lastDateT.Year() > yearMax {
  2494. yearMax = lastDateT.Year()
  2495. }
  2496. }
  2497. }
  2498. }
  2499. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  2500. extraConfigStr := chartInfo.ExtraConfig //图表额外数据参数
  2501. var barConfig data_manage.BarChartInfoReq
  2502. // 柱方图的一些配置
  2503. if chartInfo != nil && chartInfo.ChartType == 7 {
  2504. if chartInfo.BarConfig == `` {
  2505. msg = "柱方图未配置"
  2506. errMsg = "柱方图未配置"
  2507. return
  2508. }
  2509. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  2510. if err != nil {
  2511. msg = "柱方图配置异常"
  2512. errMsg = "柱方图配置异常"
  2513. return
  2514. }
  2515. extraConfigStr = chartInfo.BarConfig
  2516. }
  2517. // 获取图表中的指标数据
  2518. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  2519. if err != nil {
  2520. msg = "获取失败"
  2521. if errMsg != `` {
  2522. msg = errMsg
  2523. }
  2524. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2525. return
  2526. }
  2527. // 单位
  2528. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  2529. chartInfo.Unit = yDataList[0].Unit
  2530. chartInfo.UnitEn = yDataList[0].UnitEn
  2531. }
  2532. warnEdbList := make([]string, 0)
  2533. for _, v := range edbList {
  2534. if v.IsNullData {
  2535. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  2536. }
  2537. }
  2538. if len(warnEdbList) > 0 {
  2539. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  2540. }
  2541. /*
  2542. ChartClassify []*ChartClassifyView
  2543. */
  2544. chartViewClassify, _ := data_manage.GetChartClassifyViewById(chartInfo.ChartClassifyId)
  2545. if chartViewClassify != nil {
  2546. chartClassifyParent, _ := data_manage.GetChartClassifyViewById(chartClassify.ParentId)
  2547. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
  2548. }
  2549. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartViewClassify)
  2550. // 面积图 面积堆积 数据处理
  2551. if chartInfo.ChartType == utils.CHART_TYPE_AREA {
  2552. err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
  2553. if err != nil {
  2554. msg = "获取失败"
  2555. errMsg = "面积图面积堆积数据处理失败,Err:" + err.Error()
  2556. return
  2557. }
  2558. }
  2559. resp.EdbInfoList = edbList
  2560. //判断是否需要展示英文标识
  2561. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  2562. // 图表的指标来源
  2563. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  2564. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  2565. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  2566. resp.ChartInfo = chartInfo
  2567. resp.XEdbIdValue = xEdbIdValue
  2568. resp.YDataList = yDataList
  2569. resp.Status = true
  2570. resp.BarChartInfo = barConfig
  2571. resp.DataResp = dataResp
  2572. // 将数据加入缓存
  2573. if utils.Re == nil {
  2574. data, _ := json.Marshal(resp)
  2575. utils.Rc.Put(key, data, 2*time.Hour)
  2576. }
  2577. isOk = true
  2578. // 遍历到校验map
  2579. for _, v := range edbList {
  2580. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2581. ClassifyId: v.ClassifyId,
  2582. IsJoinPermission: v.IsJoinPermission,
  2583. EdbInfoId: v.EdbInfoId,
  2584. }
  2585. }
  2586. return
  2587. }
  2588. // @Param Img query string true "图片"
  2589. // @Success 200 {object} models.ResourceResp
  2590. // @router /chart_info/base64Upload [post]
  2591. func (this *ChartInfoController) ChartInfoBase64Upload() {
  2592. br := new(models.BaseResponse).Init()
  2593. defer func() {
  2594. this.Data["json"] = br
  2595. this.ServeJSON()
  2596. }()
  2597. imgData := this.GetString("Img")
  2598. if imgData == "" {
  2599. br.Msg = "图片参数错误"
  2600. br.ErrMsg = "图片参数错误,Img Is Empty"
  2601. return
  2602. }
  2603. resp := new(models.ResourceResp)
  2604. uploadDir := "static/images/"
  2605. if !utils.FileIsExist(uploadDir) {
  2606. err := os.MkdirAll(uploadDir, utils.DIR_MOD)
  2607. if err != nil {
  2608. br.Msg = "图表保存失败"
  2609. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  2610. return
  2611. }
  2612. }
  2613. //var saveToOssPath string
  2614. randStr := utils.GetRandStringNoSpecialChar(28)
  2615. var fileName, outFileName string
  2616. fileName = randStr + ".txt"
  2617. fileName = uploadDir + fileName
  2618. err := utils.SaveToFile(imgData, fileName)
  2619. if err != nil {
  2620. br.Msg = "图片保存失败"
  2621. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  2622. return
  2623. }
  2624. outFileName = randStr + ".png"
  2625. doneChannel := make(chan bool, 1)
  2626. errorChannel := make(chan error, 1)
  2627. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  2628. go func() {
  2629. output, err := cmd.CombinedOutput()
  2630. if err != nil {
  2631. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  2632. errorChannel <- err
  2633. return
  2634. }
  2635. doneChannel <- true
  2636. }()
  2637. select {
  2638. case <-time.After(30 * time.Second):
  2639. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  2640. e := cmd.Process.Kill()
  2641. if e != nil {
  2642. fmt.Println("cmd kill err: ", e.Error())
  2643. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  2644. br.Msg = "图片生成失败"
  2645. br.ErrMsg = "图片生成失败, 执行超时" + e.Error()
  2646. return
  2647. }
  2648. fmt.Println("timeout kill process")
  2649. case <-doneChannel:
  2650. fmt.Println("done")
  2651. case err := <-errorChannel:
  2652. br.Msg = "文件上传失败"
  2653. br.ErrMsg = fmt.Sprintf("execute command failure err: %s", err.Error())
  2654. fmt.Println("execute command failure err:" + err.Error())
  2655. return
  2656. }
  2657. defer func() {
  2658. os.Remove(fileName)
  2659. os.Remove(outFileName)
  2660. }()
  2661. //saveToOssPath = uploadDir + time.Now().Format("200601/20060102/")
  2662. //saveToOssPath += outFileName
  2663. //上传到阿里云 和 minio
  2664. resourceUrl := ``
  2665. //if utils.ObjectStorageClient == "minio" {
  2666. // err = services.UploadFileToMinIo("", outFileName, saveToOssPath)
  2667. // if err != nil {
  2668. // br.Msg = "文件上传失败"
  2669. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2670. // return
  2671. // }
  2672. // resourceUrl = utils.MinIoImghost + saveToOssPath
  2673. //} else {
  2674. // err = services.UploadFileToAliyun("", outFileName, saveToOssPath)
  2675. // if err != nil {
  2676. // br.Msg = "文件上传失败"
  2677. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2678. // return
  2679. // }
  2680. // resourceUrl = utils.Imghost + saveToOssPath
  2681. //}
  2682. ossClient := services.NewOssClient()
  2683. if ossClient == nil {
  2684. br.Msg = "上传失败"
  2685. br.ErrMsg = "初始化OSS服务失败"
  2686. return
  2687. }
  2688. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  2689. if err != nil {
  2690. br.Msg = "文件上传失败"
  2691. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2692. return
  2693. }
  2694. resp.ResourceUrl = resourceUrl
  2695. resp.Source = "convert"
  2696. //resp.CacheKey = imgDataKey
  2697. br.Msg = "上传成功"
  2698. br.Ret = 200
  2699. br.Success = true
  2700. br.Data = resp
  2701. return
  2702. }
  2703. // BatchChartInfoRefresh
  2704. // @Title 批量刷新图表接口
  2705. // @Description 批量刷新图表接口
  2706. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  2707. // @Success Ret=200 刷新成功
  2708. // @router /chart_info/batch_refresh [post]
  2709. func (this *EdbInfoController) BatchChartInfoRefresh() {
  2710. br := new(models.BaseResponse).Init()
  2711. defer func() {
  2712. this.Data["json"] = br
  2713. this.ServeJSON()
  2714. }()
  2715. sysUser := this.SysUser
  2716. if sysUser == nil {
  2717. br.Msg = "请登录"
  2718. br.ErrMsg = "请登录,SysUser Is Empty"
  2719. br.Ret = 408
  2720. return
  2721. }
  2722. var req data_manage.BatchChartRefreshReq
  2723. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2724. if err != nil {
  2725. br.Msg = "参数解析异常!"
  2726. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2727. return
  2728. }
  2729. //获取所有的图表列表
  2730. _, chartInfoList, err := data_manage.GetChartInfoListByUniqueCodeSlice(req.ChartInfoCode)
  2731. if len(chartInfoList) <= 0 {
  2732. br.Ret = 200
  2733. br.Success = true
  2734. br.Msg = "刷新成功"
  2735. return
  2736. }
  2737. redisKey := data.GetBatchChartRefreshKey(req.Source, req.ReportId, req.ReportChapterId)
  2738. // 图表中的指标刷新
  2739. err, isAsync := data.BatchChartInfoRefreshV2(chartInfoList, redisKey)
  2740. if err != nil {
  2741. br.Msg = "刷新失败"
  2742. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2743. return
  2744. }
  2745. br.Msg = "刷新成功"
  2746. if isAsync {
  2747. br.Msg = "研报中图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2748. }
  2749. br.Ret = 200
  2750. br.Success = true
  2751. }
  2752. // CopyChartInfo
  2753. // @Title 复制并新增图表接口
  2754. // @Description 新增图表接口
  2755. // @Param request body data_manage.CopyAddChartInfoReq true "type json string"
  2756. // @Success 200 {object} data_manage.AddChartInfoResp
  2757. // @router /chart_info/copy [post]
  2758. func (this *ChartInfoController) CopyChartInfo() {
  2759. br := new(models.BaseResponse).Init()
  2760. defer func() {
  2761. this.Data["json"] = br
  2762. this.ServeJSON()
  2763. }()
  2764. sysUser := this.SysUser
  2765. if sysUser == nil {
  2766. br.Msg = "请登录"
  2767. br.ErrMsg = "请登录,SysUser Is Empty"
  2768. br.Ret = 408
  2769. return
  2770. }
  2771. deleteCache := true
  2772. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  2773. defer func() {
  2774. if deleteCache {
  2775. utils.Rc.Delete(cacheKey)
  2776. }
  2777. }()
  2778. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2779. deleteCache = false
  2780. br.Msg = "系统处理中,请稍后重试!"
  2781. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2782. return
  2783. }
  2784. var req data_manage.CopyAddChartInfoReq
  2785. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2786. if err != nil {
  2787. br.Msg = "参数解析异常!"
  2788. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2789. return
  2790. }
  2791. // 获取原图表信息
  2792. oldChartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2793. if err != nil {
  2794. br.Msg = "获取原图表信息失败"
  2795. br.ErrMsg = "获取原图表信息失败,Err:" + err.Error()
  2796. return
  2797. }
  2798. //校验图表名称是否重复
  2799. req.ChartName = strings.Trim(req.ChartName, " ")
  2800. if req.ChartName == "" {
  2801. br.Msg = "请填写图表名称!"
  2802. return
  2803. }
  2804. if req.ChartClassifyId <= 0 {
  2805. br.Msg = "分类参数错误!"
  2806. return
  2807. }
  2808. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2809. if err != nil {
  2810. if err.Error() == utils.ErrNoRow() {
  2811. br.Msg = "分类不存在"
  2812. br.ErrMsg = "分类不存在"
  2813. return
  2814. }
  2815. br.Msg = "获取分类信息失败"
  2816. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  2817. return
  2818. }
  2819. if chartClassify == nil {
  2820. br.Msg = "分类不存在"
  2821. br.ErrMsg = "分类不存在"
  2822. return
  2823. }
  2824. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  2825. //if err != nil && err.Error() != utils.ErrNoRow() {
  2826. // br.Msg = "保存失败"
  2827. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2828. // return
  2829. //}
  2830. //if count > 0 {
  2831. // br.Msg = "所选指标已存在相同指标"
  2832. // return
  2833. //}
  2834. //判断图表是否存在
  2835. var condition string
  2836. var pars []interface{}
  2837. condition += " AND chart_classify_id=? "
  2838. pars = append(pars, req.ChartClassifyId)
  2839. condition += " AND chart_name=? AND source = ? "
  2840. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2841. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  2842. if err != nil {
  2843. br.Msg = "判断图表名称是否存在失败"
  2844. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2845. return
  2846. }
  2847. if count > 0 {
  2848. br.Msg = "图表已存在,请重新填写"
  2849. br.IsSendEmail = false
  2850. return
  2851. }
  2852. //获取原图表关联的指标信息列表
  2853. edbMappingList, err := data_manage.GetChartEdbMappingList(req.ChartInfoId)
  2854. if err != nil {
  2855. br.Msg = "获取图表关联的指标信息失败!"
  2856. return
  2857. }
  2858. //添加图表
  2859. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2860. chartInfo := &data_manage.ChartInfo{
  2861. ChartInfoId: 0,
  2862. ChartName: req.ChartName,
  2863. ChartClassifyId: req.ChartClassifyId,
  2864. SysUserId: sysUser.AdminId,
  2865. SysUserRealName: sysUser.RealName,
  2866. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2867. CreateTime: time.Now(),
  2868. ModifyTime: time.Now(),
  2869. DateType: oldChartInfo.DateType,
  2870. StartDate: oldChartInfo.StartDate,
  2871. EndDate: oldChartInfo.EndDate,
  2872. IsSetName: oldChartInfo.IsSetName,
  2873. EdbInfoIds: oldChartInfo.EdbInfoIds,
  2874. ChartType: oldChartInfo.ChartType,
  2875. Calendar: oldChartInfo.Calendar,
  2876. SeasonStartDate: oldChartInfo.SeasonStartDate,
  2877. SeasonEndDate: oldChartInfo.SeasonEndDate,
  2878. ChartImage: oldChartInfo.ChartImage,
  2879. BarConfig: oldChartInfo.BarConfig,
  2880. //Sort: sort,
  2881. LeftMin: oldChartInfo.LeftMin,
  2882. LeftMax: oldChartInfo.LeftMax,
  2883. RightMin: oldChartInfo.RightMin,
  2884. RightMax: oldChartInfo.RightMax,
  2885. Right2Min: oldChartInfo.Right2Min,
  2886. Right2Max: oldChartInfo.Right2Max,
  2887. Disabled: oldChartInfo.Disabled,
  2888. Source: oldChartInfo.Source,
  2889. ExtraConfig: oldChartInfo.ExtraConfig,
  2890. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  2891. StartYear: oldChartInfo.StartYear,
  2892. Unit: oldChartInfo.Unit,
  2893. UnitEn: oldChartInfo.UnitEn,
  2894. ChartThemeId: oldChartInfo.ChartThemeId,
  2895. SourcesFrom: oldChartInfo.SourcesFrom,
  2896. Instructions: oldChartInfo.Instructions,
  2897. MarkersLines: oldChartInfo.MarkersLines,
  2898. MarkersAreas: oldChartInfo.MarkersAreas,
  2899. }
  2900. newId, err := data_manage.AddChartInfo(chartInfo)
  2901. if err != nil {
  2902. br.Msg = "保存失败"
  2903. br.ErrMsg = "保存失败,Err:" + err.Error()
  2904. return
  2905. }
  2906. chartInfo.ChartInfoId = int(newId)
  2907. // 添加图表与指标的关联关系
  2908. {
  2909. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2910. for _, v := range edbMappingList {
  2911. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2912. mapItem := &data_manage.ChartEdbMapping{
  2913. //ChartEdbMappingId: 0,
  2914. ChartInfoId: chartInfo.ChartInfoId,
  2915. EdbInfoId: v.EdbInfoId,
  2916. CreateTime: time.Now(),
  2917. ModifyTime: time.Now(),
  2918. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2919. MaxData: v.MaxData,
  2920. MinData: v.MinData,
  2921. IsOrder: v.IsOrder,
  2922. IsAxis: v.IsAxis,
  2923. EdbInfoType: v.EdbInfoType,
  2924. LeadValue: v.LeadValue,
  2925. LeadUnit: v.LeadUnit,
  2926. ChartStyle: v.ChartStyle,
  2927. ChartColor: v.ChartColor,
  2928. ChartWidth: v.ChartWidth,
  2929. Source: v.Source,
  2930. EdbAliasName: v.EdbAliasName,
  2931. IsConvert: v.IsConvert,
  2932. ConvertType: v.ConvertType,
  2933. ConvertValue: v.ConvertValue,
  2934. ConvertUnit: v.ConvertEnUnit,
  2935. ConvertEnUnit: v.ConvertEnUnit,
  2936. }
  2937. mapList = append(mapList, mapItem)
  2938. }
  2939. err = data_manage.AddChartEdbMapping(mapList)
  2940. if err != nil {
  2941. br.Msg = "保存失败"
  2942. br.ErrMsg = "保存失败,Err:" + err.Error()
  2943. return
  2944. }
  2945. }
  2946. //添加es数据
  2947. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2948. //修改my eta es数据
  2949. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  2950. //新增操作日志
  2951. {
  2952. chartLog := new(data_manage.ChartInfoLog)
  2953. chartLog.ChartInfoId = chartInfo.ChartInfoId
  2954. chartLog.ChartName = req.ChartName
  2955. chartLog.ChartClassifyId = req.ChartClassifyId
  2956. chartLog.SysUserId = sysUser.AdminId
  2957. chartLog.SysUserRealName = sysUser.RealName
  2958. chartLog.UniqueCode = chartInfo.UniqueCode
  2959. chartLog.CreateTime = time.Now()
  2960. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2961. chartLog.Status = "新增图表"
  2962. chartLog.Method = this.Ctx.Input.URI()
  2963. go data_manage.AddChartInfoLog(chartLog)
  2964. }
  2965. // 试用平台更新用户累计新增图表数
  2966. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  2967. if e != nil {
  2968. br.Msg = "操作失败"
  2969. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  2970. return
  2971. }
  2972. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  2973. go func() {
  2974. var r etaTrialService.EtaTrialUserReq
  2975. r.Mobile = adminItem.Mobile
  2976. _, _ = etaTrialService.UpdateUserChartNum(r)
  2977. }()
  2978. }
  2979. br.Ret = 200
  2980. br.Success = true
  2981. br.Msg = "保存成功"
  2982. br.Data = data_manage.AddChartInfoResp{
  2983. ChartInfoId: chartInfo.ChartInfoId,
  2984. UniqueCode: chartInfo.UniqueCode,
  2985. ChartType: chartInfo.ChartType,
  2986. }
  2987. br.IsAddLog = true
  2988. }
  2989. // PreviewBarChartInfo
  2990. // @Title 图表-获取预览的柱方图数据
  2991. // @Description 图表-获取指标详情接口
  2992. // @Param request body data_manage.BarChartInfoReq true "type json string"
  2993. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  2994. // @router /chart_info/preview_bar [post]
  2995. func (this *ChartInfoController) PreviewBarChartInfo() {
  2996. br := new(models.BaseResponse).Init()
  2997. defer func() {
  2998. this.Data["json"] = br
  2999. this.ServeJSON()
  3000. }()
  3001. var req data_manage.BarChartInfoReq
  3002. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3003. if err != nil {
  3004. br.Msg = "参数解析异常!"
  3005. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3006. return
  3007. }
  3008. sysUser := this.SysUser
  3009. if sysUser == nil {
  3010. br.Msg = "请登录"
  3011. br.ErrMsg = "请登录,SysUser Is Empty"
  3012. br.Ret = 408
  3013. return
  3014. }
  3015. if len(req.EdbInfoIdList) <= 0 {
  3016. br.Msg = "请选择指标"
  3017. return
  3018. }
  3019. if len(req.DateList) <= 0 {
  3020. br.Msg = "请选择日期"
  3021. return
  3022. }
  3023. chartInfo := new(data_manage.ChartInfoView)
  3024. chartType := utils.CHART_TYPE_BAR
  3025. //edbInfoIdList := make([]int, 0)
  3026. edbInfoIdsList := make([]string, 0)
  3027. convertMap := make(map[int]*data_manage.ChartEdbInfoMapping, 0)
  3028. for _, v := range req.EdbInfoIdList {
  3029. edbInfoIdsList = append(edbInfoIdsList, strconv.Itoa(v.EdbInfoId))
  3030. convertMap[v.EdbInfoId] = &data_manage.ChartEdbInfoMapping{
  3031. IsConvert: v.IsConvert,
  3032. ConvertType: v.ConvertType,
  3033. ConvertValue: v.ConvertValue,
  3034. ConvertUnit: v.ConvertUnit,
  3035. ConvertEnUnit: v.ConvertEnUnit,
  3036. }
  3037. }
  3038. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoId(strings.Join(edbInfoIdsList, ","))
  3039. if err != nil {
  3040. br.Msg = "获取失败"
  3041. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3042. return
  3043. }
  3044. for i, item := range mappingList {
  3045. if _, ok := convertMap[item.EdbInfoId]; ok {
  3046. mappingList[i].ConvertValue = convertMap[item.EdbInfoId].ConvertValue
  3047. mappingList[i].ConvertUnit = convertMap[item.EdbInfoId].ConvertUnit
  3048. mappingList[i].ConvertEnUnit = convertMap[item.EdbInfoId].ConvertEnUnit
  3049. mappingList[i].ConvertType = convertMap[item.EdbInfoId].ConvertType
  3050. mappingList[i].IsConvert = convertMap[item.EdbInfoId].IsConvert
  3051. }
  3052. }
  3053. // 获取图表中的指标数据
  3054. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, string(this.Ctx.Input.RequestBody), "")
  3055. if err != nil {
  3056. br.Msg = "获取失败"
  3057. if errMsg != `` {
  3058. br.Msg = errMsg
  3059. }
  3060. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3061. return
  3062. }
  3063. // 单位
  3064. if len(yDataList) > 0 {
  3065. chartInfo.Unit = yDataList[0].Unit
  3066. chartInfo.UnitEn = yDataList[0].UnitEn
  3067. }
  3068. warnEdbList := make([]string, 0)
  3069. for _, v := range edbList {
  3070. if v.IsNullData {
  3071. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3072. }
  3073. for _, reqEdb := range req.EdbInfoIdList {
  3074. if v.EdbInfoId == reqEdb.EdbInfoId {
  3075. v.EdbAliasName = reqEdb.Name
  3076. }
  3077. }
  3078. }
  3079. if len(warnEdbList) > 0 {
  3080. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3081. }
  3082. // 图表的指标来源
  3083. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3084. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3085. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3086. //图表操作权限
  3087. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3088. //判断是否需要展示英文标识
  3089. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3090. chartInfo.Button = data_manage.ChartViewButton{
  3091. IsEdit: chartInfo.IsEdit,
  3092. IsEnChart: chartInfo.IsEnChart,
  3093. IsAdd: chartInfo.IsAdd,
  3094. IsCopy: true,
  3095. IsSetName: chartInfo.IsSetName,
  3096. }
  3097. resp := new(data_manage.ChartInfoDetailResp)
  3098. resp.ChartInfo = chartInfo
  3099. resp.EdbInfoList = edbList
  3100. resp.XEdbIdValue = xEdbIdValue
  3101. resp.YDataList = yDataList
  3102. resp.DataResp = dataResp
  3103. br.Ret = 200
  3104. br.Success = true
  3105. br.Msg = "获取成功"
  3106. br.Data = resp
  3107. }
  3108. // PreviewSectionScatterChartInfo
  3109. // @Title 图表-获取预览的截面散点图数据
  3110. // @Description 图表-获取指标详情接口
  3111. // @Param request body data_manage.SectionScatter true "type json string"
  3112. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3113. // @router /chart_info/preview/time_section [post]
  3114. func (this *ChartInfoController) PreviewSectionScatterChartInfo() {
  3115. br := new(models.BaseResponse).Init()
  3116. defer func() {
  3117. this.Data["json"] = br
  3118. this.ServeJSON()
  3119. }()
  3120. var req data_manage.PreviewSectionScatterChartReq
  3121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3122. if err != nil {
  3123. br.Msg = "参数解析异常!"
  3124. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3125. return
  3126. }
  3127. chartInfo := new(data_manage.ChartInfoView)
  3128. sysUser := this.SysUser
  3129. if sysUser == nil {
  3130. br.Msg = "请登录"
  3131. br.ErrMsg = "请登录,SysUser Is Empty"
  3132. br.Ret = 408
  3133. return
  3134. }
  3135. chartType := utils.CHART_TYPE_SECTION_SCATTER
  3136. edbInfoIdArr, err, errMsg := data.CheckChartExtraConfig(chartType, req.ExtraConfig)
  3137. if err != nil {
  3138. br.Msg = errMsg
  3139. br.ErrMsg = "添加失败:" + err.Error()
  3140. return
  3141. }
  3142. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3143. // 获取图表中的指标数据
  3144. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3145. if err != nil {
  3146. br.Msg = "获取失败"
  3147. if errMsg != `` {
  3148. br.Msg = errMsg
  3149. }
  3150. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3151. return
  3152. }
  3153. warnEdbList := make([]string, 0)
  3154. for _, v := range edbList {
  3155. if v.IsNullData {
  3156. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3157. }
  3158. }
  3159. if len(warnEdbList) > 0 {
  3160. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3161. }
  3162. // 图表的指标来源
  3163. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3164. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3165. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3166. //图表操作权限
  3167. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3168. //判断是否需要展示英文标识
  3169. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3170. chartInfo.Button = data_manage.ChartViewButton{
  3171. IsEdit: chartInfo.IsEdit,
  3172. IsEnChart: chartInfo.IsEnChart,
  3173. IsAdd: chartInfo.IsAdd,
  3174. IsCopy: true,
  3175. IsSetName: chartInfo.IsSetName,
  3176. }
  3177. resp := new(data_manage.ChartInfoDetailResp)
  3178. resp.ChartInfo = chartInfo
  3179. resp.EdbInfoList = edbList
  3180. resp.XEdbIdValue = xEdbIdValue
  3181. resp.YDataList = yDataList
  3182. resp.DataResp = dataResp
  3183. br.Ret = 200
  3184. br.Success = true
  3185. br.Msg = "获取成功"
  3186. br.Data = resp
  3187. }
  3188. // GetBatchChartRefreshResult
  3189. // @Title 获取批量刷新图表结果的接口
  3190. // @Description 获取批量刷新图表结果的接口
  3191. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  3192. // @Success Ret=200 刷新成功
  3193. // @router /chart_info/batch_refresh/result [post]
  3194. func (this *EdbInfoController) GetBatchChartRefreshResult() {
  3195. br := new(models.BaseResponse).Init()
  3196. defer func() {
  3197. this.Data["json"] = br
  3198. this.ServeJSON()
  3199. }()
  3200. sysUser := this.SysUser
  3201. if sysUser == nil {
  3202. br.Msg = "请登录"
  3203. br.ErrMsg = "请登录,SysUser Is Empty"
  3204. br.Ret = 408
  3205. return
  3206. }
  3207. var req data_manage.BatchChartRefreshReq
  3208. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3209. if err != nil {
  3210. br.Msg = "参数解析异常!"
  3211. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3212. return
  3213. }
  3214. // 刷新结果,默认是刷新完成了
  3215. refreshResult := data.CheckBatchChartRefreshResult(req.Source, req.ReportId, req.ReportChapterId)
  3216. resp := response.ChartRefreshResp{
  3217. RefreshResult: refreshResult,
  3218. }
  3219. br.Data = resp
  3220. br.Ret = 200
  3221. br.Success = true
  3222. }
  3223. //
  3224. //修复数据时间戳
  3225. //func init() {
  3226. // fmt.Println("start")
  3227. // edbInfoList,err:=data_manage.GetEdbInfo()
  3228. // if err!=nil {
  3229. // fmt.Println("Err:",err.Error())
  3230. // return
  3231. // }
  3232. // startDate:=time.Now().AddDate(-40,0,0).Format(utils.FormatDate)
  3233. // endDate:=time.Now().Format(utils.FormatDate)
  3234. // for _,v:=range edbInfoList{
  3235. // dataList,err:=data_manage.GetEdbDataList(v.Source,v.EdbInfoId,startDate,endDate)
  3236. // if err!=nil {
  3237. // fmt.Println("GetEdbDataList Err:",err.Error())
  3238. // return
  3239. // }
  3240. // for _,dv:=range dataList{
  3241. // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3242. // if err!=nil {
  3243. // fmt.Println("time.Parse Err:"+err.Error())
  3244. // return
  3245. // }
  3246. // timestamp:=dataTime.UnixNano()/1e6
  3247. // err=data_manage.ModifyEdbDatadTimestamp(v.Source,dv.EdbDataId,timestamp)
  3248. // if err!=nil{
  3249. // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3250. // return
  3251. // }
  3252. // fmt.Println(v.Source,dv.EdbDataId,timestamp)
  3253. // }
  3254. // }
  3255. // //time.Sleep(2*time.Minute)
  3256. //
  3257. // //startDate:=time.Now().AddDate(-30,0,0).Format(utils.FormatDate)
  3258. // //endDate:=time.Now().Format(utils.FormatDate)
  3259. // //dataList,err:=data_manage.GetEdbDataList(1,100099,startDate,endDate)
  3260. // //if err!=nil {
  3261. // // fmt.Println("GetEdbDataList Err:",err.Error())
  3262. // // return
  3263. // //}
  3264. // //for _,dv:=range dataList{
  3265. // // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3266. // // if err!=nil {
  3267. // // fmt.Println("time.Parse Err:"+err.Error())
  3268. // // return
  3269. // // }
  3270. // // timestamp:=dataTime.UnixNano()/1e6
  3271. // // err=data_manage.ModifyEdbDatadTimestamp(2,dv.EdbDataId,timestamp)
  3272. // // if err!=nil{
  3273. // // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3274. // // return
  3275. // // }
  3276. // // fmt.Println(2,dv.EdbDataId,timestamp)
  3277. // //}
  3278. //
  3279. // fmt.Println("end")
  3280. //}
  3281. //修复指标配置数据
  3282. //func init() {
  3283. // fmt.Println("start")
  3284. // edbInfo, err := data_manage.GetEdbInfoAll()
  3285. // fmt.Println(err)
  3286. // for k, v := range edbInfoAll {
  3287. // fmt.Println(k, v.EdbInfoId)
  3288. // item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  3289. // fmt.Println(item, err)
  3290. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  3291. // if err != nil {
  3292. // fmt.Println("ModifyEdbInfoMaxAndMinInfo Err:" + err.Error())
  3293. // }
  3294. // }
  3295. // fmt.Println("end")
  3296. //}
  3297. //func init() {
  3298. // fmt.Println("start")
  3299. // var yearArr []int
  3300. // yearArr = append(yearArr, 2021)
  3301. // yearArr = append(yearArr, 2010)
  3302. // yearArr = append(yearArr, 2020)
  3303. // yearArr = append(yearArr, 2017)
  3304. // yearArr = append(yearArr, 2019)
  3305. // sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  3306. // fmt.Println(yearArr)
  3307. // fmt.Println("end")
  3308. //}
  3309. //季度指标数据计算(公历转农历)
  3310. //func init() {
  3311. // fmt.Println("start AddCalculateQuarter")
  3312. // list:=make([]*data_manage.EdbDataList,0)
  3313. // data_manage.AddCalculateQuarterV4(list)
  3314. // fmt.Println("end AddCalculateQuarter")
  3315. // //return
  3316. //}
  3317. /*
  3318. {
  3319. "ChartClassifyId": 58,
  3320. "ChartName": "公历转农历6",
  3321. "ChartType": 2,
  3322. "ChartEdbInfoList": [
  3323. {
  3324. "ChartColor": "#00f",
  3325. "ChartStyle": "spline",
  3326. "ChartWidth": 3,
  3327. "EdbInfoId": 100466,
  3328. "EdbInfoType": 1,
  3329. "IsAxis": 1,
  3330. "IsOrder": false,
  3331. "LeadUnit": "",
  3332. "LeadValue": 0,
  3333. "MaxData": 0.2943,
  3334. "MinData": -0.2448
  3335. }
  3336. ]
  3337. }
  3338. */
  3339. //func init() {
  3340. // fmt.Println("start")
  3341. // data_manage.AddCalculateQuarter(100466,6,"C2108252836")
  3342. // fmt.Println("end")
  3343. //}
  3344. //func init() {
  3345. // data.AddAllChartInfo()
  3346. //}
  3347. // 截面散点示例数据
  3348. //func init() {
  3349. //
  3350. // seriesList := []data_manage.SectionScatterSeriesItemReq{
  3351. // {
  3352. // Name: "系列1名称",
  3353. // Color: "红红火火色",
  3354. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3355. // {
  3356. // XEdbInfoId: 1,
  3357. // YEdbInfoId: 2,
  3358. // Name: "破铜",
  3359. // NameEn: "Po Tong",
  3360. // XDateType: 1,
  3361. // XDate: "",
  3362. // XDateValue: 0,
  3363. // YDateType: 2,
  3364. // YDate: "",
  3365. // YDateValue: 30,
  3366. // IsShow: false,
  3367. // },
  3368. // {
  3369. // XEdbInfoId: 3,
  3370. // YEdbInfoId: 4,
  3371. // Name: "烂铁",
  3372. // NameEn: "Lan Tie",
  3373. // XDateType: 2,
  3374. // XDate: "",
  3375. // XDateValue: 30,
  3376. // YDateType: 3,
  3377. // YDate: "2023-02-17",
  3378. // YDateValue: 0,
  3379. // IsShow: false,
  3380. // },
  3381. // },
  3382. // ShowLine: false,
  3383. // ShowEquation: false,
  3384. // ShowR: false,
  3385. // },
  3386. // {
  3387. // Name: "系列2名称",
  3388. // Color: "恍恍惚惚色",
  3389. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3390. // {
  3391. // XEdbInfoId: 1,
  3392. // YEdbInfoId: 2,
  3393. // Name: "破铜",
  3394. // NameEn: "Po Tong",
  3395. // XDateType: 3,
  3396. // XDate: "2023-02-17",
  3397. // XDateValue: 0,
  3398. // YDateType: 2,
  3399. // YDate: "",
  3400. // YDateValue: 60,
  3401. // IsShow: true,
  3402. // },
  3403. // {
  3404. // XEdbInfoId: 3,
  3405. // YEdbInfoId: 4,
  3406. // Name: "烂铁",
  3407. // NameEn: "Lan Tie",
  3408. // XDateType: 3,
  3409. // XDate: "2023-02-17",
  3410. // XDateValue: 0,
  3411. // YDateType: 2,
  3412. // YDate: "",
  3413. // YDateValue: 60,
  3414. // IsShow: false,
  3415. // },
  3416. // },
  3417. // ShowLine: false,
  3418. // ShowEquation: false,
  3419. // ShowR: false,
  3420. // },
  3421. // }
  3422. // rel := data_manage.SectionScatter{
  3423. // XName: "X轴名称",
  3424. // XNameEn: "X english name",
  3425. // XUnitName: "X轴单位",
  3426. // XUnitNameEn: "X unit english name",
  3427. // YName: "Y轴名称",
  3428. // YNameEn: "Y english name",
  3429. // YUnitName: "Y轴单位",
  3430. // YUnitNameEn: "Y unit english name",
  3431. // SeriesList: seriesList,
  3432. // }
  3433. // jsonByte, _ := json.Marshal(rel)
  3434. // utils.FileLog.Info(string(jsonByte))
  3435. //}
  3436. // func init() {
  3437. // var a, b float64
  3438. //
  3439. // testX := []float64{1, 3, 8, 7, 9}
  3440. // testY := []float64{10, 12, 24, 21, 34}
  3441. // {
  3442. // coordinateData := make([]utils.Coordinate, 0)
  3443. // for k, x := range testX {
  3444. // tmpCoordinate1 := utils.Coordinate{
  3445. // X: x,
  3446. // Y: testY[k],
  3447. // }
  3448. // coordinateData = append(coordinateData, tmpCoordinate1)
  3449. // }
  3450. // a, b = utils.GetLinearResult(coordinateData)
  3451. // trendLine := fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  3452. //
  3453. // fmt.Println(trendLine)
  3454. // }
  3455. //
  3456. // {
  3457. // r2 := utils.CalculationDecisive(testX, testY)
  3458. // fmt.Println(r2)
  3459. // }
  3460. // }
  3461. // FixChart 修复季节性图的日期数据
  3462. //func FixChart() {
  3463. // var condition string
  3464. // var pars []interface{}
  3465. //
  3466. // // 普通图表
  3467. // condition += ` AND chart_type = ? AND season_start_date != "" `
  3468. // pars = append(pars, 2)
  3469. //
  3470. // //获取图表信息
  3471. // list, err := data_manage.GetChartInfoListByCondition(condition, pars, 0, 10000)
  3472. // if err != nil {
  3473. // fmt.Println("err:", err)
  3474. // return
  3475. // }
  3476. //
  3477. // for _, v := range list {
  3478. // fmt.Println(v)
  3479. // v.SeasonStartDate = v.SeasonStartDate + "-01"
  3480. // v.SeasonEndDate = v.SeasonEndDate + "-12"
  3481. // err = v.Update([]string{"SeasonStartDate", "SeasonEndDate"})
  3482. // if err != nil {
  3483. // fmt.Println(v.ChartInfoId, ";", v.ChartName, "修改失敗")
  3484. // }
  3485. // data.EsAddOrEditChartInfo(v.ChartInfoId)
  3486. // data.EsAddOrEditMyChartInfoByChartInfoId(v.ChartInfoId)
  3487. // }
  3488. //}
  3489. // PreviewRadarChartInfo
  3490. // @Title 图表-获取预览的雷达图
  3491. // @Description 图表-获取预览的雷达图
  3492. // @Param ExtraConfig body data_manage.PreviewRadarChartReq true "type json string"
  3493. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3494. // @router /chart_info/preview/radar [post]
  3495. func (this *ChartInfoController) PreviewRadarChartInfo() {
  3496. br := new(models.BaseResponse).Init()
  3497. defer func() {
  3498. this.Data["json"] = br
  3499. this.ServeJSON()
  3500. }()
  3501. var req data_manage.PreviewRadarChartReq
  3502. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3503. if err != nil {
  3504. br.Msg = "参数解析异常!"
  3505. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3506. return
  3507. }
  3508. chartInfo := new(data_manage.ChartInfoView)
  3509. sysUser := this.SysUser
  3510. if sysUser == nil {
  3511. br.Msg = "请登录"
  3512. br.ErrMsg = "请登录,SysUser Is Empty"
  3513. br.Ret = 408
  3514. return
  3515. }
  3516. chartType := utils.CHART_TYPE_RADAR
  3517. if len(req.ChartEdbInfoList) <= 0 {
  3518. br.Msg = "请选择指标"
  3519. return
  3520. }
  3521. edbInfoIdArr := make([]int, 0)
  3522. edbInfoNameMap := make(map[int]string)
  3523. for _, v := range req.ChartEdbInfoList {
  3524. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  3525. edbInfoNameMap[v.EdbInfoId] = v.EdbAliasName
  3526. }
  3527. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3528. for _, v := range mappingList {
  3529. if name, ok := edbInfoNameMap[v.EdbInfoId]; ok {
  3530. v.EdbAliasName = name
  3531. }
  3532. }
  3533. // 获取图表中的指标数据
  3534. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3535. if err != nil {
  3536. br.Msg = "获取失败"
  3537. if errMsg != `` {
  3538. br.Msg = errMsg
  3539. }
  3540. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3541. return
  3542. }
  3543. warnEdbList := make([]string, 0)
  3544. for _, v := range edbList {
  3545. if v.IsNullData {
  3546. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3547. }
  3548. }
  3549. if len(warnEdbList) > 0 {
  3550. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3551. }
  3552. //图表操作权限
  3553. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3554. //判断是否需要展示英文标识
  3555. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3556. chartInfo.Button = data_manage.ChartViewButton{
  3557. IsEdit: chartInfo.IsEdit,
  3558. IsEnChart: chartInfo.IsEnChart,
  3559. IsAdd: chartInfo.IsAdd,
  3560. IsCopy: true,
  3561. IsSetName: chartInfo.IsSetName,
  3562. }
  3563. // 图表的指标来源
  3564. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3565. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3566. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3567. resp := new(data_manage.ChartInfoDetailResp)
  3568. resp.ChartInfo = chartInfo
  3569. resp.EdbInfoList = edbList
  3570. resp.XEdbIdValue = xEdbIdValue
  3571. resp.YDataList = yDataList
  3572. resp.DataResp = dataResp
  3573. br.Ret = 200
  3574. br.Success = true
  3575. br.Msg = "获取成功"
  3576. br.Data = resp
  3577. }
  3578. // ChartInfoDetail
  3579. // @Title 获取图表详情-数据转换
  3580. // @Description 获取图表详情接口-数据转换
  3581. // @Param ChartInfoId query int true "图表id"
  3582. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  3583. // @Param StartDate query string true "自定义开始日期"
  3584. // @Param EndDate query string true "自定义结束日期"
  3585. // @Param Calendar query string true "公历/农历"
  3586. // @Param SeasonStartDate query string true "季节性图开始日期"
  3587. // @Param SeasonEndDate query string true "季节性图结束日期"
  3588. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  3589. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  3590. // @Success 200 {object} data_manage.ChartInfoDetailResp
  3591. // @router /chart_info/convert/detail [get]
  3592. func (this *ChartInfoController) ChartInfoConvertDetail() {
  3593. br := new(models.BaseResponse).Init()
  3594. defer func() {
  3595. this.Data["json"] = br
  3596. this.ServeJSON()
  3597. }()
  3598. sysUser := this.SysUser
  3599. if sysUser == nil {
  3600. br.Msg = "请登录"
  3601. br.ErrMsg = "请登录,SysUser Is Empty"
  3602. br.Ret = 408
  3603. return
  3604. }
  3605. chartInfoId, _ := this.GetInt("ChartInfoId")
  3606. dateType, _ := this.GetInt("DateType")
  3607. fmt.Println("dateType:", dateType)
  3608. if dateType <= 0 {
  3609. dateType = 3
  3610. }
  3611. startDate := this.GetString("StartDate")
  3612. endDate := this.GetString("EndDate")
  3613. startYear, _ := this.GetInt("StartYear")
  3614. edbInfoId := this.GetString("EdbInfoId")
  3615. chartType, _ := this.GetInt("ChartType")
  3616. calendar := this.GetString("Calendar")
  3617. if calendar == "" {
  3618. calendar = "公历"
  3619. }
  3620. isConvert, _ := this.GetInt("IsConvert")
  3621. convertType, _ := this.GetInt("ConvertType")
  3622. convertValue, _ := this.GetFloat("ConvertValue")
  3623. convertUnit := this.GetString("ConvertUnit")
  3624. convertEnUnit := this.GetString("ConvertEnUnit")
  3625. isAxis, _ := this.GetInt("IsAxis")
  3626. resp := new(data_manage.ChartInfoDetailResp)
  3627. var err error
  3628. chartInfo := new(data_manage.ChartInfoView)
  3629. chartInfo.HaveOperaAuth = true
  3630. // 通过id获取详情
  3631. if chartInfoId > 0 {
  3632. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  3633. if err != nil {
  3634. if err.Error() == utils.ErrNoRow() {
  3635. br.Msg = "该图表已删除,自动查看下一图表"
  3636. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  3637. br.Ret = 406
  3638. return
  3639. }
  3640. br.Msg = "获取失败"
  3641. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  3642. return
  3643. }
  3644. chartType = chartInfo.ChartType
  3645. // 获取主题样式
  3646. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  3647. if err != nil {
  3648. br.Msg = "获取失败"
  3649. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  3650. return
  3651. }
  3652. chartInfo.ChartThemeStyle = chartTheme.Config
  3653. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  3654. // 图表数据权限
  3655. {
  3656. // 图表分类
  3657. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  3658. if err != nil {
  3659. br.Msg = "获取失败"
  3660. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  3661. return
  3662. }
  3663. // 已授权分类id
  3664. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  3665. if err != nil {
  3666. br.Msg = "获取失败"
  3667. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  3668. return
  3669. }
  3670. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  3671. }
  3672. //图表操作权限
  3673. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  3674. chartInfo.Button = data_manage.ChartViewButton{
  3675. IsEdit: chartInfo.IsEdit,
  3676. IsEnChart: chartInfo.IsEnChart,
  3677. IsAdd: chartInfo.IsAdd,
  3678. IsCopy: true,
  3679. IsSetName: chartInfo.IsSetName,
  3680. }
  3681. }
  3682. // 有数据权限的话,获取图表的数据
  3683. if chartInfo.HaveOperaAuth {
  3684. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  3685. if chartInfoId > 0 {
  3686. mappingList, err = data_manage.GetChartEdbMappingList(chartInfoId)
  3687. if err != nil {
  3688. br.Msg = "获取失败"
  3689. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3690. return
  3691. }
  3692. } else {
  3693. if edbInfoId != "" {
  3694. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoId(edbInfoId)
  3695. if err != nil {
  3696. br.Msg = "获取失败"
  3697. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3698. return
  3699. }
  3700. if isConvert == 1 && len(mappingList) > 0 {
  3701. for i, _ := range mappingList {
  3702. mappingList[i].IsConvert = 1
  3703. mappingList[i].ConvertType = convertType
  3704. mappingList[i].ConvertValue = convertValue
  3705. mappingList[i].ConvertUnit = convertUnit
  3706. mappingList[i].ConvertEnUnit = convertEnUnit
  3707. }
  3708. }
  3709. }
  3710. }
  3711. // 图表额外数据参数
  3712. extraConfigStr := chartInfo.ExtraConfig
  3713. // 柱方图的一些配置
  3714. var barConfig data_manage.BarChartInfoReq
  3715. if chartInfo != nil && chartInfo.ChartType == 7 {
  3716. if chartInfo.BarConfig == `` {
  3717. br.Msg = "柱方图未配置"
  3718. br.ErrMsg = "柱方图未配置"
  3719. return
  3720. }
  3721. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  3722. if err != nil {
  3723. br.Msg = "柱方图配置异常"
  3724. br.ErrMsg = "柱方图配置异常"
  3725. return
  3726. }
  3727. extraConfigStr = chartInfo.BarConfig
  3728. }
  3729. yearMax := 0
  3730. if dateType == utils.DateTypeNYears {
  3731. for _, v := range mappingList {
  3732. if v.LatestDate != "" {
  3733. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  3734. if tErr != nil {
  3735. br.Msg = "获取失败"
  3736. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  3737. return
  3738. }
  3739. if lastDateT.Year() > yearMax {
  3740. yearMax = lastDateT.Year()
  3741. }
  3742. }
  3743. }
  3744. }
  3745. // 开始/结束日期
  3746. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  3747. // 获取图表中的指标数据
  3748. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartConvertEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, isAxis)
  3749. if err != nil {
  3750. br.Msg = "获取失败"
  3751. if errMsg != `` {
  3752. br.Msg = errMsg
  3753. }
  3754. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3755. return
  3756. }
  3757. // 单位
  3758. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  3759. chartInfo.Unit = yDataList[0].Unit
  3760. chartInfo.UnitEn = yDataList[0].UnitEn
  3761. }
  3762. warnEdbList := make([]string, 0)
  3763. for _, v := range edbList {
  3764. if v.IsNullData {
  3765. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3766. }
  3767. }
  3768. if len(warnEdbList) > 0 {
  3769. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3770. }
  3771. if chartInfoId > 0 && chartInfo != nil {
  3772. //判断是否加入我的图库
  3773. {
  3774. var myChartCondition string
  3775. var myChartPars []interface{}
  3776. myChartCondition += ` AND a.admin_id=? `
  3777. myChartPars = append(myChartPars, sysUser.AdminId)
  3778. myChartCondition += ` AND a.chart_info_id=? `
  3779. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  3780. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  3781. if err != nil && err.Error() != utils.ErrNoRow() {
  3782. br.Msg = "获取失败"
  3783. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  3784. return
  3785. }
  3786. if myChartList != nil && len(myChartList) > 0 {
  3787. chartInfo.IsAdd = true
  3788. chartInfo.MyChartId = myChartList[0].MyChartId
  3789. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  3790. }
  3791. }
  3792. if chartInfo.ChartType == 2 {
  3793. if chartInfo.SeasonStartDate != "" {
  3794. chartInfo.StartDate = chartInfo.SeasonStartDate
  3795. chartInfo.EndDate = chartInfo.SeasonEndDate
  3796. if chartInfo.DateType == 3 {
  3797. chartInfo.DateType = 5
  3798. }
  3799. }
  3800. }
  3801. }
  3802. // 图表的指标来源
  3803. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3804. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3805. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3806. //判断是否需要展示英文标识
  3807. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3808. resp.EdbInfoList = edbList
  3809. resp.XEdbIdValue = xEdbIdValue
  3810. resp.YDataList = yDataList
  3811. resp.DataResp = dataResp
  3812. resp.BarChartInfo = barConfig
  3813. }
  3814. resp.ChartInfo = chartInfo
  3815. br.Ret = 200
  3816. br.Success = true
  3817. br.Msg = "获取成功"
  3818. br.Data = resp
  3819. }
  3820. // ChartInfoImgSetByBase64
  3821. // @Title 图表图片上传
  3822. // @Param Img query string true "图片"
  3823. // @Param ChartInfoId query int true "图表ID"
  3824. // @Success 200 {object} models.ResourceResp
  3825. // @router /chart_info/image/set_by_base64 [post]
  3826. func (this *ChartInfoController) ChartInfoImgSetByBase64() {
  3827. br := new(models.BaseResponse).Init()
  3828. defer func() {
  3829. this.Data["json"] = br
  3830. this.ServeJSON()
  3831. }()
  3832. imgData := this.GetString("Img")
  3833. if imgData == "" {
  3834. br.Msg = "图片参数错误"
  3835. br.ErrMsg = "图片参数错误,Img Is Empty"
  3836. return
  3837. }
  3838. chartInfoId, _ := this.GetInt("ChartInfoId", 0)
  3839. if chartInfoId <= 0 {
  3840. br.Msg = "图片参数错误"
  3841. br.ErrMsg = "图片参数错误,Img Is Empty"
  3842. return
  3843. }
  3844. resp := new(models.ResourceResp)
  3845. uploadDir := "static/images/"
  3846. if !utils.FileIsExist(uploadDir) {
  3847. err := os.MkdirAll(uploadDir, utils.DIR_MOD)
  3848. if err != nil {
  3849. br.Msg = "图表保存失败"
  3850. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3851. return
  3852. }
  3853. }
  3854. //var saveToOssPath string
  3855. randStr := utils.GetRandStringNoSpecialChar(28)
  3856. var fileName, outFileName string
  3857. fileName = randStr + ".txt"
  3858. fileName = uploadDir + fileName
  3859. err := utils.SaveToFile(imgData, fileName)
  3860. if err != nil {
  3861. br.Msg = "图片保存失败"
  3862. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  3863. return
  3864. }
  3865. // 删除临时存储的svg文件
  3866. defer func() {
  3867. err = os.Remove(fileName)
  3868. if err != nil {
  3869. utils.FileLog.Info("删除临时存储的svg文件失败, err: " + err.Error())
  3870. }
  3871. }()
  3872. outFileName = randStr + ".png"
  3873. doneChannel := make(chan bool, 1)
  3874. errorChannel := make(chan error, 1)
  3875. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  3876. go func() {
  3877. output, err := cmd.CombinedOutput()
  3878. if err != nil {
  3879. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  3880. errorChannel <- err
  3881. return
  3882. }
  3883. doneChannel <- true
  3884. }()
  3885. defer func() {
  3886. _ = os.Remove(outFileName)
  3887. if err != nil {
  3888. utils.FileLog.Info("删除生产的图片文件失败, err: " + err.Error())
  3889. }
  3890. }()
  3891. select {
  3892. case <-time.After(30 * time.Second):
  3893. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  3894. e := cmd.Process.Kill()
  3895. if e != nil {
  3896. fmt.Println("cmd kill err: ", e.Error())
  3897. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  3898. br.Msg = "图片生成失败"
  3899. br.ErrMsg = "图片生成失败, 执行超时" + e.Error()
  3900. return
  3901. }
  3902. fmt.Println("timeout kill process")
  3903. case <-doneChannel:
  3904. fmt.Println("done")
  3905. case err := <-errorChannel:
  3906. br.Msg = "文件上传失败"
  3907. br.ErrMsg = fmt.Sprintf("execute command failure err: %s", err.Error())
  3908. fmt.Println("execute command failure err:" + err.Error())
  3909. return
  3910. }
  3911. //上传到阿里云 和 minio
  3912. resourceUrl := ``
  3913. ossClient := services.NewOssClient()
  3914. if ossClient == nil {
  3915. br.Msg = "上传失败"
  3916. br.ErrMsg = "初始化OSS服务失败"
  3917. return
  3918. }
  3919. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  3920. if err != nil {
  3921. br.Msg = "文件上传失败"
  3922. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  3923. return
  3924. }
  3925. // 修改图表的缩略图信息
  3926. if chartInfoId > 0 && resourceUrl != "" {
  3927. err = data_manage.EditChartInfoImageV2(chartInfoId, resourceUrl)
  3928. if err != nil {
  3929. br.Msg = "保存失败"
  3930. br.ErrMsg = "保存失败,Err:" + err.Error()
  3931. return
  3932. }
  3933. //修改es数据
  3934. go data.EsAddOrEditChartInfo(chartInfoId)
  3935. //修改my eta es数据
  3936. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfoId)
  3937. }
  3938. resp.ResourceUrl = resourceUrl
  3939. resp.Source = "convert"
  3940. //resp.CacheKey = imgDataKey
  3941. br.Msg = "上传成功"
  3942. br.Ret = 200
  3943. br.Success = true
  3944. br.Data = resp
  3945. return
  3946. }