chart_info.go 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/data_manage/chart_theme"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/services/data/data_manage_permission"
  11. rangeServ "eta/eta_api/services/data/range_analysis"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // RangeChartChartInfoController 区间计算图表管理
  21. type RangeChartChartInfoController struct {
  22. controllers.BaseAuthController
  23. }
  24. // Preview
  25. // @Title 区间计算图表-预览数据
  26. // @Description 区间计算图表-获取预览数据
  27. // @Param StartDate query string true "自定义开始日期"
  28. // @Param EndDate query string true "自定义结束日期"
  29. // @Param LeadValue query int true "领先值"
  30. // @Param LeadUnit query string true "领先单位:年,天,月,季,周"
  31. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  32. // @router /chart_info/preview [post]
  33. func (this *RangeChartChartInfoController) Preview() {
  34. br := new(models.BaseResponse).Init()
  35. defer func() {
  36. this.Data["json"] = br
  37. this.ServeJSON()
  38. }()
  39. sysUser := this.SysUser
  40. if sysUser == nil {
  41. br.Msg = "请登录"
  42. br.ErrMsg = "请登录,SysUser Is Empty"
  43. br.Ret = 408
  44. return
  45. }
  46. var req data_manage.ChartRangeAnalysisPreviewReq
  47. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  48. br.Msg = "参数解析异常!"
  49. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  50. return
  51. }
  52. // 基础校验
  53. if len(req.ChartEdbInfoList) == 0 {
  54. br.Msg = "请选择指标"
  55. return
  56. }
  57. err, msg, isSendEmail := rangeServ.CheckChartRangeExtraConfig(req.ExtraConfig)
  58. if err != nil {
  59. br.Msg = msg
  60. br.ErrMsg = err.Error()
  61. br.IsSendEmail = isSendEmail
  62. return
  63. }
  64. // 获取指标信息
  65. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  66. edbInfoMappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  67. for _, v := range req.ChartEdbInfoList {
  68. edbInfoMapping, e := data_manage.GetChartEdbMappingByEdbInfoId(v.EdbInfoId)
  69. if e != nil {
  70. br.Msg = "获取失败"
  71. br.ErrMsg = "获取区间计算图表, A指标mapping信息失败, Err:" + e.Error()
  72. return
  73. }
  74. if v.EdbAliasName != "" {
  75. edbInfoMapping.EdbAliasName = v.EdbAliasName
  76. }
  77. edbInfoMapping.IsAxis = v.IsAxis
  78. edbInfoMappingList = append(edbInfoMappingList, edbInfoMapping)
  79. }
  80. chartInfo := new(data_manage.ChartInfoView)
  81. chartInfo.ChartType = 1
  82. chartInfo.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  83. chartThemeType, err := chart_theme.GetChartThemeTypeByChartTypeAndSource(chartInfo.ChartType, 1)
  84. if err != nil {
  85. return
  86. }
  87. chartTheme, err := data.GetChartThemeConfig(chartThemeType.DefaultChartThemeId, utils.CHART_SOURCE_DEFAULT, chartInfo.ChartType)
  88. if err != nil {
  89. br.Msg = "获取失败"
  90. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  91. return
  92. }
  93. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  94. chartInfo.ChartThemeStyle = chartTheme.Config
  95. // 获取图表x轴y轴
  96. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(0, req.DateType, req.StartYear, req.StartDate, req.EndDate, edbInfoMappingList, &req.ExtraConfig)
  97. if e != nil {
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  100. return
  101. }
  102. //添加配置信息
  103. if req.ExtraConfig.MultipleGraphConfigId == 0 {
  104. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  105. //MultipleGraphConfigId: 0,
  106. SysUserId: sysUser.AdminId,
  107. SysUserRealName: sysUser.RealName,
  108. ModifyTime: time.Now(),
  109. CreateTime: time.Now(),
  110. }
  111. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  112. if err != nil {
  113. br.Msg = "获取失败"
  114. br.ErrMsg = "新增区间计算图表配置失败, Err: " + err.Error()
  115. return
  116. }
  117. dataResp.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  118. }
  119. resp := new(data_manage.ChartInfoDetailResp)
  120. resp.ChartInfo = chartInfo
  121. resp.EdbInfoList = edbList
  122. resp.XEdbIdValue = xEdbIdValue
  123. resp.DataResp = dataResp
  124. br.Data = resp
  125. br.Ret = 200
  126. br.Success = true
  127. br.Msg = "获取成功"
  128. }
  129. // Add
  130. // @Title 新增图表接口
  131. // @Description 新增图表接口
  132. // @Param request body data_manage.AddChartInfoReq true "type json string"
  133. // @Success 200 {object} data_manage.AddChartInfoResp
  134. // @router /chart_info/add [post]
  135. func (this *RangeChartChartInfoController) Add() {
  136. br := new(models.BaseResponse).Init()
  137. defer func() {
  138. this.Data["json"] = br
  139. this.ServeJSON()
  140. }()
  141. sysUser := this.SysUser
  142. if sysUser == nil {
  143. br.Msg = "请登录"
  144. br.ErrMsg = "请登录,SysUser Is Empty"
  145. br.Ret = 408
  146. return
  147. }
  148. deleteCache := true
  149. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  150. defer func() {
  151. if deleteCache {
  152. _ = utils.Rc.Delete(cacheKey)
  153. }
  154. }()
  155. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  156. deleteCache = false
  157. br.Msg = "系统处理中,请稍后重试!"
  158. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  159. return
  160. }
  161. var req data_manage.AddChartInfoReq
  162. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  163. if err != nil {
  164. br.Msg = "参数解析异常!"
  165. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  166. return
  167. }
  168. if req.DateType == 0 {
  169. req.DateType = 3
  170. }
  171. chartInfo, err, errMsg, isSendEmail := rangeServ.AddChartInfo(req, utils.CHART_SOURCE_RANGE_ANALYSIS, sysUser, this.Lang)
  172. if err != nil {
  173. br.Msg = "保存失败"
  174. if errMsg != `` {
  175. br.Msg = errMsg
  176. }
  177. br.ErrMsg = err.Error()
  178. br.IsSendEmail = isSendEmail
  179. return
  180. }
  181. //新增操作日志
  182. {
  183. chartLog := new(data_manage.ChartInfoLog)
  184. chartLog.ChartInfoId = chartInfo.ChartInfoId
  185. chartLog.ChartName = req.ChartName
  186. chartLog.ChartClassifyId = req.ChartClassifyId
  187. chartLog.SysUserId = sysUser.AdminId
  188. chartLog.SysUserRealName = sysUser.RealName
  189. chartLog.UniqueCode = chartInfo.UniqueCode
  190. chartLog.CreateTime = time.Now()
  191. chartLog.Content = string(this.Ctx.Input.RequestBody)
  192. chartLog.Status = "新增区间计算图表"
  193. chartLog.Method = this.Ctx.Input.URI()
  194. go data_manage.AddChartInfoLog(chartLog)
  195. }
  196. resp := new(data_manage.AddChartInfoResp)
  197. resp.ChartInfoId = chartInfo.ChartInfoId
  198. resp.UniqueCode = chartInfo.UniqueCode
  199. resp.ChartType = chartInfo.ChartType
  200. br.Ret = 200
  201. br.Success = true
  202. br.Msg = "保存成功"
  203. br.Data = resp
  204. br.IsAddLog = true
  205. }
  206. // Edit
  207. // @Title 编辑图表接口
  208. // @Description 编辑图表接口
  209. // @Param request body data_manage.EditChartInfoReq true "type json string"
  210. // @Success Ret=200 保存成功
  211. // @router /chart_info/edit [post]
  212. func (this *RangeChartChartInfoController) Edit() {
  213. br := new(models.BaseResponse).Init()
  214. defer func() {
  215. this.Data["json"] = br
  216. this.ServeJSON()
  217. }()
  218. sysUser := this.SysUser
  219. if sysUser == nil {
  220. br.Msg = "请登录"
  221. br.ErrMsg = "请登录,SysUser Is Empty"
  222. br.Ret = 408
  223. return
  224. }
  225. var req data_manage.EditChartInfoReq
  226. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  227. if err != nil {
  228. br.Msg = "参数解析异常!"
  229. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  230. return
  231. }
  232. chartItem, err, errMsg, isSendEmail := rangeServ.EditChartInfo(req, sysUser, this.Lang)
  233. if err != nil {
  234. br.Msg = "保存失败"
  235. if errMsg != `` {
  236. br.Msg = errMsg
  237. }
  238. br.ErrMsg = err.Error()
  239. br.IsSendEmail = isSendEmail
  240. return
  241. }
  242. resp := new(data_manage.AddChartInfoResp)
  243. resp.ChartInfoId = chartItem.ChartInfoId
  244. resp.UniqueCode = chartItem.UniqueCode
  245. resp.ChartType = req.ChartType
  246. //新增操作日志
  247. {
  248. chartLog := new(data_manage.ChartInfoLog)
  249. chartLog.ChartName = chartItem.ChartName
  250. chartLog.ChartInfoId = req.ChartInfoId
  251. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  252. chartLog.SysUserId = sysUser.AdminId
  253. chartLog.SysUserRealName = sysUser.RealName
  254. chartLog.UniqueCode = chartItem.UniqueCode
  255. chartLog.CreateTime = time.Now()
  256. chartLog.Content = string(this.Ctx.Input.RequestBody)
  257. chartLog.Status = "编辑区间计算图表"
  258. chartLog.Method = this.Ctx.Input.URL()
  259. go data_manage.AddChartInfoLog(chartLog)
  260. }
  261. br.Ret = 200
  262. br.Success = true
  263. br.Msg = "保存成功"
  264. br.Data = resp
  265. br.IsAddLog = true
  266. }
  267. // Detail
  268. // @Title 获取图表详情
  269. // @Description 获取图表详情接口
  270. // @Param ChartInfoId query int true "图表id"
  271. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  272. // @Param StartDate query string true "自定义开始日期"
  273. // @Param EndDate query string true "自定义结束日期"
  274. // @Param Calendar query string true "公历/农历"
  275. // @Param SeasonStartDate query string true "季节性图开始日期"
  276. // @Param SeasonEndDate query string true "季节性图结束日期"
  277. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  278. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  279. // @Success 200 {object} data_manage.ChartInfoDetailResp
  280. // @router /chart_info/detail [get]
  281. func (this *RangeChartChartInfoController) Detail() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. this.Data["json"] = br
  285. this.ServeJSON()
  286. }()
  287. sysUser := this.SysUser
  288. if sysUser == nil {
  289. br.Msg = "请登录"
  290. br.ErrMsg = "请登录,SysUser Is Empty"
  291. br.Ret = 408
  292. return
  293. }
  294. chartInfoId, _ := this.GetInt("ChartInfoId")
  295. if chartInfoId <= 0 {
  296. br.Msg = "参数有误"
  297. return
  298. }
  299. dateType, _ := this.GetInt("DateType")
  300. fmt.Println("dateType:", dateType)
  301. startDate := this.GetString("StartDate")
  302. endDate := this.GetString("EndDate")
  303. startYear, _ := this.GetInt("StartYear")
  304. var err error
  305. chartInfo := new(data_manage.ChartInfoView)
  306. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  307. if err != nil {
  308. if err.Error() == utils.ErrNoRow() {
  309. br.Msg = "图被删除,请刷新页面"
  310. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  311. return
  312. }
  313. br.Msg = "获取失败"
  314. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  315. return
  316. }
  317. if dateType <= 0 {
  318. dateType = chartInfo.DateType
  319. }
  320. if startDate == "" {
  321. startDate = chartInfo.StartDate
  322. }
  323. if endDate == "" {
  324. endDate = chartInfo.EndDate
  325. }
  326. if startYear <= 0 {
  327. startYear = chartInfo.StartYear
  328. }
  329. // 获取主题样式
  330. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, chartInfo.ChartType)
  331. if err != nil {
  332. br.Msg = "获取失败"
  333. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  334. return
  335. }
  336. chartInfo.ChartThemeStyle = chartTheme.Config
  337. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  338. // 获取指标信息
  339. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  340. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  341. if err != nil {
  342. br.Msg = "获取失败"
  343. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  344. return
  345. }
  346. // 获取图表数据
  347. if len(edbInfoMappingList) == 0 {
  348. br.Msg = "获取失败"
  349. br.ErrMsg = "图表没有指标,无法计算"
  350. return
  351. }
  352. // 区间计算图表配置校验
  353. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  354. if chartInfo.ExtraConfig == `` {
  355. br.Msg = "配置信息错误"
  356. return
  357. }
  358. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  359. if err != nil {
  360. br.Msg = "配置信息错误"
  361. br.ErrMsg = "图表配置信息错误,Err:" + err.Error()
  362. return
  363. }
  364. // 获取图表x轴y轴
  365. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  366. if e != nil {
  367. br.Msg = "获取失败"
  368. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  369. return
  370. }
  371. // 判断是否加入我的图库
  372. if chartInfoId > 0 && chartInfo != nil {
  373. {
  374. var myChartCondition string
  375. var myChartPars []interface{}
  376. myChartCondition += ` AND a.admin_id=? `
  377. myChartPars = append(myChartPars, sysUser.AdminId)
  378. myChartCondition += ` AND a.chart_info_id=? `
  379. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  380. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  381. if err != nil && err.Error() != utils.ErrNoRow() {
  382. br.Msg = "获取失败"
  383. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  384. return
  385. }
  386. if myChartList != nil && len(myChartList) > 0 {
  387. chartInfo.IsAdd = true
  388. chartInfo.MyChartId = myChartList[0].MyChartId
  389. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  390. }
  391. }
  392. }
  393. //图表操作权限
  394. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  395. //判断是否需要展示英文标识
  396. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  397. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  398. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  399. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  400. // 另存为
  401. chartInfo.Button = data_manage.ChartViewButton{
  402. IsEdit: chartInfo.IsEdit,
  403. IsEnChart: chartInfo.IsEnChart,
  404. IsAdd: chartInfo.IsAdd,
  405. IsCopy: chartInfo.IsEdit,
  406. IsSetName: chartInfo.IsSetName,
  407. }
  408. // 指标权限
  409. {
  410. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  411. classifyMap := make(map[int]*data_manage.EdbClassify)
  412. // 分类
  413. {
  414. classifyIdList := make([]int, 0)
  415. for _, v := range edbList {
  416. classifyIdList = append(classifyIdList, v.ClassifyId)
  417. }
  418. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  419. if tmpErr != nil {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  422. return
  423. }
  424. for _, v := range classifyList {
  425. classifyMap[v.ClassifyId] = v
  426. }
  427. }
  428. // 遍历到校验map
  429. for _, v := range edbList {
  430. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  431. ClassifyId: v.ClassifyId,
  432. IsJoinPermission: v.IsJoinPermission,
  433. EdbInfoId: v.EdbInfoId,
  434. }
  435. }
  436. // 获取所有有权限的指标和分类
  437. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  438. if err != nil {
  439. br.Msg = "获取失败"
  440. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  441. return
  442. }
  443. for _, v := range edbList {
  444. // 数据权限
  445. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  446. if !ok {
  447. continue
  448. }
  449. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  450. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  451. }
  452. }
  453. }
  454. // 图表当前分类的分类树
  455. classifyLevels := make([]string, 0)
  456. {
  457. list, e := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_RANGE_ANALYSIS)
  458. if e != nil {
  459. br.Msg = "获取失败"
  460. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  461. return
  462. }
  463. parents := data.GetChartClassifyParentRecursive(list, chartInfo.ChartClassifyId)
  464. sort.Slice(parents, func(i, j int) bool {
  465. return parents[i].Level < parents[i].Level
  466. })
  467. for _, v := range parents {
  468. classifyLevels = append(classifyLevels, v.UniqueCode)
  469. }
  470. }
  471. resp := new(data_manage.ChartInfoDetailResp)
  472. resp.ChartInfo = chartInfo
  473. resp.EdbInfoList = edbList
  474. resp.XEdbIdValue = xEdbIdValue
  475. resp.DataResp = dataResp
  476. resp.ClassifyLevels = classifyLevels
  477. br.Ret = 200
  478. br.Success = true
  479. br.Msg = "获取成功"
  480. br.Data = resp
  481. }
  482. // DetailFromUniqueCode
  483. // @Title 根据编码获取图表详情
  484. // @Description 根据编码获取图表详情接口
  485. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  486. // @Param IsCache query bool true "是否走缓存,默认false"
  487. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  488. // @router /chart_info/detail/from_unique_code [get]
  489. func (this *RangeChartChartInfoController) DetailFromUniqueCode() {
  490. br := new(models.BaseResponse).Init()
  491. defer func() {
  492. this.Data["json"] = br
  493. this.ServeJSON()
  494. }()
  495. sysUser := this.SysUser
  496. if sysUser == nil {
  497. br.Msg = "请登录"
  498. br.ErrMsg = "请登录,SysUser Is Empty"
  499. br.Ret = 408
  500. return
  501. }
  502. adminId := sysUser.AdminId
  503. uniqueCode := this.GetString("UniqueCode")
  504. if uniqueCode == "" {
  505. br.Msg = "参数错误"
  506. br.ErrMsg = "参数错误,uniqueCode is empty"
  507. return
  508. }
  509. //是否走缓存
  510. isCache, _ := this.GetBool("IsCache")
  511. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  512. status := true
  513. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  514. if err != nil {
  515. if err.Error() == utils.ErrNoRow() {
  516. status = false
  517. } else {
  518. br.Msg = "获取失败"
  519. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  520. return
  521. }
  522. }
  523. if chartInfo == nil {
  524. status = false
  525. }
  526. if !status {
  527. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  528. resp.EdbInfoList = endInfoList
  529. resp.ChartInfo = chartInfo
  530. resp.Status = false
  531. br.Ret = 200
  532. br.Success = true
  533. br.Msg = "获取成功"
  534. br.Data = resp
  535. return
  536. }
  537. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  538. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  539. if utils.Re == nil && isCache {
  540. if utils.Re == nil && utils.Rc.IsExist(key) {
  541. if d, e := utils.Rc.RedisBytes(key); e == nil {
  542. err := json.Unmarshal(d, &resp)
  543. if err == nil && resp != nil {
  544. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  545. var myCond string
  546. var myPars []interface{}
  547. myCond += ` AND a.admin_id=? `
  548. myPars = append(myPars, adminId)
  549. myCond += ` AND a.chart_info_id=? `
  550. myPars = append(myPars, chartInfo.ChartInfoId)
  551. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  552. if err != nil && err.Error() != utils.ErrNoRow() {
  553. br.Msg = "获取失败"
  554. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  555. return
  556. }
  557. resp.ChartInfo.IsAdd = false
  558. resp.ChartInfo.MyChartId = 0
  559. resp.ChartInfo.MyChartClassifyId = ""
  560. if myList != nil && len(myList) > 0 {
  561. resp.ChartInfo.IsAdd = true
  562. resp.ChartInfo.MyChartId = myList[0].MyChartId
  563. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  564. }
  565. br.Ret = 200
  566. br.Success = true
  567. br.Msg = "获取成功"
  568. br.Data = resp
  569. return
  570. }
  571. }
  572. }
  573. }
  574. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  575. if !isOk {
  576. br.Msg = msg
  577. br.ErrMsg = errMsg
  578. return
  579. }
  580. br.Ret = 200
  581. br.Success = true
  582. br.Msg = "获取成功"
  583. br.Data = resp
  584. }
  585. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  586. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  587. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  588. adminId := sysUser.AdminId
  589. // 指标数据map
  590. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  591. defer func() {
  592. if isOk {
  593. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  594. {
  595. //判断是否加入我的图库
  596. var myChartCondition string
  597. var myChartPars []interface{}
  598. myChartCondition += ` AND a.admin_id=? `
  599. myChartPars = append(myChartPars, adminId)
  600. myChartCondition += ` AND a.chart_info_id=? `
  601. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  602. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  603. if err != nil && err.Error() != utils.ErrNoRow() {
  604. msg = "获取失败"
  605. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  606. return
  607. }
  608. if myChartList != nil && len(myChartList) > 0 {
  609. chartInfo.IsAdd = true
  610. chartInfo.MyChartId = myChartList[0].MyChartId
  611. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  612. }
  613. }
  614. resp.ChartInfo.HaveOperaAuth = true
  615. // 指标权限
  616. {
  617. classifyMap := make(map[int]*data_manage.EdbClassify)
  618. // 分类
  619. {
  620. classifyIdList := make([]int, 0)
  621. for _, v := range resp.EdbInfoList {
  622. classifyIdList = append(classifyIdList, v.ClassifyId)
  623. }
  624. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  625. if tmpErr != nil {
  626. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  627. return
  628. }
  629. for _, v := range classifyList {
  630. classifyMap[v.ClassifyId] = v
  631. }
  632. }
  633. // 指标
  634. if len(edbClassifyPermissionMap) < 0 {
  635. edbInfoIdList := make([]int, 0)
  636. for _, v := range resp.EdbInfoList {
  637. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  638. }
  639. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  640. if tmpErr != nil {
  641. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  642. return
  643. }
  644. for _, v := range edbInfoList {
  645. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  646. ClassifyId: v.ClassifyId,
  647. IsJoinPermission: v.IsJoinPermission,
  648. EdbInfoId: v.EdbInfoId,
  649. }
  650. }
  651. }
  652. // 获取所有有权限的指标和分类
  653. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  654. if err != nil {
  655. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  656. return
  657. }
  658. for _, v := range resp.EdbInfoList {
  659. // 数据权限
  660. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  661. if !ok {
  662. continue
  663. }
  664. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  665. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  666. }
  667. }
  668. }
  669. }
  670. }()
  671. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  672. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  673. if utils.Re == nil && isCache {
  674. if utils.Re == nil && utils.Rc.IsExist(key) {
  675. if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  676. err := json.Unmarshal(chartData, &resp)
  677. if err != nil || resp == nil {
  678. return
  679. }
  680. isOk = true
  681. fmt.Println("source redis")
  682. return
  683. }
  684. }
  685. }
  686. // 获取主题样式
  687. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  688. if err != nil {
  689. msg = "获取失败"
  690. errMsg = "获取主题信息失败,Err:" + err.Error()
  691. return
  692. }
  693. chartInfo.ChartThemeStyle = chartTheme.Config
  694. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  695. chartInfoId := chartInfo.ChartInfoId
  696. // 获取指标信息
  697. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  698. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  699. if err != nil {
  700. msg = "获取失败"
  701. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  702. return
  703. }
  704. dateType := chartInfo.DateType
  705. // 开始/结束日期
  706. startYear := chartInfo.StartYear
  707. startDate := chartInfo.StartDate
  708. endDate := chartInfo.EndDate
  709. // 区间计算图表配置校验
  710. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  711. if chartInfo.ExtraConfig == `` {
  712. msg = "配置信息错误"
  713. return
  714. }
  715. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  716. if err != nil {
  717. msg = "配置信息错误"
  718. errMsg = "图表配置信息错误,Err:" + err.Error()
  719. return
  720. }
  721. // 获取图表数据
  722. if len(edbInfoMappingList) == 0 {
  723. msg = "获取失败"
  724. errMsg = "图表没有指标,无法计算"
  725. return
  726. }
  727. // 获取图表x轴y轴
  728. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  729. if e != nil {
  730. msg = "获取失败"
  731. errMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  732. return
  733. }
  734. if chartInfoId > 0 && chartInfo != nil {
  735. //判断是否加入我的图库
  736. {
  737. var myChartCondition string
  738. var myChartPars []interface{}
  739. myChartCondition += ` AND a.admin_id=? `
  740. myChartPars = append(myChartPars, sysUser.AdminId)
  741. myChartCondition += ` AND a.chart_info_id=? `
  742. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  743. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  744. if err != nil && err.Error() != utils.ErrNoRow() {
  745. msg = "获取失败"
  746. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  747. return
  748. }
  749. if myChartList != nil && len(myChartList) > 0 {
  750. chartInfo.IsAdd = true
  751. chartInfo.MyChartId = myChartList[0].MyChartId
  752. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  753. }
  754. }
  755. }
  756. //图表操作权限
  757. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  758. //判断是否需要展示英文标识
  759. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  760. chartInfo.UnitEn = ""
  761. // 另存为
  762. chartInfo.Button = data_manage.ChartViewButton{
  763. IsEdit: chartInfo.IsEdit,
  764. IsEnChart: chartInfo.IsEnChart,
  765. IsAdd: chartInfo.IsAdd,
  766. IsCopy: chartInfo.IsEdit,
  767. IsSetName: chartInfo.IsSetName,
  768. }
  769. // 图表的指标来源
  770. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  771. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  772. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  773. resp.ChartInfo = chartInfo
  774. resp.EdbInfoList = edbList
  775. resp.XEdbIdValue = xEdbIdValue
  776. resp.DataResp = dataResp
  777. resp.Status = true
  778. // 遍历到校验map
  779. for _, v := range edbList {
  780. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  781. ClassifyId: v.ClassifyId,
  782. IsJoinPermission: v.IsJoinPermission,
  783. EdbInfoId: v.EdbInfoId,
  784. }
  785. }
  786. // 将数据加入缓存
  787. if utils.Re == nil {
  788. d, _ := json.Marshal(resp)
  789. _ = utils.Rc.Put(key, d, 2*time.Hour)
  790. }
  791. isOk = true
  792. return
  793. }
  794. // List
  795. // @Title 区间计算图表列表接口
  796. // @Description 区间计算图表列表接口
  797. // @Param PageSize query int true "每页数据条数"
  798. // @Param CurrentIndex query int true "当前页页码,从1开始"
  799. // @Param ChartClassifyId query int true "分类id"
  800. // @Param Keyword query string true "搜索关键词"
  801. // @Param IsShowMe query bool true "是否只看我的,true、false"
  802. // @Param Source query int true "图表类型,3:区间计算,4:滚动区间计算"
  803. // @Success 200 {object} data_manage.ChartListResp
  804. // @router /chart_info/list [get]
  805. func (this *RangeChartChartInfoController) List() {
  806. br := new(models.BaseResponse).Init()
  807. defer func() {
  808. this.Data["json"] = br
  809. this.ServeJSON()
  810. }()
  811. sysUser := this.SysUser
  812. if sysUser == nil {
  813. br.Msg = "请登录"
  814. br.ErrMsg = "请登录,SysUser Is Empty"
  815. br.Ret = 408
  816. return
  817. }
  818. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  819. pageSize, _ := this.GetInt("PageSize")
  820. currentIndex, _ := this.GetInt("CurrentIndex")
  821. keyword := this.GetString("KeyWord")
  822. var total int
  823. page := paging.GetPaging(currentIndex, pageSize, total)
  824. var startSize int
  825. if pageSize <= 0 {
  826. pageSize = utils.PageSize20
  827. }
  828. if currentIndex <= 0 {
  829. currentIndex = 1
  830. }
  831. startSize = paging.StartIndex(currentIndex, pageSize)
  832. source := utils.CHART_SOURCE_RANGE_ANALYSIS
  833. var condition string
  834. var pars []interface{}
  835. // 普通图表
  836. condition += ` AND source = ? `
  837. pars = append(pars, source)
  838. if chartClassifyId > 0 {
  839. chartClassifyId, err := data_manage.GetChartClassify(chartClassifyId)
  840. if err != nil && err.Error() != utils.ErrNoRow() {
  841. br.Msg = "获取图表信息失败"
  842. br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error()
  843. return
  844. }
  845. condition += " AND chart_classify_id IN(" + chartClassifyId + ") "
  846. }
  847. if keyword != "" {
  848. //将关键词按照空格分割
  849. keywords := strings.Split(keyword, " ")
  850. condition += ` AND ( chart_name LIKE '%` + keywords[0] + `%' `
  851. for k, key := range keywords {
  852. if k == 0 {
  853. continue
  854. }
  855. condition += ` OR chart_name LIKE '%` + key + `%' `
  856. }
  857. condition += ` )`
  858. }
  859. //只看我的
  860. isShowMe, _ := this.GetBool("IsShowMe")
  861. if isShowMe {
  862. condition += ` AND sys_user_id = ? `
  863. pars = append(pars, sysUser.AdminId)
  864. }
  865. // 获取当前账号的不可见指标
  866. noPermissionChartIdList := make([]int, 0)
  867. {
  868. obj := data_manage.EdbInfoNoPermissionAdmin{}
  869. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  870. if err != nil && err.Error() != utils.ErrNoRow() {
  871. br.Msg = "获取失败"
  872. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  873. return
  874. }
  875. for _, v := range confList {
  876. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  877. }
  878. }
  879. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  880. if lenNoPermissionChartIdList > 0 {
  881. condition += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  882. pars = append(pars, noPermissionChartIdList)
  883. }
  884. //获取图表信息
  885. list, err := data_manage.GetChartListByCondition(condition, pars, startSize, pageSize)
  886. if err != nil && err.Error() != utils.ErrNoRow() {
  887. br.Success = true
  888. br.Msg = "获取图表信息失败"
  889. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  890. return
  891. }
  892. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  893. if err != nil && err.Error() != utils.ErrNoRow() {
  894. br.Msg = "获取图表信息失败"
  895. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  896. return
  897. }
  898. myChartMap := make(map[int]*data_manage.MyChartView)
  899. for _, v := range myChartList {
  900. myChartMap[v.ChartInfoId] = v
  901. }
  902. listLen := len(list)
  903. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  904. if listLen > 0 {
  905. chartInfoIds := ""
  906. for _, v := range list {
  907. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  908. }
  909. if chartInfoIds != "" {
  910. chartInfoIds = strings.Trim(chartInfoIds, ",")
  911. //判断是否需要展示英文标识
  912. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  913. if e != nil {
  914. br.Msg = "获取失败"
  915. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  916. return
  917. }
  918. for _, v := range edbList {
  919. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  920. }
  921. }
  922. }
  923. for i := 0; i < listLen; i++ {
  924. //判断是否需要展示英文标识
  925. if edbTmpList, ok := chartEdbMap[list[i].ChartInfoId]; ok {
  926. list[i].IsEnChart = data.CheckIsEnChart(list[i].ChartNameEn, edbTmpList, list[i].Source, list[i].ChartType)
  927. }
  928. if existItem, ok := myChartMap[list[i].ChartInfoId]; ok {
  929. list[i].IsAdd = true
  930. list[i].MyChartId = existItem.MyChartId
  931. list[i].MyChartClassifyId = existItem.MyChartClassifyId
  932. }
  933. }
  934. resp := new(data_manage.ChartListResp)
  935. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  936. items := make([]*data_manage.ChartInfoView, 0)
  937. resp.Paging = page
  938. resp.List = items
  939. br.Ret = 200
  940. br.Success = true
  941. br.Msg = "获取成功"
  942. return
  943. }
  944. dataCount, err := data_manage.GetChartListCountByCondition(condition, pars)
  945. if err != nil && err.Error() != utils.ErrNoRow() {
  946. br.Msg = "获取指标信息失败"
  947. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  948. return
  949. }
  950. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  951. resp.Paging = page
  952. resp.List = list
  953. br.Ret = 200
  954. br.Success = true
  955. br.Msg = "获取成功"
  956. br.Data = resp
  957. }
  958. // Copy
  959. // @Title 复制并新增图表接口
  960. // @Description 新增图表接口
  961. // @Params request body data_manage.CopyAddChartInfoReq true "type json string"
  962. // @Success 200 {object} data_manage.AddChartInfoResp
  963. // @router /chart_info/copy [post]
  964. func (this *RangeChartChartInfoController) Copy() {
  965. br := new(models.BaseResponse).Init()
  966. defer func() {
  967. if br.ErrMsg == "" {
  968. br.IsSendEmail = false
  969. }
  970. this.Data["json"] = br
  971. this.ServeJSON()
  972. }()
  973. sysUser := this.SysUser
  974. if sysUser == nil {
  975. br.Msg = "请登录"
  976. br.ErrMsg = "请登录,SysUser Is Empty"
  977. br.Ret = 408
  978. return
  979. }
  980. var req data_manage.CopyAddChartInfoReq
  981. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  982. if err != nil {
  983. br.Msg = "参数解析异常!"
  984. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  985. return
  986. }
  987. if req.ChartInfoId <= 0 {
  988. br.Msg = "参数有误"
  989. br.ErrMsg = fmt.Sprintf("参数有误, ChartInfoId: %d", req.ChartInfoId)
  990. return
  991. }
  992. req.ChartName = strings.TrimSpace(req.ChartName)
  993. if req.ChartName == "" {
  994. br.Msg = "请输入图表名称"
  995. return
  996. }
  997. if req.ChartClassifyId <= 0 {
  998. br.Msg = "请选择图表分类"
  999. return
  1000. }
  1001. deleteCache := true
  1002. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1003. defer func() {
  1004. if deleteCache {
  1005. utils.Rc.Delete(cacheKey)
  1006. }
  1007. }()
  1008. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1009. deleteCache = false
  1010. br.Msg = "系统处理中,请稍后重试!"
  1011. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1012. return
  1013. }
  1014. chartSource := utils.CHART_SOURCE_RANGE_ANALYSIS
  1015. // 校验分类、图表名称
  1016. {
  1017. var cond string
  1018. var pars []interface{}
  1019. switch this.Lang {
  1020. case utils.EnLangVersion:
  1021. cond += " AND chart_name_en = ? AND source = ? "
  1022. default:
  1023. cond += " AND chart_name = ? AND source = ? "
  1024. }
  1025. pars = append(pars, req.ChartName, chartSource)
  1026. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  1027. if e != nil {
  1028. br.Msg = "保存失败"
  1029. br.ErrMsg = fmt.Sprintf("获取同名图表失败, Err: %v", e)
  1030. return
  1031. }
  1032. if count > 0 {
  1033. br.Msg = "图表名称已存在, 请重新填写"
  1034. return
  1035. }
  1036. _, e = data_manage.GetChartClassifyById(req.ChartClassifyId)
  1037. if e != nil {
  1038. if e.Error() == utils.ErrNoRow() {
  1039. br.Msg = "分类不存在"
  1040. return
  1041. }
  1042. br.Msg = "保存失败"
  1043. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  1044. return
  1045. }
  1046. }
  1047. // 图表信息
  1048. originChart, e := data_manage.GetChartInfoById(req.ChartInfoId)
  1049. if e != nil {
  1050. if e.Error() == utils.ErrNoRow() {
  1051. br.Msg = "原图表不存在"
  1052. return
  1053. }
  1054. br.Msg = "保存失败"
  1055. br.ErrMsg = fmt.Sprintf("获取原图表信息失败, Err: %v", e)
  1056. return
  1057. }
  1058. if originChart.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  1059. br.Msg = `滚动区间计算图不支持另存为`
  1060. br.IsSendEmail = false
  1061. return
  1062. }
  1063. // 普通区间计算图表
  1064. chartInfo := new(data_manage.ChartInfo)
  1065. newChart, err, errMsg, isSendEmail := rangeServ.CopyChartInfo(req.ChartClassifyId, req.ChartName, originChart, sysUser, this.Lang)
  1066. chartInfo = newChart
  1067. if err != nil {
  1068. br.Msg = "保存失败"
  1069. if errMsg != `` {
  1070. br.Msg = errMsg
  1071. }
  1072. br.ErrMsg = err.Error()
  1073. br.IsSendEmail = isSendEmail
  1074. return
  1075. }
  1076. // 新增操作日志
  1077. {
  1078. chartLog := new(data_manage.ChartInfoLog)
  1079. chartLog.ChartInfoId = chartInfo.ChartInfoId
  1080. chartLog.ChartName = req.ChartName
  1081. chartLog.ChartClassifyId = req.ChartClassifyId
  1082. chartLog.SysUserId = sysUser.AdminId
  1083. chartLog.SysUserRealName = sysUser.RealName
  1084. chartLog.UniqueCode = chartInfo.UniqueCode
  1085. chartLog.CreateTime = time.Now()
  1086. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1087. chartLog.Status = "复制区间计算图表"
  1088. chartLog.Method = this.Ctx.Input.URI()
  1089. go data_manage.AddChartInfoLog(chartLog)
  1090. }
  1091. br.Ret = 200
  1092. br.Success = true
  1093. br.Msg = "保存成功"
  1094. br.Data = data_manage.AddChartInfoResp{
  1095. ChartInfoId: chartInfo.ChartInfoId,
  1096. UniqueCode: chartInfo.UniqueCode,
  1097. ChartType: chartInfo.ChartType,
  1098. }
  1099. br.IsAddLog = true
  1100. }
  1101. // Refresh
  1102. // @Title 图表刷新接口
  1103. // @Description 图表刷新接口
  1104. // @Param ChartInfoId query int true "图表id"
  1105. // @Param UniqueCode query string true "唯一code"
  1106. // @Success Ret=200 刷新成功
  1107. // @router /chart_info/refresh [get]
  1108. func (this *RangeChartChartInfoController) Refresh() {
  1109. br := new(models.BaseResponse).Init()
  1110. defer func() {
  1111. this.Data["json"] = br
  1112. this.ServeJSON()
  1113. }()
  1114. sysUser := this.SysUser
  1115. if sysUser == nil {
  1116. br.Msg = "请登录"
  1117. br.ErrMsg = "请登录,SysUser Is Empty"
  1118. br.Ret = 408
  1119. return
  1120. }
  1121. chartInfoId, _ := this.GetInt("ChartInfoId")
  1122. uniqueCode := this.GetString("UniqueCode")
  1123. if chartInfoId <= 0 && uniqueCode == `` {
  1124. br.Msg = "参数错误"
  1125. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  1126. return
  1127. }
  1128. var chartInfo *data_manage.ChartInfo
  1129. var err error
  1130. if chartInfoId > 0 {
  1131. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  1132. } else {
  1133. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  1134. }
  1135. if err != nil {
  1136. if err.Error() == utils.ErrNoRow() {
  1137. br.Msg = "图表已被删除,无需刷新"
  1138. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1139. return
  1140. }
  1141. br.Msg = "刷新失败"
  1142. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1143. return
  1144. }
  1145. // 刷新区间计算图表
  1146. isAsync, e := rangeServ.ChartInfoRefresh(chartInfo.ChartInfoId, chartInfo.UniqueCode)
  1147. if e != nil {
  1148. br.Msg = "刷新失败"
  1149. br.ErrMsg = "刷新区间计算图表失败, Err:" + err.Error()
  1150. return
  1151. }
  1152. // 多因子区间计算异步刷新, 前端提示
  1153. if isAsync {
  1154. br.Ret = 200
  1155. br.Success = true
  1156. br.Msg = "刷新时间较长, 请10分钟后查看"
  1157. return
  1158. }
  1159. //清除图表缓存
  1160. {
  1161. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1162. _ = utils.Rc.Delete(key)
  1163. }
  1164. br.Ret = 200
  1165. br.Success = true
  1166. br.Msg = "刷新成功"
  1167. }
  1168. // MultipleGraphConfigSaveEdb
  1169. // @Title 保存指标接口
  1170. // @Description 保存指标接口
  1171. // @Param request body request.SaveMultipleGraphEdbReq true "type json string"
  1172. // @Success Ret=200 返回指标id
  1173. // @router /edb/save [post]
  1174. func (this *RangeChartChartInfoController) MultipleGraphConfigSaveEdb() {
  1175. br := new(models.BaseResponse).Init()
  1176. defer func() {
  1177. this.Data["json"] = br
  1178. this.ServeJSON()
  1179. }()
  1180. sysUser := this.SysUser
  1181. if sysUser == nil {
  1182. br.Msg = "请登录"
  1183. br.ErrMsg = "请登录,SysUser Is Empty"
  1184. br.Ret = 408
  1185. return
  1186. }
  1187. var req data_manage.SaveChartRangeAnalysisEdbReq
  1188. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1189. if err != nil {
  1190. br.Msg = "参数解析异常!"
  1191. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1192. return
  1193. }
  1194. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1195. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1196. br.Msg = "系统处理中,请稍后重试!"
  1197. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1198. return
  1199. }
  1200. defer func() {
  1201. _ = utils.Rc.Delete(cacheKey)
  1202. }()
  1203. fromEdbInfoIds := make([]int, 0)
  1204. edbInfoIds := make([]int, 0)
  1205. for _, v := range req.EdbInfoList {
  1206. if v.EdbInfoId > 0 {
  1207. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  1208. }
  1209. fromEdbInfoIds = append(fromEdbInfoIds, v.FromEdbInfoId)
  1210. edbInfoIds = append(edbInfoIds, v.FromEdbInfoId)
  1211. }
  1212. edbInfoMappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIds)
  1213. if err != nil {
  1214. br.Msg = "获取失败"
  1215. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1216. return
  1217. }
  1218. edbInfoMap := make(map[int]*data_manage.ChartEdbInfoMapping, 0)
  1219. for _, v := range edbInfoMappingList {
  1220. edbInfoMap[v.EdbInfoId] = v
  1221. }
  1222. for _, v := range req.EdbInfoList {
  1223. if _, ok := edbInfoMap[v.FromEdbInfoId]; !ok {
  1224. br.Msg = "指标信息不存在"
  1225. br.ErrMsg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(v.FromEdbInfoId)
  1226. br.IsSendEmail = false
  1227. return
  1228. }
  1229. if v.EdbInfoId > 0 {
  1230. if _, ok := edbInfoMap[v.EdbInfoId]; !ok {
  1231. v.EdbInfoId = 0
  1232. }
  1233. }
  1234. }
  1235. // 区间计算图表配置校验
  1236. extraConfig := req.ExtraConfig
  1237. // 区间计算图表配置校验
  1238. var config data_manage.ChartRangeAnalysisExtraConf
  1239. if req.ExtraConfig == `` {
  1240. br.Msg = "请输入配置信息"
  1241. return
  1242. }
  1243. err = json.Unmarshal([]byte(req.ExtraConfig), &config)
  1244. if err != nil {
  1245. br.Msg = "配置信息格式错误"
  1246. br.ErrMsg = "配置信息格式错误,Err:" + err.Error()
  1247. return
  1248. }
  1249. err, errMsg, isSendEmail := rangeServ.CheckChartRangeExtraConfig(config)
  1250. if err != nil {
  1251. br.Msg = "配置信息校验失败"
  1252. br.ErrMsg = errMsg
  1253. br.IsSendEmail = isSendEmail
  1254. return
  1255. }
  1256. multipleGraphConfigEdbMappingList, err := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1257. if err != nil {
  1258. br.Msg = `保存失败`
  1259. br.ErrMsg = "查询配置与图表的关联关系失败,ERR:" + err.Error()
  1260. return
  1261. }
  1262. deleteEdbInfoIds := make([]int, 0) // 需要解除绑定的指标ID
  1263. configMapping := make(map[int]*data_manage.MultipleGraphConfigEdbMapping, 0)
  1264. if len(multipleGraphConfigEdbMappingList) == 0 || req.IsSaveAs {
  1265. // 需要新增的指标
  1266. } else {
  1267. // 需要更新的指标
  1268. // 查询原先所有指标的来源指标,进一步筛选出需要新增,或者更新 或者删除的指标
  1269. oldEdbInfoIds := make([]int, 0)
  1270. for _, v := range multipleGraphConfigEdbMappingList {
  1271. configMapping[v.EdbInfoId] = v
  1272. oldEdbInfoIds = append(oldEdbInfoIds, v.EdbInfoId)
  1273. }
  1274. oldEdbCalculateMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(oldEdbInfoIds)
  1275. if e != nil {
  1276. br.Msg = `保存失败`
  1277. br.ErrMsg = "获取计算指标信息失败,ERR:" + e.Error()
  1278. return
  1279. }
  1280. // 说明指标还在,没有被删除
  1281. for _, v := range oldEdbCalculateMappingList {
  1282. if !utils.InArrayByInt(fromEdbInfoIds, v.FromEdbInfoId) && (config.EdbInfoMode == 1 || req.EdbInfoType != v.FromEdbInfoType) {
  1283. deleteEdbInfoIds = append(deleteEdbInfoIds, v.EdbInfoId)
  1284. }
  1285. }
  1286. }
  1287. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1288. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1289. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1290. }
  1291. // 普通指标批量新增\批量编辑
  1292. var saveReq data_manage.BatchEdbInfoCalculateBatchSaveReq
  1293. calculateEdbList := make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0)
  1294. addReqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1295. editReqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1296. for _, v := range req.EdbInfoList {
  1297. v.CalculateId = strconv.Itoa(v.FromEdbInfoId)
  1298. v.EdbName = strings.Trim(v.EdbName, " ")
  1299. if v.EdbName == "" {
  1300. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1301. CalculateId: v.CalculateId,
  1302. Msg: "指标名称不能为空",
  1303. })
  1304. continue
  1305. }
  1306. if v.Frequency == "" {
  1307. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1308. CalculateId: v.CalculateId,
  1309. Msg: "频率不能为空",
  1310. })
  1311. continue
  1312. }
  1313. if v.Unit == "" {
  1314. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1315. CalculateId: v.CalculateId,
  1316. Msg: "单位不能为空",
  1317. })
  1318. continue
  1319. }
  1320. if v.ClassifyId <= 0 {
  1321. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1322. CalculateId: v.CalculateId,
  1323. Msg: "请选择分类",
  1324. })
  1325. continue
  1326. }
  1327. //加入缓存机制,避免创建同一个名称的指标 start
  1328. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", utils.DATA_SOURCE_CALCULATE_RANGEANLYSIS, ":", v.EdbName)
  1329. //加入缓存机制,避免创建同一个名称的指标 start
  1330. if req.EdbInfoType == 1 {
  1331. redisKey = fmt.Sprint("predict_edb_info:calculate:batch:save:", sysUser.AdminId, ":", utils.DATA_SOURCE_CALCULATE_RANGEANLYSIS, ":", v.CalculateId)
  1332. }
  1333. isExist := utils.Rc.IsExist(redisKey)
  1334. if isExist {
  1335. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1336. CalculateId: strconv.Itoa(v.FromEdbInfoId),
  1337. Msg: "指标正在处理,请勿重复提交",
  1338. })
  1339. continue
  1340. } else {
  1341. }
  1342. if v.EdbInfoId > 0 {
  1343. editReqEdbList = append(editReqEdbList, v)
  1344. } else {
  1345. addReqEdbList = append(addReqEdbList, v)
  1346. }
  1347. }
  1348. if len(editReqEdbList) <= 0 && len(addReqEdbList) <= 0 {
  1349. br.Msg = "新增失败!"
  1350. if len(resp.Fail) > 0 {
  1351. br.ErrMsg = resp.Fail[0].Msg
  1352. } else {
  1353. br.Msg = "请选择指标"
  1354. }
  1355. return
  1356. }
  1357. if req.EdbInfoType == 0 { //普通指标
  1358. saveReq.Source = utils.DATA_SOURCE_CALCULATE_RANGEANLYSIS
  1359. saveReq.CalculateFormula = extraConfig
  1360. saveReq.AdminId = sysUser.AdminId
  1361. saveReq.AdminName = sysUser.RealName
  1362. if len(addReqEdbList) > 0 {
  1363. saveReq.EdbList = addReqEdbList
  1364. // 调用指标库去更新
  1365. reqJson, err := json.Marshal(saveReq)
  1366. if err != nil {
  1367. br.Msg = "参数解析异常!"
  1368. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1369. return
  1370. }
  1371. respItem, err := data.BatchSaveEdbCalculateMultiData(string(reqJson), this.Lang)
  1372. if err != nil {
  1373. br.Msg = "新增失败!"
  1374. br.ErrMsg = "新增失败,Err:" + err.Error()
  1375. return
  1376. }
  1377. if respItem.Ret != 200 {
  1378. br.Msg = respItem.Msg
  1379. br.ErrMsg = respItem.ErrMsg
  1380. return
  1381. }
  1382. resp = respItem.Data
  1383. calculateEdbList = respItem.Data.Success
  1384. }
  1385. if len(editReqEdbList) > 0 {
  1386. saveReq.EdbList = editReqEdbList
  1387. // 调用指标库去更新
  1388. reqJson, err := json.Marshal(saveReq)
  1389. if err != nil {
  1390. br.Msg = "参数解析异常!"
  1391. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1392. return
  1393. }
  1394. respItem, err := data.BatchEditEdbCalculateMultiData(string(reqJson), this.Lang)
  1395. if err != nil {
  1396. br.Msg = "更新失败!"
  1397. br.ErrMsg = "更新失败,Err:" + err.Error()
  1398. return
  1399. }
  1400. if respItem.Ret != 200 {
  1401. br.Msg = respItem.Msg
  1402. br.ErrMsg = respItem.ErrMsg
  1403. return
  1404. }
  1405. resp.Fail = append(resp.Fail, respItem.Data.Fail...)
  1406. calculateEdbList = append(calculateEdbList, respItem.Data.Success...)
  1407. }
  1408. } else if req.EdbInfoType == 1 {
  1409. // 预测指标
  1410. for _, v := range req.EdbInfoList {
  1411. predictReq := new(data_manage.PredictEdbInfoCalculateBatchSaveReq)
  1412. predictReq.EdbInfoId = v.EdbInfoId
  1413. predictReq.CalculateFormula = extraConfig
  1414. predictReq.EdbName = v.EdbName
  1415. predictReq.Frequency = v.Frequency
  1416. predictReq.Unit = v.Unit
  1417. predictReq.ClassifyId = v.ClassifyId
  1418. predictReq.FromEdbInfoId = v.FromEdbInfoId
  1419. predictReq.AdminId = sysUser.AdminId
  1420. predictReq.AdminName = sysUser.RealName
  1421. predictReq.Source = utils.DATA_SOURCE_PREDICT_CALCULATE_RANGEANLYSIS
  1422. reqJson, err := json.Marshal(predictReq)
  1423. if err != nil {
  1424. br.Msg = "参数解析异常!"
  1425. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1426. return
  1427. }
  1428. respItem, err := data.BatchSavePredictEdbData(string(reqJson), this.Lang)
  1429. if err != nil {
  1430. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1431. CalculateId: v.CalculateId,
  1432. Msg: "新增失败!",
  1433. ErrMsg: "新增失败,Err:" + err.Error(),
  1434. })
  1435. continue
  1436. }
  1437. if respItem.Ret != 200 {
  1438. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1439. CalculateId: v.CalculateId,
  1440. Msg: respItem.Msg,
  1441. ErrMsg: respItem.ErrMsg,
  1442. })
  1443. continue
  1444. }
  1445. calculateEdbList = append(calculateEdbList, data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp{
  1446. CalculateId: v.CalculateId,
  1447. EdbInfoId: respItem.Data.EdbInfoId,
  1448. UniqueCode: respItem.Data.UniqueCode,
  1449. ClassifyId: predictReq.ClassifyId,
  1450. })
  1451. }
  1452. }
  1453. // 批量删除
  1454. if len(deleteEdbInfoIds) > 0 {
  1455. err = data_manage.DeleteMultipleGraphConfigEdbMappingByEdbIds(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS, deleteEdbInfoIds)
  1456. if err != nil {
  1457. br.Msg = "更新失败!"
  1458. br.ErrMsg = "删除失败,Err:" + err.Error()
  1459. return
  1460. }
  1461. }
  1462. // 如果不是另存为的话,那么需要建立配置与指标的关系
  1463. if !req.IsSaveAs {
  1464. // 如果指标没有建立关联关系,那么就需要添加关系
  1465. configEdbMappingList := make([]*data_manage.MultipleGraphConfigEdbMapping, 0)
  1466. for _, v := range calculateEdbList {
  1467. item, ok := configMapping[v.EdbInfoId]
  1468. if !ok {
  1469. multipleGraphConfigEdbMapping := &data_manage.MultipleGraphConfigEdbMapping{
  1470. //Id: 0,
  1471. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1472. EdbInfoId: v.EdbInfoId,
  1473. Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
  1474. ModifyTime: time.Now(),
  1475. CreateTime: time.Now(),
  1476. }
  1477. configEdbMappingList = append(configEdbMappingList, multipleGraphConfigEdbMapping)
  1478. } else {
  1479. item.ModifyTime = time.Now()
  1480. err = item.Update([]string{"ModifyTime"})
  1481. if err != nil {
  1482. br.Msg = "保存失败"
  1483. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1484. return
  1485. }
  1486. }
  1487. if len(configEdbMappingList) > 0 {
  1488. err = data_manage.AddMultipleGraphConfigEdbMappingList(configEdbMappingList)
  1489. if err != nil {
  1490. br.Msg = "保存失败"
  1491. br.ErrMsg = "保存配置与指标的关联关系失败,ERR:" + err.Error()
  1492. return
  1493. }
  1494. }
  1495. }
  1496. }
  1497. resp.Success = calculateEdbList
  1498. br.Ret = 200
  1499. br.Success = true
  1500. br.Msg = "保存成功"
  1501. br.Data = resp
  1502. br.IsAddLog = true
  1503. }
  1504. // MultipleGraphConfigEdbList
  1505. // @Title 获取指标列表接口
  1506. // @Description 获取指标列表接口
  1507. // @Param request body request.SaveMultipleGraphEdbReq true "type json string"
  1508. // @Success Ret=200 返回指标id
  1509. // @router /edb/list [get]
  1510. func (this *RangeChartChartInfoController) MultipleGraphConfigEdbList() {
  1511. br := new(models.BaseResponse).Init()
  1512. defer func() {
  1513. this.Data["json"] = br
  1514. this.ServeJSON()
  1515. }()
  1516. sysUser := this.SysUser
  1517. if sysUser == nil {
  1518. br.Msg = "请登录"
  1519. br.ErrMsg = "请登录,SysUser Is Empty"
  1520. br.Ret = 408
  1521. return
  1522. }
  1523. multipleGraphConfigId, _ := this.GetInt("MultipleGraphConfigId")
  1524. if multipleGraphConfigId <= 0 {
  1525. br.Msg = "参数错误"
  1526. br.ErrMsg = "参数错误,MultipleGraphConfigId Is Empty"
  1527. return
  1528. }
  1529. list := make([]*data_manage.ChartRangeAnalysisConfigEdbItem, 0)
  1530. resp := data_manage.ChartRangeAnalysisConfigEdbResp{EdbInfoList: list}
  1531. multipleGraphConfigEdbMappingList, err := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(multipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1532. if err != nil {
  1533. br.Msg = `保存失败`
  1534. br.ErrMsg = "查询配置与图表的关联关系失败,ERR:" + err.Error()
  1535. return
  1536. }
  1537. if len(multipleGraphConfigEdbMappingList) == 0 {
  1538. br.Ret = 200
  1539. br.Success = true
  1540. br.Msg = "操作成功"
  1541. br.Data = resp
  1542. br.IsAddLog = true
  1543. return
  1544. }
  1545. edbInfoIds := make([]int, 0)
  1546. for _, v := range multipleGraphConfigEdbMappingList {
  1547. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  1548. }
  1549. edbList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIds)
  1550. if err != nil {
  1551. br.Msg = "获取失败"
  1552. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  1553. return
  1554. }
  1555. edbInfoMap := make(map[int]*data_manage.ChartEdbInfoMapping, 0)
  1556. for _, v := range edbList {
  1557. edbInfoMap[v.EdbInfoId] = v
  1558. }
  1559. edbCalculateMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  1560. if e != nil {
  1561. br.Msg = `保存失败`
  1562. br.ErrMsg = "获取计算指标信息失败,ERR:" + e.Error()
  1563. return
  1564. }
  1565. // 说明指标还在,没有被删除
  1566. for _, v := range edbCalculateMappingList {
  1567. tmp := new(data_manage.ChartRangeAnalysisConfigEdbItem)
  1568. info, ok := edbInfoMap[v.EdbInfoId]
  1569. if ok {
  1570. tmp.EdbInfoId = v.EdbInfoId
  1571. tmp.Unit = info.Unit
  1572. tmp.UnitEn = info.UnitEn
  1573. tmp.Frequency = info.Frequency
  1574. tmp.EdbNameEn = info.EdbNameEn
  1575. tmp.EdbName = info.EdbName
  1576. tmp.ClassifyId = info.ClassifyId
  1577. tmp.FromEdbInfoId = v.FromEdbInfoId
  1578. tmp.EdbTypeInfo = info.EdbInfoCategoryType
  1579. list = append(list, tmp)
  1580. }
  1581. }
  1582. resp.EdbInfoList = list
  1583. br.Ret = 200
  1584. br.Success = true
  1585. br.Msg = "保存成功"
  1586. br.Data = resp
  1587. br.IsAddLog = true
  1588. }
  1589. // SearchByEs
  1590. // @Title 图表模糊搜索(从es获取)
  1591. // @Description 图表模糊搜索(从es获取)
  1592. // @Param Keyword query string true "图表名称"
  1593. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1594. // @Param Source query int true "来源,3:相关性,4:滚动相关性,默认0:全部"
  1595. // @Success 200 {object} data_manage.ChartInfo
  1596. // @router /chart_info/search_by_es [get]
  1597. func (this *RangeChartChartInfoController) SearchByEs() {
  1598. br := new(models.BaseResponse).Init()
  1599. defer func() {
  1600. this.Data["json"] = br
  1601. this.ServeJSON()
  1602. }()
  1603. sysUser := this.SysUser
  1604. if sysUser == nil {
  1605. br.Msg = "请登录"
  1606. br.ErrMsg = "请登录,SysUser Is Empty"
  1607. br.Ret = 408
  1608. return
  1609. }
  1610. pageSize, _ := this.GetInt("PageSize")
  1611. currentIndex, _ := this.GetInt("CurrentIndex")
  1612. var startSize int
  1613. if pageSize <= 0 {
  1614. pageSize = utils.PageSize20
  1615. }
  1616. if currentIndex <= 0 {
  1617. currentIndex = 1
  1618. }
  1619. startSize = paging.StartIndex(currentIndex, pageSize)
  1620. keyword := this.GetString("Keyword")
  1621. //只看我的
  1622. isShowMe, _ := this.GetBool("IsShowMe")
  1623. showSysId := 0
  1624. if isShowMe {
  1625. showSysId = sysUser.AdminId
  1626. }
  1627. sourceList := make([]int, 0)
  1628. sourceList = append(sourceList, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1629. var searchList []*data_manage.ChartInfo
  1630. var total int64
  1631. var err error
  1632. // 获取当前账号的不可见指标
  1633. noPermissionChartIdList := make([]int, 0)
  1634. {
  1635. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1636. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  1637. if err != nil && err.Error() != utils.ErrNoRow() {
  1638. br.Msg = "获取失败"
  1639. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1640. return
  1641. }
  1642. for _, v := range confList {
  1643. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  1644. }
  1645. }
  1646. if keyword != "" {
  1647. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1648. } else {
  1649. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1650. if err != nil && err.Error() != utils.ErrNoRow() {
  1651. br.Msg = "获取失败"
  1652. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1653. return
  1654. }
  1655. }
  1656. finalList := make([]*data_manage.ChartInfoMore, 0)
  1657. if len(searchList) > 0 {
  1658. chartInfoIds := ""
  1659. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  1660. for _, v := range searchList {
  1661. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  1662. }
  1663. if chartInfoIds != "" {
  1664. chartInfoIds = strings.Trim(chartInfoIds, ",")
  1665. //判断是否需要展示英文标识
  1666. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  1667. if e != nil {
  1668. br.Msg = "获取失败"
  1669. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  1670. return
  1671. }
  1672. for _, v := range edbList {
  1673. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  1674. }
  1675. }
  1676. for _, v := range searchList {
  1677. tmp := new(data_manage.ChartInfoMore)
  1678. tmp.ChartInfo = *v
  1679. // 图表数据权限
  1680. tmp.HaveOperaAuth = true
  1681. //判断是否需要展示英文标识
  1682. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  1683. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  1684. }
  1685. finalList = append(finalList, tmp)
  1686. }
  1687. }
  1688. //新增搜索词记录
  1689. {
  1690. searchKeyword := new(data_manage.SearchKeyword)
  1691. searchKeyword.KeyWord = keyword
  1692. searchKeyword.CreateTime = time.Now()
  1693. go data_manage.AddSearchKeyword(searchKeyword)
  1694. }
  1695. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1696. resp := data_manage.ChartInfoListByEsResp{
  1697. Paging: page,
  1698. List: finalList,
  1699. }
  1700. br.Ret = 200
  1701. br.Success = true
  1702. br.Msg = "获取成功"
  1703. br.Data = resp
  1704. }
  1705. // BaseInfoEdit
  1706. // @Title 编辑图表基础信息接口
  1707. // @Description 编辑图表基础信息接口
  1708. // @Param request body data_manage.EditChartInfoBaseReq true "type json string"
  1709. // @Success Ret=200 编辑成功
  1710. // @router /chart_info/base/edit [post]
  1711. func (this *RangeChartChartInfoController) BaseInfoEdit() {
  1712. br := new(models.BaseResponse).Init()
  1713. defer func() {
  1714. this.Data["json"] = br
  1715. this.ServeJSON()
  1716. }()
  1717. sysUser := this.SysUser
  1718. if sysUser == nil {
  1719. br.Msg = "请登录"
  1720. br.ErrMsg = "请登录,SysUser Is Empty"
  1721. br.Ret = 408
  1722. return
  1723. }
  1724. var req data_manage.EditChartRangeBaseReq
  1725. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1726. if err != nil {
  1727. br.Msg = "参数解析异常!"
  1728. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1729. return
  1730. }
  1731. req.ChartName = strings.Trim(req.ChartName, " ")
  1732. if req.ChartInfoId <= 0 {
  1733. br.Msg = "请选择图表"
  1734. return
  1735. }
  1736. if req.ChartName == "" {
  1737. br.Msg = "请输入图表名称"
  1738. return
  1739. }
  1740. chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId)
  1741. if e != nil {
  1742. if e.Error() == utils.ErrNoRow() {
  1743. br.Msg = "图表已被删除, 请刷新页面"
  1744. return
  1745. }
  1746. br.Msg = "获取图表信息失败"
  1747. br.ErrMsg = "获取图表信息失败, Err: " + e.Error()
  1748. return
  1749. }
  1750. if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
  1751. br.Msg = "该图不是区间计算图表"
  1752. return
  1753. }
  1754. var condition string
  1755. var pars []interface{}
  1756. condition += " AND chart_info_id <> ? AND source = ? "
  1757. pars = append(pars, req.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1758. switch this.Lang {
  1759. case utils.EnLangVersion:
  1760. condition += " AND chart_name_en = ? "
  1761. default:
  1762. condition += " AND chart_name = ? "
  1763. }
  1764. pars = append(pars, req.ChartName)
  1765. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  1766. if err != nil {
  1767. if err.Error() != utils.ErrNoRow() {
  1768. br.Msg = "判断英文图表名称是否存在失败"
  1769. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  1770. return
  1771. }
  1772. }
  1773. if err == nil && existItem.ChartInfoId > 0 {
  1774. br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
  1775. return
  1776. }
  1777. switch this.Lang {
  1778. case utils.EnLangVersion:
  1779. chartItem.ChartNameEn = req.ChartName
  1780. default:
  1781. chartItem.ChartName = req.ChartName
  1782. }
  1783. chartItem.ModifyTime = time.Now().Local()
  1784. if e := chartItem.Update([]string{"ChartName", "ChartNameEn", "ModifyTime"}); e != nil {
  1785. br.Msg = "操作失败"
  1786. br.ErrMsg = "更新图表信息失败, Err: " + e.Error()
  1787. return
  1788. }
  1789. //添加es数据
  1790. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1791. //修改my eta es数据
  1792. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1793. //新增操作日志
  1794. {
  1795. chartLog := new(data_manage.ChartInfoLog)
  1796. chartLog.ChartName = chartItem.ChartName
  1797. chartLog.ChartInfoId = req.ChartInfoId
  1798. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1799. chartLog.SysUserId = sysUser.AdminId
  1800. chartLog.SysUserRealName = sysUser.RealName
  1801. chartLog.UniqueCode = chartItem.UniqueCode
  1802. chartLog.CreateTime = time.Now()
  1803. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1804. chartLog.Status = "编辑区间计算图表基础信息"
  1805. chartLog.Method = this.Ctx.Input.URL()
  1806. go data_manage.AddChartInfoLog(chartLog)
  1807. }
  1808. // 清除缓存
  1809. if utils.Re == nil && utils.Rc != nil {
  1810. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  1811. _ = utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  1812. }
  1813. br.Ret = 200
  1814. br.Success = true
  1815. br.Msg = "编辑成功"
  1816. br.IsAddLog = true
  1817. }
  1818. // @Title 保存图表接口
  1819. // @Description 保存图表接口
  1820. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  1821. // @Success Ret=200 返回图表id
  1822. // @router /chart_info/save [post]
  1823. func (this *RangeChartChartInfoController) ChartInfoSave() {
  1824. br := new(models.BaseResponse).Init()
  1825. defer func() {
  1826. this.Data["json"] = br
  1827. this.ServeJSON()
  1828. }()
  1829. sysUser := this.SysUser
  1830. if sysUser == nil {
  1831. br.Msg = "请登录"
  1832. br.ErrMsg = "请登录,SysUser Is Empty"
  1833. br.Ret = 408
  1834. return
  1835. }
  1836. var req data_manage.SaveChartRangeReq
  1837. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1838. if err != nil {
  1839. br.Msg = "参数解析异常!"
  1840. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1841. return
  1842. }
  1843. if req.ChartInfoId <= 0 {
  1844. br.Msg = "参数错误!"
  1845. return
  1846. }
  1847. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1848. if err != nil {
  1849. if err.Error() == utils.ErrNoRow() {
  1850. br.Msg = "图表已被删除,请刷新页面!"
  1851. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  1852. return
  1853. }
  1854. br.Msg = "获取图表信息失败!"
  1855. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1856. return
  1857. }
  1858. //图表操作权限
  1859. //ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  1860. //if !ok {
  1861. // br.Msg = "没有该图表的操作权限"
  1862. // br.ErrMsg = "没有该图表的操作权限"
  1863. // return
  1864. //}
  1865. err = data_manage.EditRangeChartInfo(&req)
  1866. if err != nil {
  1867. br.Msg = "保存失败"
  1868. br.ErrMsg = "保存失败,Err:" + err.Error()
  1869. return
  1870. }
  1871. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  1872. if utils.Re == nil && utils.Rc != nil {
  1873. utils.Rc.Delete(key)
  1874. }
  1875. //修改es数据
  1876. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1877. //修改my eta es数据
  1878. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1879. //新增操作日志
  1880. {
  1881. chartLog := new(data_manage.ChartInfoLog)
  1882. chartLog.ChartName = chartItem.ChartName
  1883. chartLog.ChartInfoId = req.ChartInfoId
  1884. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1885. chartLog.SysUserId = sysUser.AdminId
  1886. chartLog.SysUserRealName = sysUser.RealName
  1887. chartLog.UniqueCode = chartItem.UniqueCode
  1888. chartLog.CreateTime = time.Now()
  1889. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1890. chartLog.Status = "修改配置项"
  1891. chartLog.Method = this.Ctx.Input.URI()
  1892. go data_manage.AddChartInfoLog(chartLog)
  1893. }
  1894. br.Ret = 200
  1895. br.Success = true
  1896. br.Msg = "保存成功"
  1897. br.IsAddLog = true
  1898. }