classify.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. package line_feature
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/models/system"
  8. "eta_gn/eta_api/services/data"
  9. "eta_gn/eta_api/utils"
  10. "time"
  11. )
  12. // LineFeaturesChartClassifyController 统计特征图表分类
  13. type LineFeaturesChartClassifyController struct {
  14. controllers.BaseAuthController
  15. }
  16. // ChartClassifyList
  17. // @Title 统计特征图表分类列表
  18. // @Description 统计特征图表分类列表接口
  19. // @Param Source query int true "图表类型,3:统计特征,4:滚动统计特征"
  20. // @Success 200 {object} data_manage.ChartClassifyListResp
  21. // @router /chart_classify/list [get]
  22. func (this *LineFeaturesChartClassifyController) ChartClassifyList() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. resp := new(data_manage.ChartClassifyListResp)
  29. // 获取当前账号的不可见指标
  30. noPermissionChartIdMap := make(map[int]bool)
  31. {
  32. obj := data_manage.EdbInfoNoPermissionAdmin{}
  33. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  34. if err != nil && !utils.IsErrNoRow(err) {
  35. br.Msg = "获取失败"
  36. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  37. return
  38. }
  39. for _, v := range confList {
  40. noPermissionChartIdMap[v.ChartInfoId] = true
  41. }
  42. }
  43. source := utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION
  44. rootList, err := data_manage.GetChartClassifyByParentId(0, source, this.SysUser.AdminId)
  45. if err != nil && !utils.IsErrNoRow(err) {
  46. br.Msg = "获取失败"
  47. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  48. return
  49. }
  50. allChartInfo, err := data_manage.GetChartInfoAll([]int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, this.SysUser.AdminId)
  51. if err != nil && !utils.IsErrNoRow(err) {
  52. br.Msg = "获取失败"
  53. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  54. return
  55. }
  56. chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
  57. for _, v := range allChartInfo {
  58. if v.SysUserId != this.SysUser.AdminId {
  59. continue
  60. }
  61. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  62. }
  63. rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
  64. for _, v := range rootList {
  65. rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
  66. if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
  67. v.Children = existItems
  68. } else {
  69. items := make([]*data_manage.ChartClassifyItems, 0)
  70. v.Children = items
  71. }
  72. }
  73. // 移除没有权限的图表
  74. allNodes := data.HandleNoPermissionChart(rootList, noPermissionChartIdMap, this.SysUser.AdminId)
  75. resp.AllNodes = allNodes
  76. br.Ret = 200
  77. br.Success = true
  78. br.Msg = "获取成功"
  79. br.Data = resp
  80. }
  81. // getChartClassifyListForMe 获取我创建的图表
  82. func getChartClassifyListForMe(adminInfo system.Admin, resp *data_manage.ChartClassifyListResp) (errMsg string, err error) {
  83. // 获取所有的分类
  84. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, adminInfo.AdminId)
  85. if err != nil && !utils.IsErrNoRow(err) {
  86. errMsg = "获取失败"
  87. return
  88. }
  89. // /获取所有的图表
  90. allChartInfo, err := data_manage.GetChartInfoByAdminId([]int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, adminInfo.AdminId)
  91. if err != nil && !utils.IsErrNoRow(err) {
  92. errMsg = "获取失败"
  93. return
  94. }
  95. chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
  96. for _, v := range allChartInfo {
  97. chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
  98. }
  99. for k, v := range rootList {
  100. if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
  101. v.Children = existItems
  102. } else {
  103. items := make([]*data_manage.ChartClassifyItems, 0)
  104. v.Children = items
  105. }
  106. rootList[k] = v
  107. }
  108. resp.AllNodes = rootList
  109. return
  110. }
  111. // ChartClassifyItems
  112. // @Title 获取所有统计特征图表分类接口-不包含图表
  113. // @Description 获取所有统计特征图表分类接口-不包含图表
  114. // @Success 200 {object} data_manage.ChartClassifyListResp
  115. // @router /chart_classify/items [get]
  116. func (this *LineFeaturesChartClassifyController) ChartClassifyItems() {
  117. br := new(models.BaseResponse).Init()
  118. defer func() {
  119. this.Data["json"] = br
  120. this.ServeJSON()
  121. }()
  122. rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, this.SysUser.AdminId)
  123. if err != nil {
  124. br.Msg = "获取失败"
  125. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  126. return
  127. }
  128. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  129. for k := range rootList {
  130. rootNode := rootList[k]
  131. nodeAll = append(nodeAll, rootNode)
  132. }
  133. resp := new(data_manage.ChartClassifyListResp)
  134. resp.AllNodes = nodeAll
  135. br.Ret = 200
  136. br.Success = true
  137. br.Msg = "获取成功"
  138. br.Data = resp
  139. }
  140. // AddChartClassify
  141. // @Title 新增统计特征图表分类
  142. // @Description 新增统计特征图表分类接口
  143. // @Param request body data_manage.AddChartClassifyReq true "type json string"
  144. // @Success 200 Ret=200 保存成功
  145. // @router /chart_classify/add [post]
  146. func (this *LineFeaturesChartClassifyController) AddChartClassify() {
  147. br := new(models.BaseResponse).Init()
  148. defer func() {
  149. this.Data["json"] = br
  150. this.ServeJSON()
  151. }()
  152. var req data_manage.AddChartClassifyReq
  153. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  154. if err != nil {
  155. br.Msg = "参数解析异常!"
  156. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  157. return
  158. }
  159. if req.ChartClassifyName == "" {
  160. br.Msg = "请输入分类名称"
  161. br.IsSendEmail = false
  162. return
  163. }
  164. if req.ParentId < 0 {
  165. br.Msg = "参数错误"
  166. br.IsSendEmail = false
  167. return
  168. }
  169. // 新增图表分类
  170. _, err, errMsg, isSendEmail := data.AddChartClassify(req.ChartClassifyName, req.ParentId, req.Level, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, this.Lang, this.SysUser)
  171. if err != nil {
  172. br.Msg = errMsg
  173. br.ErrMsg = "添加分类失败,Err:" + err.Error()
  174. br.IsSendEmail = isSendEmail
  175. return
  176. }
  177. br.Ret = 200
  178. br.Msg = "添加成功"
  179. br.Success = true
  180. }
  181. // EditChartClassify
  182. // @Title 修改统计特征图表分类
  183. // @Description 修改统计特征图表分类接口
  184. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  185. // @Success 200 Ret=200 修改成功
  186. // @router /chart_classify/edit [post]
  187. func (this *LineFeaturesChartClassifyController) EditChartClassify() {
  188. br := new(models.BaseResponse).Init()
  189. defer func() {
  190. this.Data["json"] = br
  191. this.ServeJSON()
  192. }()
  193. var req data_manage.EditChartClassifyReq
  194. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  195. if err != nil {
  196. br.Msg = "参数解析异常!"
  197. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  198. return
  199. }
  200. if req.ChartClassifyName == "" {
  201. br.Msg = "请输入分类名称"
  202. br.IsSendEmail = false
  203. return
  204. }
  205. if req.ChartClassifyId <= 0 {
  206. br.Msg = "参数错误"
  207. br.IsSendEmail = false
  208. return
  209. }
  210. // 编辑图表分类
  211. _, err, errMsg, isSendEmail := data.EditChartClassify(req.ChartClassifyId, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, req.ChartClassifyName, this.Lang, this.SysUser)
  212. if err != nil {
  213. br.Msg = errMsg
  214. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  215. br.IsSendEmail = isSendEmail
  216. return
  217. }
  218. br.Ret = 200
  219. br.Msg = "修改成功"
  220. br.Success = true
  221. br.IsAddLog = true
  222. }
  223. // DeleteChartClassifyCheck
  224. // @Title 删除图表检测接口
  225. // @Description 删除图表检测接口
  226. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  227. // @Success 200 Ret=200 检测成功
  228. // @router /chart_classify/delete/check [post]
  229. func (this *LineFeaturesChartClassifyController) DeleteChartClassifyCheck() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. var req data_manage.ChartClassifyDeleteCheckReq
  236. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  237. if err != nil {
  238. br.Msg = "参数解析异常!"
  239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  240. return
  241. }
  242. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  243. br.Msg = "参数错误"
  244. br.IsSendEmail = false
  245. return
  246. }
  247. var deleteStatus int
  248. var tipsMsg string
  249. childIds := make([]int, 0)
  250. //删除分类
  251. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  252. //判断统计特征图表分类下,是否含有图表
  253. classifyAll, err := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION)
  254. if err != nil {
  255. br.Msg = "删除失败"
  256. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  257. return
  258. }
  259. classifyAllMap := make(map[int]*data_manage.ChartClassifyItems)
  260. for _, v := range classifyAll {
  261. classifyAllMap[v.ChartClassifyId] = v
  262. }
  263. data.GetChartClassifyChildIds(req.ChartClassifyId, classifyAllMap, &childIds)
  264. condition := ``
  265. pars := make([]interface{}, 0)
  266. if len(childIds) > 0 {
  267. ids := utils.IntArr2joinString(childIds, ",")
  268. condition = ` AND chart_classify_id IN (` + ids + `) `
  269. } else {
  270. condition = ` AND chart_classify_id = ? `
  271. pars = append(pars, req.ChartClassifyId)
  272. }
  273. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  274. if err != nil {
  275. br.Msg = "判断图表名称是否存在失败"
  276. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  277. return
  278. }
  279. //count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  280. //if err != nil {
  281. // br.Msg = "删除失败"
  282. // br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  283. // return
  284. //}
  285. if count > 0 {
  286. deleteStatus = 1
  287. tipsMsg = "该分类下关联图表不可删除"
  288. }
  289. }
  290. if deleteStatus != 1 && req.ChartInfoId == 0 {
  291. //classifyCount, err := data_manage.GetChartClassifyCountByClassifyId(req.ChartClassifyId)
  292. //if err != nil && !utils.IsErrNoRow(err) {
  293. // br.Msg = "删除失败"
  294. // br.ErrMsg = "分类下是否含有图表失败,Err:" + err.Error()
  295. // return
  296. //}
  297. if len(childIds) > 0 {
  298. deleteStatus = 2
  299. tipsMsg = "确认删除当前目录及包含的子目录吗"
  300. }
  301. }
  302. if deleteStatus == 0 {
  303. tipsMsg = "可删除,进行删除操作"
  304. }
  305. resp := new(data_manage.ChartClassifyDeleteCheckResp)
  306. resp.DeleteStatus = deleteStatus
  307. resp.TipsMsg = tipsMsg
  308. br.Ret = 200
  309. br.Msg = "检测成功"
  310. br.Success = true
  311. br.Data = resp
  312. }
  313. // DeleteChartClassify
  314. // @Title 删除统计特征图表分类/图表
  315. // @Description 删除统计特征图表分类/图表接口
  316. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  317. // @Success 200 Ret=200 删除成功
  318. // @router /chart_classify/delete [post]
  319. func (this *LineFeaturesChartClassifyController) DeleteChartClassify() {
  320. br := new(models.BaseResponse).Init()
  321. defer func() {
  322. this.Data["json"] = br
  323. this.ServeJSON()
  324. }()
  325. sysUser := this.SysUser
  326. if sysUser == nil {
  327. br.Msg = "请登录"
  328. br.ErrMsg = "请登录,SysUser Is Empty"
  329. br.Ret = 408
  330. return
  331. }
  332. var req data_manage.DeleteChartClassifyReq
  333. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  334. if err != nil {
  335. br.Msg = "参数解析异常!"
  336. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  337. return
  338. }
  339. if req.ChartClassifyId < 0 && req.ChartInfoId <= 0 {
  340. br.Msg = "参数错误"
  341. br.IsSendEmail = false
  342. return
  343. }
  344. //删除分类
  345. if req.ChartClassifyId > 0 && req.ChartInfoId == 0 {
  346. //判断是否含有指标
  347. childIds := make([]int, 0)
  348. classifyAll, err := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION)
  349. if err != nil {
  350. br.Msg = "删除失败"
  351. br.ErrMsg = "获取分类数据失败,Err:" + err.Error()
  352. return
  353. }
  354. classifyAllMap := make(map[int]*data_manage.ChartClassifyItems)
  355. for _, v := range classifyAll {
  356. classifyAllMap[v.ChartClassifyId] = v
  357. }
  358. data.GetChartClassifyChildIds(req.ChartClassifyId, classifyAllMap, &childIds)
  359. condition := ``
  360. pars := make([]interface{}, 0)
  361. if len(childIds) > 0 {
  362. ids := utils.IntArr2joinString(childIds, ",")
  363. condition = ` AND chart_classify_id IN (` + ids + `) `
  364. } else {
  365. condition = ` AND chart_classify_id = ? `
  366. pars = append(pars, req.ChartClassifyId)
  367. }
  368. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  369. if err != nil {
  370. br.Msg = "判断图表名称是否存在失败"
  371. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  372. return
  373. }
  374. //count, err := data_manage.GetChartInfoCountByClassifyId(req.ChartClassifyId)
  375. //if err != nil && !utils.IsErrNoRow(err) {
  376. // br.Msg = "删除失败"
  377. // br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  378. // return
  379. //}
  380. if count > 0 {
  381. br.Msg = "该目录下存在关联指标,不可删除"
  382. br.IsSendEmail = false
  383. return
  384. }
  385. delIds := append(childIds, req.ChartClassifyId)
  386. err = data_manage.DeleteChartClassifyByIds(delIds)
  387. //err = data_manage.DeleteChartClassify(req.ChartClassifyId)
  388. if err != nil {
  389. br.Msg = "删除失败"
  390. br.ErrMsg = "删除失败,Err:" + err.Error()
  391. return
  392. }
  393. }
  394. resp := new(data_manage.AddChartInfoResp)
  395. //删除图表
  396. if req.ChartInfoId > 0 {
  397. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  398. if err != nil {
  399. if utils.IsErrNoRow(err) {
  400. br.Msg = "图表已删除,请刷新页面"
  401. br.ErrMsg = "指标不存在,Err:" + err.Error()
  402. return
  403. } else {
  404. br.Msg = "删除失败"
  405. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  406. return
  407. }
  408. }
  409. if chartInfo == nil {
  410. br.Msg = "图表已删除,请刷新页面"
  411. return
  412. }
  413. //图表操作权限
  414. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  415. if !ok {
  416. br.Msg = "没有该图表的操作权限"
  417. br.ErrMsg = "没有该图表的操作权限"
  418. return
  419. }
  420. myIds := make([]int, 0)
  421. {
  422. // 获取引用该图表的MyCharts, 用于ES删除
  423. var myCond string
  424. var myPars []interface{}
  425. myCond += ` AND a.chart_info_id = ? `
  426. myPars = append(myPars, req.ChartInfoId)
  427. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  428. if e != nil {
  429. br.Msg = "删除失败"
  430. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  431. return
  432. }
  433. for _, m := range myCharts {
  434. myIds = append(myIds, m.MyChartId)
  435. }
  436. }
  437. //删除图表及关联指标
  438. err = data_manage.DeleteChartInfoAndData(chartInfo.ChartInfoId)
  439. if err != nil {
  440. br.Msg = "删除失败"
  441. br.ErrMsg = "删除失败,Err:" + err.Error()
  442. return
  443. }
  444. //删除ES
  445. {
  446. go data.DeleteChartInfoToEs(chartInfo.ChartInfoId)
  447. // 删除MY ETA 图表 es数据
  448. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  449. }
  450. source := chartInfo.Source // 统计特征图表
  451. var condition string
  452. var pars []interface{}
  453. condition += " AND chart_classify_id=? AND source = ? "
  454. pars = append(pars, chartInfo.ChartClassifyId, source)
  455. condition += " AND chart_info_id>? ORDER BY create_time ASC LIMIT 1 "
  456. pars = append(pars, req.ChartInfoId)
  457. nextItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  458. if err != nil && !utils.IsErrNoRow(err) {
  459. br.Msg = "删除失败"
  460. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  461. return
  462. }
  463. if nextItem != nil {
  464. resp.UniqueCode = nextItem.UniqueCode
  465. resp.ChartInfoId = nextItem.ChartInfoId
  466. } else {
  467. var condition string
  468. var pars []interface{}
  469. condition += " AND level=1 "
  470. //pars = append(pars, chartInfo.ChartClassifyId)
  471. condition += " AND chart_classify_id>? ORDER BY chart_classify_id ASC LIMIT 1 "
  472. pars = append(pars, chartInfo.ChartClassifyId)
  473. classifyItem, err := data_manage.GetChartClassifyByCondition(condition, pars)
  474. if err != nil && !utils.IsErrNoRow(err) {
  475. br.Msg = "删除失败"
  476. br.ErrMsg = "获取下一级图库分类信息失败,Err:" + err.Error()
  477. return
  478. }
  479. if classifyItem != nil {
  480. nextItem, err = data_manage.GetNextChartInfo(chartInfo.ChartClassifyId)
  481. if err != nil && !utils.IsErrNoRow(err) {
  482. br.Msg = "删除失败"
  483. br.ErrMsg = "获取下一级图库信息失败,Err:" + err.Error()
  484. return
  485. }
  486. if nextItem != nil {
  487. resp.UniqueCode = nextItem.UniqueCode
  488. resp.ChartInfoId = nextItem.ChartInfoId
  489. }
  490. }
  491. }
  492. //新增操作日志
  493. {
  494. chartLog := new(data_manage.ChartInfoLog)
  495. chartLog.ChartName = chartInfo.ChartName
  496. chartLog.ChartInfoId = req.ChartInfoId
  497. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  498. chartLog.SysUserId = sysUser.AdminId
  499. chartLog.SysUserRealName = sysUser.RealName
  500. chartLog.UniqueCode = chartInfo.UniqueCode
  501. chartLog.CreateTime = time.Now()
  502. chartLog.Content = string(this.Ctx.Input.RequestBody)
  503. chartLog.Status = "删除图表"
  504. chartLog.Method = this.Ctx.Input.URI()
  505. go data_manage.AddChartInfoLog(chartLog)
  506. }
  507. }
  508. br.Ret = 200
  509. br.Msg = "删除成功"
  510. br.Success = true
  511. br.Data = resp
  512. br.IsAddLog = true
  513. }
  514. // ChartClassifyMove
  515. // @Title 统计特征图表分类移动接口
  516. // @Description 统计特征图表分类移动接口
  517. // @Success 200 {object} data_manage.MoveChartClassifyReq
  518. // @router /chart_classify/move [post]
  519. func (this *LineFeaturesChartClassifyController) ChartClassifyMove() {
  520. br := new(models.BaseResponse).Init()
  521. defer func() {
  522. this.Data["json"] = br
  523. this.ServeJSON()
  524. }()
  525. sysUser := this.SysUser
  526. if sysUser == nil {
  527. br.Msg = "请登录"
  528. br.ErrMsg = "请登录,SysUser Is Empty"
  529. br.Ret = 408
  530. return
  531. }
  532. var req data_manage.MoveChartClassifyReq
  533. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  534. if err != nil {
  535. br.Msg = "参数解析异常!"
  536. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  537. return
  538. }
  539. if req.ClassifyId <= 0 {
  540. br.Msg = "参数错误"
  541. br.ErrMsg = "分类id小于等于0"
  542. return
  543. }
  544. //判断分类是否存在
  545. chartClassifyInfo, err := data_manage.GetChartClassifyById(req.ClassifyId)
  546. if err != nil {
  547. br.Msg = "移动失败"
  548. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  549. return
  550. }
  551. if chartClassifyInfo.Source != utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION {
  552. br.Msg = "分类异常"
  553. br.ErrMsg = "分类异常,不是统计特征图表的分类"
  554. return
  555. }
  556. updateCol := make([]string, 0)
  557. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  558. if chartClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  559. parentChartClassifyInfo, err := data_manage.GetChartClassifyById(req.ParentClassifyId)
  560. if err != nil {
  561. br.Msg = "移动失败"
  562. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  563. return
  564. }
  565. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  566. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  567. chartClassifyInfo.ModifyTime = time.Now()
  568. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  569. }
  570. //如果有传入 上一个兄弟节点分类id
  571. if req.PrevClassifyId > 0 {
  572. //上一个兄弟节点
  573. prevClassify, err := data_manage.GetChartClassifyById(req.PrevClassifyId)
  574. if err != nil {
  575. br.Msg = "移动失败"
  576. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  577. return
  578. }
  579. //如果是移动在两个兄弟节点之间
  580. if req.NextClassifyId > 0 {
  581. //下一个兄弟节点
  582. nextClassify, err := data_manage.GetChartClassifyById(req.NextClassifyId)
  583. if err != nil {
  584. br.Msg = "移动失败"
  585. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  586. return
  587. }
  588. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  589. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == chartClassifyInfo.Sort {
  590. //变更兄弟节点的排序
  591. updateSortStr := `sort + 2`
  592. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr)
  593. } else {
  594. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  595. if nextClassify.Sort-prevClassify.Sort == 1 {
  596. //变更兄弟节点的排序
  597. updateSortStr := `sort + 1`
  598. _ = data_manage.UpdateChartClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  599. }
  600. }
  601. }
  602. chartClassifyInfo.Sort = prevClassify.Sort + 1
  603. chartClassifyInfo.ModifyTime = time.Now()
  604. updateCol = append(updateCol, "Sort", "ModifyTime")
  605. } else {
  606. firstClassify, err := data_manage.GetFirstChartClassifyByParentId(chartClassifyInfo.ParentId)
  607. if err != nil && !utils.IsErrNoRow(err) {
  608. br.Msg = "移动失败"
  609. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  610. return
  611. }
  612. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  613. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  614. updateSortStr := ` sort + 1 `
  615. _ = data_manage.UpdateChartClassifySortByParentId(firstClassify.ParentId, firstClassify.ChartClassifyId-1, 0, updateSortStr)
  616. }
  617. chartClassifyInfo.Sort = 0 //那就是排在第一位
  618. chartClassifyInfo.ModifyTime = time.Now()
  619. updateCol = append(updateCol, "Sort", "ModifyTime")
  620. }
  621. //更新
  622. if len(updateCol) > 0 {
  623. err = chartClassifyInfo.Update(updateCol)
  624. if err != nil {
  625. br.Msg = "移动失败"
  626. br.ErrMsg = "修改失败,Err:" + err.Error()
  627. return
  628. }
  629. }
  630. br.Ret = 200
  631. br.Success = true
  632. br.Msg = "移动成功"
  633. }