classify.go 22 KB

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