chart_info.go 62 KB

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