chart_info.go 62 KB

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