line_chart_classify.go 19 KB

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