chart_classify.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. }
  415. if deleteStatus == 0 {
  416. tipsMsg = "可删除,进行删除操作"
  417. }
  418. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  419. resp.DeleteStatus = deleteStatus
  420. resp.TipsMsg = tipsMsg
  421. br.Ret = 200
  422. br.Msg = "检测成功"
  423. br.Success = true
  424. br.Data = resp
  425. }
  426. // @Title 删除图表分类/图表
  427. // @Description 删除图表分类/图表接口
  428. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  429. // @Success 200 Ret=200 删除成功
  430. // @router /chart_classify/delete [post]
  431. func (this *ChartClassifyController) DeleteChartClassify() {
  432. br := new(models.BaseResponse).Init()
  433. defer func() {
  434. this.Data["json"] = br
  435. this.ServeJSON()
  436. }()
  437. sysUser := this.SysUser
  438. if sysUser == nil {
  439. br.Msg = "请登录"
  440. br.ErrMsg = "请登录,SysUser Is Empty"
  441. br.Ret = 408
  442. return
  443. }
  444. var req data_manage.DeleteChartClassifyReq
  445. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  446. if err != nil {
  447. br.Msg = "参数解析异常!"
  448. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  449. return
  450. }
  451. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  452. br.Msg = "参数错误"
  453. br.IsSendEmail = false
  454. return
  455. }
  456. //删除分类
  457. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  458. // 查找分类
  459. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  460. if err != nil {
  461. br.Msg = "保存失败"
  462. br.Msg = "获取分类信息失败,Err:" + err.Error()
  463. return
  464. }
  465. // 已授权分类id
  466. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, item.ChartClassifyId)
  467. if err != nil {
  468. br.Msg = "获取失败"
  469. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  470. return
  471. }
  472. // 权限校验
  473. {
  474. haveOperaAuth := data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ChartClassifyId, permissionClassifyIdList)
  475. button := data.GetChartClassifyOpButton(this.SysUser, item.SysUserId, haveOperaAuth)
  476. if !button.OpButton {
  477. br.Msg = "无操作权限"
  478. br.IsSendEmail = false
  479. return
  480. }
  481. }
  482. //判断是否含有指标
  483. childIds := make([]int, 0)
  484. classifyAll, err := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_DEFAULT)
  485. if err != nil {
  486. br.Msg = "删除失败"
  487. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  488. return
  489. }
  490. classifyAllMap := make(map[int]*data_manage.ChartClassifyItems)
  491. for _, v := range classifyAll {
  492. classifyAllMap[v.ChartClassifyId] = v
  493. }
  494. data.GetChartClassifyChildIds(req.ChartClassifyId, classifyAllMap, &childIds)
  495. condition := ``
  496. pars := make([]interface{}, 0)
  497. if len(childIds) > 0 {
  498. ids := utils.IntArr2joinString(childIds, ",")
  499. condition = ` AND chart_classify_id IN (` + ids + `) `
  500. } else {
  501. condition = ` AND chart_classify_id = ? `
  502. pars = append(pars, req.ChartClassifyId)
  503. }
  504. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  505. if err != nil {
  506. br.Msg = "判断图表名称是否存在失败"
  507. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  508. return
  509. }
  510. if count > 0 {
  511. br.Msg = "该目录下存在关联指标,不可删除"
  512. br.IsSendEmail = false
  513. return
  514. }
  515. delIds := append(childIds, req.ChartClassifyId)
  516. err = data_manage.DeleteChartClassifyByIds(delIds)
  517. //err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  518. if err != nil {
  519. br.Msg = "删除失败"
  520. br.ErrMsg = "删除失败,Err:" + err.Error()
  521. return
  522. }
  523. }
  524. resp := new(data_manage.AddChartInfoResp)
  525. //删除图表
  526. if req.ChartInfoId > 0 {
  527. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  528. if err != nil {
  529. if utils.IsErrNoRow(err) {
  530. br.Msg = "图表已删除,请刷新页面"
  531. br.ErrMsg = "指标不存在,Err:" + err.Error()
  532. return
  533. } else {
  534. br.Msg = "删除失败"
  535. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  536. return
  537. }
  538. }
  539. if chartInfo == nil {
  540. br.Msg = "图表已删除,请刷新页面"
  541. return
  542. }
  543. chartClassifyItem, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  544. if err != nil {
  545. br.Msg = "获取失败"
  546. br.ErrMsg = "获取图表分类详情数据失败,Err:" + err.Error()
  547. return
  548. }
  549. // 已授权图表、分类id
  550. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  551. if err != nil {
  552. br.Msg = "获取失败"
  553. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  554. return
  555. }
  556. // 权限校验
  557. {
  558. haveOperaAuth := data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassifyItem.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  559. //图表操作权限
  560. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, haveOperaAuth)
  561. if !ok {
  562. br.Msg = "没有该图表的操作权限"
  563. br.ErrMsg = "没有该图表的操作权限"
  564. return
  565. }
  566. }
  567. // 获取引用该图表的MyCharts, 用于ES删除
  568. var myCond string
  569. var myPars []interface{}
  570. myCond += ` AND a.chart_info_id = ? `
  571. myPars = append(myPars, req.ChartInfoId)
  572. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  573. if e != nil {
  574. br.Msg = "删除失败"
  575. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  576. return
  577. }
  578. myIds := make([]int, 0)
  579. for _, m := range myCharts {
  580. myIds = append(myIds, m.MyChartId)
  581. }
  582. //删除图表及关联指标
  583. err = data_manage.DeleteChartInfoAndData(req.ChartInfoId)
  584. if err != nil {
  585. br.Msg = "删除失败"
  586. br.ErrMsg = "删除失败,Err:" + err.Error()
  587. return
  588. }
  589. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  590. err = data_manage.DeleteChartSeriesAndEdbMapping(req.ChartInfoId)
  591. if err != nil {
  592. br.Msg = "删除失败"
  593. br.ErrMsg = "删除失败,Err:" + err.Error()
  594. return
  595. }
  596. }
  597. //删除ES
  598. {
  599. go data.DeleteChartInfoToEs(req.ChartInfoId)
  600. // 删除MY ETA 图表 es数据
  601. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  602. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  603. }
  604. var condition string
  605. var pars []interface{}
  606. condition += " AND chart_classify_id=? AND source = ? "
  607. pars = append(pars, chartInfo.ChartClassifyId, utils.CHART_SOURCE_DEFAULT)
  608. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  609. pars = append(pars, req.ChartInfoId)
  610. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  611. if err != nil && !utils.IsErrNoRow(err) {
  612. br.Msg = "删除失败"
  613. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  614. return
  615. }
  616. if nextItem != nil {
  617. resp.UniqueCode = nextItem.UniqueCode
  618. resp.ChartInfoId = nextItem.ChartInfoId
  619. } else {
  620. var condition string
  621. var pars []interface{}
  622. condition += " AND level=3 "
  623. //pars = append(pars, chartInfo.ChartClassifyId)
  624. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  625. pars = append(pars, chartInfo.ChartClassifyId)
  626. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  627. if err != nil && !utils.IsErrNoRow(err) {
  628. br.Msg = "删除失败"
  629. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  630. return
  631. }
  632. if classifyItem != nil {
  633. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  634. if err != nil && !utils.IsErrNoRow(err) {
  635. br.Msg = "删除失败"
  636. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  637. return
  638. }
  639. if nextItem != nil {
  640. resp.UniqueCode = nextItem.UniqueCode
  641. resp.ChartInfoId = nextItem.ChartInfoId
  642. }
  643. }
  644. }
  645. // 删除图表中的指标引用
  646. _ = data_manage.DeleteEdbRelationByObjectId(req.ChartInfoId, utils.EDB_RELATION_CHART)
  647. //新增操作日志
  648. {
  649. chartLog := new(data_manage.ChartInfoLog)
  650. chartLog.ChartName = chartInfo.ChartName
  651. chartLog.ChartInfoId = req.ChartInfoId
  652. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  653. chartLog.SysUserId = sysUser.AdminId
  654. chartLog.SysUserRealName = sysUser.RealName
  655. chartLog.UniqueCode = chartInfo.UniqueCode
  656. chartLog.CreateTime = time.Now()
  657. chartLog.Content = string(this.Ctx.Input.RequestBody)
  658. chartLog.Status = "删除图表"
  659. chartLog.Method = this.Ctx.Input.URI()
  660. go data_manage.AddChartInfoLog(chartLog)
  661. }
  662. }
  663. br.Ret = 200
  664. br.Msg = "删除成功"
  665. br.Success = true
  666. br.Data = resp
  667. br.IsAddLog = true
  668. }
  669. // ChartClassifyMove
  670. // @Title 图表分类移动接口
  671. // @Description 图表分类移动接口
  672. // @Success 200 {object} data_manage.MoveChartClassifyReq
  673. // @router /chart_classify/move [post]
  674. func (this *ChartClassifyController) ChartClassifyMove() {
  675. br := new(models.BaseResponse).Init()
  676. defer func() {
  677. if br.ErrMsg == "" {
  678. br.IsSendEmail = false
  679. }
  680. this.Data["json"] = br
  681. this.ServeJSON()
  682. }()
  683. sysUser := this.SysUser
  684. if sysUser == nil {
  685. br.Msg = "请登录"
  686. br.ErrMsg = "请登录,SysUser Is Empty"
  687. br.Ret = 408
  688. return
  689. }
  690. var req data_manage.MoveChartClassifyReq
  691. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  692. if err != nil {
  693. br.Msg = "参数解析异常!"
  694. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  695. return
  696. }
  697. if req.ClassifyId <= 0 {
  698. br.Msg = "参数错误"
  699. br.ErrMsg = "分类id小于等于0"
  700. return
  701. }
  702. //判断分类是否存在
  703. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  704. if err != nil {
  705. if utils.IsErrNoRow(err) {
  706. br.Msg = "分类不存在,请刷新页面"
  707. return
  708. }
  709. br.Msg = "移动失败"
  710. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  711. return
  712. }
  713. // 校验移动的父级目录下是否有重名分类
  714. exists, e := data_manage.GetChartClassifyByParentIdAndName(utils.CHART_SOURCE_DEFAULT, req.ParentClassifyId, chartClassifyInfo.ChartClassifyName, req.ClassifyId)
  715. if e != nil && !utils.IsErrNoRow(e) {
  716. br.Msg = "移动失败"
  717. br.ErrMsg = "获取父级目录下的同名分类失败, Err: " + e.Error()
  718. return
  719. }
  720. if e == nil && exists.ChartClassifyId > 0 {
  721. br.Msg = "移动失败,分类名称已存在"
  722. return
  723. }
  724. // 已授权分类id
  725. permissionClassifyIdList, err := data_manage_permission.GetUserChartClassifyPermissionList(this.SysUser.AdminId, chartClassifyInfo.ChartClassifyId)
  726. if err != nil {
  727. br.Msg = "获取失败"
  728. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  729. return
  730. }
  731. // 权限校验
  732. {
  733. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
  734. button := data.GetChartClassifyOpButton(this.SysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
  735. if !button.OpButton {
  736. br.Msg = "无操作权限"
  737. br.IsSendEmail = false
  738. return
  739. }
  740. }
  741. if chartClassifyInfo.Source != utils.CHART_SOURCE_DEFAULT {
  742. br.Msg = "分类异常"
  743. br.ErrMsg = "分类异常,不是ETA图库的分类"
  744. return
  745. }
  746. updateCol := make([]string, 0)
  747. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  748. if chartClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  749. parentChartClassifyInfo, err := data_manage.GetChartClassifyById(req.ParentClassifyId)
  750. if err != nil {
  751. br.Msg = "移动失败"
  752. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  753. return
  754. }
  755. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  756. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  757. chartClassifyInfo.ModifyTime = time.Now()
  758. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  759. }
  760. //如果有传入 上一个兄弟节点分类id
  761. if req.PrevClassifyId > 0 {
  762. //上一个兄弟节点
  763. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  764. if err != nil {
  765. br.Msg = "移动失败"
  766. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  767. return
  768. }
  769. //如果是移动在两个兄弟节点之间
  770. if req.NextClassifyId > 0 {
  771. //下一个兄弟节点
  772. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  773. if err != nil {
  774. br.Msg = "移动失败"
  775. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  776. return
  777. }
  778. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  779. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  780. //变更兄弟节点的排序
  781. updateSortStr := `sort + 2`
  782. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  783. } else {
  784. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  785. if nextClassify.Sort-prevClassify.Sort == 1 {
  786. //变更兄弟节点的排序
  787. updateSortStr := `sort + 1`
  788. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  789. }
  790. }
  791. }
  792. chartClassifyInfo.Sort = prevClassify.Sort + 1
  793. chartClassifyInfo.ModifyTime = time.Now()
  794. updateCol = append(updateCol, "Sort", "ModifyTime")
  795. } else {
  796. firstClassify, err := data_manage.GetFirstChartClassifyByParentId(chartClassifyInfo.ParentId)
  797. if err != nil && !utils.IsErrNoRow(err) {
  798. br.Msg = "移动失败"
  799. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  800. return
  801. }
  802. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  803. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  804. updateSortStr := ` sort + 1 `
  805. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  806. }
  807. chartClassifyInfo.Sort = 0 //那就是排在第一位
  808. chartClassifyInfo.ModifyTime = time.Now()
  809. updateCol = append(updateCol, "Sort", "ModifyTime")
  810. }
  811. //更新
  812. if len(updateCol) > 0 {
  813. err = chartClassifyInfo.Update(updateCol)
  814. if err != nil {
  815. br.Msg = "移动失败"
  816. br.ErrMsg = "修改失败,Err:" + err.Error()
  817. return
  818. }
  819. }
  820. br.Ret = 200
  821. br.Success = true
  822. br.Msg = "移动成功"
  823. }
  824. // ChartClassifyChartListV2
  825. // @Title 根据图表分类获取图表列表
  826. // @Description 根据图表分类获取图表列表接口
  827. // @Param ChartClassifyId query bool true "图片分类id"
  828. // @Success 200 {object} data_manage.ChartClassifyListResp
  829. // @router /chart_classify/chart/list [get]
  830. func (this *ChartClassifyController) ChartClassifyChartListV2() {
  831. br := new(models.BaseResponse).Init()
  832. defer func() {
  833. this.Data["json"] = br
  834. this.ServeJSON()
  835. }()
  836. sysUser := this.SysUser
  837. if sysUser == nil {
  838. br.Msg = "请登录"
  839. br.ErrMsg = "请登录,SysUser Is Empty"
  840. br.Ret = 408
  841. return
  842. }
  843. resp := new(data_manage.ChartClassifyListResp)
  844. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  845. if chartClassifyId <= 0 {
  846. br.Msg = "参数错误"
  847. return
  848. }
  849. // 获取当前账号的不可见指标
  850. noPermissionChartIdMap := make(map[int]bool)
  851. {
  852. obj := data_manage.EdbInfoNoPermissionAdmin{}
  853. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  854. if err != nil && !utils.IsErrNoRow(err) {
  855. br.Msg = "获取失败"
  856. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  857. return
  858. }
  859. for _, v := range confList {
  860. noPermissionChartIdMap[v.ChartInfoId] = true
  861. }
  862. }
  863. allChartInfo, err := data_manage.GetChartInfoAllByClassifyId(utils.CHART_SOURCE_DEFAULT, chartClassifyId)
  864. if err != nil && !utils.IsErrNoRow(err) {
  865. br.Msg = "获取失败"
  866. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  867. return
  868. }
  869. // 移除没有权限的图表
  870. allNodes := data.HandleNoPermissionChart(allChartInfo, noPermissionChartIdMap, this.SysUser.AdminId)
  871. editShareChartInfoIdMap, err := data.GetAllEditSharedChartInfoIdMapByReceivedUserId(this.SysUser.AdminId)
  872. if err != nil {
  873. br.Msg = "获取失败"
  874. br.ErrMsg = "获取所有有编辑权限的分享图表失败,Err:" + err.Error()
  875. return
  876. }
  877. for k, item := range allNodes {
  878. item.Button = data.GetChartOpButton(this.SysUser, item.SysUserId, item.ChartInfoId, item.HaveOperaAuth, editShareChartInfoIdMap)
  879. item.Button.AddButton = false
  880. item.Button.OpButton = false
  881. item.Button.DeleteButton = false
  882. allNodes[k] = item
  883. }
  884. resp.AllNodes = allNodes
  885. br.Ret = 200
  886. br.Success = true
  887. br.Msg = "获取成功"
  888. br.Data = resp
  889. }