chart_classify.go 31 KB

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