chart_classify.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/services/data/data_manage_permission"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "time"
  14. )
  15. // 数据管理-分类模块
  16. type ChartClassifyController struct {
  17. controllers.BaseAuthController
  18. }
  19. //
  20. //// @Title 图表分类列表
  21. //// @Description 图表分类列表接口
  22. //// @Success 200 {object} data_manage.ChartClassifyListResp
  23. //// @router /chart_classify/list [get]
  24. //func (this *ChartClassifyController) ChartClassifyList() {
  25. // br := new(models.BaseResponse).Init()
  26. // defer func() {
  27. // this.Data["json"] = br
  28. // this.ServeJSON()
  29. // }()
  30. // rootList, err := data_manage.GetChartClassifyByParentId(0)
  31. // if err != nil {
  32. // br.Msg = "获取失败"
  33. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  34. // return
  35. // }
  36. //
  37. // classifyAll, err := data_manage.GetChartClassifyAll()
  38. // if err != nil {
  39. // br.Msg = "获取失败"
  40. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  41. // return
  42. // }
  43. //
  44. // nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  45. // for k := range rootList {
  46. // rootNode := rootList[k]
  47. // data.ChartClassifyListMakeTree(classifyAll, rootNode)
  48. // nodeAll = append(nodeAll, rootNode)
  49. // }
  50. // resp := new(data_manage.ChartClassifyListResp)
  51. // resp.AllNodes = nodeAll
  52. // br.Ret = 200
  53. // br.Success = true
  54. // br.Msg = "获取成功"
  55. // br.Data = resp
  56. //}
  57. // ChartClassifyListV2
  58. // @Title 图表分类列表
  59. // @Description 图表分类列表接口
  60. // @Param IsShowMe query bool true "是否只看我的,true、false"
  61. // @Success 200 {object} data_manage.ChartClassifyListResp
  62. // @router /chart_classify/list [get]
  63. func (this *ChartClassifyController) ChartClassifyListV2() {
  64. br := new(models.BaseResponse).Init()
  65. defer func() {
  66. this.Data["json"] = br
  67. this.ServeJSON()
  68. }()
  69. resp := new(data_manage.ChartClassifyListResp)
  70. // 获取当前账号的不可见指标
  71. noPermissionChartIdMap := make(map[int]bool)
  72. {
  73. obj := data_manage.EdbInfoNoPermissionAdmin{}
  74. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  75. if err != nil && err.Error() != utils.ErrNoRow() {
  76. br.Msg = "获取失败"
  77. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  78. return
  79. }
  80. for _, v := range confList {
  81. noPermissionChartIdMap[v.ChartInfoId] = true
  82. }
  83. }
  84. isShowMe, _ := this.GetBool("IsShowMe")
  85. if isShowMe {
  86. errMsg, err := getChartClassifyListForMe(*this.SysUser, resp)
  87. if err != nil {
  88. br.Msg = errMsg
  89. br.ErrMsg = err.Error()
  90. return
  91. }
  92. // 移除没有权限的图表
  93. allNodes := data.HandleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  94. resp.AllNodes = allNodes
  95. br.Ret = 200
  96. br.Success = true
  97. br.Msg = "获取成功"
  98. br.Data = resp
  99. fmt.Println("source my classify")
  100. return
  101. }
  102. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  103. key := utils.CACHE_CHART_CLASSIFY
  104. if utils.Re == nil {
  105. if utils.Re == nil && utils.Rc.IsExist(key) {
  106. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  107. err := json.Unmarshal(redisData, &resp)
  108. if err == nil && resp != nil {
  109. // 移除没有权限的图表
  110. allNodes := data.HandleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  111. resp.AllNodes = allNodes
  112. br.Ret = 200
  113. br.Success = true
  114. br.Msg = "获取成功"
  115. br.Data = resp
  116. fmt.Println("source redis")
  117. return
  118. }
  119. }
  120. }
  121. }
  122. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  123. if err != nil && err.Error() != utils.ErrNoRow() {
  124. br.Msg = "获取失败"
  125. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  126. return
  127. }
  128. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  129. if err != nil && err.Error() != utils.ErrNoRow() {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  132. return
  133. }
  134. allChartInfo, err := data_manage.GetChartInfoAll([]int{utils.CHART_SOURCE_DEFAULT})
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. br.Msg = "获取失败"
  137. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  138. return
  139. }
  140. chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
  141. for _, v := range allChartInfo {
  142. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  143. }
  144. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  145. for _, v := range classifyAll {
  146. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  147. if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
  148. v.Children = existItems
  149. } else {
  150. items := make([]*data_manage.ChartClassifyItems, 0)
  151. v.Children = items
  152. }
  153. }
  154. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  155. for _, v := range rootList {
  156. if existItems, ok := rootChildMap[v.ChartClassifyId]; ok {
  157. v.Children = existItems
  158. } else {
  159. items := make([]*data_manage.ChartClassifyItems, 0)
  160. v.Children = items
  161. }
  162. nodeAll = append(nodeAll, v)
  163. }
  164. //
  165. //
  166. //nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  167. //for k := range rootList {
  168. // rootNode := rootList[k]
  169. // data.ChartClassifyListMakeTree(classifyAll, rootNode)
  170. // nodeAll = append(nodeAll, rootNode)
  171. //}
  172. resp.AllNodes = nodeAll
  173. // 将数据加入缓存
  174. if utils.Re == nil {
  175. redisData, _ := json.Marshal(resp)
  176. utils.Rc.Put(key, redisData, 2*time.Hour)
  177. }
  178. // 移除没有权限的图表
  179. allNodes := data.HandleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  180. resp.AllNodes = allNodes
  181. br.Ret = 200
  182. br.Success = true
  183. br.Msg = "获取成功"
  184. br.Data = resp
  185. }
  186. // getChartClassifyListForMe 获取我创建的图表
  187. func getChartClassifyListForMe(adminInfo system.Admin, resp *data_manage.ChartClassifyListResp) (errMsg string, err error) {
  188. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  189. if err != nil && err.Error() != utils.ErrNoRow() {
  190. errMsg = "获取失败"
  191. return
  192. }
  193. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  194. if err != nil && err.Error() != utils.ErrNoRow() {
  195. errMsg = "获取失败"
  196. return
  197. }
  198. allChartInfo, err := data_manage.GetChartInfoByAdminId([]int{utils.CHART_SOURCE_DEFAULT}, adminInfo.AdminId)
  199. if err != nil && err.Error() != utils.ErrNoRow() {
  200. errMsg = "获取失败"
  201. return
  202. }
  203. chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
  204. for _, v := range allChartInfo {
  205. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  206. }
  207. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  208. for _, v := range classifyAll {
  209. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  210. if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
  211. v.Children = existItems
  212. } else {
  213. items := make([]*data_manage.ChartClassifyItems, 0)
  214. v.Children = items
  215. }
  216. }
  217. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  218. for _, v := range rootList {
  219. if existItems, ok := rootChildMap[v.ChartClassifyId]; ok {
  220. v.Children = existItems
  221. } else {
  222. items := make([]*data_manage.ChartClassifyItems, 0)
  223. v.Children = items
  224. }
  225. nodeAll = append(nodeAll, v)
  226. }
  227. resp.AllNodes = nodeAll
  228. return
  229. }
  230. // ChartClassifyItems
  231. // @Title 获取所有图表分类接口-不包含图表
  232. // @Description 获取所有图表分类接口-不包含图表
  233. // @Success 200 {object} data_manage.ChartClassifyListResp
  234. // @router /chart_classify/items [get]
  235. func (this *ChartClassifyController) ChartClassifyItems() {
  236. br := new(models.BaseResponse).Init()
  237. defer func() {
  238. this.Data["json"] = br
  239. this.ServeJSON()
  240. }()
  241. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  242. if err != nil {
  243. br.Msg = "获取失败"
  244. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  245. return
  246. }
  247. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  248. if err != nil {
  249. br.Msg = "获取失败"
  250. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  251. return
  252. }
  253. // 已授权分类id
  254. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, 0)
  255. if err != nil {
  256. br.Msg = "获取失败"
  257. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  258. return
  259. }
  260. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  261. for k := range rootList {
  262. rootNode := rootList[k]
  263. data.ChartClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode, permissionClassifyIdList)
  264. nodeAll = append(nodeAll, rootNode)
  265. }
  266. language := `CN`
  267. // 指标显示的语言
  268. {
  269. configDetail, _ := system.GetConfigDetailByCode(this.SysUser.AdminId, system.ChartLanguageVar)
  270. if configDetail != nil {
  271. language = configDetail.ConfigValue
  272. } else {
  273. configDetail, _ = system.GetDefaultConfigDetailByCode(system.ChartLanguageVar)
  274. if configDetail != nil {
  275. language = configDetail.ConfigValue
  276. }
  277. }
  278. }
  279. // 是否允许添加分类
  280. canOpClassify := true
  281. // 如果不是 超管 或者 ficc管理员 或者 ficc研究员,那么就没有权限
  282. //if !utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, this.SysUser.RoleTypeCode) {
  283. // canOpClassify = false
  284. //}
  285. resp := data_manage.ChartClassifyListResp{
  286. AllNodes: nodeAll,
  287. Language: language,
  288. CanOpClassify: canOpClassify,
  289. }
  290. br.Ret = 200
  291. br.Success = true
  292. br.Msg = "获取成功"
  293. br.Data = resp
  294. }
  295. // @Title 新增图表分类
  296. // @Description 新增图表分类接口
  297. // @Param request body data_manage.AddChartClassifyReq true "type json string"
  298. // @Success 200 Ret=200 保存成功
  299. // @router /chart_classify/add [post]
  300. func (this *ChartClassifyController) AddChartClassify() {
  301. br := new(models.BaseResponse).Init()
  302. defer func() {
  303. this.Data["json"] = br
  304. this.ServeJSON()
  305. }()
  306. var req data_manage.AddChartClassifyReq
  307. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  308. if err != nil {
  309. br.Msg = "参数解析异常!"
  310. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  311. return
  312. }
  313. if req.ChartClassifyName == "" {
  314. br.Msg = "请输入分类名称"
  315. br.IsSendEmail = false
  316. return
  317. }
  318. if req.ParentId < 0 {
  319. br.Msg = "参数错误"
  320. br.IsSendEmail = false
  321. return
  322. }
  323. count, err := data_manage.GetChartClassifyCount(req.ChartClassifyName, req.ParentId, utils.CHART_SOURCE_DEFAULT)
  324. if err != nil {
  325. br.Msg = "判断名称是否已存在失败"
  326. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  327. return
  328. }
  329. if count > 0 {
  330. br.Msg = "分类名称已存在,请重新输入"
  331. br.IsSendEmail = false
  332. return
  333. }
  334. //获取该层级下最大的排序数
  335. maxSort, err := data_manage.GetChartClassifyMaxSort(req.ParentId, utils.CHART_SOURCE_DEFAULT)
  336. classify := new(data_manage.ChartClassify)
  337. classify.ParentId = req.ParentId
  338. classify.ChartClassifyName = req.ChartClassifyName
  339. classify.HasData = 0
  340. classify.CreateTime = time.Now()
  341. classify.ModifyTime = time.Now()
  342. classify.SysUserId = this.SysUser.AdminId
  343. classify.SysUserRealName = this.SysUser.RealName
  344. classify.Level = req.Level + 1
  345. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  346. classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  347. classify.Sort = maxSort + 1
  348. classify.Source = utils.CHART_SOURCE_DEFAULT
  349. _, err = data_manage.AddChartClassify(classify)
  350. if err != nil {
  351. br.Msg = "保存分类失败"
  352. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  353. return
  354. }
  355. br.Ret = 200
  356. br.Msg = "保存成功"
  357. br.Success = true
  358. }
  359. // @Title 修改图表分类
  360. // @Description 修改图表分类接口
  361. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  362. // @Success 200 Ret=200 修改成功
  363. // @router /chart_classify/edit [post]
  364. func (this *ChartClassifyController) EditChartClassify() {
  365. br := new(models.BaseResponse).Init()
  366. defer func() {
  367. this.Data["json"] = br
  368. this.ServeJSON()
  369. }()
  370. var req data_manage.EditChartClassifyReq
  371. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  372. if err != nil {
  373. br.Msg = "参数解析异常!"
  374. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  375. return
  376. }
  377. if req.ChartClassifyName == "" {
  378. br.Msg = "请输入分类名称"
  379. br.IsSendEmail = false
  380. return
  381. }
  382. if req.ChartClassifyId <= 0 {
  383. br.Msg = "参数错误"
  384. br.IsSendEmail = false
  385. return
  386. }
  387. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  388. if err != nil {
  389. br.Msg = "保存失败"
  390. br.Msg = "获取分类信息失败,Err:" + err.Error()
  391. return
  392. }
  393. // 已授权分类id
  394. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, item.ChartClassifyId)
  395. if err != nil {
  396. br.Msg = "获取失败"
  397. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  398. return
  399. }
  400. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ChartClassifyId, permissionClassifyIdList)
  401. // 权限校验
  402. {
  403. button := data.GetChartClassifyOpButton(this.SysUser, item.SysUserId, haveOperaAuth)
  404. if !button.OpButton {
  405. br.Msg = "无操作权限"
  406. br.IsSendEmail = false
  407. return
  408. }
  409. }
  410. if item.ChartClassifyName != req.ChartClassifyName {
  411. count, err := data_manage.GetChartClassifyCount(req.ChartClassifyName, item.ParentId, utils.CHART_SOURCE_DEFAULT)
  412. if err != nil {
  413. br.Msg = "判断名称是否已存在失败"
  414. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  415. return
  416. }
  417. if count > 0 {
  418. br.Msg = "分类名称已存在,请重新输入"
  419. br.IsSendEmail = false
  420. return
  421. }
  422. err = data_manage.EditChartClassify(req.ChartClassifyId, utils.CHART_SOURCE_DEFAULT, req.ChartClassifyName)
  423. if err != nil {
  424. br.Msg = "保存失败"
  425. br.ErrMsg = "保存失败,Err:" + err.Error()
  426. return
  427. }
  428. }
  429. br.Ret = 200
  430. br.Msg = "保存成功"
  431. br.Success = true
  432. br.IsAddLog = true
  433. }
  434. // @Title 删除图表检测接口
  435. // @Description 删除图表检测接口
  436. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  437. // @Success 200 Ret=200 检测成功
  438. // @router /chart_classify/delete/check [post]
  439. func (this *ChartClassifyController) DeleteChartClassifyCheck() {
  440. br := new(models.BaseResponse).Init()
  441. defer func() {
  442. this.Data["json"] = br
  443. this.ServeJSON()
  444. }()
  445. var req data_manage.ChartClassifyDeleteCheckReq
  446. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  447. if err != nil {
  448. br.Msg = "参数解析异常!"
  449. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  450. return
  451. }
  452. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  453. br.Msg = "参数错误"
  454. br.IsSendEmail = false
  455. return
  456. }
  457. var deleteStatus int
  458. var tipsMsg string
  459. //删除分类
  460. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  461. // 查找分类
  462. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  463. if err != nil {
  464. br.Msg = "保存失败"
  465. br.Msg = "获取分类信息失败,Err:" + err.Error()
  466. return
  467. }
  468. // 已授权分类id
  469. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, item.ChartClassifyId)
  470. if err != nil {
  471. br.Msg = "获取失败"
  472. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  473. return
  474. }
  475. // 权限校验
  476. {
  477. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ChartClassifyId, permissionClassifyIdList)
  478. button := data.GetChartClassifyOpButton(this.SysUser, item.SysUserId, haveOperaAuth)
  479. if !button.OpButton {
  480. br.Msg = "无操作权限"
  481. br.IsSendEmail = false
  482. return
  483. }
  484. }
  485. //判断图表分类下,是否含有图表
  486. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  487. if err != nil {
  488. br.Msg = "删除失败"
  489. br.ErrMsg = "分类下是否含有指标失败,Err:" + err.Error()
  490. return
  491. }
  492. if count > 0 {
  493. deleteStatus = 1
  494. tipsMsg = "该分类下关联图表不可删除"
  495. }
  496. }
  497. if deleteStatus != 1 && req.ChartInfoId == 0 {
  498. classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId)
  499. if err != nil && err.Error() != utils.ErrNoRow() {
  500. br.Msg = "删除失败"
  501. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  502. return
  503. }
  504. if classifyCount > 0 {
  505. deleteStatus = 2
  506. tipsMsg = "确认删除当前目录及包含的子目录吗"
  507. }
  508. }
  509. if deleteStatus == 0 {
  510. tipsMsg = "可删除,进行删除操作"
  511. }
  512. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  513. resp.DeleteStatus = deleteStatus
  514. resp.TipsMsg = tipsMsg
  515. br.Ret = 200
  516. br.Msg = "检测成功"
  517. br.Success = true
  518. br.Data = resp
  519. }
  520. // @Title 删除图表分类/图表
  521. // @Description 删除图表分类/图表接口
  522. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  523. // @Success 200 Ret=200 删除成功
  524. // @router /chart_classify/delete [post]
  525. func (this *ChartClassifyController) DeleteChartClassify() {
  526. br := new(models.BaseResponse).Init()
  527. defer func() {
  528. this.Data["json"] = br
  529. this.ServeJSON()
  530. }()
  531. sysUser := this.SysUser
  532. if sysUser == nil {
  533. br.Msg = "请登录"
  534. br.ErrMsg = "请登录,SysUser Is Empty"
  535. br.Ret = 408
  536. return
  537. }
  538. var req data_manage.DeleteChartClassifyReq
  539. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  540. if err != nil {
  541. br.Msg = "参数解析异常!"
  542. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  543. return
  544. }
  545. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  546. br.Msg = "参数错误"
  547. br.IsSendEmail = false
  548. return
  549. }
  550. //删除分类
  551. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  552. // 查找分类
  553. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  554. if err != nil {
  555. br.Msg = "保存失败"
  556. br.Msg = "获取分类信息失败,Err:" + err.Error()
  557. return
  558. }
  559. // 已授权分类id
  560. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, item.ChartClassifyId)
  561. if err != nil {
  562. br.Msg = "获取失败"
  563. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  564. return
  565. }
  566. // 权限校验
  567. {
  568. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ChartClassifyId, permissionClassifyIdList)
  569. button := data.GetChartClassifyOpButton(this.SysUser, item.SysUserId, haveOperaAuth)
  570. if !button.OpButton {
  571. br.Msg = "无操作权限"
  572. br.IsSendEmail = false
  573. return
  574. }
  575. }
  576. //判断是否含有指标
  577. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  578. if err != nil && err.Error() != utils.ErrNoRow() {
  579. br.Msg = "删除失败"
  580. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  581. return
  582. }
  583. if count > 0 {
  584. br.Msg = "该目录下存在关联指标,不可删除"
  585. br.IsSendEmail = false
  586. return
  587. }
  588. err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  589. if err != nil {
  590. br.Msg = "删除失败"
  591. br.ErrMsg = "删除失败,Err:" + err.Error()
  592. return
  593. }
  594. }
  595. resp := new(data_manage.AddChartInfoResp)
  596. //删除图表
  597. if req.ChartInfoId > 0 {
  598. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  599. if err != nil {
  600. if err.Error() == utils.ErrNoRow() {
  601. br.Msg = "图表已删除,请刷新页面"
  602. br.ErrMsg = "指标不存在,Err:" + err.Error()
  603. return
  604. } else {
  605. br.Msg = "删除失败"
  606. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  607. return
  608. }
  609. }
  610. if chartInfo == nil {
  611. br.Msg = "图表已删除,请刷新页面"
  612. return
  613. }
  614. //图表操作权限
  615. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
  616. if !ok {
  617. br.Msg = "没有该图表的操作权限"
  618. br.ErrMsg = "没有该图表的操作权限"
  619. return
  620. }
  621. // 获取引用该图表的MyCharts, 用于ES删除
  622. var myCond string
  623. var myPars []interface{}
  624. myCond += ` AND a.chart_info_id = ? `
  625. myPars = append(myPars, req.ChartInfoId)
  626. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  627. if e != nil {
  628. br.Msg = "删除失败"
  629. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  630. return
  631. }
  632. myIds := make([]int, 0)
  633. for _, m := range myCharts {
  634. myIds = append(myIds, m.MyChartId)
  635. }
  636. //删除图表及关联指标
  637. err = data_manage.DeleteChartInfoAndData(req.ChartInfoId)
  638. if err != nil {
  639. br.Msg = "删除失败"
  640. br.ErrMsg = "删除失败,Err:" + err.Error()
  641. return
  642. }
  643. //删除ES
  644. {
  645. go data.EsDeleteChartInfo(req.ChartInfoId)
  646. // 删除MY ETA 图表 es数据
  647. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  648. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  649. }
  650. var condition string
  651. var pars []interface{}
  652. condition += " AND chart_classify_id=? AND source = ? "
  653. pars = append(pars, chartInfo.ChartClassifyId, utils.CHART_SOURCE_DEFAULT)
  654. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  655. pars = append(pars, req.ChartInfoId)
  656. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  657. if err != nil && err.Error() != utils.ErrNoRow() {
  658. br.Msg = "删除失败"
  659. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  660. return
  661. }
  662. if nextItem != nil {
  663. resp.UniqueCode = nextItem.UniqueCode
  664. resp.ChartInfoId = nextItem.ChartInfoId
  665. } else {
  666. var condition string
  667. var pars []interface{}
  668. condition += " AND level=3 "
  669. //pars = append(pars, chartInfo.ChartClassifyId)
  670. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  671. pars = append(pars, chartInfo.ChartClassifyId)
  672. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  673. if err != nil && err.Error() != utils.ErrNoRow() {
  674. br.Msg = "删除失败"
  675. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  676. return
  677. }
  678. if classifyItem != nil {
  679. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  680. if err != nil && err.Error() != utils.ErrNoRow() {
  681. br.Msg = "删除失败"
  682. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  683. return
  684. }
  685. if nextItem != nil {
  686. resp.UniqueCode = nextItem.UniqueCode
  687. resp.ChartInfoId = nextItem.ChartInfoId
  688. }
  689. }
  690. }
  691. //新增操作日志
  692. {
  693. chartLog := new(data_manage.ChartInfoLog)
  694. chartLog.ChartName = chartInfo.ChartName
  695. chartLog.ChartInfoId = req.ChartInfoId
  696. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  697. chartLog.SysUserId = sysUser.AdminId
  698. chartLog.SysUserRealName = sysUser.RealName
  699. chartLog.UniqueCode = chartInfo.UniqueCode
  700. chartLog.CreateTime = time.Now()
  701. chartLog.Content = string(this.Ctx.Input.RequestBody)
  702. chartLog.Status = "删除图表"
  703. chartLog.Method = this.Ctx.Input.URI()
  704. go data_manage.AddChartInfoLog(chartLog)
  705. }
  706. }
  707. br.Ret = 200
  708. br.Msg = "删除成功"
  709. br.Success = true
  710. br.Data = resp
  711. br.IsAddLog = true
  712. }
  713. // ChartClassifyMove
  714. // @Title 图表分类移动接口
  715. // @Description 图表分类移动接口
  716. // @Success 200 {object} data_manage.MoveChartClassifyReq
  717. // @router /chart_classify/move [post]
  718. func (this *ChartClassifyController) ChartClassifyMove() {
  719. br := new(models.BaseResponse).Init()
  720. defer func() {
  721. if br.ErrMsg == "" {
  722. br.IsSendEmail = false
  723. }
  724. this.Data["json"] = br
  725. this.ServeJSON()
  726. }()
  727. sysUser := this.SysUser
  728. if sysUser == nil {
  729. br.Msg = "请登录"
  730. br.ErrMsg = "请登录,SysUser Is Empty"
  731. br.Ret = 408
  732. return
  733. }
  734. var req data_manage.MoveChartClassifyReq
  735. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  736. if err != nil {
  737. br.Msg = "参数解析异常!"
  738. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  739. return
  740. }
  741. if req.ClassifyId <= 0 {
  742. br.Msg = "参数错误"
  743. br.ErrMsg = "分类id小于等于0"
  744. return
  745. }
  746. //判断分类是否存在
  747. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  748. if err != nil {
  749. if err.Error() == utils.ErrNoRow() {
  750. br.Msg = "分类不存在,请刷新页面"
  751. return
  752. }
  753. br.Msg = "移动失败"
  754. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  755. return
  756. }
  757. // 校验移动的父级目录下是否有重名分类
  758. exists, e := data_manage.GetChartClassifyByParentIdAndName(req.ParentClassifyId, chartClassifyInfo.ChartClassifyName, req.ClassifyId)
  759. if e != nil && e.Error() != utils.ErrNoRow() {
  760. br.Msg = "移动失败"
  761. br.ErrMsg = "获取父级目录下的同名分类失败, Err: " + e.Error()
  762. return
  763. }
  764. if exists != nil {
  765. br.Msg = "移动失败,分类名称已存在"
  766. return
  767. }
  768. // 已授权分类id
  769. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, chartClassifyInfo.ChartClassifyId)
  770. if err != nil {
  771. br.Msg = "获取失败"
  772. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  773. return
  774. }
  775. // 权限校验
  776. {
  777. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
  778. button := data.GetChartClassifyOpButton(this.SysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
  779. if !button.OpButton {
  780. br.Msg = "无操作权限"
  781. br.IsSendEmail = false
  782. return
  783. }
  784. }
  785. if chartClassifyInfo.Source != utils.CHART_SOURCE_DEFAULT {
  786. br.Msg = "分类异常"
  787. br.ErrMsg = "分类异常,不是ETA图库的分类"
  788. return
  789. }
  790. updateCol := make([]string, 0)
  791. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  792. if chartClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  793. parentChartClassifyInfo, err := data_manage.GetChartClassifyById(req.ParentClassifyId)
  794. if err != nil {
  795. br.Msg = "移动失败"
  796. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  797. return
  798. }
  799. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  800. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  801. chartClassifyInfo.ModifyTime = time.Now()
  802. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  803. }
  804. //如果有传入 上一个兄弟节点分类id
  805. if req.PrevClassifyId > 0 {
  806. //上一个兄弟节点
  807. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  808. if err != nil {
  809. br.Msg = "移动失败"
  810. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  811. return
  812. }
  813. //如果是移动在两个兄弟节点之间
  814. if req.NextClassifyId > 0 {
  815. //下一个兄弟节点
  816. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  817. if err != nil {
  818. br.Msg = "移动失败"
  819. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  820. return
  821. }
  822. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  823. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  824. //变更兄弟节点的排序
  825. updateSortStr := `sort + 2`
  826. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  827. } else {
  828. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  829. if nextClassify.Sort-prevClassify.Sort == 1 {
  830. //变更兄弟节点的排序
  831. updateSortStr := `sort + 1`
  832. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  833. }
  834. }
  835. }
  836. chartClassifyInfo.Sort = prevClassify.Sort + 1
  837. chartClassifyInfo.ModifyTime = time.Now()
  838. updateCol = append(updateCol, "Sort", "ModifyTime")
  839. } else {
  840. firstClassify, err := data_manage.GetFirstChartClassifyByParentId(chartClassifyInfo.ParentId)
  841. if err != nil && err.Error() != utils.ErrNoRow() {
  842. br.Msg = "移动失败"
  843. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  844. return
  845. }
  846. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  847. if firstClassify != nil && firstClassify.Sort == 0 {
  848. updateSortStr := ` sort + 1 `
  849. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  850. }
  851. chartClassifyInfo.Sort = 0 //那就是排在第一位
  852. chartClassifyInfo.ModifyTime = time.Now()
  853. updateCol = append(updateCol, "Sort", "ModifyTime")
  854. }
  855. //更新
  856. if len(updateCol) > 0 {
  857. err = chartClassifyInfo.Update(updateCol)
  858. if err != nil {
  859. br.Msg = "移动失败"
  860. br.ErrMsg = "修改失败,Err:" + err.Error()
  861. return
  862. }
  863. }
  864. br.Ret = 200
  865. br.Success = true
  866. br.Msg = "移动成功"
  867. }
  868. // getChartClassifyListForMe 获取我创建的图表分类
  869. func getChartClassifyListForMeV2(adminInfo system.Admin, resp *data_manage.ChartClassifyListResp) (errMsg string, err error) {
  870. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  871. if err != nil && err.Error() != utils.ErrNoRow() {
  872. errMsg = "获取失败"
  873. return
  874. }
  875. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  876. if err != nil && err.Error() != utils.ErrNoRow() {
  877. errMsg = "获取失败"
  878. return
  879. }
  880. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  881. for _, v := range classifyAll {
  882. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  883. }
  884. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  885. for _, v := range rootList {
  886. if existItems, ok := rootChildMap[v.ChartClassifyId]; ok {
  887. v.Children = existItems
  888. } else {
  889. items := make([]*data_manage.ChartClassifyItems, 0)
  890. v.Children = items
  891. }
  892. nodeAll = append(nodeAll, v)
  893. }
  894. resp.AllNodes = nodeAll
  895. return
  896. }
  897. // ChartClassifyChartListV2
  898. // @Title 根据图表分类获取图表列表
  899. // @Description 根据图表分类获取图表列表接口
  900. // @Param IsShowMe query bool true "是否只看我的,true、false"
  901. // @Param ChartClassifyId query bool true "图片分类id"
  902. // @Success 200 {object} data_manage.ChartClassifyListResp
  903. // @router /chart_classify/chart/list [get]
  904. func (this *ChartClassifyController) ChartClassifyChartListV2() {
  905. br := new(models.BaseResponse).Init()
  906. defer func() {
  907. this.Data["json"] = br
  908. this.ServeJSON()
  909. }()
  910. sysUser := this.SysUser
  911. if sysUser == nil {
  912. br.Msg = "请登录"
  913. br.ErrMsg = "请登录,SysUser Is Empty"
  914. br.Ret = 408
  915. return
  916. }
  917. resp := new(data_manage.ChartClassifyListResp)
  918. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  919. if chartClassifyId <= 0 {
  920. br.Msg = "参数错误"
  921. return
  922. }
  923. // 获取当前账号的不可见指标
  924. noPermissionChartIdMap := make(map[int]bool)
  925. {
  926. obj := data_manage.EdbInfoNoPermissionAdmin{}
  927. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  928. if err != nil && err.Error() != utils.ErrNoRow() {
  929. br.Msg = "获取失败"
  930. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  931. return
  932. }
  933. for _, v := range confList {
  934. noPermissionChartIdMap[v.ChartInfoId] = true
  935. }
  936. }
  937. isShowMe, _ := this.GetBool("IsShowMe")
  938. if isShowMe {
  939. allChartInfo, err := data_manage.GetChartInfoByAdminIdAndClassify([]int{utils.CHART_SOURCE_DEFAULT}, sysUser.AdminId, chartClassifyId)
  940. if err != nil && err.Error() != utils.ErrNoRow() {
  941. br.Msg = "获取失败"
  942. br.ErrMsg = "获取全部数据失败,Err:" + err.Error()
  943. return
  944. }
  945. // 移除没有权限的图表
  946. allNodes := data.HandleNoPermissionChart(allChartInfo, noPermissionChartIdMap)
  947. resp.AllNodes = allNodes
  948. br.Ret = 200
  949. br.Success = true
  950. br.Msg = "获取成功"
  951. br.Data = resp
  952. fmt.Println("source my classify")
  953. return
  954. }
  955. allChartInfo, err := data_manage.GetChartInfoAllByClassifyId(utils.CHART_SOURCE_DEFAULT, chartClassifyId)
  956. if err != nil && err.Error() != utils.ErrNoRow() {
  957. br.Msg = "获取失败"
  958. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  959. return
  960. }
  961. // 移除没有权限的图表
  962. allNodes := data.HandleNoPermissionChart(allChartInfo, noPermissionChartIdMap)
  963. for k, item := range allNodes {
  964. item.Button = data.GetChartOpButton(this.SysUser, item.SysUserId)
  965. item.Button.AddButton = false
  966. item.Button.OpButton = false
  967. item.Button.DeleteButton = false
  968. allNodes[k] = item
  969. }
  970. resp.AllNodes = allNodes
  971. br.Ret = 200
  972. br.Success = true
  973. br.Msg = "获取成功"
  974. br.Data = resp
  975. }