chart_classify.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/models/system"
  9. "hongze/hz_crm_api/services/data"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  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 := handleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  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 data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  106. err := json.Unmarshal(data, &resp)
  107. if err == nil && resp != nil {
  108. // 移除没有权限的图表
  109. allNodes := handleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  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. data, _ := json.Marshal(resp)
  175. utils.Rc.Put(key, data, 2*time.Hour)
  176. }
  177. // 移除没有权限的图表
  178. allNodes := handleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  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. // handleNoPermissionChart 图表列表返回,将没有权限的图表移除
  230. func handleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool) (newAllNodes []*data_manage.ChartClassifyItems) {
  231. // 移除没有权限的图表
  232. newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
  233. for _, node := range allNodes {
  234. // 二级分类
  235. tmpNodeInfo := *node
  236. tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
  237. if node.Children != nil {
  238. for _, chartList := range node.Children {
  239. tmpInfo := *chartList
  240. tmpList := make([]*data_manage.ChartClassifyItems, 0)
  241. if chartList.Children != nil {
  242. for _, chartInfo := range chartList.Children {
  243. // 如果指标不可见,那么就不返回该指标
  244. if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
  245. continue
  246. }
  247. tmpList = append(tmpList, chartInfo)
  248. }
  249. }
  250. tmpInfo.Children = tmpList
  251. tmpNodeList = append(tmpNodeList, &tmpInfo)
  252. }
  253. }
  254. tmpNodeInfo.Children = tmpNodeList
  255. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  256. }
  257. return
  258. }
  259. // @Title 获取所有图表分类接口-不包含图表
  260. // @Description 获取所有图表分类接口-不包含图表
  261. // @Success 200 {object} data_manage.ChartClassifyListResp
  262. // @router /chart_classify/items [get]
  263. func (this *ChartClassifyController) ChartClassifyItems() {
  264. br := new(models.BaseResponse).Init()
  265. defer func() {
  266. this.Data["json"] = br
  267. this.ServeJSON()
  268. }()
  269. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  270. if err != nil {
  271. br.Msg = "获取失败"
  272. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  273. return
  274. }
  275. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  276. if err != nil {
  277. br.Msg = "获取失败"
  278. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  279. return
  280. }
  281. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  282. for k := range rootList {
  283. rootNode := rootList[k]
  284. data.ChartClassifyItemsMakeTree(classifyAll, rootNode)
  285. nodeAll = append(nodeAll, rootNode)
  286. }
  287. language := `CN`
  288. // 指标显示的语言
  289. {
  290. configDetail, _ := system.GetConfigDetailByCode(this.SysUser.AdminId, system.ChartLanguageVar)
  291. if configDetail != nil {
  292. language = configDetail.ConfigValue
  293. } else {
  294. configDetail, _ = system.GetDefaultConfigDetailByCode(system.ChartLanguageVar)
  295. if configDetail != nil {
  296. language = configDetail.ConfigValue
  297. }
  298. }
  299. }
  300. resp := new(data_manage.ChartClassifyListResp)
  301. resp.AllNodes = nodeAll
  302. resp.Language = language
  303. br.Ret = 200
  304. br.Success = true
  305. br.Msg = "获取成功"
  306. br.Data = resp
  307. }
  308. // @Title 新增图表分类
  309. // @Description 新增图表分类接口
  310. // @Param request body data_manage.AddChartClassifyReq true "type json string"
  311. // @Success 200 Ret=200 保存成功
  312. // @router /chart_classify/add [post]
  313. func (this *ChartClassifyController) AddChartClassify() {
  314. br := new(models.BaseResponse).Init()
  315. defer func() {
  316. this.Data["json"] = br
  317. this.ServeJSON()
  318. }()
  319. var req data_manage.AddChartClassifyReq
  320. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  321. if err != nil {
  322. br.Msg = "参数解析异常!"
  323. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  324. return
  325. }
  326. if req.ChartClassifyName == "" {
  327. br.Msg = "请输入分类名称"
  328. br.IsSendEmail = false
  329. return
  330. }
  331. if req.ParentId < 0 {
  332. br.Msg = "参数错误"
  333. br.IsSendEmail = false
  334. return
  335. }
  336. count, err := data_manage.GetChartClassifyCount(req.ChartClassifyName, req.ParentId, utils.CHART_SOURCE_DEFAULT)
  337. if err != nil {
  338. br.Msg = "判断名称是否已存在失败"
  339. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  340. return
  341. }
  342. if count > 0 {
  343. br.Msg = "分类名称已存在,请重新输入"
  344. br.IsSendEmail = false
  345. return
  346. }
  347. //获取该层级下最大的排序数
  348. maxSort, err := data_manage.GetChartClassifyMaxSort(req.ParentId, utils.CHART_SOURCE_DEFAULT)
  349. classify := new(data_manage.ChartClassify)
  350. classify.ParentId = req.ParentId
  351. classify.ChartClassifyName = req.ChartClassifyName
  352. classify.HasData = 0
  353. classify.CreateTime = time.Now()
  354. classify.ModifyTime = time.Now()
  355. classify.SysUserId = this.SysUser.AdminId
  356. classify.SysUserRealName = this.SysUser.RealName
  357. classify.Level = req.Level + 1
  358. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  359. classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  360. classify.Sort = maxSort + 1
  361. classify.Source = utils.CHART_SOURCE_DEFAULT
  362. _, err = data_manage.AddChartClassify(classify)
  363. if err != nil {
  364. br.Msg = "保存分类失败"
  365. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  366. return
  367. }
  368. br.Ret = 200
  369. br.Msg = "保存成功"
  370. br.Success = true
  371. }
  372. // @Title 修改图表分类
  373. // @Description 修改图表分类接口
  374. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  375. // @Success 200 Ret=200 修改成功
  376. // @router /chart_classify/edit [post]
  377. func (this *ChartClassifyController) EditChartClassify() {
  378. br := new(models.BaseResponse).Init()
  379. defer func() {
  380. this.Data["json"] = br
  381. this.ServeJSON()
  382. }()
  383. var req data_manage.EditChartClassifyReq
  384. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  385. if err != nil {
  386. br.Msg = "参数解析异常!"
  387. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  388. return
  389. }
  390. if req.ChartClassifyName == "" {
  391. br.Msg = "请输入分类名称"
  392. br.IsSendEmail = false
  393. return
  394. }
  395. if req.ChartClassifyId <= 0 {
  396. br.Msg = "参数错误"
  397. br.IsSendEmail = false
  398. return
  399. }
  400. item, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  401. if err != nil {
  402. br.Msg = "保存失败"
  403. br.Msg = "获取分类信息失败,Err:" + err.Error()
  404. return
  405. }
  406. if item.ChartClassifyName != req.ChartClassifyName {
  407. count, err := data_manage.GetChartClassifyCount(req.ChartClassifyName, item.ParentId, utils.CHART_SOURCE_DEFAULT)
  408. if err != nil {
  409. br.Msg = "判断名称是否已存在失败"
  410. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  411. return
  412. }
  413. if count > 0 {
  414. br.Msg = "分类名称已存在,请重新输入"
  415. br.IsSendEmail = false
  416. return
  417. }
  418. err = data_manage.EditChartClassify(req.ChartClassifyId, utils.CHART_SOURCE_DEFAULT, req.ChartClassifyName)
  419. if err != nil {
  420. br.Msg = "保存失败"
  421. br.ErrMsg = "保存失败,Err:" + err.Error()
  422. return
  423. }
  424. }
  425. br.Ret = 200
  426. br.Msg = "保存成功"
  427. br.Success = true
  428. br.IsAddLog = true
  429. }
  430. // @Title 删除图表检测接口
  431. // @Description 删除图表检测接口
  432. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  433. // @Success 200 Ret=200 检测成功
  434. // @router /chart_classify/delete/check [post]
  435. func (this *ChartClassifyController) DeleteChartClassifyCheck() {
  436. br := new(models.BaseResponse).Init()
  437. defer func() {
  438. this.Data["json"] = br
  439. this.ServeJSON()
  440. }()
  441. var req data_manage.ChartClassifyDeleteCheckReq
  442. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  443. if err != nil {
  444. br.Msg = "参数解析异常!"
  445. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  446. return
  447. }
  448. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  449. br.Msg = "参数错误"
  450. br.IsSendEmail = false
  451. return
  452. }
  453. var deleteStatus int
  454. var tipsMsg string
  455. //删除分类
  456. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  457. //判断图表分类下,是否含有图表
  458. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  459. if err != nil {
  460. br.Msg = "删除失败"
  461. br.ErrMsg = "分类下是否含有指标失败,Err:" + err.Error()
  462. return
  463. }
  464. if count > 0 {
  465. deleteStatus = 1
  466. tipsMsg = "该分类下关联图表不可删除"
  467. }
  468. }
  469. if deleteStatus != 1 && req.ChartInfoId == 0 {
  470. classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId)
  471. if err != nil && err.Error() != utils.ErrNoRow() {
  472. br.Msg = "删除失败"
  473. br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  474. return
  475. }
  476. if classifyCount > 0 {
  477. deleteStatus = 2
  478. tipsMsg = "确认删除当前目录及包含的子目录吗"
  479. }
  480. }
  481. if deleteStatus == 0 {
  482. tipsMsg = "可删除,进行删除操作"
  483. }
  484. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  485. resp.DeleteStatus = deleteStatus
  486. resp.TipsMsg = tipsMsg
  487. br.Ret = 200
  488. br.Msg = "检测成功"
  489. br.Success = true
  490. br.Data = resp
  491. }
  492. // @Title 删除图表分类/图表
  493. // @Description 删除图表分类/图表接口
  494. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  495. // @Success 200 Ret=200 删除成功
  496. // @router /chart_classify/delete [post]
  497. func (this *ChartClassifyController) DeleteChartClassify() {
  498. br := new(models.BaseResponse).Init()
  499. defer func() {
  500. this.Data["json"] = br
  501. this.ServeJSON()
  502. }()
  503. sysUser := this.SysUser
  504. if sysUser == nil {
  505. br.Msg = "请登录"
  506. br.ErrMsg = "请登录,SysUser Is Empty"
  507. br.Ret = 408
  508. return
  509. }
  510. var req data_manage.DeleteChartClassifyReq
  511. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  512. if err != nil {
  513. br.Msg = "参数解析异常!"
  514. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  515. return
  516. }
  517. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  518. br.Msg = "参数错误"
  519. br.IsSendEmail = false
  520. return
  521. }
  522. //删除分类
  523. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  524. //判断是否含有指标
  525. count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  526. if err != nil && err.Error() != utils.ErrNoRow() {
  527. br.Msg = "删除失败"
  528. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  529. return
  530. }
  531. if count > 0 {
  532. br.Msg = "该目录下存在关联指标,不可删除"
  533. br.IsSendEmail = false
  534. return
  535. }
  536. err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  537. if err != nil {
  538. br.Msg = "删除失败"
  539. br.ErrMsg = "删除失败,Err:" + err.Error()
  540. return
  541. }
  542. }
  543. resp := new(data_manage.AddChartInfoResp)
  544. //删除图表
  545. if req.ChartInfoId > 0 {
  546. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  547. if err != nil {
  548. if err.Error() == utils.ErrNoRow() {
  549. br.Msg = "图表已删除,请刷新页面"
  550. br.ErrMsg = "指标不存在,Err:" + err.Error()
  551. return
  552. } else {
  553. br.Msg = "删除失败"
  554. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  555. return
  556. }
  557. }
  558. if chartInfo == nil {
  559. br.Msg = "图表已删除,请刷新页面"
  560. return
  561. }
  562. //图表操作权限
  563. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
  564. if !ok {
  565. br.Msg = "没有该图表的操作权限"
  566. br.ErrMsg = "没有该图表的操作权限"
  567. return
  568. }
  569. // 获取引用该图表的MyCharts, 用于ES删除
  570. var myCond string
  571. var myPars []interface{}
  572. myCond += ` AND a.chart_info_id = ? `
  573. myPars = append(myPars, req.ChartInfoId)
  574. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  575. if e != nil {
  576. br.Msg = "删除失败"
  577. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  578. return
  579. }
  580. myIds := make([]int, 0)
  581. for _, m := range myCharts {
  582. myIds = append(myIds, m.MyChartId)
  583. }
  584. //删除图表及关联指标
  585. err = data_manage.DeleteChartInfoAndData(req.ChartInfoId)
  586. if err != nil {
  587. br.Msg = "删除失败"
  588. br.ErrMsg = "删除失败,Err:" + err.Error()
  589. return
  590. }
  591. //删除ES
  592. {
  593. go data.EsDeleteChartInfo(req.ChartInfoId)
  594. // 删除MY ETA 图表 es数据
  595. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  596. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  597. }
  598. var condition string
  599. var pars []interface{}
  600. condition += " AND chart_classify_id=? AND source = ? "
  601. pars = append(pars, chartInfo.ChartClassifyId, utils.CHART_SOURCE_DEFAULT)
  602. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  603. pars = append(pars, req.ChartInfoId)
  604. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  605. if err != nil && err.Error() != utils.ErrNoRow() {
  606. br.Msg = "删除失败"
  607. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  608. return
  609. }
  610. if nextItem != nil {
  611. resp.UniqueCode = nextItem.UniqueCode
  612. resp.ChartInfoId = nextItem.ChartInfoId
  613. } else {
  614. var condition string
  615. var pars []interface{}
  616. condition += " AND level=3 "
  617. //pars = append(pars, chartInfo.ChartClassifyId)
  618. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  619. pars = append(pars, chartInfo.ChartClassifyId)
  620. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  621. if err != nil && err.Error() != utils.ErrNoRow() {
  622. br.Msg = "删除失败"
  623. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  624. return
  625. }
  626. if classifyItem != nil {
  627. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  628. if err != nil && err.Error() != utils.ErrNoRow() {
  629. br.Msg = "删除失败"
  630. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  631. return
  632. }
  633. if nextItem != nil {
  634. resp.UniqueCode = nextItem.UniqueCode
  635. resp.ChartInfoId = nextItem.ChartInfoId
  636. }
  637. }
  638. }
  639. //新增操作日志
  640. {
  641. chartLog := new(data_manage.ChartInfoLog)
  642. chartLog.ChartName = chartInfo.ChartName
  643. chartLog.ChartInfoId = req.ChartInfoId
  644. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  645. chartLog.SysUserId = sysUser.AdminId
  646. chartLog.SysUserRealName = sysUser.RealName
  647. chartLog.UniqueCode = chartInfo.UniqueCode
  648. chartLog.CreateTime = time.Now()
  649. chartLog.Content = string(this.Ctx.Input.RequestBody)
  650. chartLog.Status = "删除图表"
  651. chartLog.Method = this.Ctx.Input.URI()
  652. go data_manage.AddChartInfoLog(chartLog)
  653. }
  654. }
  655. br.Ret = 200
  656. br.Msg = "删除成功"
  657. br.Success = true
  658. br.Data = resp
  659. br.IsAddLog = true
  660. }
  661. // ChartClassifyMove
  662. // @Title 图表分类移动接口
  663. // @Description 图表分类移动接口
  664. // @Success 200 {object} data_manage.MoveChartClassifyReq
  665. // @router /chart_classify/move [post]
  666. func (this *ChartClassifyController) ChartClassifyMove() {
  667. br := new(models.BaseResponse).Init()
  668. defer func() {
  669. this.Data["json"] = br
  670. this.ServeJSON()
  671. }()
  672. sysUser := this.SysUser
  673. if sysUser == nil {
  674. br.Msg = "请登录"
  675. br.ErrMsg = "请登录,SysUser Is Empty"
  676. br.Ret = 408
  677. return
  678. }
  679. var req data_manage.MoveChartClassifyReq
  680. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  681. if err != nil {
  682. br.Msg = "参数解析异常!"
  683. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  684. return
  685. }
  686. if req.ClassifyId <= 0 {
  687. br.Msg = "参数错误"
  688. br.ErrMsg = "分类id小于等于0"
  689. return
  690. }
  691. //判断分类是否存在
  692. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  693. if err != nil {
  694. br.Msg = "移动失败"
  695. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  696. return
  697. }
  698. if chartClassifyInfo.Source != utils.CHART_SOURCE_DEFAULT {
  699. br.Msg = "分类异常"
  700. br.ErrMsg = "分类异常,不是ETA图库的分类"
  701. return
  702. }
  703. updateCol := make([]string, 0)
  704. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  705. if chartClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  706. parentChartClassifyInfo, err := data_manage.GetChartClassifyById(req.ParentClassifyId)
  707. if err != nil {
  708. br.Msg = "移动失败"
  709. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  710. return
  711. }
  712. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  713. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  714. chartClassifyInfo.ModifyTime = time.Now()
  715. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  716. }
  717. //如果有传入 上一个兄弟节点分类id
  718. if req.PrevClassifyId > 0 {
  719. //上一个兄弟节点
  720. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  721. if err != nil {
  722. br.Msg = "移动失败"
  723. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  724. return
  725. }
  726. //如果是移动在两个兄弟节点之间
  727. if req.NextClassifyId > 0 {
  728. //下一个兄弟节点
  729. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  730. if err != nil {
  731. br.Msg = "移动失败"
  732. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  733. return
  734. }
  735. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  736. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  737. //变更兄弟节点的排序
  738. updateSortStr := `sort + 2`
  739. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  740. } else {
  741. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  742. if nextClassify.Sort-prevClassify.Sort == 1 {
  743. //变更兄弟节点的排序
  744. updateSortStr := `sort + 1`
  745. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  746. }
  747. }
  748. }
  749. chartClassifyInfo.Sort = prevClassify.Sort + 1
  750. chartClassifyInfo.ModifyTime = time.Now()
  751. updateCol = append(updateCol, "Sort", "ModifyTime")
  752. } else {
  753. firstClassify, err := data_manage.GetFirstChartClassifyByParentId(chartClassifyInfo.ParentId)
  754. if err != nil && err.Error() != utils.ErrNoRow() {
  755. br.Msg = "移动失败"
  756. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  757. return
  758. }
  759. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  760. if firstClassify != nil && firstClassify.Sort == 0 {
  761. updateSortStr := ` sort + 1 `
  762. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  763. }
  764. chartClassifyInfo.Sort = 0 //那就是排在第一位
  765. chartClassifyInfo.ModifyTime = time.Now()
  766. updateCol = append(updateCol, "Sort", "ModifyTime")
  767. }
  768. //更新
  769. if len(updateCol) > 0 {
  770. err = chartClassifyInfo.Update(updateCol)
  771. if err != nil {
  772. br.Msg = "移动失败"
  773. br.ErrMsg = "修改失败,Err:" + err.Error()
  774. return
  775. }
  776. }
  777. br.Ret = 200
  778. br.Success = true
  779. br.Msg = "移动成功"
  780. }
  781. // getChartClassifyListForMe 获取我创建的图表分类
  782. func getChartClassifyListForMeV2(adminInfo system.Admin, resp *data_manage.ChartClassifyListResp) (errMsg string, err error) {
  783. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_DEFAULT)
  784. if err != nil && err.Error() != utils.ErrNoRow() {
  785. errMsg = "获取失败"
  786. return
  787. }
  788. classifyAll, err := data_manage.GetChartClassifyAll(utils.CHART_SOURCE_DEFAULT)
  789. if err != nil && err.Error() != utils.ErrNoRow() {
  790. errMsg = "获取失败"
  791. return
  792. }
  793. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  794. for _, v := range classifyAll {
  795. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  796. }
  797. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  798. for _, v := range rootList {
  799. if existItems, ok := rootChildMap[v.ChartClassifyId]; ok {
  800. v.Children = existItems
  801. } else {
  802. items := make([]*data_manage.ChartClassifyItems, 0)
  803. v.Children = items
  804. }
  805. nodeAll = append(nodeAll, v)
  806. }
  807. resp.AllNodes = nodeAll
  808. return
  809. }
  810. // ChartClassifyChartListV2
  811. // @Title 根据图表分类获取图表列表
  812. // @Description 根据图表分类获取图表列表接口
  813. // @Param IsShowMe query bool true "是否只看我的,true、false"
  814. // @Param ChartClassifyId query bool true "图片分类id"
  815. // @Success 200 {object} data_manage.ChartClassifyListResp
  816. // @router /chart_classify/chart/list [get]
  817. func (this *ChartClassifyController) ChartClassifyChartListV2() {
  818. br := new(models.BaseResponse).Init()
  819. defer func() {
  820. this.Data["json"] = br
  821. this.ServeJSON()
  822. }()
  823. sysUser := this.SysUser
  824. if sysUser == nil {
  825. br.Msg = "请登录"
  826. br.ErrMsg = "请登录,SysUser Is Empty"
  827. br.Ret = 408
  828. return
  829. }
  830. resp := new(data_manage.ChartClassifyListResp)
  831. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  832. if chartClassifyId <= 0 {
  833. br.Msg = "参数错误"
  834. return
  835. }
  836. // 获取当前账号的不可见指标
  837. noPermissionChartIdMap := make(map[int]bool)
  838. {
  839. obj := data_manage.EdbInfoNoPermissionAdmin{}
  840. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  841. if err != nil && err.Error() != utils.ErrNoRow() {
  842. br.Msg = "获取失败"
  843. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  844. return
  845. }
  846. for _, v := range confList {
  847. noPermissionChartIdMap[v.ChartInfoId] = true
  848. }
  849. }
  850. isShowMe, _ := this.GetBool("IsShowMe")
  851. if isShowMe {
  852. errMsg, err := getChartClassifyListForMeV2(*this.SysUser, resp)
  853. if err != nil {
  854. br.Msg = errMsg
  855. br.ErrMsg = err.Error()
  856. return
  857. }
  858. // 移除没有权限的图表
  859. allNodes := handleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
  860. resp.AllNodes = allNodes
  861. br.Ret = 200
  862. br.Success = true
  863. br.Msg = "获取成功"
  864. br.Data = resp
  865. fmt.Println("source my classify")
  866. return
  867. }
  868. allChartInfo, err := data_manage.GetChartInfoAllByClassifyId(utils.CHART_SOURCE_DEFAULT, chartClassifyId)
  869. if err != nil && err.Error() != utils.ErrNoRow() {
  870. br.Msg = "获取失败"
  871. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  872. return
  873. }
  874. // 移除没有权限的图表
  875. allNodes := handleNoPermissionChart(allChartInfo, noPermissionChartIdMap)
  876. resp.AllNodes = allNodes
  877. br.Ret = 200
  878. br.Success = true
  879. br.Msg = "获取成功"
  880. br.Data = resp
  881. }