edb_monitor_classify.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package edbmonitor
  2. import (
  3. "errors"
  4. edbmonitor "eta/eta_api/models/edb_monitor"
  5. "eta/eta_api/models/edb_monitor/request"
  6. "eta/eta_api/models/edb_monitor/response"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "time"
  10. )
  11. func GetEdbMonitorClassifyTree() (resp []*response.EdbMonitorClassifyTree, msg string, err error) {
  12. classifyList, err := edbmonitor.GetEdbMonitorClassifyList()
  13. if err != nil {
  14. msg = "获取分类列表失败"
  15. return
  16. }
  17. resp = generateClassifyTree(classifyList, 0)
  18. return
  19. }
  20. func SaveEdbMonitorClassify(req request.EdbMonitorClassifySaveReq) (msg string, err error) {
  21. childClassify, er := edbmonitor.GetChildEdbMonitorClassifyById(req.ParentId)
  22. if er != nil {
  23. msg = "分类信息保存失败"
  24. err = fmt.Errorf("get parent classify err:%w", er)
  25. return
  26. }
  27. var parentClassify *edbmonitor.EdbMonitorClassify
  28. if req.ParentId > 0 {
  29. parentClassify, er = edbmonitor.GetEdbMonitorClassifyById(req.ParentId)
  30. if er != nil {
  31. if er.Error() == utils.ErrNoRow() {
  32. msg = "父级分类不存在"
  33. err = errors.New("parent classify not exist")
  34. return
  35. }
  36. msg = "分类信息保存失败"
  37. err = fmt.Errorf("get parent classify err:%w", er)
  38. return
  39. }
  40. }
  41. var rootId int
  42. if parentClassify != nil {
  43. rootId = parentClassify.RootId
  44. if rootId == 0 {
  45. rootId = parentClassify.ParentId
  46. }
  47. if rootId == 0 {
  48. rootId = parentClassify.ClassifyId
  49. }
  50. }
  51. if req.ClassifyId > 0 {
  52. classifyInfo, er := edbmonitor.GetEdbMonitorClassifyById(req.ClassifyId)
  53. if er != nil {
  54. if er.Error() == utils.ErrNoRow() {
  55. msg = "分类不存在"
  56. return
  57. }
  58. msg = "分类信息保存失败"
  59. return
  60. }
  61. if classifyInfo.Level != req.Level {
  62. msg = "分类层级不能修改"
  63. return
  64. }
  65. var updateCols []string
  66. if classifyInfo.ClassifyName != req.ClassifyName {
  67. classifyInfo.ClassifyName = req.ClassifyName
  68. updateCols = append(updateCols, "classify_name")
  69. }
  70. if classifyInfo.ParentId != req.ParentId {
  71. classifyInfo.ParentId = req.ParentId
  72. updateCols = append(updateCols, "parent_id")
  73. maxSort, er := edbmonitor.GetEdbMonitorClassifyMaxSortByParentId(classifyInfo.ParentId)
  74. if er != nil {
  75. msg = "分类信息保存失败"
  76. err = fmt.Errorf("get max sort err:%w", er)
  77. return
  78. }
  79. classifyInfo.Sort = maxSort + 1
  80. updateCols = append(updateCols, "sort")
  81. }
  82. for _, v := range childClassify {
  83. if v.ClassifyName == req.ClassifyName {
  84. msg = "分类名称重复"
  85. err = errors.New("classify name repeat")
  86. return
  87. }
  88. }
  89. childClassifyIds, er := edbmonitor.GetChildEdbMonitorClassifyIdById(req.ClassifyId)
  90. if er != nil {
  91. msg = "分类信息保存失败"
  92. err = fmt.Errorf("get child classify id err:%w", er)
  93. return
  94. }
  95. if classifyInfo.RootId != rootId {
  96. classifyInfo.RootId = rootId
  97. updateCols = append(updateCols, "root_id")
  98. }
  99. err = classifyInfo.Update(updateCols, childClassifyIds)
  100. if err != nil {
  101. msg = "分类信息保存失败"
  102. return
  103. }
  104. } else {
  105. for _, v := range childClassify {
  106. if v.ClassifyName == req.ClassifyName {
  107. msg = "分类名称重复"
  108. err = errors.New("classify name repeat")
  109. return
  110. }
  111. }
  112. maxSort, er := edbmonitor.GetEdbMonitorClassifyMaxSortByParentId(req.ParentId)
  113. if er != nil {
  114. msg = "分类信息保存失败"
  115. err = fmt.Errorf("get max sort err:%w", er)
  116. return
  117. }
  118. tmp := new(edbmonitor.EdbMonitorClassify)
  119. tmp.ClassifyName = req.ClassifyName
  120. tmp.ParentId = req.ParentId
  121. tmp.RootId = rootId
  122. tmp.Level = req.Level
  123. tmp.Sort = maxSort + 1
  124. tmp.CreateTime = time.Now()
  125. _, er = tmp.Insert()
  126. if er != nil {
  127. msg = "分类信息保存失败"
  128. err = fmt.Errorf("insert classify err:%w", er)
  129. return
  130. }
  131. }
  132. return
  133. }
  134. func DeleteEdbMonitorClassify(req request.EdbMonitorClassifyDeleteReq) (msg string, err error) {
  135. classifyInfo, er := edbmonitor.GetEdbMonitorClassifyById(req.ClassifyId)
  136. if er != nil {
  137. if er.Error() == utils.ErrNoRow() {
  138. msg = "分类不存在"
  139. return
  140. }
  141. msg = "分类信息删除失败"
  142. return
  143. }
  144. var classifyIds []int
  145. switch classifyInfo.Level {
  146. case 1:
  147. classifyList, er := edbmonitor.GetChildEdbMonitorClassifyByRootId(classifyInfo.ClassifyId)
  148. if er != nil {
  149. msg = "分类删除失败"
  150. err = fmt.Errorf("get child classify by root err:%w", er)
  151. return
  152. }
  153. classifyIds = append(classifyIds, classifyInfo.ClassifyId)
  154. for _, v := range classifyList {
  155. classifyIds = append(classifyIds, v.ClassifyId)
  156. }
  157. case 2:
  158. classifyList, er := edbmonitor.GetChildEdbMonitorClassifyById(classifyInfo.ClassifyId)
  159. if er != nil {
  160. msg = "分类删除失败"
  161. err = fmt.Errorf("get child classify err:%w", er)
  162. return
  163. }
  164. classifyIds = append(classifyIds, classifyInfo.ClassifyId)
  165. for _, v := range classifyList {
  166. classifyIds = append(classifyIds, v.ClassifyId)
  167. }
  168. case 3:
  169. classifyIds = append(classifyIds, classifyInfo.ClassifyId)
  170. }
  171. count, er := edbmonitor.GetEdbMonitorInfoCountByClassifyId(classifyIds)
  172. if er != nil {
  173. msg = "分类删除失败"
  174. err = fmt.Errorf("get classify info count err:%w", er)
  175. return
  176. }
  177. if count > 0 {
  178. msg = "分类下存在关联预警,不能删除"
  179. err = fmt.Errorf("分类下存在关联预警,不能删除")
  180. return
  181. }
  182. err = edbmonitor.DeleteEdbMonitorClassifyByIdList(classifyIds)
  183. if err != nil {
  184. msg = "分类删除失败"
  185. return
  186. }
  187. return
  188. }
  189. // MoveEdbMonitorClassify 移动分类
  190. func MoveEdbMonitorClassify(req request.MoveEdbMonitorClassifyReq) (msg string, err error) {
  191. classifyInfo, er := edbmonitor.GetEdbMonitorClassifyById(req.ClassifyId)
  192. if er != nil {
  193. err = er
  194. if er.Error() == utils.ErrNoRow() {
  195. msg = "分类不存在,请刷新页面"
  196. return
  197. }
  198. msg = "移动失败"
  199. return
  200. }
  201. // 校验移动的父级目录下是否有重名分类
  202. exists, er := edbmonitor.GetEdbMonitorClassifyByParentIdAndName(req.ParentClassifyId, classifyInfo.ClassifyName, req.ClassifyId)
  203. if er != nil && er.Error() != utils.ErrNoRow() {
  204. msg = "移动失败"
  205. err = er
  206. return
  207. }
  208. if exists != nil {
  209. msg = "移动失败,分类名称已存在"
  210. err = errors.New("classify name repeat")
  211. return
  212. }
  213. updateCol := make([]string, 0)
  214. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  215. var parentRootId int
  216. if classifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  217. parentClassify, er := edbmonitor.GetEdbMonitorClassifyById(req.ParentClassifyId)
  218. if er != nil {
  219. err = er
  220. if er.Error() == utils.ErrNoRow() {
  221. msg = "父级分类不存在,请刷新页面"
  222. return
  223. }
  224. msg = "移动失败"
  225. return
  226. }
  227. if parentClassify.Level > 2 {
  228. msg = "移动失败,不能移动到子分类"
  229. err = errors.New("move to child classify")
  230. return
  231. }
  232. if parentClassify.Level+1 != classifyInfo.Level {
  233. msg = "移动失败,只能移动到同一层级的分类"
  234. err = errors.New("move to child classify")
  235. return
  236. }
  237. parentRootId = parentClassify.RootId
  238. classifyInfo.ParentId = parentClassify.ClassifyId
  239. classifyInfo.Level = parentClassify.Level + 1
  240. updateCol = append(updateCol, "parent_id", "level")
  241. }
  242. //如果有传入 上一个兄弟节点分类id
  243. if req.PrevClassifyId > 0 {
  244. //上一个兄弟节点
  245. prevClassify, er := edbmonitor.GetEdbMonitorClassifyById(req.PrevClassifyId)
  246. if er != nil {
  247. msg = "移动失败"
  248. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:%w", er)
  249. return
  250. }
  251. //如果是移动在两个兄弟节点之间
  252. if req.NextClassifyId > 0 {
  253. //下一个兄弟节点
  254. nextClassify, er := edbmonitor.GetEdbMonitorClassifyById(req.NextClassifyId)
  255. if er != nil {
  256. msg = "移动失败"
  257. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:%w", er)
  258. return
  259. }
  260. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  261. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == classifyInfo.Sort {
  262. //变更兄弟节点的排序
  263. updateSortStr := `sort + 2`
  264. _ = edbmonitor.UpdateEdbMonitorClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  265. } else {
  266. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  267. if nextClassify.Sort-prevClassify.Sort == 1 {
  268. //变更兄弟节点的排序
  269. updateSortStr := `sort + 1`
  270. _ = edbmonitor.UpdateEdbMonitorClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  271. }
  272. }
  273. }
  274. classifyInfo.Sort = prevClassify.Sort + 1
  275. updateCol = append(updateCol, "Sort")
  276. } else {
  277. firstClassify, er := edbmonitor.GetFirstEdbMonitorClassifyByParentId(classifyInfo.ParentId)
  278. if er != nil && er.Error() != utils.ErrNoRow() {
  279. msg = "移动失败"
  280. err = fmt.Errorf("获取父级分类下的排序第一条的分类信息失败,Err:%w", er)
  281. return
  282. }
  283. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  284. if firstClassify != nil && firstClassify.Sort == 0 {
  285. updateSortStr := ` sort + 1 `
  286. _ = edbmonitor.UpdateEdbMonitorClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
  287. }
  288. classifyInfo.Sort = 0 //那就是排在第一位
  289. updateCol = append(updateCol, "Sort")
  290. }
  291. var classifyIds []int
  292. if classifyInfo.Level == 2 {
  293. classifyIds, er = edbmonitor.GetChildEdbMonitorClassifyIdById(classifyInfo.ClassifyId)
  294. if er != nil {
  295. msg = "分类移动失败"
  296. err = fmt.Errorf("获取子分类id失败 err:%w", er)
  297. return
  298. }
  299. }
  300. if len(updateCol) > 0 {
  301. if parentRootId != 0 {
  302. classifyInfo.RootId = parentRootId
  303. } else {
  304. classifyInfo.RootId = req.ParentClassifyId
  305. }
  306. updateCol = append(updateCol, "root_id")
  307. err = classifyInfo.Update(updateCol, classifyIds)
  308. if err != nil {
  309. msg = "移动失败"
  310. return
  311. }
  312. }
  313. return
  314. }
  315. func generateClassifyTree(classifyList []*edbmonitor.EdbMonitorClassify, parentId int) []*response.EdbMonitorClassifyTree {
  316. res := make([]*response.EdbMonitorClassifyTree, 0)
  317. for _, v := range classifyList {
  318. if v.ParentId == parentId {
  319. tmp := new(response.EdbMonitorClassifyTree)
  320. tmp.ClassifyId = v.ClassifyId
  321. tmp.ClassifyName = v.ClassifyName
  322. tmp.Level = v.Level
  323. tmp.Children = generateClassifyTree(classifyList, v.ClassifyId)
  324. res = append(res, tmp)
  325. }
  326. }
  327. return res
  328. }