chart_classify.go 31 KB

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