chart_classify.go 26 KB

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