chart_info.go 63 KB

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