chart_info.go 63 KB

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