chart_info.go 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315
  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. utils.FileLog.Info(`面积图未开启面积堆积`)
  1158. return
  1159. }
  1160. }
  1161. if tmpConfig.IsHeap == 1 {
  1162. standardIndexMap := make(map[string]*data_manage.EdbDataList)
  1163. var startDate, endDate string
  1164. for _, v := range edbDataList {
  1165. // 判断是否为基准指标
  1166. if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
  1167. if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
  1168. startDate = dataList[0].DataTime
  1169. endDate = dataList[len(dataList)-1].DataTime
  1170. for _, dataObject := range dataList {
  1171. standardIndexMap[dataObject.DataTime] = dataObject
  1172. }
  1173. }
  1174. break
  1175. }
  1176. }
  1177. strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
  1178. if err != nil {
  1179. return err, "创建空值处理器失败"
  1180. }
  1181. err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
  1182. if err != nil {
  1183. return err, err.Error()
  1184. }
  1185. // 时间戳处理
  1186. for _, mapping := range edbDataList {
  1187. if dataList, ok := mapping.DataList.([]*data_manage.EdbDataList); ok {
  1188. for _, dataInfo := range dataList {
  1189. toFormatTime := utils.StringToFormatTime(dataInfo.DataTime, utils.FormatDate)
  1190. dataInfo.DataTimestamp = toFormatTime.UnixMilli()
  1191. }
  1192. }
  1193. }
  1194. }
  1195. return nil, ""
  1196. }
  1197. // PreviewChartInfoDetail
  1198. // @Title 获取图表详情(预览)
  1199. // @Description 获取图表详情接口(预览)
  1200. // @Param request body data_manage.PreviewChartInfoReq true "type json string"
  1201. // @Success 200 {object} data_manage.ChartInfoDetailResp
  1202. // @router /chart_info/detail/preview [post]
  1203. func (this *ChartInfoController) PreviewChartInfoDetail() {
  1204. br := new(models.BaseResponse).Init()
  1205. defer func() {
  1206. this.Data["json"] = br
  1207. this.ServeJSON()
  1208. }()
  1209. sysUser := this.SysUser
  1210. if sysUser == nil {
  1211. br.Msg = "请登录"
  1212. br.ErrMsg = "请登录,SysUser Is Empty"
  1213. br.Ret = 408
  1214. return
  1215. }
  1216. var req data_manage.PreviewChartInfoReq
  1217. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1218. if err != nil {
  1219. br.Msg = "参数解析异常!"
  1220. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1221. return
  1222. }
  1223. if req.DateType <= 0 {
  1224. req.DateType = 3
  1225. }
  1226. // edbInfoId 指标id
  1227. if len(req.ChartEdbInfoList) <= 0 {
  1228. br.Msg = "请选择指标"
  1229. br.ErrMsg = "指标为空"
  1230. br.IsSendEmail = false
  1231. return
  1232. }
  1233. calendar := req.Calendar
  1234. if calendar == "" {
  1235. calendar = "公历"
  1236. }
  1237. chartInfo := new(data_manage.ChartInfoView)
  1238. chartInfo.ChartType = req.ChartType
  1239. edbInfoIdList := make([]int, 0)
  1240. edbInfoIdMapping := make(map[int]*data_manage.ChartSaveItem)
  1241. for _, v := range req.ChartEdbInfoList {
  1242. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  1243. edbInfoIdMapping[v.EdbInfoId] = v
  1244. }
  1245. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  1246. if err != nil {
  1247. br.Msg = "获取失败"
  1248. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1249. return
  1250. }
  1251. yearMax := 0
  1252. for k, v := range mappingList {
  1253. if tmpV, ok := edbInfoIdMapping[v.EdbInfoId]; ok {
  1254. v.EdbInfoType = tmpV.EdbInfoType
  1255. v.MaxData = tmpV.MaxData
  1256. v.MinData = tmpV.MinData
  1257. v.IsOrder = tmpV.IsOrder
  1258. v.IsAxis = tmpV.IsAxis
  1259. v.EdbInfoType = tmpV.EdbInfoType
  1260. v.LeadValue = tmpV.LeadValue
  1261. v.LeadUnit = tmpV.LeadUnit
  1262. v.ChartStyle = tmpV.ChartStyle
  1263. v.ChartColor = tmpV.ChartColor
  1264. v.PredictChartColor = tmpV.PredictChartColor
  1265. v.ChartWidth = tmpV.ChartWidth
  1266. //v.Source = tmpV.Source
  1267. // 得到指标更新的最新的年份
  1268. if req.DateType == utils.DateTypeNYears && v.LatestDate != "" {
  1269. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  1270. if tErr != nil {
  1271. br.Msg = "获取失败"
  1272. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  1273. return
  1274. }
  1275. if lastDateT.Year() > yearMax {
  1276. yearMax = lastDateT.Year()
  1277. }
  1278. }
  1279. }
  1280. if req.ChartEdbInfoList[k].IsConvert == 1 {
  1281. v.IsConvert = 1
  1282. v.ConvertType = req.ChartEdbInfoList[k].ConvertType
  1283. v.ConvertValue = req.ChartEdbInfoList[k].ConvertValue
  1284. v.ConvertUnit = req.ChartEdbInfoList[k].ConvertUnit
  1285. v.ConvertEnUnit = req.ChartEdbInfoList[k].ConvertEnUnit
  1286. }
  1287. mappingList[k] = v
  1288. }
  1289. // 开始/结束日期
  1290. startDate, endDate := utils.GetDateByDateTypeV2(req.DateType, req.StartDate, req.EndDate, req.StartYear, yearMax)
  1291. if startDate == "" {
  1292. br.Msg = "获取失败"
  1293. br.ErrMsg = "获取失败 时间格式错误"
  1294. return
  1295. }
  1296. var seasonExtraConfig string
  1297. if req.ChartType == 2 {
  1298. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  1299. if tErr != nil {
  1300. br.Msg = "季节性图表配置信息异常"
  1301. br.ErrMsg = "季节性图表配置信息异常,Err:" + tErr.Error()
  1302. return
  1303. }
  1304. seasonExtraConfig = string(seasonExtra)
  1305. }
  1306. // 图表额外数据参数
  1307. extraConfigStr := chartInfo.ExtraConfig
  1308. // 柱方图的一些配置
  1309. var barConfig data_manage.BarChartInfoReq
  1310. if chartInfo != nil && chartInfo.ChartType == 7 {
  1311. if chartInfo.BarConfig == `` {
  1312. br.Msg = "柱方图未配置"
  1313. br.ErrMsg = "柱方图未配置"
  1314. return
  1315. }
  1316. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  1317. if err != nil {
  1318. br.Msg = "柱方图配置异常"
  1319. br.ErrMsg = "柱方图配置异常"
  1320. return
  1321. }
  1322. extraConfigStr = chartInfo.BarConfig
  1323. }
  1324. // 获取图表中的指标数据
  1325. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, req.ChartType, calendar, startDate, endDate, mappingList, extraConfigStr, seasonExtraConfig)
  1326. if err != nil {
  1327. br.Msg = "获取失败"
  1328. if errMsg != `` {
  1329. br.Msg = errMsg
  1330. }
  1331. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1332. return
  1333. }
  1334. // 单位
  1335. if req.ChartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  1336. chartInfo.Unit = yDataList[0].Unit
  1337. chartInfo.UnitEn = yDataList[0].UnitEn
  1338. }
  1339. warnEdbList := make([]string, 0)
  1340. for _, v := range edbList {
  1341. if v.IsNullData {
  1342. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1343. }
  1344. }
  1345. if len(warnEdbList) > 0 {
  1346. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1347. }
  1348. //图表操作权限
  1349. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  1350. //判断是否需要展示英文标识
  1351. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  1352. chartInfo.Button = data_manage.ChartViewButton{
  1353. IsEdit: chartInfo.IsEdit,
  1354. IsEnChart: chartInfo.IsEnChart,
  1355. IsAdd: chartInfo.IsAdd,
  1356. IsCopy: true,
  1357. IsSetName: chartInfo.IsSetName,
  1358. }
  1359. // 图表的指标来源
  1360. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  1361. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  1362. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  1363. resp := new(data_manage.ChartInfoDetailResp)
  1364. resp.ChartInfo = chartInfo
  1365. resp.EdbInfoList = edbList
  1366. resp.XEdbIdValue = xEdbIdValue
  1367. resp.YDataList = yDataList
  1368. resp.BarChartInfo = barConfig
  1369. resp.DataResp = dataResp
  1370. br.Ret = 200
  1371. br.Success = true
  1372. br.Msg = "获取成功"
  1373. br.Data = resp
  1374. }
  1375. // ChartInfoDetailV2
  1376. // @Title 获取图表详情
  1377. // @Description 获取图表详情接口
  1378. // @Param ChartInfoId query int true "图表id"
  1379. // @Success 200 {object} data_manage.ChartInfoDetailResp
  1380. // @router /chart_info/detail/v2 [get]
  1381. func (this *ChartInfoController) ChartInfoDetailV2() {
  1382. br := new(models.BaseResponse).Init()
  1383. defer func() {
  1384. this.Data["json"] = br
  1385. this.ServeJSON()
  1386. }()
  1387. sysUser := this.SysUser
  1388. if sysUser == nil {
  1389. br.Msg = "请登录"
  1390. br.ErrMsg = "请登录,SysUser Is Empty"
  1391. br.Ret = 408
  1392. return
  1393. }
  1394. chartInfoId, _ := this.GetInt("ChartInfoId")
  1395. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  1396. if chartInfoId <= 0 {
  1397. br.Msg = "请选择图表"
  1398. br.IsSendEmail = false
  1399. return
  1400. }
  1401. chartInfo, err := data_manage.GetChartInfoViewById(chartInfoId)
  1402. if err != nil {
  1403. if err.Error() == utils.ErrNoRow() {
  1404. br.Msg = "该图表已删除,自动查看下一图表"
  1405. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  1406. br.Ret = 406
  1407. return
  1408. }
  1409. br.Msg = "获取失败"
  1410. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1411. return
  1412. }
  1413. resp := new(data_manage.ChartInfoDetailResp)
  1414. // 图表数据权限
  1415. {
  1416. // 图表分类
  1417. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  1418. if err != nil {
  1419. br.Msg = "获取失败"
  1420. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  1421. return
  1422. }
  1423. // 已授权分类id
  1424. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  1425. if err != nil {
  1426. br.Msg = "获取失败"
  1427. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  1428. return
  1429. }
  1430. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  1431. }
  1432. // 获取主题样式
  1433. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  1434. if err != nil {
  1435. br.Msg = "获取失败"
  1436. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  1437. return
  1438. }
  1439. chartInfo.ChartThemeStyle = chartTheme.Config
  1440. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  1441. dateType := chartInfo.DateType
  1442. fmt.Println("dateType:", dateType)
  1443. chartType := chartInfo.ChartType
  1444. startDate := chartInfo.StartDate
  1445. endDate := chartInfo.EndDate
  1446. seasonStartDate := chartInfo.SeasonStartDate
  1447. seasonEndDate := chartInfo.SeasonEndDate
  1448. startYear := chartInfo.StartYear
  1449. calendar := chartInfo.Calendar
  1450. if calendar == "" {
  1451. calendar = "公历"
  1452. }
  1453. // 相关联指标
  1454. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1455. if err != nil {
  1456. br.Msg = "获取失败"
  1457. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1458. return
  1459. }
  1460. if chartType == 2 {
  1461. startDate = seasonStartDate
  1462. endDate = seasonEndDate
  1463. if dateType <= 0 {
  1464. if startDate != "" {
  1465. dateType = 5
  1466. } else {
  1467. dateType = utils.DateTypeNYears
  1468. }
  1469. }
  1470. } else {
  1471. if dateType <= 0 {
  1472. dateType = 3
  1473. }
  1474. }
  1475. yearMax := 0
  1476. if dateType == utils.DateTypeNYears {
  1477. for _, v := range mappingList {
  1478. if v.LatestDate != "" {
  1479. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  1480. if tErr != nil {
  1481. br.Msg = "获取失败"
  1482. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  1483. return
  1484. }
  1485. if lastDateT.Year() > yearMax {
  1486. yearMax = lastDateT.Year()
  1487. }
  1488. }
  1489. }
  1490. }
  1491. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  1492. if chartInfo.ChartType == 2 {
  1493. chartInfo.StartDate = startDate
  1494. chartInfo.EndDate = endDate
  1495. }
  1496. // 图表额外数据参数
  1497. extraConfigStr := chartInfo.ExtraConfig
  1498. // 柱方图的一些配置
  1499. var barConfig data_manage.BarChartInfoReq
  1500. if chartInfo != nil && chartInfo.ChartType == 7 {
  1501. if chartInfo.BarConfig == `` {
  1502. br.Msg = "柱方图未配置"
  1503. br.ErrMsg = "柱方图未配置"
  1504. return
  1505. }
  1506. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  1507. if err != nil {
  1508. br.Msg = "柱方图配置异常"
  1509. br.ErrMsg = "柱方图配置异常"
  1510. return
  1511. }
  1512. extraConfigStr = chartInfo.BarConfig
  1513. }
  1514. if chartInfo.HaveOperaAuth {
  1515. // 获取图表中的指标数据
  1516. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  1517. if err != nil {
  1518. br.Msg = "获取失败"
  1519. if errMsg != `` {
  1520. br.Msg = errMsg
  1521. }
  1522. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1523. return
  1524. }
  1525. // 单位
  1526. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  1527. chartInfo.Unit = yDataList[0].Unit
  1528. chartInfo.UnitEn = yDataList[0].UnitEn
  1529. }
  1530. warnEdbList := make([]string, 0)
  1531. for _, v := range edbList {
  1532. if v.IsNullData {
  1533. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  1534. }
  1535. }
  1536. if len(warnEdbList) > 0 {
  1537. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  1538. }
  1539. if chartInfoId > 0 && chartInfo != nil {
  1540. //判断是否加入我的图库
  1541. {
  1542. var myChartCondition string
  1543. var myChartPars []interface{}
  1544. myChartCondition += ` AND a.admin_id=? `
  1545. myChartPars = append(myChartPars, sysUser.AdminId)
  1546. myChartCondition += ` AND a.chart_info_id=? `
  1547. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1548. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1549. if err != nil && err.Error() != utils.ErrNoRow() {
  1550. br.Msg = "获取失败"
  1551. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1552. return
  1553. }
  1554. if myChartList != nil && len(myChartList) > 0 {
  1555. chartInfo.IsAdd = true
  1556. chartInfo.MyChartId = myChartList[0].MyChartId
  1557. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1558. }
  1559. }
  1560. }
  1561. //判断是否需要展示英文标识
  1562. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  1563. // 图表的指标来源
  1564. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  1565. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  1566. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  1567. // 指标权限
  1568. {
  1569. classifyMap := make(map[int]*data_manage.EdbClassify)
  1570. // 分类
  1571. {
  1572. classifyIdList := make([]int, 0)
  1573. for _, v := range edbList {
  1574. classifyIdList = append(classifyIdList, v.ClassifyId)
  1575. }
  1576. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1577. if tmpErr != nil {
  1578. br.Msg = "获取失败"
  1579. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  1580. return
  1581. }
  1582. for _, v := range classifyList {
  1583. classifyMap[v.ClassifyId] = v
  1584. }
  1585. }
  1586. // 获取所有有权限的指标和分类
  1587. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1588. if err != nil {
  1589. br.Msg = "获取失败"
  1590. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1591. return
  1592. }
  1593. for _, item := range edbList {
  1594. // 数据权限
  1595. if currClassify, ok := classifyMap[item.ClassifyId]; ok {
  1596. item.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(item.IsJoinPermission, currClassify.IsJoinPermission, item.EdbInfoId, item.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1597. }
  1598. }
  1599. }
  1600. resp.EdbInfoList = edbList
  1601. resp.XEdbIdValue = xEdbIdValue
  1602. resp.YDataList = yDataList
  1603. resp.DataResp = dataResp
  1604. } else {
  1605. resp.EdbInfoList = mappingList
  1606. }
  1607. //图表操作权限
  1608. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  1609. chartInfo.Button = data_manage.ChartViewButton{
  1610. IsEdit: chartInfo.IsEdit,
  1611. IsEnChart: chartInfo.IsEnChart,
  1612. IsAdd: chartInfo.IsAdd,
  1613. IsCopy: true,
  1614. IsSetName: chartInfo.IsSetName,
  1615. }
  1616. resp.ChartInfo = chartInfo
  1617. resp.BarChartInfo = barConfig
  1618. br.Ret = 200
  1619. br.Success = true
  1620. br.Msg = "获取成功"
  1621. br.Data = resp
  1622. }
  1623. // @Title 图表指标模糊搜索
  1624. // @Description 图表指标模糊搜索接口
  1625. // @Param KeyWord query string true "指标名称/ID"
  1626. // @Success 200 {object} data_manage.ChartEdbInfo
  1627. // @router /chart_info/edb_info/search [get]
  1628. func (this *ChartInfoController) ChartInfoEdbInfoSearch() {
  1629. br := new(models.BaseResponse).Init()
  1630. defer func() {
  1631. this.Data["json"] = br
  1632. this.ServeJSON()
  1633. }()
  1634. sysUser := this.SysUser
  1635. if sysUser == nil {
  1636. br.Msg = "请登录"
  1637. br.ErrMsg = "请登录,SysUser Is Empty"
  1638. br.Ret = 408
  1639. return
  1640. }
  1641. KeyWord := this.GetString("KeyWord")
  1642. //if KeyWord == "" {
  1643. // br.Msg = "请输入指标名称/指标ID"
  1644. // return
  1645. //}
  1646. var err error
  1647. searchList := make([]*data_manage.ChartEdbInfo, 0)
  1648. searchList, err = data_manage.EdbInfoSearchByKeyWord(KeyWord)
  1649. if err != nil {
  1650. br.Msg = "获取失败"
  1651. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1652. return
  1653. }
  1654. if searchList == nil {
  1655. searchList = make([]*data_manage.ChartEdbInfo, 0)
  1656. }
  1657. lenSearchList := len(searchList)
  1658. for i := 0; i < lenSearchList; i++ {
  1659. searchList[i].EdbNameAlias = searchList[i].EdbName
  1660. }
  1661. for i := 0; i < lenSearchList; i++ {
  1662. for j := 0; j < lenSearchList; j++ {
  1663. if (searchList[i].EdbNameAlias == searchList[j].EdbNameAlias) &&
  1664. (searchList[i].EdbInfoId != searchList[j].EdbInfoId) &&
  1665. !(strings.Contains(searchList[i].EdbName, searchList[i].SourceName)) {
  1666. searchList[i].EdbName = searchList[i].EdbName + "(" + searchList[i].SourceName + ")"
  1667. }
  1668. }
  1669. }
  1670. br.Ret = 200
  1671. br.Success = true
  1672. br.Msg = "获取成功"
  1673. br.Data = searchList
  1674. }
  1675. // @Title 图表-获取指标详情
  1676. // @Description 图表-获取指标详情接口
  1677. // @Param EdbInfoId query int true "指标ID"
  1678. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今"
  1679. // @Param StartDate query string true "自定义开始日期"
  1680. // @Param EndDate query string true "自定义结束日期"
  1681. // @Param EdbInfoType query int true "1:标准指标,0:领先指标,默认为标准指标"
  1682. // @Param LeadValue query int true "领先值"
  1683. // @Param LeadUnit query string true "领先单位:天,月,季,周"
  1684. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  1685. // @router /chart_info/edb_info/detail [get]
  1686. func (this *ChartInfoController) ChartInfoEdbInfoDetail() {
  1687. br := new(models.BaseResponse).Init()
  1688. defer func() {
  1689. this.Data["json"] = br
  1690. this.ServeJSON()
  1691. }()
  1692. sysUser := this.SysUser
  1693. if sysUser == nil {
  1694. br.Msg = "请登录"
  1695. br.ErrMsg = "请登录,SysUser Is Empty"
  1696. br.Ret = 408
  1697. return
  1698. }
  1699. edbInfoId, _ := this.GetInt("EdbInfoId")
  1700. if edbInfoId <= 0 {
  1701. br.Msg = "参数错误"
  1702. return
  1703. }
  1704. dateType, _ := this.GetInt("DateType")
  1705. if dateType <= 0 {
  1706. dateType = 3
  1707. }
  1708. edbInfoType, _ := this.GetInt("EdbInfoType")
  1709. if edbInfoType < 0 {
  1710. edbInfoType = 1
  1711. }
  1712. leadValue, _ := this.GetInt("LeadValue")
  1713. if leadValue < 0 {
  1714. leadValue = 0
  1715. }
  1716. leadUnit := this.GetString("LeadUnit")
  1717. startDate := this.GetString("StartDate")
  1718. endDate := this.GetString("EndDate")
  1719. startYear, _ := this.GetInt("StartYear")
  1720. isTimeBetween := false //是否是时间区间
  1721. if dateType == 5 {
  1722. isTimeBetween = true
  1723. }
  1724. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1725. if err != nil {
  1726. br.Msg = "获取失败"
  1727. br.Msg = "获取失败,Err:" + err.Error()
  1728. return
  1729. }
  1730. maxYear := 0
  1731. if edbInfo.LatestDate != "" {
  1732. latestDateT, _ := time.Parse(utils.FormatDate, edbInfo.LatestDate)
  1733. maxYear = latestDateT.Year()
  1734. }
  1735. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, maxYear)
  1736. if startDate == "" {
  1737. br.Msg = "参数错误"
  1738. br.Msg = "参数错误,无效的查询日期"
  1739. return
  1740. }
  1741. var startDateReal string
  1742. var diffSeconds int64
  1743. if leadValue > 0 && leadUnit != "" {
  1744. var startTimeRealTemp time.Time
  1745. startDateParse, err := time.Parse(utils.FormatDate, startDate)
  1746. if err != nil {
  1747. br.Msg = "获取数据失败"
  1748. br.ErrMsg = "获取数据失败,日期格式错误:startDate:" + startDate
  1749. return
  1750. }
  1751. switch leadUnit {
  1752. case "天":
  1753. startTimeRealTemp = startDateParse.AddDate(0, 0, -leadValue)
  1754. case "月":
  1755. startTimeRealTemp = startDateParse.AddDate(0, -leadValue, 0)
  1756. case "季":
  1757. startTimeRealTemp = startDateParse.AddDate(0, -3*leadValue, 0)
  1758. case "周":
  1759. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*leadValue)
  1760. case "年":
  1761. startTimeRealTemp = startDateParse.AddDate(-leadValue, 0, 0)
  1762. }
  1763. if startTimeRealTemp.Before(startDateParse) {
  1764. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  1765. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  1766. } else {
  1767. startDateReal = startDate
  1768. diffSeconds = 0
  1769. }
  1770. } else {
  1771. startDateReal = startDate
  1772. }
  1773. var dataList []*data_manage.EdbDataList
  1774. var minData, maxData float64
  1775. switch edbInfo.EdbInfoType {
  1776. case 0: //普通源指标
  1777. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDateReal, endDate)
  1778. if err != nil {
  1779. br.Msg = "获取失败"
  1780. br.Msg = "获取失败,Err:" + err.Error()
  1781. return
  1782. }
  1783. //查询区间内最大最小值
  1784. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(edbInfo.Source, edbInfo.SubSource, edbInfoId, startDateReal, endDate)
  1785. if err != nil {
  1786. br.Msg = "获取失败"
  1787. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  1788. return
  1789. }
  1790. case 1: //预测指标
  1791. tmpDataList, sourceEdbInfoItem, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDateReal, endDate, isTimeBetween)
  1792. if err != nil {
  1793. br.Msg = "获取失败"
  1794. br.Msg = "获取失败,Err:" + err.Error()
  1795. return
  1796. }
  1797. dataList = tmpDataList
  1798. // 有预测数据,且为普通的预测指标
  1799. if len(dataList) > 0 && edbInfo.EdbInfoType == 0 {
  1800. //查询区间内最大最小值
  1801. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, sourceEdbInfoItem.EdbInfoId, startDateReal, endDate)
  1802. if err != nil {
  1803. br.Msg = "获取失败"
  1804. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  1805. return
  1806. }
  1807. tmpValue := dataList[len(dataList)-1]
  1808. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1809. if edbInfo.MaxValue < tmpValue.Value {
  1810. maxData = tmpValue.Value
  1811. }
  1812. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1813. if edbInfo.MinValue > tmpValue.Value {
  1814. minData = tmpValue.Value
  1815. }
  1816. }
  1817. }
  1818. moveLatestDate := edbInfo.LatestDate
  1819. if diffSeconds != 0 {
  1820. dataListLen := len(dataList)
  1821. for i := 0; i < dataListLen; i++ {
  1822. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  1823. }
  1824. day, err := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  1825. if err != nil {
  1826. br.Msg = "获取失败"
  1827. br.Msg = "获取领先滞后的的日期间隔失败,Err:" + err.Error()
  1828. return
  1829. }
  1830. moveLatestDateTime, err := time.ParseInLocation(utils.FormatDate, moveLatestDate, time.Local)
  1831. if err != nil {
  1832. br.Msg = "获取失败"
  1833. br.Msg = "指标日期转换失败,Err:" + err.Error()
  1834. return
  1835. }
  1836. moveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  1837. }
  1838. mapping := new(data_manage.ChartEdbInfoMapping)
  1839. mapping.EdbInfoId = edbInfo.EdbInfoId
  1840. mapping.EdbInfoCategoryType = edbInfo.EdbInfoType
  1841. mapping.SourceName = edbInfo.SourceName
  1842. mapping.Source = edbInfo.Source
  1843. mapping.EdbCode = edbInfo.EdbCode
  1844. mapping.EdbName = edbInfo.EdbName
  1845. mapping.EdbType = edbInfo.EdbType
  1846. mapping.Frequency = edbInfo.Frequency
  1847. mapping.Unit = edbInfo.Unit
  1848. mapping.StartDate = edbInfo.StartDate
  1849. mapping.EndDate = edbInfo.EndDate
  1850. mapping.ChartEdbMappingId = 0
  1851. mapping.ChartInfoId = 0
  1852. mapping.MaxData = maxData
  1853. mapping.MinData = minData
  1854. mapping.IsOrder = false
  1855. mapping.IsAxis = 1
  1856. mapping.EdbInfoType = 1
  1857. mapping.LeadValue = 0
  1858. mapping.LeadUnit = ""
  1859. mapping.LeadUnitEn = ""
  1860. mapping.ChartStyle = ""
  1861. mapping.ChartColor = ""
  1862. mapping.PredictChartColor = ""
  1863. mapping.ChartWidth = 0
  1864. mapping.LatestDate = edbInfo.LatestDate
  1865. mapping.MoveLatestDate = moveLatestDate
  1866. mapping.DataList = dataList
  1867. resp := new(data_manage.ChartEdbInfoDetailResp)
  1868. resp.EdbInfo = mapping
  1869. br.Ret = 200
  1870. br.Success = true
  1871. br.Msg = "获取成功"
  1872. br.Data = resp
  1873. }
  1874. // ChartInfoSearch
  1875. // @Title 图表模糊搜索
  1876. // @Description 图表模糊搜索接口
  1877. // @Param KeyWord query string true "图表名称"
  1878. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1879. // @Success 200 {object} data_manage.ChartInfo
  1880. // @router /chart_info/search [get]
  1881. func (this *ChartInfoController) ChartInfoSearch() {
  1882. br := new(models.BaseResponse).Init()
  1883. defer func() {
  1884. this.Data["json"] = br
  1885. this.ServeJSON()
  1886. }()
  1887. sysUser := this.SysUser
  1888. if sysUser == nil {
  1889. br.Msg = "请登录"
  1890. br.ErrMsg = "请登录,SysUser Is Empty"
  1891. br.Ret = 408
  1892. return
  1893. }
  1894. keyWord := this.GetString("KeyWord")
  1895. searchList := make([]data_manage.ChartInfoMore, 0)
  1896. //var err error
  1897. //if KeyWord == "" {
  1898. // searchList, err = data_manage.ChartInfoSearchByKeyWord(KeyWord)
  1899. // if err != nil && err.Error() != utils.ErrNoRow() {
  1900. // br.Msg = "获取失败"
  1901. // br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1902. // return
  1903. // }
  1904. //} else {
  1905. // searchList, err = data.EsSearchChartInfo(KeyWord)
  1906. // if err != nil && err.Error() != utils.ErrNoRow() {
  1907. // br.Msg = "获取失败"
  1908. // br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1909. // return
  1910. // }
  1911. //}
  1912. //只看我的
  1913. isShowMe, _ := this.GetBool("IsShowMe")
  1914. showSysId := 0
  1915. if isShowMe {
  1916. showSysId = sysUser.AdminId
  1917. }
  1918. var keyWordArr []string
  1919. keyWordArr = append(keyWordArr, keyWord)
  1920. if keyWord != "" {
  1921. newKeyWord := strings.Split(keyWord, " ")
  1922. keyWordArr = append(keyWordArr, newKeyWord...)
  1923. }
  1924. existMap := make(map[int]string)
  1925. for _, v := range keyWordArr {
  1926. v = strings.Replace(v, " ", "%", -1)
  1927. newSearchList, err := data_manage.ChartInfoSearchByKeyWord(v, showSysId)
  1928. if err != nil && err.Error() != utils.ErrNoRow() {
  1929. br.Msg = "获取失败"
  1930. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1931. return
  1932. }
  1933. for _, nv := range newSearchList {
  1934. if _, ok := existMap[nv.ChartInfoId]; !ok {
  1935. searchList = append(searchList, data_manage.ChartInfoMore{
  1936. ChartInfo: *nv,
  1937. })
  1938. }
  1939. existMap[nv.ChartInfoId] = nv.ChartName
  1940. }
  1941. }
  1942. if searchList == nil {
  1943. searchList = make([]data_manage.ChartInfoMore, 0)
  1944. }
  1945. // 数据权限
  1946. if len(searchList) > 0 {
  1947. // 涉及分类id
  1948. classifyIdList := make([]int, 0)
  1949. for _, v := range searchList {
  1950. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  1951. }
  1952. // 图表分类
  1953. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  1954. if err != nil {
  1955. br.Msg = "获取失败"
  1956. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  1957. return
  1958. }
  1959. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  1960. for _, v := range chartClassifyList {
  1961. chartClassifyMap[v.ChartClassifyId] = v
  1962. }
  1963. // 已授权图表和分类id
  1964. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  1965. if err != nil {
  1966. br.Msg = "获取失败"
  1967. br.ErrMsg = "已授权图表和分类id失败,Err:" + err.Error()
  1968. return
  1969. }
  1970. // 权限判断
  1971. for k, v := range searchList {
  1972. if currClassify, ok := chartClassifyMap[v.ChartClassifyId]; ok {
  1973. searchList[k].HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  1974. }
  1975. }
  1976. }
  1977. //新增搜索词记录
  1978. {
  1979. searchKeyword := new(data_manage.SearchKeyword)
  1980. searchKeyword.KeyWord = keyWord
  1981. searchKeyword.CreateTime = time.Now()
  1982. go data_manage.AddSearchKeyword(searchKeyword)
  1983. }
  1984. br.Ret = 200
  1985. br.Success = true
  1986. br.Msg = "获取成功"
  1987. br.Data = searchList
  1988. }
  1989. // ChartInfoSearchByEs
  1990. // @Title 图表模糊搜索(从es获取)
  1991. // @Description 图表模糊搜索(从es获取)
  1992. // @Param Keyword query string true "图表名称"
  1993. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1994. // @Success 200 {object} data_manage.ChartInfo
  1995. // @router /chart_info/search_by_es [get]
  1996. func (this *ChartInfoController) ChartInfoSearchByEs() {
  1997. br := new(models.BaseResponse).Init()
  1998. defer func() {
  1999. this.Data["json"] = br
  2000. this.ServeJSON()
  2001. }()
  2002. sysUser := this.SysUser
  2003. if sysUser == nil {
  2004. br.Msg = "请登录"
  2005. br.ErrMsg = "请登录,SysUser Is Empty"
  2006. br.Ret = 408
  2007. return
  2008. }
  2009. pageSize, _ := this.GetInt("PageSize")
  2010. currentIndex, _ := this.GetInt("CurrentIndex")
  2011. var startSize int
  2012. if pageSize <= 0 {
  2013. pageSize = utils.PageSize20
  2014. }
  2015. if currentIndex <= 0 {
  2016. currentIndex = 1
  2017. }
  2018. startSize = paging.StartIndex(currentIndex, pageSize)
  2019. keyword := this.GetString("Keyword")
  2020. //只看我的
  2021. isShowMe, _ := this.GetBool("IsShowMe")
  2022. showSysId := 0
  2023. if isShowMe {
  2024. showSysId = sysUser.AdminId
  2025. }
  2026. var searchList []*data_manage.ChartInfoMore
  2027. var total int64
  2028. var err error
  2029. // 获取当前账号的不可见指标
  2030. noPermissionChartIdList := make([]int, 0)
  2031. {
  2032. obj := data_manage.EdbInfoNoPermissionAdmin{}
  2033. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  2034. if err != nil && err.Error() != utils.ErrNoRow() {
  2035. br.Msg = "获取失败"
  2036. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  2037. return
  2038. }
  2039. for _, v := range confList {
  2040. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  2041. }
  2042. }
  2043. // 是否走ES
  2044. isEs := false
  2045. if keyword != "" {
  2046. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList, startSize, pageSize)
  2047. isEs = true
  2048. } else {
  2049. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList, startSize, pageSize)
  2050. if err != nil && err.Error() != utils.ErrNoRow() {
  2051. br.Msg = "获取失败"
  2052. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2053. return
  2054. }
  2055. }
  2056. finalList := make([]*data_manage.ChartInfoMore, 0)
  2057. if len(searchList) > 0 {
  2058. // 涉及分类id
  2059. classifyIdList := make([]int, 0)
  2060. // 图表id
  2061. chartInfoIdList := make([]int, 0)
  2062. chartInfoIds := ""
  2063. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  2064. for _, v := range searchList {
  2065. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  2066. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  2067. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  2068. }
  2069. if chartInfoIds != "" {
  2070. chartInfoIds = strings.Trim(chartInfoIds, ",")
  2071. //判断是否需要展示英文标识
  2072. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  2073. if e != nil {
  2074. br.Msg = "获取失败"
  2075. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  2076. return
  2077. }
  2078. for _, v := range edbList {
  2079. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  2080. }
  2081. }
  2082. // 当前列表中的分类map
  2083. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  2084. // 图表分类
  2085. {
  2086. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  2087. if err != nil {
  2088. br.Msg = "获取失败"
  2089. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  2090. return
  2091. }
  2092. for _, v := range chartClassifyList {
  2093. chartClassifyMap[v.ChartClassifyId] = v
  2094. }
  2095. }
  2096. // 图表
  2097. if isEs {
  2098. // 当前列表中的图表map
  2099. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  2100. tmpChartList, err := data_manage.GetChartInfoByIdList(chartInfoIdList)
  2101. if err != nil {
  2102. br.Msg = "获取失败"
  2103. br.ErrMsg = "获取所有有权限的图表失败,Err:" + err.Error()
  2104. return
  2105. }
  2106. for _, tmpChartInfo := range tmpChartList {
  2107. chartInfoMap[tmpChartInfo.ChartInfoId] = tmpChartInfo
  2108. }
  2109. for _, v := range searchList {
  2110. // 图表权限
  2111. chartViewInfo, ok := chartInfoMap[v.ChartInfoId]
  2112. if !ok {
  2113. continue
  2114. }
  2115. v.IsJoinPermission = chartViewInfo.IsJoinPermission
  2116. }
  2117. }
  2118. // 已授权图表和分类id
  2119. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2120. if err != nil {
  2121. br.Msg = "获取失败"
  2122. br.ErrMsg = "已授权图表和分类id失败,Err:" + err.Error()
  2123. return
  2124. }
  2125. for _, v := range searchList {
  2126. tmp := new(data_manage.ChartInfoMore)
  2127. tmp.ChartInfo = v.ChartInfo
  2128. //判断是否需要展示英文标识
  2129. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  2130. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  2131. }
  2132. // 图表权限
  2133. if currClassify, ok := chartClassifyMap[v.ChartClassifyId]; ok {
  2134. tmp.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.ChartInfoId, v.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2135. }
  2136. tmp.SearchText = v.SearchText
  2137. if tmp.SearchText == "" {
  2138. tmp.SearchText = v.ChartName
  2139. }
  2140. finalList = append(finalList, tmp)
  2141. }
  2142. }
  2143. //新增搜索词记录
  2144. {
  2145. searchKeyword := new(data_manage.SearchKeyword)
  2146. searchKeyword.KeyWord = keyword
  2147. searchKeyword.CreateTime = time.Now()
  2148. go data_manage.AddSearchKeyword(searchKeyword)
  2149. }
  2150. page := paging.GetPaging(currentIndex, pageSize, int(total))
  2151. resp := data_manage.ChartInfoListByEsResp{
  2152. Paging: page,
  2153. List: finalList,
  2154. }
  2155. br.Ret = 200
  2156. br.Success = true
  2157. br.Msg = "获取成功"
  2158. br.Data = resp
  2159. }
  2160. // @Title 图表刷新接口
  2161. // @Description 图表刷新接口
  2162. // @Param ChartInfoId query int true "图表id"
  2163. // @Param UniqueCode query string true "唯一code"
  2164. // @Success Ret=200 刷新成功
  2165. // @router /chart_info/refresh [get]
  2166. func (this *EdbInfoController) ChartInfoRefresh() {
  2167. br := new(models.BaseResponse).Init()
  2168. defer func() {
  2169. this.Data["json"] = br
  2170. this.ServeJSON()
  2171. }()
  2172. sysUser := this.SysUser
  2173. if sysUser == nil {
  2174. br.Msg = "请登录"
  2175. br.ErrMsg = "请登录,SysUser Is Empty"
  2176. br.Ret = 408
  2177. return
  2178. }
  2179. chartInfoId, _ := this.GetInt("ChartInfoId")
  2180. uniqueCode := this.GetString("UniqueCode")
  2181. if chartInfoId <= 0 && uniqueCode == `` {
  2182. br.Msg = "参数错误"
  2183. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  2184. return
  2185. }
  2186. var chartInfo *data_manage.ChartInfo
  2187. var err error
  2188. if chartInfoId > 0 {
  2189. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  2190. } else {
  2191. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  2192. }
  2193. if err != nil {
  2194. if err.Error() == utils.ErrNoRow() {
  2195. br.Msg = "图表已被删除,无需刷新"
  2196. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  2197. return
  2198. }
  2199. br.Msg = "刷新失败"
  2200. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2201. return
  2202. }
  2203. err, isAsync := data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  2204. if err != nil {
  2205. br.Msg = "刷新失败"
  2206. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2207. return
  2208. }
  2209. /*
  2210. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  2211. //if err != nil {
  2212. // br.Msg = "刷新失败"
  2213. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2214. // return
  2215. //}
  2216. */
  2217. //清除图表缓存
  2218. {
  2219. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  2220. _ = utils.Rc.Delete(key)
  2221. }
  2222. br.Ret = 200
  2223. br.Success = true
  2224. br.Msg = "刷新成功"
  2225. if isAsync {
  2226. br.Msg = "图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2227. }
  2228. }
  2229. // @Title 设置图表图片
  2230. // @Description 设置图表图片接口
  2231. // @Param request body data_manage.SetChartInfoImageReq true "type json string"
  2232. // @Success Ret=200 保存成功
  2233. // @router /chart_info/image/set [post]
  2234. func (this *ChartInfoController) ChartInfoImageSet() {
  2235. br := new(models.BaseResponse).Init()
  2236. defer func() {
  2237. this.Data["json"] = br
  2238. this.ServeJSON()
  2239. }()
  2240. sysUser := this.SysUser
  2241. if sysUser == nil {
  2242. br.Msg = "请登录"
  2243. br.ErrMsg = "请登录,SysUser Is Empty"
  2244. br.Ret = 408
  2245. return
  2246. }
  2247. var req data_manage.SetChartInfoImageReq
  2248. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2249. if err != nil {
  2250. br.Msg = "参数解析异常!"
  2251. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2252. return
  2253. }
  2254. if req.ChartInfoId > 0 && req.ImageUrl != "" {
  2255. err = data_manage.EditChartInfoImage(&req)
  2256. if err != nil {
  2257. br.Msg = "保存失败"
  2258. br.ErrMsg = "保存失败,Err:" + err.Error()
  2259. return
  2260. }
  2261. //修改es数据
  2262. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  2263. //修改my eta es数据
  2264. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  2265. }
  2266. br.Ret = 200
  2267. br.Success = true
  2268. br.Msg = "保存成功"
  2269. }
  2270. // ChartInfoDetailFromUniqueCode
  2271. // @Title 根据编码获取图表详情
  2272. // @Description 根据编码获取图表详情接口
  2273. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  2274. // @Param IsCache query bool true "是否走缓存,默认false"
  2275. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  2276. // @router /chart_info/detail/from_unique_code [get]
  2277. func (this *ChartInfoController) ChartInfoDetailFromUniqueCode() {
  2278. br := new(models.BaseResponse).Init()
  2279. defer func() {
  2280. this.Data["json"] = br
  2281. this.ServeJSON()
  2282. }()
  2283. sysUser := this.SysUser
  2284. if sysUser == nil {
  2285. br.Msg = "请登录"
  2286. br.ErrMsg = "请登录,SysUser Is Empty"
  2287. br.Ret = 408
  2288. return
  2289. }
  2290. uniqueCode := this.GetString("UniqueCode")
  2291. if uniqueCode == "" {
  2292. br.Msg = "参数错误"
  2293. br.ErrMsg = "参数错误,uniqueCode is empty"
  2294. return
  2295. }
  2296. //是否走缓存
  2297. isCache, _ := this.GetBool("IsCache")
  2298. status := true
  2299. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  2300. if err != nil {
  2301. if err.Error() == utils.ErrNoRow() {
  2302. status = false
  2303. } else {
  2304. br.Msg = "获取失败"
  2305. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  2306. return
  2307. }
  2308. }
  2309. if chartInfo == nil {
  2310. status = false
  2311. }
  2312. if !status {
  2313. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2314. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  2315. resp.EdbInfoList = endInfoList
  2316. resp.ChartInfo = chartInfo
  2317. resp.Status = false
  2318. return
  2319. }
  2320. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  2321. if !isOk {
  2322. br.Msg = msg
  2323. br.ErrMsg = errMsg
  2324. return
  2325. }
  2326. br.Ret = 200
  2327. br.Success = true
  2328. msg = "获取成功"
  2329. br.Data = resp
  2330. }
  2331. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  2332. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  2333. msg = `获取失败`
  2334. adminId := sysUser.AdminId
  2335. // 图表数据权限
  2336. // 图表分类
  2337. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  2338. if err != nil {
  2339. errMsg = "获取图表分类信息失败,Err:" + err.Error()
  2340. return
  2341. }
  2342. // 已授权分类id
  2343. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  2344. if err != nil {
  2345. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  2346. return
  2347. }
  2348. // 指标数据map
  2349. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  2350. defer func() {
  2351. if isOk {
  2352. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  2353. {
  2354. //判断是否加入我的图库
  2355. var myChartCondition string
  2356. var myChartPars []interface{}
  2357. myChartCondition += ` AND a.admin_id=? `
  2358. myChartPars = append(myChartPars, adminId)
  2359. myChartCondition += ` AND a.chart_info_id=? `
  2360. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  2361. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  2362. if err != nil && err.Error() != utils.ErrNoRow() {
  2363. msg = "获取失败"
  2364. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  2365. return
  2366. }
  2367. if myChartList != nil && len(myChartList) > 0 {
  2368. chartInfo.IsAdd = true
  2369. chartInfo.MyChartId = myChartList[0].MyChartId
  2370. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  2371. }
  2372. }
  2373. // 图表权限校验
  2374. {
  2375. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  2376. //图表操作权限
  2377. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  2378. chartInfo.Button = data_manage.ChartViewButton{
  2379. IsEdit: chartInfo.IsEdit,
  2380. IsEnChart: chartInfo.IsEnChart,
  2381. IsAdd: chartInfo.IsAdd,
  2382. IsCopy: true,
  2383. IsSetName: chartInfo.IsSetName,
  2384. }
  2385. //if !resp.ChartInfo.HaveOperaAuth {
  2386. // for _, v := range resp.EdbInfoList {
  2387. // v.DataList = nil
  2388. // }
  2389. // resp.DataResp = nil
  2390. // resp.XEdbIdValue = []int{}
  2391. // resp.YDataList = []data_manage.YData{}
  2392. //}
  2393. }
  2394. // 指标权限
  2395. {
  2396. classifyMap := make(map[int]*data_manage.EdbClassify)
  2397. // 分类
  2398. {
  2399. classifyIdList := make([]int, 0)
  2400. for _, v := range resp.EdbInfoList {
  2401. classifyIdList = append(classifyIdList, v.ClassifyId)
  2402. }
  2403. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  2404. if tmpErr != nil {
  2405. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  2406. return
  2407. }
  2408. for _, v := range classifyList {
  2409. classifyMap[v.ClassifyId] = v
  2410. }
  2411. }
  2412. // 指标
  2413. if len(edbClassifyPermissionMap) < 0 {
  2414. edbInfoIdList := make([]int, 0)
  2415. for _, v := range resp.EdbInfoList {
  2416. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  2417. }
  2418. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  2419. if tmpErr != nil {
  2420. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  2421. return
  2422. }
  2423. for _, v := range edbInfoList {
  2424. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2425. ClassifyId: v.ClassifyId,
  2426. IsJoinPermission: v.IsJoinPermission,
  2427. EdbInfoId: v.EdbInfoId,
  2428. }
  2429. }
  2430. }
  2431. // 获取所有有权限的指标和分类
  2432. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  2433. if err != nil {
  2434. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  2435. return
  2436. }
  2437. for _, v := range resp.EdbInfoList {
  2438. // 数据权限
  2439. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  2440. if !ok {
  2441. continue
  2442. }
  2443. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  2444. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  2445. }
  2446. }
  2447. }
  2448. }
  2449. }()
  2450. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  2451. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  2452. if utils.Re == nil && isCache {
  2453. if utils.Re == nil && utils.Rc.IsExist(key) {
  2454. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  2455. err := json.Unmarshal(redisData, &resp)
  2456. if err != nil || resp == nil {
  2457. return
  2458. }
  2459. isOk = true
  2460. fmt.Println("source redis")
  2461. return
  2462. }
  2463. }
  2464. }
  2465. // 获取主题样式
  2466. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  2467. if err != nil {
  2468. msg = "获取失败"
  2469. errMsg = "获取主题信息失败,Err:" + err.Error()
  2470. return
  2471. }
  2472. chartInfo.ChartThemeStyle = chartTheme.Config
  2473. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  2474. chartInfoId := chartInfo.ChartInfoId
  2475. dateType := chartInfo.DateType
  2476. startDate := chartInfo.StartDate
  2477. endDate := chartInfo.EndDate
  2478. startYear := chartInfo.StartYear
  2479. calendar := chartInfo.Calendar
  2480. chartType := chartInfo.ChartType
  2481. if calendar == "" {
  2482. calendar = "公历"
  2483. }
  2484. fmt.Println("a dateType:", dateType)
  2485. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  2486. if err != nil {
  2487. msg = "获取失败"
  2488. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2489. return
  2490. }
  2491. yearMax := 0
  2492. if dateType == utils.DateTypeNYears {
  2493. for _, v := range mappingList {
  2494. if v.LatestDate != "" {
  2495. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  2496. if tErr != nil {
  2497. msg = "获取失败"
  2498. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  2499. return
  2500. }
  2501. if lastDateT.Year() > yearMax {
  2502. yearMax = lastDateT.Year()
  2503. }
  2504. }
  2505. }
  2506. }
  2507. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  2508. extraConfigStr := chartInfo.ExtraConfig //图表额外数据参数
  2509. var barConfig data_manage.BarChartInfoReq
  2510. // 柱方图的一些配置
  2511. if chartInfo != nil && chartInfo.ChartType == 7 {
  2512. if chartInfo.BarConfig == `` {
  2513. msg = "柱方图未配置"
  2514. errMsg = "柱方图未配置"
  2515. return
  2516. }
  2517. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  2518. if err != nil {
  2519. msg = "柱方图配置异常"
  2520. errMsg = "柱方图配置异常"
  2521. return
  2522. }
  2523. extraConfigStr = chartInfo.BarConfig
  2524. }
  2525. // 获取图表中的指标数据
  2526. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  2527. if err != nil {
  2528. msg = "获取失败"
  2529. if errMsg != `` {
  2530. msg = errMsg
  2531. }
  2532. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  2533. return
  2534. }
  2535. // 单位
  2536. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  2537. chartInfo.Unit = yDataList[0].Unit
  2538. chartInfo.UnitEn = yDataList[0].UnitEn
  2539. }
  2540. warnEdbList := make([]string, 0)
  2541. for _, v := range edbList {
  2542. if v.IsNullData {
  2543. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  2544. }
  2545. }
  2546. if len(warnEdbList) > 0 {
  2547. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  2548. }
  2549. /*
  2550. ChartClassify []*ChartClassifyView
  2551. */
  2552. chartViewClassify, _ := data_manage.GetChartClassifyViewById(chartInfo.ChartClassifyId)
  2553. if chartViewClassify != nil {
  2554. chartClassifyParent, _ := data_manage.GetChartClassifyViewById(chartClassify.ParentId)
  2555. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
  2556. }
  2557. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartViewClassify)
  2558. // 面积图 面积堆积 数据处理
  2559. if chartInfo.ChartType == utils.CHART_TYPE_AREA {
  2560. err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
  2561. if err != nil {
  2562. msg = "获取失败"
  2563. errMsg = "面积图面积堆积数据处理失败,Err:" + err.Error()
  2564. return
  2565. }
  2566. }
  2567. resp.EdbInfoList = edbList
  2568. //判断是否需要展示英文标识
  2569. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  2570. // 图表的指标来源
  2571. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  2572. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  2573. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  2574. resp.ChartInfo = chartInfo
  2575. resp.XEdbIdValue = xEdbIdValue
  2576. resp.YDataList = yDataList
  2577. resp.Status = true
  2578. resp.BarChartInfo = barConfig
  2579. resp.DataResp = dataResp
  2580. // 将数据加入缓存
  2581. if utils.Re == nil {
  2582. data, _ := json.Marshal(resp)
  2583. utils.Rc.Put(key, data, 2*time.Hour)
  2584. }
  2585. isOk = true
  2586. // 遍历到校验map
  2587. for _, v := range edbList {
  2588. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  2589. ClassifyId: v.ClassifyId,
  2590. IsJoinPermission: v.IsJoinPermission,
  2591. EdbInfoId: v.EdbInfoId,
  2592. }
  2593. }
  2594. return
  2595. }
  2596. // @Param Img query string true "图片"
  2597. // @Success 200 {object} models.ResourceResp
  2598. // @router /chart_info/base64Upload [post]
  2599. func (this *ChartInfoController) ChartInfoBase64Upload() {
  2600. br := new(models.BaseResponse).Init()
  2601. defer func() {
  2602. this.Data["json"] = br
  2603. this.ServeJSON()
  2604. }()
  2605. imgData := this.GetString("Img")
  2606. if imgData == "" {
  2607. br.Msg = "图片参数错误"
  2608. br.ErrMsg = "图片参数错误,Img Is Empty"
  2609. return
  2610. }
  2611. resp := new(models.ResourceResp)
  2612. uploadDir := "static/images/"
  2613. if !utils.FileIsExist(uploadDir) {
  2614. err := os.MkdirAll(uploadDir, utils.DIR_MOD)
  2615. if err != nil {
  2616. br.Msg = "图表保存失败"
  2617. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  2618. return
  2619. }
  2620. }
  2621. //var saveToOssPath string
  2622. randStr := utils.GetRandStringNoSpecialChar(28)
  2623. var fileName, outFileName string
  2624. fileName = randStr + ".txt"
  2625. fileName = uploadDir + fileName
  2626. err := utils.SaveToFile(imgData, fileName)
  2627. if err != nil {
  2628. br.Msg = "图片保存失败"
  2629. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  2630. return
  2631. }
  2632. outFileName = randStr + ".png"
  2633. doneChannel := make(chan bool, 1)
  2634. errorChannel := make(chan error, 1)
  2635. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  2636. go func() {
  2637. output, err := cmd.CombinedOutput()
  2638. if err != nil {
  2639. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  2640. errorChannel <- err
  2641. return
  2642. }
  2643. doneChannel <- true
  2644. }()
  2645. select {
  2646. case <-time.After(30 * time.Second):
  2647. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  2648. e := cmd.Process.Kill()
  2649. if e != nil {
  2650. fmt.Println("cmd kill err: ", e.Error())
  2651. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  2652. br.Msg = "图片生成失败"
  2653. br.ErrMsg = "图片生成失败, 执行超时" + e.Error()
  2654. return
  2655. }
  2656. fmt.Println("timeout kill process")
  2657. case <-doneChannel:
  2658. fmt.Println("done")
  2659. case err := <-errorChannel:
  2660. br.Msg = "文件上传失败"
  2661. br.ErrMsg = fmt.Sprintf("execute command failure err: %s", err.Error())
  2662. fmt.Println("execute command failure err:" + err.Error())
  2663. return
  2664. }
  2665. defer func() {
  2666. os.Remove(fileName)
  2667. os.Remove(outFileName)
  2668. }()
  2669. //saveToOssPath = uploadDir + time.Now().Format("200601/20060102/")
  2670. //saveToOssPath += outFileName
  2671. //上传到阿里云 和 minio
  2672. resourceUrl := ``
  2673. //if utils.ObjectStorageClient == "minio" {
  2674. // err = services.UploadFileToMinIo("", outFileName, saveToOssPath)
  2675. // if err != nil {
  2676. // br.Msg = "文件上传失败"
  2677. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2678. // return
  2679. // }
  2680. // resourceUrl = utils.MinIoImghost + saveToOssPath
  2681. //} else {
  2682. // err = services.UploadFileToAliyun("", outFileName, saveToOssPath)
  2683. // if err != nil {
  2684. // br.Msg = "文件上传失败"
  2685. // br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2686. // return
  2687. // }
  2688. // resourceUrl = utils.Imghost + saveToOssPath
  2689. //}
  2690. ossClient := services.NewOssClient()
  2691. if ossClient == nil {
  2692. br.Msg = "上传失败"
  2693. br.ErrMsg = "初始化OSS服务失败"
  2694. return
  2695. }
  2696. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  2697. if err != nil {
  2698. br.Msg = "文件上传失败"
  2699. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  2700. return
  2701. }
  2702. resp.ResourceUrl = resourceUrl
  2703. resp.Source = "convert"
  2704. //resp.CacheKey = imgDataKey
  2705. br.Msg = "上传成功"
  2706. br.Ret = 200
  2707. br.Success = true
  2708. br.Data = resp
  2709. return
  2710. }
  2711. // BatchChartInfoRefresh
  2712. // @Title 批量刷新图表接口
  2713. // @Description 批量刷新图表接口
  2714. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  2715. // @Success Ret=200 刷新成功
  2716. // @router /chart_info/batch_refresh [post]
  2717. func (this *EdbInfoController) BatchChartInfoRefresh() {
  2718. br := new(models.BaseResponse).Init()
  2719. defer func() {
  2720. this.Data["json"] = br
  2721. this.ServeJSON()
  2722. }()
  2723. sysUser := this.SysUser
  2724. if sysUser == nil {
  2725. br.Msg = "请登录"
  2726. br.ErrMsg = "请登录,SysUser Is Empty"
  2727. br.Ret = 408
  2728. return
  2729. }
  2730. var req data_manage.BatchChartRefreshReq
  2731. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2732. if err != nil {
  2733. br.Msg = "参数解析异常!"
  2734. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2735. return
  2736. }
  2737. //获取所有的图表列表
  2738. _, chartInfoList, err := data_manage.GetChartInfoListByUniqueCodeSlice(req.ChartInfoCode)
  2739. if len(chartInfoList) <= 0 {
  2740. br.Ret = 200
  2741. br.Success = true
  2742. br.Msg = "刷新成功"
  2743. return
  2744. }
  2745. redisKey := data.GetBatchChartRefreshKey(req.Source, req.ReportId, req.ReportChapterId)
  2746. // 图表中的指标刷新
  2747. err, isAsync := data.BatchChartInfoRefreshV2(chartInfoList, redisKey)
  2748. if err != nil {
  2749. br.Msg = "刷新失败"
  2750. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  2751. return
  2752. }
  2753. br.Msg = "刷新成功"
  2754. if isAsync {
  2755. br.Msg = "研报中图表关联指标较多,请10分钟后刷新页面查看最新数据"
  2756. }
  2757. br.Ret = 200
  2758. br.Success = true
  2759. }
  2760. // CopyChartInfo
  2761. // @Title 复制并新增图表接口
  2762. // @Description 新增图表接口
  2763. // @Param request body data_manage.CopyAddChartInfoReq true "type json string"
  2764. // @Success 200 {object} data_manage.AddChartInfoResp
  2765. // @router /chart_info/copy [post]
  2766. func (this *ChartInfoController) CopyChartInfo() {
  2767. br := new(models.BaseResponse).Init()
  2768. defer func() {
  2769. this.Data["json"] = br
  2770. this.ServeJSON()
  2771. }()
  2772. sysUser := this.SysUser
  2773. if sysUser == nil {
  2774. br.Msg = "请登录"
  2775. br.ErrMsg = "请登录,SysUser Is Empty"
  2776. br.Ret = 408
  2777. return
  2778. }
  2779. deleteCache := true
  2780. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  2781. defer func() {
  2782. if deleteCache {
  2783. utils.Rc.Delete(cacheKey)
  2784. }
  2785. }()
  2786. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2787. deleteCache = false
  2788. br.Msg = "系统处理中,请稍后重试!"
  2789. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  2790. return
  2791. }
  2792. var req data_manage.CopyAddChartInfoReq
  2793. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2794. if err != nil {
  2795. br.Msg = "参数解析异常!"
  2796. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2797. return
  2798. }
  2799. // 获取原图表信息
  2800. oldChartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  2801. if err != nil {
  2802. br.Msg = "获取原图表信息失败"
  2803. br.ErrMsg = "获取原图表信息失败,Err:" + err.Error()
  2804. return
  2805. }
  2806. //校验图表名称是否重复
  2807. req.ChartName = strings.Trim(req.ChartName, " ")
  2808. if req.ChartName == "" {
  2809. br.Msg = "请填写图表名称!"
  2810. return
  2811. }
  2812. if req.ChartClassifyId <= 0 {
  2813. br.Msg = "分类参数错误!"
  2814. return
  2815. }
  2816. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2817. if err != nil {
  2818. if err.Error() == utils.ErrNoRow() {
  2819. br.Msg = "分类不存在"
  2820. br.ErrMsg = "分类不存在"
  2821. return
  2822. }
  2823. br.Msg = "获取分类信息失败"
  2824. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  2825. return
  2826. }
  2827. if chartClassify == nil {
  2828. br.Msg = "分类不存在"
  2829. br.ErrMsg = "分类不存在"
  2830. return
  2831. }
  2832. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  2833. //if err != nil && err.Error() != utils.ErrNoRow() {
  2834. // br.Msg = "保存失败"
  2835. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2836. // return
  2837. //}
  2838. //if count > 0 {
  2839. // br.Msg = "所选指标已存在相同指标"
  2840. // return
  2841. //}
  2842. //判断图表是否存在
  2843. var condition string
  2844. var pars []interface{}
  2845. condition += " AND chart_classify_id=? "
  2846. pars = append(pars, req.ChartClassifyId)
  2847. condition += " AND chart_name=? AND source = ? "
  2848. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2849. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  2850. if err != nil {
  2851. br.Msg = "判断图表名称是否存在失败"
  2852. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2853. return
  2854. }
  2855. if count > 0 {
  2856. br.Msg = "图表已存在,请重新填写"
  2857. br.IsSendEmail = false
  2858. return
  2859. }
  2860. //获取原图表关联的指标信息列表
  2861. edbMappingList, err := data_manage.GetChartEdbMappingList(req.ChartInfoId)
  2862. if err != nil {
  2863. br.Msg = "获取图表关联的指标信息失败!"
  2864. return
  2865. }
  2866. //添加图表
  2867. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2868. chartInfo := &data_manage.ChartInfo{
  2869. ChartInfoId: 0,
  2870. ChartName: req.ChartName,
  2871. ChartClassifyId: req.ChartClassifyId,
  2872. SysUserId: sysUser.AdminId,
  2873. SysUserRealName: sysUser.RealName,
  2874. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2875. CreateTime: time.Now(),
  2876. ModifyTime: time.Now(),
  2877. DateType: oldChartInfo.DateType,
  2878. StartDate: oldChartInfo.StartDate,
  2879. EndDate: oldChartInfo.EndDate,
  2880. IsSetName: oldChartInfo.IsSetName,
  2881. EdbInfoIds: oldChartInfo.EdbInfoIds,
  2882. ChartType: oldChartInfo.ChartType,
  2883. Calendar: oldChartInfo.Calendar,
  2884. SeasonStartDate: oldChartInfo.SeasonStartDate,
  2885. SeasonEndDate: oldChartInfo.SeasonEndDate,
  2886. ChartImage: oldChartInfo.ChartImage,
  2887. BarConfig: oldChartInfo.BarConfig,
  2888. //Sort: sort,
  2889. LeftMin: oldChartInfo.LeftMin,
  2890. LeftMax: oldChartInfo.LeftMax,
  2891. RightMin: oldChartInfo.RightMin,
  2892. RightMax: oldChartInfo.RightMax,
  2893. Right2Min: oldChartInfo.Right2Min,
  2894. Right2Max: oldChartInfo.Right2Max,
  2895. Disabled: oldChartInfo.Disabled,
  2896. Source: oldChartInfo.Source,
  2897. ExtraConfig: oldChartInfo.ExtraConfig,
  2898. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  2899. StartYear: oldChartInfo.StartYear,
  2900. Unit: oldChartInfo.Unit,
  2901. UnitEn: oldChartInfo.UnitEn,
  2902. ChartThemeId: oldChartInfo.ChartThemeId,
  2903. SourcesFrom: oldChartInfo.SourcesFrom,
  2904. Instructions: oldChartInfo.Instructions,
  2905. MarkersLines: oldChartInfo.MarkersLines,
  2906. MarkersAreas: oldChartInfo.MarkersAreas,
  2907. }
  2908. newId, err := data_manage.AddChartInfo(chartInfo)
  2909. if err != nil {
  2910. br.Msg = "保存失败"
  2911. br.ErrMsg = "保存失败,Err:" + err.Error()
  2912. return
  2913. }
  2914. chartInfo.ChartInfoId = int(newId)
  2915. // 添加图表与指标的关联关系
  2916. {
  2917. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2918. for _, v := range edbMappingList {
  2919. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2920. mapItem := &data_manage.ChartEdbMapping{
  2921. //ChartEdbMappingId: 0,
  2922. ChartInfoId: chartInfo.ChartInfoId,
  2923. EdbInfoId: v.EdbInfoId,
  2924. CreateTime: time.Now(),
  2925. ModifyTime: time.Now(),
  2926. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  2927. MaxData: v.MaxData,
  2928. MinData: v.MinData,
  2929. IsOrder: v.IsOrder,
  2930. IsAxis: v.IsAxis,
  2931. EdbInfoType: v.EdbInfoType,
  2932. LeadValue: v.LeadValue,
  2933. LeadUnit: v.LeadUnit,
  2934. ChartStyle: v.ChartStyle,
  2935. ChartColor: v.ChartColor,
  2936. ChartWidth: v.ChartWidth,
  2937. Source: v.Source,
  2938. EdbAliasName: v.EdbAliasName,
  2939. IsConvert: v.IsConvert,
  2940. ConvertType: v.ConvertType,
  2941. ConvertValue: v.ConvertValue,
  2942. ConvertUnit: v.ConvertEnUnit,
  2943. ConvertEnUnit: v.ConvertEnUnit,
  2944. }
  2945. mapList = append(mapList, mapItem)
  2946. }
  2947. err = data_manage.AddChartEdbMapping(mapList)
  2948. if err != nil {
  2949. br.Msg = "保存失败"
  2950. br.ErrMsg = "保存失败,Err:" + err.Error()
  2951. return
  2952. }
  2953. }
  2954. //添加es数据
  2955. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2956. //修改my eta es数据
  2957. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  2958. //新增操作日志
  2959. {
  2960. chartLog := new(data_manage.ChartInfoLog)
  2961. chartLog.ChartInfoId = chartInfo.ChartInfoId
  2962. chartLog.ChartName = req.ChartName
  2963. chartLog.ChartClassifyId = req.ChartClassifyId
  2964. chartLog.SysUserId = sysUser.AdminId
  2965. chartLog.SysUserRealName = sysUser.RealName
  2966. chartLog.UniqueCode = chartInfo.UniqueCode
  2967. chartLog.CreateTime = time.Now()
  2968. chartLog.Content = string(this.Ctx.Input.RequestBody)
  2969. chartLog.Status = "新增图表"
  2970. chartLog.Method = this.Ctx.Input.URI()
  2971. go data_manage.AddChartInfoLog(chartLog)
  2972. }
  2973. // 试用平台更新用户累计新增图表数
  2974. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  2975. if e != nil {
  2976. br.Msg = "操作失败"
  2977. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  2978. return
  2979. }
  2980. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  2981. go func() {
  2982. var r etaTrialService.EtaTrialUserReq
  2983. r.Mobile = adminItem.Mobile
  2984. _, _ = etaTrialService.UpdateUserChartNum(r)
  2985. }()
  2986. }
  2987. br.Ret = 200
  2988. br.Success = true
  2989. br.Msg = "保存成功"
  2990. br.Data = data_manage.AddChartInfoResp{
  2991. ChartInfoId: chartInfo.ChartInfoId,
  2992. UniqueCode: chartInfo.UniqueCode,
  2993. ChartType: chartInfo.ChartType,
  2994. }
  2995. br.IsAddLog = true
  2996. }
  2997. // PreviewBarChartInfo
  2998. // @Title 图表-获取预览的柱方图数据
  2999. // @Description 图表-获取指标详情接口
  3000. // @Param request body data_manage.BarChartInfoReq true "type json string"
  3001. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3002. // @router /chart_info/preview_bar [post]
  3003. func (this *ChartInfoController) PreviewBarChartInfo() {
  3004. br := new(models.BaseResponse).Init()
  3005. defer func() {
  3006. this.Data["json"] = br
  3007. this.ServeJSON()
  3008. }()
  3009. var req data_manage.BarChartInfoReq
  3010. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3011. if err != nil {
  3012. br.Msg = "参数解析异常!"
  3013. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3014. return
  3015. }
  3016. sysUser := this.SysUser
  3017. if sysUser == nil {
  3018. br.Msg = "请登录"
  3019. br.ErrMsg = "请登录,SysUser Is Empty"
  3020. br.Ret = 408
  3021. return
  3022. }
  3023. if len(req.EdbInfoIdList) <= 0 {
  3024. br.Msg = "请选择指标"
  3025. return
  3026. }
  3027. if len(req.DateList) <= 0 {
  3028. br.Msg = "请选择日期"
  3029. return
  3030. }
  3031. chartInfo := new(data_manage.ChartInfoView)
  3032. chartType := utils.CHART_TYPE_BAR
  3033. //edbInfoIdList := make([]int, 0)
  3034. edbInfoIdsList := make([]string, 0)
  3035. convertMap := make(map[int]*data_manage.ChartEdbInfoMapping, 0)
  3036. for _, v := range req.EdbInfoIdList {
  3037. edbInfoIdsList = append(edbInfoIdsList, strconv.Itoa(v.EdbInfoId))
  3038. convertMap[v.EdbInfoId] = &data_manage.ChartEdbInfoMapping{
  3039. IsConvert: v.IsConvert,
  3040. ConvertType: v.ConvertType,
  3041. ConvertValue: v.ConvertValue,
  3042. ConvertUnit: v.ConvertUnit,
  3043. ConvertEnUnit: v.ConvertEnUnit,
  3044. }
  3045. }
  3046. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoId(strings.Join(edbInfoIdsList, ","))
  3047. if err != nil {
  3048. br.Msg = "获取失败"
  3049. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3050. return
  3051. }
  3052. for i, item := range mappingList {
  3053. if _, ok := convertMap[item.EdbInfoId]; ok {
  3054. mappingList[i].ConvertValue = convertMap[item.EdbInfoId].ConvertValue
  3055. mappingList[i].ConvertUnit = convertMap[item.EdbInfoId].ConvertUnit
  3056. mappingList[i].ConvertEnUnit = convertMap[item.EdbInfoId].ConvertEnUnit
  3057. mappingList[i].ConvertType = convertMap[item.EdbInfoId].ConvertType
  3058. mappingList[i].IsConvert = convertMap[item.EdbInfoId].IsConvert
  3059. }
  3060. }
  3061. // 获取图表中的指标数据
  3062. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, string(this.Ctx.Input.RequestBody), "")
  3063. if err != nil {
  3064. br.Msg = "获取失败"
  3065. if errMsg != `` {
  3066. br.Msg = errMsg
  3067. }
  3068. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3069. return
  3070. }
  3071. // 单位
  3072. if len(yDataList) > 0 {
  3073. chartInfo.Unit = yDataList[0].Unit
  3074. chartInfo.UnitEn = yDataList[0].UnitEn
  3075. }
  3076. warnEdbList := make([]string, 0)
  3077. for _, v := range edbList {
  3078. if v.IsNullData {
  3079. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3080. }
  3081. for _, reqEdb := range req.EdbInfoIdList {
  3082. if v.EdbInfoId == reqEdb.EdbInfoId {
  3083. v.EdbAliasName = reqEdb.Name
  3084. }
  3085. }
  3086. }
  3087. if len(warnEdbList) > 0 {
  3088. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3089. }
  3090. // 图表的指标来源
  3091. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3092. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3093. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3094. //图表操作权限
  3095. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3096. //判断是否需要展示英文标识
  3097. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3098. chartInfo.Button = data_manage.ChartViewButton{
  3099. IsEdit: chartInfo.IsEdit,
  3100. IsEnChart: chartInfo.IsEnChart,
  3101. IsAdd: chartInfo.IsAdd,
  3102. IsCopy: true,
  3103. IsSetName: chartInfo.IsSetName,
  3104. }
  3105. resp := new(data_manage.ChartInfoDetailResp)
  3106. resp.ChartInfo = chartInfo
  3107. resp.EdbInfoList = edbList
  3108. resp.XEdbIdValue = xEdbIdValue
  3109. resp.YDataList = yDataList
  3110. resp.DataResp = dataResp
  3111. br.Ret = 200
  3112. br.Success = true
  3113. br.Msg = "获取成功"
  3114. br.Data = resp
  3115. }
  3116. // PreviewSectionScatterChartInfo
  3117. // @Title 图表-获取预览的截面散点图数据
  3118. // @Description 图表-获取指标详情接口
  3119. // @Param request body data_manage.SectionScatter true "type json string"
  3120. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3121. // @router /chart_info/preview/time_section [post]
  3122. func (this *ChartInfoController) PreviewSectionScatterChartInfo() {
  3123. br := new(models.BaseResponse).Init()
  3124. defer func() {
  3125. this.Data["json"] = br
  3126. this.ServeJSON()
  3127. }()
  3128. var req data_manage.PreviewSectionScatterChartReq
  3129. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3130. if err != nil {
  3131. br.Msg = "参数解析异常!"
  3132. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3133. return
  3134. }
  3135. chartInfo := new(data_manage.ChartInfoView)
  3136. sysUser := this.SysUser
  3137. if sysUser == nil {
  3138. br.Msg = "请登录"
  3139. br.ErrMsg = "请登录,SysUser Is Empty"
  3140. br.Ret = 408
  3141. return
  3142. }
  3143. chartType := utils.CHART_TYPE_SECTION_SCATTER
  3144. edbInfoIdArr, err, errMsg := data.CheckChartExtraConfig(chartType, req.ExtraConfig)
  3145. if err != nil {
  3146. br.Msg = errMsg
  3147. br.ErrMsg = "添加失败:" + err.Error()
  3148. return
  3149. }
  3150. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3151. // 获取图表中的指标数据
  3152. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3153. if err != nil {
  3154. br.Msg = "获取失败"
  3155. if errMsg != `` {
  3156. br.Msg = errMsg
  3157. }
  3158. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3159. return
  3160. }
  3161. warnEdbList := make([]string, 0)
  3162. for _, v := range edbList {
  3163. if v.IsNullData {
  3164. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3165. }
  3166. }
  3167. if len(warnEdbList) > 0 {
  3168. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3169. }
  3170. // 图表的指标来源
  3171. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3172. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3173. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3174. //图表操作权限
  3175. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3176. //判断是否需要展示英文标识
  3177. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3178. chartInfo.Button = data_manage.ChartViewButton{
  3179. IsEdit: chartInfo.IsEdit,
  3180. IsEnChart: chartInfo.IsEnChart,
  3181. IsAdd: chartInfo.IsAdd,
  3182. IsCopy: true,
  3183. IsSetName: chartInfo.IsSetName,
  3184. }
  3185. resp := new(data_manage.ChartInfoDetailResp)
  3186. resp.ChartInfo = chartInfo
  3187. resp.EdbInfoList = edbList
  3188. resp.XEdbIdValue = xEdbIdValue
  3189. resp.YDataList = yDataList
  3190. resp.DataResp = dataResp
  3191. br.Ret = 200
  3192. br.Success = true
  3193. br.Msg = "获取成功"
  3194. br.Data = resp
  3195. }
  3196. // GetBatchChartRefreshResult
  3197. // @Title 获取批量刷新图表结果的接口
  3198. // @Description 获取批量刷新图表结果的接口
  3199. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  3200. // @Success Ret=200 刷新成功
  3201. // @router /chart_info/batch_refresh/result [post]
  3202. func (this *EdbInfoController) GetBatchChartRefreshResult() {
  3203. br := new(models.BaseResponse).Init()
  3204. defer func() {
  3205. this.Data["json"] = br
  3206. this.ServeJSON()
  3207. }()
  3208. sysUser := this.SysUser
  3209. if sysUser == nil {
  3210. br.Msg = "请登录"
  3211. br.ErrMsg = "请登录,SysUser Is Empty"
  3212. br.Ret = 408
  3213. return
  3214. }
  3215. var req data_manage.BatchChartRefreshReq
  3216. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3217. if err != nil {
  3218. br.Msg = "参数解析异常!"
  3219. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3220. return
  3221. }
  3222. // 刷新结果,默认是刷新完成了
  3223. refreshResult := data.CheckBatchChartRefreshResult(req.Source, req.ReportId, req.ReportChapterId)
  3224. resp := response.ChartRefreshResp{
  3225. RefreshResult: refreshResult,
  3226. }
  3227. br.Data = resp
  3228. br.Ret = 200
  3229. br.Success = true
  3230. }
  3231. //
  3232. //修复数据时间戳
  3233. //func init() {
  3234. // fmt.Println("start")
  3235. // edbInfoList,err:=data_manage.GetEdbInfo()
  3236. // if err!=nil {
  3237. // fmt.Println("Err:",err.Error())
  3238. // return
  3239. // }
  3240. // startDate:=time.Now().AddDate(-40,0,0).Format(utils.FormatDate)
  3241. // endDate:=time.Now().Format(utils.FormatDate)
  3242. // for _,v:=range edbInfoList{
  3243. // dataList,err:=data_manage.GetEdbDataList(v.Source,v.EdbInfoId,startDate,endDate)
  3244. // if err!=nil {
  3245. // fmt.Println("GetEdbDataList Err:",err.Error())
  3246. // return
  3247. // }
  3248. // for _,dv:=range dataList{
  3249. // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3250. // if err!=nil {
  3251. // fmt.Println("time.Parse Err:"+err.Error())
  3252. // return
  3253. // }
  3254. // timestamp:=dataTime.UnixNano()/1e6
  3255. // err=data_manage.ModifyEdbDatadTimestamp(v.Source,dv.EdbDataId,timestamp)
  3256. // if err!=nil{
  3257. // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3258. // return
  3259. // }
  3260. // fmt.Println(v.Source,dv.EdbDataId,timestamp)
  3261. // }
  3262. // }
  3263. // //time.Sleep(2*time.Minute)
  3264. //
  3265. // //startDate:=time.Now().AddDate(-30,0,0).Format(utils.FormatDate)
  3266. // //endDate:=time.Now().Format(utils.FormatDate)
  3267. // //dataList,err:=data_manage.GetEdbDataList(1,100099,startDate,endDate)
  3268. // //if err!=nil {
  3269. // // fmt.Println("GetEdbDataList Err:",err.Error())
  3270. // // return
  3271. // //}
  3272. // //for _,dv:=range dataList{
  3273. // // dataTime,err:=time.Parse(utils.FormatDate,dv.DataTime)
  3274. // // if err!=nil {
  3275. // // fmt.Println("time.Parse Err:"+err.Error())
  3276. // // return
  3277. // // }
  3278. // // timestamp:=dataTime.UnixNano()/1e6
  3279. // // err=data_manage.ModifyEdbDatadTimestamp(2,dv.EdbDataId,timestamp)
  3280. // // if err!=nil{
  3281. // // fmt.Println("ModifyEdbDatadTimestamp Err:"+err.Error())
  3282. // // return
  3283. // // }
  3284. // // fmt.Println(2,dv.EdbDataId,timestamp)
  3285. // //}
  3286. //
  3287. // fmt.Println("end")
  3288. //}
  3289. //修复指标配置数据
  3290. //func init() {
  3291. // fmt.Println("start")
  3292. // edbInfo, err := data_manage.GetEdbInfoAll()
  3293. // fmt.Println(err)
  3294. // for k, v := range edbInfoAll {
  3295. // fmt.Println(k, v.EdbInfoId)
  3296. // item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  3297. // fmt.Println(item, err)
  3298. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  3299. // if err != nil {
  3300. // fmt.Println("ModifyEdbInfoMaxAndMinInfo Err:" + err.Error())
  3301. // }
  3302. // }
  3303. // fmt.Println("end")
  3304. //}
  3305. //func init() {
  3306. // fmt.Println("start")
  3307. // var yearArr []int
  3308. // yearArr = append(yearArr, 2021)
  3309. // yearArr = append(yearArr, 2010)
  3310. // yearArr = append(yearArr, 2020)
  3311. // yearArr = append(yearArr, 2017)
  3312. // yearArr = append(yearArr, 2019)
  3313. // sort.Sort(sort.Reverse(sort.IntSlice(yearArr)))
  3314. // fmt.Println(yearArr)
  3315. // fmt.Println("end")
  3316. //}
  3317. //季度指标数据计算(公历转农历)
  3318. //func init() {
  3319. // fmt.Println("start AddCalculateQuarter")
  3320. // list:=make([]*data_manage.EdbDataList,0)
  3321. // data_manage.AddCalculateQuarterV4(list)
  3322. // fmt.Println("end AddCalculateQuarter")
  3323. // //return
  3324. //}
  3325. /*
  3326. {
  3327. "ChartClassifyId": 58,
  3328. "ChartName": "公历转农历6",
  3329. "ChartType": 2,
  3330. "ChartEdbInfoList": [
  3331. {
  3332. "ChartColor": "#00f",
  3333. "ChartStyle": "spline",
  3334. "ChartWidth": 3,
  3335. "EdbInfoId": 100466,
  3336. "EdbInfoType": 1,
  3337. "IsAxis": 1,
  3338. "IsOrder": false,
  3339. "LeadUnit": "",
  3340. "LeadValue": 0,
  3341. "MaxData": 0.2943,
  3342. "MinData": -0.2448
  3343. }
  3344. ]
  3345. }
  3346. */
  3347. //func init() {
  3348. // fmt.Println("start")
  3349. // data_manage.AddCalculateQuarter(100466,6,"C2108252836")
  3350. // fmt.Println("end")
  3351. //}
  3352. //func init() {
  3353. // data.AddAllChartInfo()
  3354. //}
  3355. // 截面散点示例数据
  3356. //func init() {
  3357. //
  3358. // seriesList := []data_manage.SectionScatterSeriesItemReq{
  3359. // {
  3360. // Name: "系列1名称",
  3361. // Color: "红红火火色",
  3362. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3363. // {
  3364. // XEdbInfoId: 1,
  3365. // YEdbInfoId: 2,
  3366. // Name: "破铜",
  3367. // NameEn: "Po Tong",
  3368. // XDateType: 1,
  3369. // XDate: "",
  3370. // XDateValue: 0,
  3371. // YDateType: 2,
  3372. // YDate: "",
  3373. // YDateValue: 30,
  3374. // IsShow: false,
  3375. // },
  3376. // {
  3377. // XEdbInfoId: 3,
  3378. // YEdbInfoId: 4,
  3379. // Name: "烂铁",
  3380. // NameEn: "Lan Tie",
  3381. // XDateType: 2,
  3382. // XDate: "",
  3383. // XDateValue: 30,
  3384. // YDateType: 3,
  3385. // YDate: "2023-02-17",
  3386. // YDateValue: 0,
  3387. // IsShow: false,
  3388. // },
  3389. // },
  3390. // ShowLine: false,
  3391. // ShowEquation: false,
  3392. // ShowR: false,
  3393. // },
  3394. // {
  3395. // Name: "系列2名称",
  3396. // Color: "恍恍惚惚色",
  3397. // EdbList: []data_manage.SectionScatterEdbItemReq{
  3398. // {
  3399. // XEdbInfoId: 1,
  3400. // YEdbInfoId: 2,
  3401. // Name: "破铜",
  3402. // NameEn: "Po Tong",
  3403. // XDateType: 3,
  3404. // XDate: "2023-02-17",
  3405. // XDateValue: 0,
  3406. // YDateType: 2,
  3407. // YDate: "",
  3408. // YDateValue: 60,
  3409. // IsShow: true,
  3410. // },
  3411. // {
  3412. // XEdbInfoId: 3,
  3413. // YEdbInfoId: 4,
  3414. // Name: "烂铁",
  3415. // NameEn: "Lan Tie",
  3416. // XDateType: 3,
  3417. // XDate: "2023-02-17",
  3418. // XDateValue: 0,
  3419. // YDateType: 2,
  3420. // YDate: "",
  3421. // YDateValue: 60,
  3422. // IsShow: false,
  3423. // },
  3424. // },
  3425. // ShowLine: false,
  3426. // ShowEquation: false,
  3427. // ShowR: false,
  3428. // },
  3429. // }
  3430. // rel := data_manage.SectionScatter{
  3431. // XName: "X轴名称",
  3432. // XNameEn: "X english name",
  3433. // XUnitName: "X轴单位",
  3434. // XUnitNameEn: "X unit english name",
  3435. // YName: "Y轴名称",
  3436. // YNameEn: "Y english name",
  3437. // YUnitName: "Y轴单位",
  3438. // YUnitNameEn: "Y unit english name",
  3439. // SeriesList: seriesList,
  3440. // }
  3441. // jsonByte, _ := json.Marshal(rel)
  3442. // utils.FileLog.Info(string(jsonByte))
  3443. //}
  3444. // func init() {
  3445. // var a, b float64
  3446. //
  3447. // testX := []float64{1, 3, 8, 7, 9}
  3448. // testY := []float64{10, 12, 24, 21, 34}
  3449. // {
  3450. // coordinateData := make([]utils.Coordinate, 0)
  3451. // for k, x := range testX {
  3452. // tmpCoordinate1 := utils.Coordinate{
  3453. // X: x,
  3454. // Y: testY[k],
  3455. // }
  3456. // coordinateData = append(coordinateData, tmpCoordinate1)
  3457. // }
  3458. // a, b = utils.GetLinearResult(coordinateData)
  3459. // trendLine := fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  3460. //
  3461. // fmt.Println(trendLine)
  3462. // }
  3463. //
  3464. // {
  3465. // r2 := utils.CalculationDecisive(testX, testY)
  3466. // fmt.Println(r2)
  3467. // }
  3468. // }
  3469. // FixChart 修复季节性图的日期数据
  3470. //func FixChart() {
  3471. // var condition string
  3472. // var pars []interface{}
  3473. //
  3474. // // 普通图表
  3475. // condition += ` AND chart_type = ? AND season_start_date != "" `
  3476. // pars = append(pars, 2)
  3477. //
  3478. // //获取图表信息
  3479. // list, err := data_manage.GetChartInfoListByCondition(condition, pars, 0, 10000)
  3480. // if err != nil {
  3481. // fmt.Println("err:", err)
  3482. // return
  3483. // }
  3484. //
  3485. // for _, v := range list {
  3486. // fmt.Println(v)
  3487. // v.SeasonStartDate = v.SeasonStartDate + "-01"
  3488. // v.SeasonEndDate = v.SeasonEndDate + "-12"
  3489. // err = v.Update([]string{"SeasonStartDate", "SeasonEndDate"})
  3490. // if err != nil {
  3491. // fmt.Println(v.ChartInfoId, ";", v.ChartName, "修改失敗")
  3492. // }
  3493. // data.EsAddOrEditChartInfo(v.ChartInfoId)
  3494. // data.EsAddOrEditMyChartInfoByChartInfoId(v.ChartInfoId)
  3495. // }
  3496. //}
  3497. // PreviewRadarChartInfo
  3498. // @Title 图表-获取预览的雷达图
  3499. // @Description 图表-获取预览的雷达图
  3500. // @Param ExtraConfig body data_manage.PreviewRadarChartReq true "type json string"
  3501. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  3502. // @router /chart_info/preview/radar [post]
  3503. func (this *ChartInfoController) PreviewRadarChartInfo() {
  3504. br := new(models.BaseResponse).Init()
  3505. defer func() {
  3506. this.Data["json"] = br
  3507. this.ServeJSON()
  3508. }()
  3509. var req data_manage.PreviewRadarChartReq
  3510. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3511. if err != nil {
  3512. br.Msg = "参数解析异常!"
  3513. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3514. return
  3515. }
  3516. chartInfo := new(data_manage.ChartInfoView)
  3517. sysUser := this.SysUser
  3518. if sysUser == nil {
  3519. br.Msg = "请登录"
  3520. br.ErrMsg = "请登录,SysUser Is Empty"
  3521. br.Ret = 408
  3522. return
  3523. }
  3524. chartType := utils.CHART_TYPE_RADAR
  3525. if len(req.ChartEdbInfoList) <= 0 {
  3526. br.Msg = "请选择指标"
  3527. return
  3528. }
  3529. edbInfoIdArr := make([]int, 0)
  3530. edbInfoNameMap := make(map[int]string)
  3531. for _, v := range req.ChartEdbInfoList {
  3532. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  3533. edbInfoNameMap[v.EdbInfoId] = v.EdbAliasName
  3534. }
  3535. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdArr)
  3536. for _, v := range mappingList {
  3537. if name, ok := edbInfoNameMap[v.EdbInfoId]; ok {
  3538. v.EdbAliasName = name
  3539. }
  3540. }
  3541. // 获取图表中的指标数据
  3542. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(0, chartType, "", "", "", mappingList, req.ExtraConfig, "")
  3543. if err != nil {
  3544. br.Msg = "获取失败"
  3545. if errMsg != `` {
  3546. br.Msg = errMsg
  3547. }
  3548. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3549. return
  3550. }
  3551. warnEdbList := make([]string, 0)
  3552. for _, v := range edbList {
  3553. if v.IsNullData {
  3554. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3555. }
  3556. }
  3557. if len(warnEdbList) > 0 {
  3558. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3559. }
  3560. //图表操作权限
  3561. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  3562. //判断是否需要展示英文标识
  3563. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, utils.CHART_SOURCE_DEFAULT, chartType)
  3564. chartInfo.Button = data_manage.ChartViewButton{
  3565. IsEdit: chartInfo.IsEdit,
  3566. IsEnChart: chartInfo.IsEnChart,
  3567. IsAdd: chartInfo.IsAdd,
  3568. IsCopy: true,
  3569. IsSetName: chartInfo.IsSetName,
  3570. }
  3571. // 图表的指标来源
  3572. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3573. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3574. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3575. resp := new(data_manage.ChartInfoDetailResp)
  3576. resp.ChartInfo = chartInfo
  3577. resp.EdbInfoList = edbList
  3578. resp.XEdbIdValue = xEdbIdValue
  3579. resp.YDataList = yDataList
  3580. resp.DataResp = dataResp
  3581. br.Ret = 200
  3582. br.Success = true
  3583. br.Msg = "获取成功"
  3584. br.Data = resp
  3585. }
  3586. // ChartInfoDetail
  3587. // @Title 获取图表详情-数据转换
  3588. // @Description 获取图表详情接口-数据转换
  3589. // @Param ChartInfoId query int true "图表id"
  3590. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  3591. // @Param StartDate query string true "自定义开始日期"
  3592. // @Param EndDate query string true "自定义结束日期"
  3593. // @Param Calendar query string true "公历/农历"
  3594. // @Param SeasonStartDate query string true "季节性图开始日期"
  3595. // @Param SeasonEndDate query string true "季节性图结束日期"
  3596. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  3597. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  3598. // @Success 200 {object} data_manage.ChartInfoDetailResp
  3599. // @router /chart_info/convert/detail [get]
  3600. func (this *ChartInfoController) ChartInfoConvertDetail() {
  3601. br := new(models.BaseResponse).Init()
  3602. defer func() {
  3603. this.Data["json"] = br
  3604. this.ServeJSON()
  3605. }()
  3606. sysUser := this.SysUser
  3607. if sysUser == nil {
  3608. br.Msg = "请登录"
  3609. br.ErrMsg = "请登录,SysUser Is Empty"
  3610. br.Ret = 408
  3611. return
  3612. }
  3613. chartInfoId, _ := this.GetInt("ChartInfoId")
  3614. dateType, _ := this.GetInt("DateType")
  3615. fmt.Println("dateType:", dateType)
  3616. if dateType <= 0 {
  3617. dateType = 3
  3618. }
  3619. startDate := this.GetString("StartDate")
  3620. endDate := this.GetString("EndDate")
  3621. startYear, _ := this.GetInt("StartYear")
  3622. edbInfoId := this.GetString("EdbInfoId")
  3623. chartType, _ := this.GetInt("ChartType")
  3624. calendar := this.GetString("Calendar")
  3625. if calendar == "" {
  3626. calendar = "公历"
  3627. }
  3628. isConvert, _ := this.GetInt("IsConvert")
  3629. convertType, _ := this.GetInt("ConvertType")
  3630. convertValue, _ := this.GetFloat("ConvertValue")
  3631. convertUnit := this.GetString("ConvertUnit")
  3632. convertEnUnit := this.GetString("ConvertEnUnit")
  3633. isAxis, _ := this.GetInt("IsAxis")
  3634. resp := new(data_manage.ChartInfoDetailResp)
  3635. var err error
  3636. chartInfo := new(data_manage.ChartInfoView)
  3637. chartInfo.HaveOperaAuth = true
  3638. // 通过id获取详情
  3639. if chartInfoId > 0 {
  3640. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  3641. if err != nil {
  3642. if err.Error() == utils.ErrNoRow() {
  3643. br.Msg = "该图表已删除,自动查看下一图表"
  3644. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  3645. br.Ret = 406
  3646. return
  3647. }
  3648. br.Msg = "获取失败"
  3649. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  3650. return
  3651. }
  3652. chartType = chartInfo.ChartType
  3653. // 获取主题样式
  3654. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  3655. if err != nil {
  3656. br.Msg = "获取失败"
  3657. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  3658. return
  3659. }
  3660. chartInfo.ChartThemeStyle = chartTheme.Config
  3661. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  3662. // 图表数据权限
  3663. {
  3664. // 图表分类
  3665. chartClassify, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  3666. if err != nil {
  3667. br.Msg = "获取失败"
  3668. br.ErrMsg = "获取图表分类信息失败,Err:" + err.Error()
  3669. return
  3670. }
  3671. // 已授权分类id
  3672. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  3673. if err != nil {
  3674. br.Msg = "获取失败"
  3675. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  3676. return
  3677. }
  3678. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  3679. }
  3680. //图表操作权限
  3681. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  3682. chartInfo.Button = data_manage.ChartViewButton{
  3683. IsEdit: chartInfo.IsEdit,
  3684. IsEnChart: chartInfo.IsEnChart,
  3685. IsAdd: chartInfo.IsAdd,
  3686. IsCopy: true,
  3687. IsSetName: chartInfo.IsSetName,
  3688. }
  3689. }
  3690. // 有数据权限的话,获取图表的数据
  3691. if chartInfo.HaveOperaAuth {
  3692. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  3693. if chartInfoId > 0 {
  3694. mappingList, err = data_manage.GetChartEdbMappingList(chartInfoId)
  3695. if err != nil {
  3696. br.Msg = "获取失败"
  3697. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3698. return
  3699. }
  3700. } else {
  3701. if edbInfoId != "" {
  3702. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoId(edbInfoId)
  3703. if err != nil {
  3704. br.Msg = "获取失败"
  3705. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3706. return
  3707. }
  3708. if isConvert == 1 && len(mappingList) > 0 {
  3709. for i, _ := range mappingList {
  3710. mappingList[i].IsConvert = 1
  3711. mappingList[i].ConvertType = convertType
  3712. mappingList[i].ConvertValue = convertValue
  3713. mappingList[i].ConvertUnit = convertUnit
  3714. mappingList[i].ConvertEnUnit = convertEnUnit
  3715. }
  3716. }
  3717. }
  3718. }
  3719. // 图表额外数据参数
  3720. extraConfigStr := chartInfo.ExtraConfig
  3721. // 柱方图的一些配置
  3722. var barConfig data_manage.BarChartInfoReq
  3723. if chartInfo != nil && chartInfo.ChartType == 7 {
  3724. if chartInfo.BarConfig == `` {
  3725. br.Msg = "柱方图未配置"
  3726. br.ErrMsg = "柱方图未配置"
  3727. return
  3728. }
  3729. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  3730. if err != nil {
  3731. br.Msg = "柱方图配置异常"
  3732. br.ErrMsg = "柱方图配置异常"
  3733. return
  3734. }
  3735. extraConfigStr = chartInfo.BarConfig
  3736. }
  3737. yearMax := 0
  3738. if dateType == utils.DateTypeNYears {
  3739. for _, v := range mappingList {
  3740. if v.LatestDate != "" {
  3741. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  3742. if tErr != nil {
  3743. br.Msg = "获取失败"
  3744. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  3745. return
  3746. }
  3747. if lastDateT.Year() > yearMax {
  3748. yearMax = lastDateT.Year()
  3749. }
  3750. }
  3751. }
  3752. }
  3753. // 开始/结束日期
  3754. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  3755. // 获取图表中的指标数据
  3756. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartConvertEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, isAxis)
  3757. if err != nil {
  3758. br.Msg = "获取失败"
  3759. if errMsg != `` {
  3760. br.Msg = errMsg
  3761. }
  3762. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  3763. return
  3764. }
  3765. // 单位
  3766. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  3767. chartInfo.Unit = yDataList[0].Unit
  3768. chartInfo.UnitEn = yDataList[0].UnitEn
  3769. }
  3770. warnEdbList := make([]string, 0)
  3771. for _, v := range edbList {
  3772. if v.IsNullData {
  3773. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  3774. }
  3775. }
  3776. if len(warnEdbList) > 0 {
  3777. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  3778. }
  3779. if chartInfoId > 0 && chartInfo != nil {
  3780. //判断是否加入我的图库
  3781. {
  3782. var myChartCondition string
  3783. var myChartPars []interface{}
  3784. myChartCondition += ` AND a.admin_id=? `
  3785. myChartPars = append(myChartPars, sysUser.AdminId)
  3786. myChartCondition += ` AND a.chart_info_id=? `
  3787. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  3788. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  3789. if err != nil && err.Error() != utils.ErrNoRow() {
  3790. br.Msg = "获取失败"
  3791. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  3792. return
  3793. }
  3794. if myChartList != nil && len(myChartList) > 0 {
  3795. chartInfo.IsAdd = true
  3796. chartInfo.MyChartId = myChartList[0].MyChartId
  3797. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  3798. }
  3799. }
  3800. if chartInfo.ChartType == 2 {
  3801. if chartInfo.SeasonStartDate != "" {
  3802. chartInfo.StartDate = chartInfo.SeasonStartDate
  3803. chartInfo.EndDate = chartInfo.SeasonEndDate
  3804. if chartInfo.DateType == 3 {
  3805. chartInfo.DateType = 5
  3806. }
  3807. }
  3808. }
  3809. }
  3810. // 图表的指标来源
  3811. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  3812. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  3813. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  3814. //判断是否需要展示英文标识
  3815. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  3816. resp.EdbInfoList = edbList
  3817. resp.XEdbIdValue = xEdbIdValue
  3818. resp.YDataList = yDataList
  3819. resp.DataResp = dataResp
  3820. resp.BarChartInfo = barConfig
  3821. }
  3822. resp.ChartInfo = chartInfo
  3823. br.Ret = 200
  3824. br.Success = true
  3825. br.Msg = "获取成功"
  3826. br.Data = resp
  3827. }
  3828. // ChartInfoImgSetByBase64
  3829. // @Title 图表图片上传
  3830. // @Param Img query string true "图片"
  3831. // @Param ChartInfoId query int true "图表ID"
  3832. // @Success 200 {object} models.ResourceResp
  3833. // @router /chart_info/image/set_by_base64 [post]
  3834. func (this *ChartInfoController) ChartInfoImgSetByBase64() {
  3835. br := new(models.BaseResponse).Init()
  3836. defer func() {
  3837. this.Data["json"] = br
  3838. this.ServeJSON()
  3839. }()
  3840. imgData := this.GetString("Img")
  3841. if imgData == "" {
  3842. br.Msg = "图片参数错误"
  3843. br.ErrMsg = "图片参数错误,Img Is Empty"
  3844. return
  3845. }
  3846. chartInfoId, _ := this.GetInt("ChartInfoId", 0)
  3847. if chartInfoId <= 0 {
  3848. br.Msg = "图片参数错误"
  3849. br.ErrMsg = "图片参数错误,Img Is Empty"
  3850. return
  3851. }
  3852. resp := new(models.ResourceResp)
  3853. uploadDir := "static/images/"
  3854. if !utils.FileIsExist(uploadDir) {
  3855. err := os.MkdirAll(uploadDir, utils.DIR_MOD)
  3856. if err != nil {
  3857. br.Msg = "图表保存失败"
  3858. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3859. return
  3860. }
  3861. }
  3862. //var saveToOssPath string
  3863. randStr := utils.GetRandStringNoSpecialChar(28)
  3864. var fileName, outFileName string
  3865. fileName = randStr + ".txt"
  3866. fileName = uploadDir + fileName
  3867. err := utils.SaveToFile(imgData, fileName)
  3868. if err != nil {
  3869. br.Msg = "图片保存失败"
  3870. br.ErrMsg = "图片保存失败,Err:" + err.Error()
  3871. return
  3872. }
  3873. // 删除临时存储的svg文件
  3874. defer func() {
  3875. err = os.Remove(fileName)
  3876. if err != nil {
  3877. utils.FileLog.Info("删除临时存储的svg文件失败, err: " + err.Error())
  3878. }
  3879. }()
  3880. outFileName = randStr + ".png"
  3881. doneChannel := make(chan bool, 1)
  3882. errorChannel := make(chan error, 1)
  3883. cmd := exec.Command("highcharts-export-server", "--infile", fileName, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "3", "--outfile", outFileName)
  3884. go func() {
  3885. output, err := cmd.CombinedOutput()
  3886. if err != nil {
  3887. utils.FileLog.Info("execute command failed, output: , error: \n" + string(output) + err.Error())
  3888. errorChannel <- err
  3889. return
  3890. }
  3891. doneChannel <- true
  3892. }()
  3893. defer func() {
  3894. _ = os.Remove(outFileName)
  3895. if err != nil {
  3896. utils.FileLog.Info("删除生产的图片文件失败, err: " + err.Error())
  3897. }
  3898. }()
  3899. select {
  3900. case <-time.After(30 * time.Second):
  3901. utils.FileLog.Info("执行超过30秒 杀死超时进程")
  3902. e := cmd.Process.Kill()
  3903. if e != nil {
  3904. fmt.Println("cmd kill err: ", e.Error())
  3905. utils.FileLog.Info(fmt.Sprintf("cmd kill err: %s", e.Error()))
  3906. br.Msg = "图片生成失败"
  3907. br.ErrMsg = "图片生成失败, 执行超时" + e.Error()
  3908. return
  3909. }
  3910. fmt.Println("timeout kill process")
  3911. case <-doneChannel:
  3912. fmt.Println("done")
  3913. case err := <-errorChannel:
  3914. br.Msg = "文件上传失败"
  3915. br.ErrMsg = fmt.Sprintf("execute command failure err: %s", err.Error())
  3916. fmt.Println("execute command failure err:" + err.Error())
  3917. return
  3918. }
  3919. //上传到阿里云 和 minio
  3920. resourceUrl := ``
  3921. ossClient := services.NewOssClient()
  3922. if ossClient == nil {
  3923. br.Msg = "上传失败"
  3924. br.ErrMsg = "初始化OSS服务失败"
  3925. return
  3926. }
  3927. resourceUrl, err = ossClient.UploadFile(outFileName, outFileName, "")
  3928. if err != nil {
  3929. br.Msg = "文件上传失败"
  3930. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  3931. return
  3932. }
  3933. // 修改图表的缩略图信息
  3934. if chartInfoId > 0 && resourceUrl != "" {
  3935. err = data_manage.EditChartInfoImageV2(chartInfoId, resourceUrl)
  3936. if err != nil {
  3937. br.Msg = "保存失败"
  3938. br.ErrMsg = "保存失败,Err:" + err.Error()
  3939. return
  3940. }
  3941. //修改es数据
  3942. go data.EsAddOrEditChartInfo(chartInfoId)
  3943. //修改my eta es数据
  3944. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfoId)
  3945. }
  3946. resp.ResourceUrl = resourceUrl
  3947. resp.Source = "convert"
  3948. //resp.CacheKey = imgDataKey
  3949. br.Msg = "上传成功"
  3950. br.Ret = 200
  3951. br.Success = true
  3952. br.Data = resp
  3953. return
  3954. }