chart_info.go 61 KB

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