chart_classify.go 31 KB

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