chart_info.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/controllers"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/models/data_manage/chart_theme"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services/data"
  10. "eta/eta_mobile/services/data/data_manage_permission"
  11. rangeServ "eta/eta_mobile/services/data/range_analysis"
  12. "eta/eta_mobile/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // RangeChartChartInfoController 区间计算图表管理
  20. type RangeChartChartInfoController struct {
  21. controllers.BaseAuthController
  22. }
  23. // Preview
  24. // @Title 区间计算图表-预览数据
  25. // @Description 区间计算图表-获取预览数据
  26. // @Param StartDate query string true "自定义开始日期"
  27. // @Param EndDate query string true "自定义结束日期"
  28. // @Param LeadValue query int true "领先值"
  29. // @Param LeadUnit query string true "领先单位:年,天,月,季,周"
  30. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  31. // @router /chart_info/preview [post]
  32. func (this *RangeChartChartInfoController) Preview() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. sysUser := this.SysUser
  39. if sysUser == nil {
  40. br.Msg = "请登录"
  41. br.ErrMsg = "请登录,SysUser Is Empty"
  42. br.Ret = 408
  43. return
  44. }
  45. var req data_manage.ChartRangeAnalysisPreviewReq
  46. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  47. br.Msg = "参数解析异常!"
  48. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  49. return
  50. }
  51. // 基础校验
  52. if len(req.ChartEdbInfoList) == 0 {
  53. br.Msg = "请选择指标"
  54. return
  55. }
  56. err, msg, isSendEmail := rangeServ.CheckChartRangeExtraConfig(req.ExtraConfig)
  57. if err != nil {
  58. br.Msg = msg
  59. br.ErrMsg = err.Error()
  60. br.IsSendEmail = isSendEmail
  61. return
  62. }
  63. // 获取指标信息
  64. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  65. edbInfoMappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  66. for _, v := range req.ChartEdbInfoList {
  67. edbInfoMapping, e := data_manage.GetChartEdbMappingByEdbInfoId(v.EdbInfoId)
  68. if e != nil {
  69. br.Msg = "获取失败"
  70. br.ErrMsg = "获取区间计算图表, A指标mapping信息失败, Err:" + e.Error()
  71. return
  72. }
  73. if v.EdbAliasName != "" {
  74. edbInfoMapping.EdbAliasName = v.EdbAliasName
  75. }
  76. edbInfoMapping.IsAxis = v.IsAxis
  77. edbInfoMappingList = append(edbInfoMappingList, edbInfoMapping)
  78. }
  79. chartInfo := new(data_manage.ChartInfoView)
  80. chartInfo.ChartType = 1
  81. chartInfo.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  82. chartTheme, err := chart_theme.GetSystemChartTheme(chartInfo.ChartType)
  83. if err != nil {
  84. br.Msg = "获取失败"
  85. br.ErrMsg = "获取图表主题失败, Err:" + err.Error()
  86. return
  87. }
  88. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  89. chartInfo.ChartThemeStyle = chartTheme.Config
  90. // 获取图表x轴y轴
  91. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(0, req.DateType, req.StartYear, req.StartDate, req.EndDate, edbInfoMappingList, &req.ExtraConfig)
  92. if e != nil {
  93. br.Msg = "获取失败"
  94. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  95. return
  96. }
  97. if req.ExtraConfig.DataConvertType > 0 && req.ExtraConfig.DataConvertConf.Unit != "" && req.ExtraConfig.DataConvertConf.Unit != "无" {
  98. chartInfo.Unit = req.ExtraConfig.DataConvertConf.Unit
  99. chartInfo.UnitEn = req.ExtraConfig.DataConvertConf.EnUnit
  100. }
  101. //添加配置信息
  102. if req.ExtraConfig.MultipleGraphConfigId == 0 {
  103. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  104. //MultipleGraphConfigId: 0,
  105. SysUserId: sysUser.AdminId,
  106. SysUserRealName: sysUser.RealName,
  107. ModifyTime: time.Now(),
  108. CreateTime: time.Now(),
  109. }
  110. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  111. if err != nil {
  112. br.Msg = "获取失败"
  113. br.ErrMsg = "新增区间计算图表配置失败, Err: " + err.Error()
  114. return
  115. }
  116. dataResp.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  117. }
  118. resp := new(data_manage.ChartInfoDetailResp)
  119. resp.ChartInfo = chartInfo
  120. resp.EdbInfoList = edbList
  121. resp.XEdbIdValue = xEdbIdValue
  122. resp.DataResp = dataResp
  123. br.Data = resp
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "获取成功"
  127. }
  128. // Detail
  129. // @Title 获取图表详情
  130. // @Description 获取图表详情接口
  131. // @Param ChartInfoId query int true "图表id"
  132. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  133. // @Param StartDate query string true "自定义开始日期"
  134. // @Param EndDate query string true "自定义结束日期"
  135. // @Param Calendar query string true "公历/农历"
  136. // @Param SeasonStartDate query string true "季节性图开始日期"
  137. // @Param SeasonEndDate query string true "季节性图结束日期"
  138. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  139. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  140. // @Success 200 {object} data_manage.ChartInfoDetailResp
  141. // @router /chart_info/detail [get]
  142. func (this *RangeChartChartInfoController) Detail() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. this.Data["json"] = br
  146. this.ServeJSON()
  147. }()
  148. sysUser := this.SysUser
  149. if sysUser == nil {
  150. br.Msg = "请登录"
  151. br.ErrMsg = "请登录,SysUser Is Empty"
  152. br.Ret = 408
  153. return
  154. }
  155. chartInfoId, _ := this.GetInt("ChartInfoId")
  156. if chartInfoId <= 0 {
  157. br.Msg = "参数有误"
  158. return
  159. }
  160. dateType, _ := this.GetInt("DateType")
  161. fmt.Println("dateType:", dateType)
  162. startDate := this.GetString("StartDate")
  163. endDate := this.GetString("EndDate")
  164. startYear, _ := this.GetInt("StartYear")
  165. var err error
  166. chartInfo := new(data_manage.ChartInfoView)
  167. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  168. if err != nil {
  169. if err.Error() == utils.ErrNoRow() {
  170. br.Msg = "图被删除,请刷新页面"
  171. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  172. return
  173. }
  174. br.Msg = "获取失败"
  175. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  176. return
  177. }
  178. if dateType <= 0 {
  179. dateType = chartInfo.DateType
  180. }
  181. if startDate == "" {
  182. startDate = chartInfo.StartDate
  183. }
  184. if endDate == "" {
  185. endDate = chartInfo.EndDate
  186. }
  187. if startYear <= 0 {
  188. startYear = chartInfo.StartYear
  189. }
  190. // 获取主题样式
  191. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, chartInfo.ChartType)
  192. if err != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  195. return
  196. }
  197. chartInfo.ChartThemeStyle = chartTheme.Config
  198. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  199. // 获取指标信息
  200. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  201. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  202. if err != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  205. return
  206. }
  207. // 获取图表数据
  208. if len(edbInfoMappingList) == 0 {
  209. br.Msg = "获取失败"
  210. br.ErrMsg = "图表没有指标,无法计算"
  211. return
  212. }
  213. // 区间计算图表配置校验
  214. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  215. if chartInfo.ExtraConfig == `` {
  216. br.Msg = "配置信息错误"
  217. return
  218. }
  219. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  220. if err != nil {
  221. br.Msg = "配置信息错误"
  222. br.ErrMsg = "图表配置信息错误,Err:" + err.Error()
  223. return
  224. }
  225. // 获取图表x轴y轴
  226. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  227. if e != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  230. return
  231. }
  232. // 判断是否加入我的图库
  233. if chartInfoId > 0 && chartInfo != nil {
  234. {
  235. var myChartCondition string
  236. var myChartPars []interface{}
  237. myChartCondition += ` AND a.admin_id=? `
  238. myChartPars = append(myChartPars, sysUser.AdminId)
  239. myChartCondition += ` AND a.chart_info_id=? `
  240. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  241. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  242. if err != nil && err.Error() != utils.ErrNoRow() {
  243. br.Msg = "获取失败"
  244. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  245. return
  246. }
  247. if myChartList != nil && len(myChartList) > 0 {
  248. chartInfo.IsAdd = true
  249. chartInfo.MyChartId = myChartList[0].MyChartId
  250. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  251. }
  252. }
  253. }
  254. //图表操作权限
  255. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  256. //判断是否需要展示英文标识
  257. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  258. // todo 中英文处理
  259. if extraConfig.DataConvertType > 0 && extraConfig.DataConvertConf.Unit != "" && extraConfig.DataConvertConf.Unit != "无" {
  260. chartInfo.Unit = extraConfig.DataConvertConf.Unit
  261. chartInfo.UnitEn = extraConfig.DataConvertConf.EnUnit
  262. }
  263. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  264. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  265. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  266. // 另存为
  267. chartInfo.Button = data_manage.ChartViewButton{
  268. IsEdit: chartInfo.IsEdit,
  269. IsEnChart: chartInfo.IsEnChart,
  270. IsAdd: chartInfo.IsAdd,
  271. IsCopy: chartInfo.IsEdit,
  272. IsSetName: chartInfo.IsSetName,
  273. }
  274. // 指标权限
  275. {
  276. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  277. classifyMap := make(map[int]*data_manage.EdbClassify)
  278. // 分类
  279. {
  280. classifyIdList := make([]int, 0)
  281. for _, v := range edbList {
  282. classifyIdList = append(classifyIdList, v.ClassifyId)
  283. }
  284. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  285. if tmpErr != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  288. return
  289. }
  290. for _, v := range classifyList {
  291. classifyMap[v.ClassifyId] = v
  292. }
  293. }
  294. // 遍历到校验map
  295. for _, v := range edbList {
  296. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  297. ClassifyId: v.ClassifyId,
  298. IsJoinPermission: v.IsJoinPermission,
  299. EdbInfoId: v.EdbInfoId,
  300. }
  301. }
  302. // 获取所有有权限的指标和分类
  303. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  304. if err != nil {
  305. br.Msg = "获取失败"
  306. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  307. return
  308. }
  309. for _, v := range edbList {
  310. // 数据权限
  311. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  312. if !ok {
  313. continue
  314. }
  315. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  316. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  317. }
  318. }
  319. }
  320. resp := new(data_manage.ChartInfoDetailResp)
  321. resp.ChartInfo = chartInfo
  322. resp.EdbInfoList = edbList
  323. resp.XEdbIdValue = xEdbIdValue
  324. resp.DataResp = dataResp
  325. br.Ret = 200
  326. br.Success = true
  327. br.Msg = "获取成功"
  328. br.Data = resp
  329. }
  330. // DetailFromUniqueCode
  331. // @Title 根据编码获取图表详情
  332. // @Description 根据编码获取图表详情接口
  333. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  334. // @Param IsCache query bool true "是否走缓存,默认false"
  335. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  336. // @router /chart_info/detail/from_unique_code [get]
  337. func (this *RangeChartChartInfoController) DetailFromUniqueCode() {
  338. br := new(models.BaseResponse).Init()
  339. defer func() {
  340. this.Data["json"] = br
  341. this.ServeJSON()
  342. }()
  343. sysUser := this.SysUser
  344. if sysUser == nil {
  345. br.Msg = "请登录"
  346. br.ErrMsg = "请登录,SysUser Is Empty"
  347. br.Ret = 408
  348. return
  349. }
  350. adminId := sysUser.AdminId
  351. uniqueCode := this.GetString("UniqueCode")
  352. if uniqueCode == "" {
  353. br.Msg = "参数错误"
  354. br.ErrMsg = "参数错误,uniqueCode is empty"
  355. return
  356. }
  357. //是否走缓存
  358. isCache, _ := this.GetBool("IsCache")
  359. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  360. status := true
  361. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  362. if err != nil {
  363. if err.Error() == utils.ErrNoRow() {
  364. status = false
  365. } else {
  366. br.Msg = "获取失败"
  367. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  368. return
  369. }
  370. }
  371. if chartInfo == nil {
  372. status = false
  373. }
  374. if !status {
  375. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  376. resp.EdbInfoList = endInfoList
  377. resp.ChartInfo = chartInfo
  378. resp.Status = false
  379. br.Ret = 200
  380. br.Success = true
  381. br.Msg = "获取成功"
  382. br.Data = resp
  383. return
  384. }
  385. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  386. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  387. if utils.Re == nil && isCache {
  388. if utils.Re == nil && utils.Rc.IsExist(key) {
  389. if d, e := utils.Rc.RedisBytes(key); e == nil {
  390. err := json.Unmarshal(d, &resp)
  391. if err == nil && resp != nil {
  392. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  393. var myCond string
  394. var myPars []interface{}
  395. myCond += ` AND a.admin_id=? `
  396. myPars = append(myPars, adminId)
  397. myCond += ` AND a.chart_info_id=? `
  398. myPars = append(myPars, chartInfo.ChartInfoId)
  399. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  400. if err != nil && err.Error() != utils.ErrNoRow() {
  401. br.Msg = "获取失败"
  402. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  403. return
  404. }
  405. resp.ChartInfo.IsAdd = false
  406. resp.ChartInfo.MyChartId = 0
  407. resp.ChartInfo.MyChartClassifyId = ""
  408. if myList != nil && len(myList) > 0 {
  409. resp.ChartInfo.IsAdd = true
  410. resp.ChartInfo.MyChartId = myList[0].MyChartId
  411. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  412. }
  413. br.Ret = 200
  414. br.Success = true
  415. br.Msg = "获取成功"
  416. br.Data = resp
  417. return
  418. }
  419. }
  420. }
  421. }
  422. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  423. if !isOk {
  424. br.Msg = msg
  425. br.ErrMsg = errMsg
  426. return
  427. }
  428. br.Ret = 200
  429. br.Success = true
  430. br.Msg = "获取成功"
  431. br.Data = resp
  432. }
  433. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  434. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  435. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  436. adminId := sysUser.AdminId
  437. // 指标数据map
  438. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  439. defer func() {
  440. if isOk {
  441. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  442. {
  443. //判断是否加入我的图库
  444. var myChartCondition string
  445. var myChartPars []interface{}
  446. myChartCondition += ` AND a.admin_id=? `
  447. myChartPars = append(myChartPars, adminId)
  448. myChartCondition += ` AND a.chart_info_id=? `
  449. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  450. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  451. if err != nil && err.Error() != utils.ErrNoRow() {
  452. msg = "获取失败"
  453. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  454. return
  455. }
  456. if myChartList != nil && len(myChartList) > 0 {
  457. chartInfo.IsAdd = true
  458. chartInfo.MyChartId = myChartList[0].MyChartId
  459. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  460. }
  461. }
  462. resp.ChartInfo.HaveOperaAuth = true
  463. // 指标权限
  464. {
  465. classifyMap := make(map[int]*data_manage.EdbClassify)
  466. // 分类
  467. {
  468. classifyIdList := make([]int, 0)
  469. for _, v := range resp.EdbInfoList {
  470. classifyIdList = append(classifyIdList, v.ClassifyId)
  471. }
  472. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  473. if tmpErr != nil {
  474. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  475. return
  476. }
  477. for _, v := range classifyList {
  478. classifyMap[v.ClassifyId] = v
  479. }
  480. }
  481. // 指标
  482. if len(edbClassifyPermissionMap) < 0 {
  483. edbInfoIdList := make([]int, 0)
  484. for _, v := range resp.EdbInfoList {
  485. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  486. }
  487. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  488. if tmpErr != nil {
  489. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  490. return
  491. }
  492. for _, v := range edbInfoList {
  493. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  494. ClassifyId: v.ClassifyId,
  495. IsJoinPermission: v.IsJoinPermission,
  496. EdbInfoId: v.EdbInfoId,
  497. }
  498. }
  499. }
  500. // 获取所有有权限的指标和分类
  501. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  502. if err != nil {
  503. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  504. return
  505. }
  506. for _, v := range resp.EdbInfoList {
  507. // 数据权限
  508. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  509. if !ok {
  510. continue
  511. }
  512. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  513. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  514. }
  515. }
  516. }
  517. }
  518. }()
  519. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  520. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  521. if utils.Re == nil && isCache {
  522. if utils.Re == nil && utils.Rc.IsExist(key) {
  523. if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  524. err := json.Unmarshal(chartData, &resp)
  525. if err != nil || resp == nil {
  526. return
  527. }
  528. isOk = true
  529. fmt.Println("source redis")
  530. return
  531. }
  532. }
  533. }
  534. // 获取主题样式
  535. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  536. if err != nil {
  537. msg = "获取失败"
  538. errMsg = "获取主题信息失败,Err:" + err.Error()
  539. return
  540. }
  541. chartInfo.ChartThemeStyle = chartTheme.Config
  542. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  543. chartInfoId := chartInfo.ChartInfoId
  544. // 获取指标信息
  545. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  546. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  547. if err != nil {
  548. msg = "获取失败"
  549. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  550. return
  551. }
  552. dateType := chartInfo.DateType
  553. // 开始/结束日期
  554. startYear := chartInfo.StartYear
  555. startDate := chartInfo.StartDate
  556. endDate := chartInfo.EndDate
  557. // 区间计算图表配置校验
  558. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  559. if chartInfo.ExtraConfig == `` {
  560. msg = "配置信息错误"
  561. return
  562. }
  563. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  564. if err != nil {
  565. msg = "配置信息错误"
  566. errMsg = "图表配置信息错误,Err:" + err.Error()
  567. return
  568. }
  569. // 获取图表数据
  570. if len(edbInfoMappingList) == 0 {
  571. msg = "获取失败"
  572. errMsg = "图表没有指标,无法计算"
  573. return
  574. }
  575. // 获取图表x轴y轴
  576. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  577. if e != nil {
  578. msg = "获取失败"
  579. errMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  580. return
  581. }
  582. if chartInfoId > 0 && chartInfo != nil {
  583. //判断是否加入我的图库
  584. {
  585. var myChartCondition string
  586. var myChartPars []interface{}
  587. myChartCondition += ` AND a.admin_id=? `
  588. myChartPars = append(myChartPars, sysUser.AdminId)
  589. myChartCondition += ` AND a.chart_info_id=? `
  590. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  591. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  592. if err != nil && err.Error() != utils.ErrNoRow() {
  593. msg = "获取失败"
  594. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  595. return
  596. }
  597. if myChartList != nil && len(myChartList) > 0 {
  598. chartInfo.IsAdd = true
  599. chartInfo.MyChartId = myChartList[0].MyChartId
  600. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  601. }
  602. }
  603. }
  604. //图表操作权限
  605. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  606. //判断是否需要展示英文标识
  607. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  608. chartInfo.UnitEn = ""
  609. // 另存为
  610. chartInfo.Button = data_manage.ChartViewButton{
  611. IsEdit: chartInfo.IsEdit,
  612. IsEnChart: chartInfo.IsEnChart,
  613. IsAdd: chartInfo.IsAdd,
  614. IsCopy: chartInfo.IsEdit,
  615. IsSetName: chartInfo.IsSetName,
  616. }
  617. // 图表的指标来源
  618. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  619. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  620. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  621. resp.ChartInfo = chartInfo
  622. resp.EdbInfoList = edbList
  623. resp.XEdbIdValue = xEdbIdValue
  624. resp.DataResp = dataResp
  625. resp.Status = true
  626. // 遍历到校验map
  627. for _, v := range edbList {
  628. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  629. ClassifyId: v.ClassifyId,
  630. IsJoinPermission: v.IsJoinPermission,
  631. EdbInfoId: v.EdbInfoId,
  632. }
  633. }
  634. // 将数据加入缓存
  635. if utils.Re == nil {
  636. d, _ := json.Marshal(resp)
  637. _ = utils.Rc.Put(key, d, 2*time.Hour)
  638. }
  639. isOk = true
  640. return
  641. }
  642. // List
  643. // @Title 区间计算图表列表接口
  644. // @Description 区间计算图表列表接口
  645. // @Param PageSize query int true "每页数据条数"
  646. // @Param CurrentIndex query int true "当前页页码,从1开始"
  647. // @Param ChartClassifyId query int true "分类id"
  648. // @Param Keyword query string true "搜索关键词"
  649. // @Param IsShowMe query bool true "是否只看我的,true、false"
  650. // @Param Source query int true "图表类型,3:区间计算,4:滚动区间计算"
  651. // @Success 200 {object} data_manage.ChartListResp
  652. // @router /chart_info/list [get]
  653. func (this *RangeChartChartInfoController) List() {
  654. br := new(models.BaseResponse).Init()
  655. defer func() {
  656. this.Data["json"] = br
  657. this.ServeJSON()
  658. }()
  659. sysUser := this.SysUser
  660. if sysUser == nil {
  661. br.Msg = "请登录"
  662. br.ErrMsg = "请登录,SysUser Is Empty"
  663. br.Ret = 408
  664. return
  665. }
  666. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  667. pageSize, _ := this.GetInt("PageSize")
  668. currentIndex, _ := this.GetInt("CurrentIndex")
  669. keyword := this.GetString("KeyWord")
  670. var total int
  671. page := paging.GetPaging(currentIndex, pageSize, total)
  672. var startSize int
  673. if pageSize <= 0 {
  674. pageSize = utils.PageSize20
  675. }
  676. if currentIndex <= 0 {
  677. currentIndex = 1
  678. }
  679. startSize = paging.StartIndex(currentIndex, pageSize)
  680. source := utils.CHART_SOURCE_RANGE_ANALYSIS
  681. var condition string
  682. var pars []interface{}
  683. // 普通图表
  684. condition += ` AND source = ? `
  685. pars = append(pars, source)
  686. if chartClassifyId > 0 {
  687. chartClassifyId, err := data_manage.GetChartClassify(chartClassifyId)
  688. if err != nil && err.Error() != utils.ErrNoRow() {
  689. br.Msg = "获取图表信息失败"
  690. br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error()
  691. return
  692. }
  693. condition += " AND chart_classify_id IN(" + chartClassifyId + ") "
  694. }
  695. if keyword != "" {
  696. //将关键词按照空格分割
  697. keywords := strings.Split(keyword, " ")
  698. condition += ` AND ( chart_name LIKE '%` + keywords[0] + `%' `
  699. for k, key := range keywords {
  700. if k == 0 {
  701. continue
  702. }
  703. condition += ` OR chart_name LIKE '%` + key + `%' `
  704. }
  705. condition += ` )`
  706. }
  707. //只看我的
  708. isShowMe, _ := this.GetBool("IsShowMe")
  709. if isShowMe {
  710. condition += ` AND sys_user_id = ? `
  711. pars = append(pars, sysUser.AdminId)
  712. }
  713. // 获取当前账号的不可见指标
  714. noPermissionChartIdList := make([]int, 0)
  715. {
  716. obj := data_manage.EdbInfoNoPermissionAdmin{}
  717. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  718. if err != nil && err.Error() != utils.ErrNoRow() {
  719. br.Msg = "获取失败"
  720. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  721. return
  722. }
  723. for _, v := range confList {
  724. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  725. }
  726. }
  727. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  728. if lenNoPermissionChartIdList > 0 {
  729. condition += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  730. pars = append(pars, noPermissionChartIdList)
  731. }
  732. //获取图表信息
  733. list, err := data_manage.GetChartListByCondition(condition, pars, startSize, pageSize)
  734. if err != nil && err.Error() != utils.ErrNoRow() {
  735. br.Success = true
  736. br.Msg = "获取图表信息失败"
  737. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  738. return
  739. }
  740. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  741. if err != nil && err.Error() != utils.ErrNoRow() {
  742. br.Msg = "获取图表信息失败"
  743. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  744. return
  745. }
  746. myChartMap := make(map[int]*data_manage.MyChartView)
  747. for _, v := range myChartList {
  748. myChartMap[v.ChartInfoId] = v
  749. }
  750. listLen := len(list)
  751. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  752. if listLen > 0 {
  753. chartInfoIds := ""
  754. for _, v := range list {
  755. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  756. }
  757. if chartInfoIds != "" {
  758. chartInfoIds = strings.Trim(chartInfoIds, ",")
  759. //判断是否需要展示英文标识
  760. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  761. if e != nil {
  762. br.Msg = "获取失败"
  763. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  764. return
  765. }
  766. for _, v := range edbList {
  767. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  768. }
  769. }
  770. }
  771. for i := 0; i < listLen; i++ {
  772. //判断是否需要展示英文标识
  773. if edbTmpList, ok := chartEdbMap[list[i].ChartInfoId]; ok {
  774. list[i].IsEnChart = data.CheckIsEnChart(list[i].ChartNameEn, edbTmpList, list[i].Source, list[i].ChartType)
  775. }
  776. if existItem, ok := myChartMap[list[i].ChartInfoId]; ok {
  777. list[i].IsAdd = true
  778. list[i].MyChartId = existItem.MyChartId
  779. list[i].MyChartClassifyId = existItem.MyChartClassifyId
  780. }
  781. }
  782. resp := new(data_manage.ChartListResp)
  783. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  784. items := make([]*data_manage.ChartInfoView, 0)
  785. resp.Paging = page
  786. resp.List = items
  787. br.Ret = 200
  788. br.Success = true
  789. br.Msg = "获取成功"
  790. return
  791. }
  792. dataCount, err := data_manage.GetChartListCountByCondition(condition, pars)
  793. if err != nil && err.Error() != utils.ErrNoRow() {
  794. br.Msg = "获取指标信息失败"
  795. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  796. return
  797. }
  798. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  799. resp.Paging = page
  800. resp.List = list
  801. br.Ret = 200
  802. br.Success = true
  803. br.Msg = "获取成功"
  804. br.Data = resp
  805. }
  806. // Copy
  807. // @Title 复制并新增图表接口
  808. // @Description 新增图表接口
  809. // @Params request body data_manage.CopyAddChartInfoReq true "type json string"
  810. // @Success 200 {object} data_manage.AddChartInfoResp
  811. // @router /chart_info/copy [post]
  812. func (this *RangeChartChartInfoController) Copy() {
  813. br := new(models.BaseResponse).Init()
  814. defer func() {
  815. if br.ErrMsg == "" {
  816. br.IsSendEmail = false
  817. }
  818. this.Data["json"] = br
  819. this.ServeJSON()
  820. }()
  821. sysUser := this.SysUser
  822. if sysUser == nil {
  823. br.Msg = "请登录"
  824. br.ErrMsg = "请登录,SysUser Is Empty"
  825. br.Ret = 408
  826. return
  827. }
  828. var req data_manage.CopyAddChartInfoReq
  829. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  830. if err != nil {
  831. br.Msg = "参数解析异常!"
  832. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  833. return
  834. }
  835. if req.ChartInfoId <= 0 {
  836. br.Msg = "参数有误"
  837. br.ErrMsg = fmt.Sprintf("参数有误, ChartInfoId: %d", req.ChartInfoId)
  838. return
  839. }
  840. req.ChartName = strings.TrimSpace(req.ChartName)
  841. if req.ChartName == "" {
  842. br.Msg = "请输入图表名称"
  843. return
  844. }
  845. if req.ChartClassifyId <= 0 {
  846. br.Msg = "请选择图表分类"
  847. return
  848. }
  849. deleteCache := true
  850. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  851. defer func() {
  852. if deleteCache {
  853. utils.Rc.Delete(cacheKey)
  854. }
  855. }()
  856. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  857. deleteCache = false
  858. br.Msg = "系统处理中,请稍后重试!"
  859. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  860. return
  861. }
  862. chartSource := utils.CHART_SOURCE_RANGE_ANALYSIS
  863. // 校验分类、图表名称
  864. {
  865. var cond string
  866. var pars []interface{}
  867. switch this.Lang {
  868. case utils.EnLangVersion:
  869. cond += " AND chart_name_en = ? AND source = ? "
  870. default:
  871. cond += " AND chart_name = ? AND source = ? "
  872. }
  873. pars = append(pars, req.ChartName, chartSource)
  874. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  875. if e != nil {
  876. br.Msg = "保存失败"
  877. br.ErrMsg = fmt.Sprintf("获取同名图表失败, Err: %v", e)
  878. return
  879. }
  880. if count > 0 {
  881. br.Msg = "图表名称已存在, 请重新填写"
  882. return
  883. }
  884. _, e = data_manage.GetChartClassifyById(req.ChartClassifyId)
  885. if e != nil {
  886. if e.Error() == utils.ErrNoRow() {
  887. br.Msg = "分类不存在"
  888. return
  889. }
  890. br.Msg = "保存失败"
  891. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  892. return
  893. }
  894. }
  895. // 图表信息
  896. originChart, e := data_manage.GetChartInfoById(req.ChartInfoId)
  897. if e != nil {
  898. if e.Error() == utils.ErrNoRow() {
  899. br.Msg = "原图表不存在"
  900. return
  901. }
  902. br.Msg = "保存失败"
  903. br.ErrMsg = fmt.Sprintf("获取原图表信息失败, Err: %v", e)
  904. return
  905. }
  906. if originChart.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  907. br.Msg = `滚动区间计算图不支持另存为`
  908. br.IsSendEmail = false
  909. return
  910. }
  911. // 普通区间计算图表
  912. chartInfo := new(data_manage.ChartInfo)
  913. newChart, err, errMsg, isSendEmail := rangeServ.CopyChartInfo(req.ChartClassifyId, req.ChartName, originChart, sysUser, this.Lang)
  914. chartInfo = newChart
  915. if err != nil {
  916. br.Msg = "保存失败"
  917. if errMsg != `` {
  918. br.Msg = errMsg
  919. }
  920. br.ErrMsg = err.Error()
  921. br.IsSendEmail = isSendEmail
  922. return
  923. }
  924. // 新增操作日志
  925. {
  926. chartLog := new(data_manage.ChartInfoLog)
  927. chartLog.ChartInfoId = chartInfo.ChartInfoId
  928. chartLog.ChartName = req.ChartName
  929. chartLog.ChartClassifyId = req.ChartClassifyId
  930. chartLog.SysUserId = sysUser.AdminId
  931. chartLog.SysUserRealName = sysUser.RealName
  932. chartLog.UniqueCode = chartInfo.UniqueCode
  933. chartLog.CreateTime = time.Now()
  934. chartLog.Content = string(this.Ctx.Input.RequestBody)
  935. chartLog.Status = "复制区间计算图表"
  936. chartLog.Method = this.Ctx.Input.URI()
  937. go data_manage.AddChartInfoLog(chartLog)
  938. }
  939. br.Ret = 200
  940. br.Success = true
  941. br.Msg = "保存成功"
  942. br.Data = data_manage.AddChartInfoResp{
  943. ChartInfoId: chartInfo.ChartInfoId,
  944. UniqueCode: chartInfo.UniqueCode,
  945. ChartType: chartInfo.ChartType,
  946. }
  947. br.IsAddLog = true
  948. }
  949. // Refresh
  950. // @Title 图表刷新接口
  951. // @Description 图表刷新接口
  952. // @Param ChartInfoId query int true "图表id"
  953. // @Param UniqueCode query string true "唯一code"
  954. // @Success Ret=200 刷新成功
  955. // @router /chart_info/refresh [get]
  956. func (this *RangeChartChartInfoController) Refresh() {
  957. br := new(models.BaseResponse).Init()
  958. defer func() {
  959. this.Data["json"] = br
  960. this.ServeJSON()
  961. }()
  962. sysUser := this.SysUser
  963. if sysUser == nil {
  964. br.Msg = "请登录"
  965. br.ErrMsg = "请登录,SysUser Is Empty"
  966. br.Ret = 408
  967. return
  968. }
  969. chartInfoId, _ := this.GetInt("ChartInfoId")
  970. uniqueCode := this.GetString("UniqueCode")
  971. if chartInfoId <= 0 && uniqueCode == `` {
  972. br.Msg = "参数错误"
  973. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  974. return
  975. }
  976. var chartInfo *data_manage.ChartInfo
  977. var err error
  978. if chartInfoId > 0 {
  979. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  980. } else {
  981. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  982. }
  983. if err != nil {
  984. if err.Error() == utils.ErrNoRow() {
  985. br.Msg = "图表已被删除,无需刷新"
  986. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  987. return
  988. }
  989. br.Msg = "刷新失败"
  990. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  991. return
  992. }
  993. // 刷新区间计算图表
  994. isAsync, e := rangeServ.ChartInfoRefresh(chartInfo.ChartInfoId, chartInfo.UniqueCode)
  995. if e != nil {
  996. br.Msg = "刷新失败"
  997. br.ErrMsg = "刷新区间计算图表失败, Err:" + err.Error()
  998. return
  999. }
  1000. // 多因子区间计算异步刷新, 前端提示
  1001. if isAsync {
  1002. br.Ret = 200
  1003. br.Success = true
  1004. br.Msg = "刷新时间较长, 请10分钟后查看"
  1005. return
  1006. }
  1007. //清除图表缓存
  1008. {
  1009. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1010. _ = utils.Rc.Delete(key)
  1011. }
  1012. br.Ret = 200
  1013. br.Success = true
  1014. br.Msg = "刷新成功"
  1015. }
  1016. // BaseInfoEdit
  1017. // @Title 编辑图表基础信息接口
  1018. // @Description 编辑图表基础信息接口
  1019. // @Param request body data_manage.EditChartInfoBaseReq true "type json string"
  1020. // @Success Ret=200 编辑成功
  1021. // @router /chart_info/base/edit [post]
  1022. func (this *RangeChartChartInfoController) BaseInfoEdit() {
  1023. br := new(models.BaseResponse).Init()
  1024. defer func() {
  1025. this.Data["json"] = br
  1026. this.ServeJSON()
  1027. }()
  1028. sysUser := this.SysUser
  1029. if sysUser == nil {
  1030. br.Msg = "请登录"
  1031. br.ErrMsg = "请登录,SysUser Is Empty"
  1032. br.Ret = 408
  1033. return
  1034. }
  1035. var req data_manage.EditChartRangeBaseReq
  1036. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1037. if err != nil {
  1038. br.Msg = "参数解析异常!"
  1039. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1040. return
  1041. }
  1042. req.ChartName = strings.Trim(req.ChartName, " ")
  1043. if req.ChartInfoId <= 0 {
  1044. br.Msg = "请选择图表"
  1045. return
  1046. }
  1047. if req.ChartName == "" {
  1048. br.Msg = "请输入图表名称"
  1049. return
  1050. }
  1051. chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId)
  1052. if e != nil {
  1053. if e.Error() == utils.ErrNoRow() {
  1054. br.Msg = "图表已被删除, 请刷新页面"
  1055. return
  1056. }
  1057. br.Msg = "获取图表信息失败"
  1058. br.ErrMsg = "获取图表信息失败, Err: " + e.Error()
  1059. return
  1060. }
  1061. if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
  1062. br.Msg = "该图不是区间计算图表"
  1063. return
  1064. }
  1065. var condition string
  1066. var pars []interface{}
  1067. condition += " AND chart_info_id <> ? AND source = ? "
  1068. pars = append(pars, req.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1069. switch this.Lang {
  1070. case utils.EnLangVersion:
  1071. condition += " AND chart_name_en = ? "
  1072. default:
  1073. condition += " AND chart_name = ? "
  1074. }
  1075. pars = append(pars, req.ChartName)
  1076. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  1077. if err != nil {
  1078. if err.Error() != utils.ErrNoRow() {
  1079. br.Msg = "判断英文图表名称是否存在失败"
  1080. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  1081. return
  1082. }
  1083. }
  1084. if err == nil && existItem.ChartInfoId > 0 {
  1085. br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
  1086. return
  1087. }
  1088. switch this.Lang {
  1089. case utils.EnLangVersion:
  1090. chartItem.ChartNameEn = req.ChartName
  1091. default:
  1092. chartItem.ChartName = req.ChartName
  1093. }
  1094. chartItem.ModifyTime = time.Now().Local()
  1095. if e := chartItem.Update([]string{"ChartName", "ChartNameEn", "ModifyTime"}); e != nil {
  1096. br.Msg = "操作失败"
  1097. br.ErrMsg = "更新图表信息失败, Err: " + e.Error()
  1098. return
  1099. }
  1100. //添加es数据
  1101. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1102. //修改my eta es数据
  1103. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1104. //新增操作日志
  1105. {
  1106. chartLog := new(data_manage.ChartInfoLog)
  1107. chartLog.ChartName = chartItem.ChartName
  1108. chartLog.ChartInfoId = req.ChartInfoId
  1109. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1110. chartLog.SysUserId = sysUser.AdminId
  1111. chartLog.SysUserRealName = sysUser.RealName
  1112. chartLog.UniqueCode = chartItem.UniqueCode
  1113. chartLog.CreateTime = time.Now()
  1114. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1115. chartLog.Status = "编辑区间计算图表基础信息"
  1116. chartLog.Method = this.Ctx.Input.URL()
  1117. go data_manage.AddChartInfoLog(chartLog)
  1118. }
  1119. // 清除缓存
  1120. if utils.Re == nil && utils.Rc != nil {
  1121. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  1122. _ = utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  1123. }
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Msg = "编辑成功"
  1127. br.IsAddLog = true
  1128. }
  1129. // @Title 保存图表接口
  1130. // @Description 保存图表接口
  1131. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  1132. // @Success Ret=200 返回图表id
  1133. // @router /chart_info/save [post]
  1134. func (this *RangeChartChartInfoController) ChartInfoSave() {
  1135. br := new(models.BaseResponse).Init()
  1136. defer func() {
  1137. this.Data["json"] = br
  1138. this.ServeJSON()
  1139. }()
  1140. sysUser := this.SysUser
  1141. if sysUser == nil {
  1142. br.Msg = "请登录"
  1143. br.ErrMsg = "请登录,SysUser Is Empty"
  1144. br.Ret = 408
  1145. return
  1146. }
  1147. var req data_manage.SaveChartRangeReq
  1148. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1149. if err != nil {
  1150. br.Msg = "参数解析异常!"
  1151. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1152. return
  1153. }
  1154. if req.ChartInfoId <= 0 {
  1155. br.Msg = "参数错误!"
  1156. return
  1157. }
  1158. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1159. if err != nil {
  1160. if err.Error() == utils.ErrNoRow() {
  1161. br.Msg = "图表已被删除,请刷新页面!"
  1162. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  1163. return
  1164. }
  1165. br.Msg = "获取图表信息失败!"
  1166. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1167. return
  1168. }
  1169. //图表操作权限
  1170. //ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  1171. //if !ok {
  1172. // br.Msg = "没有该图表的操作权限"
  1173. // br.ErrMsg = "没有该图表的操作权限"
  1174. // return
  1175. //}
  1176. err = data_manage.EditRangeChartInfo(&req)
  1177. if err != nil {
  1178. br.Msg = "保存失败"
  1179. br.ErrMsg = "保存失败,Err:" + err.Error()
  1180. return
  1181. }
  1182. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  1183. if utils.Re == nil && utils.Rc != nil {
  1184. utils.Rc.Delete(key)
  1185. }
  1186. //修改es数据
  1187. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1188. //修改my eta es数据
  1189. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1190. //新增操作日志
  1191. {
  1192. chartLog := new(data_manage.ChartInfoLog)
  1193. chartLog.ChartName = chartItem.ChartName
  1194. chartLog.ChartInfoId = req.ChartInfoId
  1195. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1196. chartLog.SysUserId = sysUser.AdminId
  1197. chartLog.SysUserRealName = sysUser.RealName
  1198. chartLog.UniqueCode = chartItem.UniqueCode
  1199. chartLog.CreateTime = time.Now()
  1200. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1201. chartLog.Status = "修改配置项"
  1202. chartLog.Method = this.Ctx.Input.URI()
  1203. go data_manage.AddChartInfoLog(chartLog)
  1204. }
  1205. br.Ret = 200
  1206. br.Success = true
  1207. br.Msg = "保存成功"
  1208. br.IsAddLog = true
  1209. }
  1210. // SearchByEs
  1211. // @Title 图表模糊搜索(从es获取)
  1212. // @Description 图表模糊搜索(从es获取)
  1213. // @Param Keyword query string true "图表名称"
  1214. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1215. // @Param Source query int true "来源,3:相关性,4:滚动相关性,默认0:全部"
  1216. // @Success 200 {object} data_manage.ChartInfo
  1217. // @router /chart_info/search_by_es [get]
  1218. func (this *RangeChartChartInfoController) SearchByEs() {
  1219. br := new(models.BaseResponse).Init()
  1220. defer func() {
  1221. this.Data["json"] = br
  1222. this.ServeJSON()
  1223. }()
  1224. sysUser := this.SysUser
  1225. if sysUser == nil {
  1226. br.Msg = "请登录"
  1227. br.ErrMsg = "请登录,SysUser Is Empty"
  1228. br.Ret = 408
  1229. return
  1230. }
  1231. pageSize, _ := this.GetInt("PageSize")
  1232. currentIndex, _ := this.GetInt("CurrentIndex")
  1233. var startSize int
  1234. if pageSize <= 0 {
  1235. pageSize = utils.PageSize20
  1236. }
  1237. if currentIndex <= 0 {
  1238. currentIndex = 1
  1239. }
  1240. startSize = paging.StartIndex(currentIndex, pageSize)
  1241. keyword := this.GetString("Keyword")
  1242. //只看我的
  1243. isShowMe, _ := this.GetBool("IsShowMe")
  1244. showSysId := 0
  1245. if isShowMe {
  1246. showSysId = sysUser.AdminId
  1247. }
  1248. sourceList := make([]int, 0)
  1249. sourceList = append(sourceList, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1250. var searchList []*data_manage.ChartInfo
  1251. var total int64
  1252. var err error
  1253. // 获取当前账号的不可见指标
  1254. noPermissionChartIdList := make([]int, 0)
  1255. {
  1256. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1257. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  1258. if err != nil && err.Error() != utils.ErrNoRow() {
  1259. br.Msg = "获取失败"
  1260. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1261. return
  1262. }
  1263. for _, v := range confList {
  1264. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  1265. }
  1266. }
  1267. if keyword != "" {
  1268. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1269. } else {
  1270. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1271. if err != nil && err.Error() != utils.ErrNoRow() {
  1272. br.Msg = "获取失败"
  1273. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1274. return
  1275. }
  1276. }
  1277. finalList := make([]*data_manage.ChartInfoMore, 0)
  1278. if len(searchList) > 0 {
  1279. chartInfoIds := ""
  1280. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  1281. for _, v := range searchList {
  1282. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  1283. }
  1284. if chartInfoIds != "" {
  1285. chartInfoIds = strings.Trim(chartInfoIds, ",")
  1286. //判断是否需要展示英文标识
  1287. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  1288. if e != nil {
  1289. br.Msg = "获取失败"
  1290. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  1291. return
  1292. }
  1293. for _, v := range edbList {
  1294. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  1295. }
  1296. }
  1297. for _, v := range searchList {
  1298. tmp := new(data_manage.ChartInfoMore)
  1299. tmp.ChartInfo = *v
  1300. // 图表数据权限
  1301. tmp.HaveOperaAuth = true
  1302. //判断是否需要展示英文标识
  1303. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  1304. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  1305. }
  1306. finalList = append(finalList, tmp)
  1307. }
  1308. }
  1309. //新增搜索词记录
  1310. {
  1311. searchKeyword := new(data_manage.SearchKeyword)
  1312. searchKeyword.KeyWord = keyword
  1313. searchKeyword.CreateTime = time.Now()
  1314. go data_manage.AddSearchKeyword(searchKeyword)
  1315. }
  1316. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1317. resp := data_manage.ChartInfoListByEsResp{
  1318. Paging: page,
  1319. List: finalList,
  1320. }
  1321. br.Ret = 200
  1322. br.Success = true
  1323. br.Msg = "获取成功"
  1324. br.Data = resp
  1325. }