chart_info.go 63 KB

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