chart_info.go 62 KB

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