chart_info.go 61 KB

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