chart_classify.go 31 KB

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