move_interface.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package _interface
  2. import (
  3. "errors"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/utils"
  6. "time"
  7. )
  8. type SingleNodeMoveInterface interface {
  9. GetNodeInfoById(nodeId int) (nodeInfo *models.NodeInfo, err error)
  10. UpdateNodeInfoSortByParentIdAndSource(parentNodeId, nodeId, prevNodeSort int, updateSortStr string, nodeType int) (err error)
  11. GetNodeMaxSort(parentId, nodeType int) (maxSort int, err error)
  12. GetFirstNodeInfoByParentId(parentId int) (nodeInfo *models.NodeInfo, err error)
  13. }
  14. // MoveSingleNode 移动节点
  15. func MoveSingleNode(sm SingleNodeMoveInterface, req models.SingleMoveNodeReq) (newNodeInfo *models.NodeInfo, updateCol []string, err error, errMsg string) {
  16. nodeId := req.NodeId
  17. parentNodeId := req.ParentNodeId
  18. prevNodeId := req.PrevNodeId
  19. nextNodeId := req.NextNodeId
  20. source := req.NodeType
  21. //首先确定移动的对象是分类还是指标
  22. //判断上一个节点是分类还是指标
  23. //判断下一个节点是分类还是指标
  24. //同时更新分类目录下的分类sort和指标sort
  25. //更新当前移动的分类或者指标sort
  26. var parentChartNodeInfo *models.NodeInfo
  27. if parentNodeId > 0 {
  28. parentChartNodeInfo, err = sm.GetNodeInfoById(parentNodeId)
  29. if err != nil {
  30. errMsg = "移动失败"
  31. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  32. return
  33. }
  34. }
  35. //如果有传入 上一个兄弟节点分类id
  36. var (
  37. //nodeInfo *models.NodeInfo
  38. prevNode *models.NodeInfo
  39. nextNode *models.NodeInfo
  40. prevSort int
  41. nextSort int
  42. )
  43. // 移动对象为分类
  44. nodeInfo, err := sm.GetNodeInfoById(nodeId)
  45. if err != nil {
  46. if err.Error() == utils.ErrNoRow() {
  47. errMsg = "当前分类不存在"
  48. err = errors.New("获取分类信息失败,Err:" + err.Error())
  49. return
  50. }
  51. errMsg = "移动失败"
  52. err = errors.New("获取分类信息失败,Err:" + err.Error())
  53. return
  54. }
  55. if prevNodeId > 0 {
  56. prevNode, err = sm.GetNodeInfoById(prevNodeId)
  57. if err != nil {
  58. errMsg = "移动失败"
  59. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  60. return
  61. }
  62. prevSort = prevNode.Sort
  63. }
  64. if nextNodeId > 0 {
  65. //下一个兄弟节点
  66. nextNode, err = sm.GetNodeInfoById(nextNodeId)
  67. if err != nil {
  68. errMsg = "移动失败"
  69. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  70. return
  71. }
  72. nextSort = nextNode.Sort
  73. }
  74. newNodeInfo, updateCol, err, errMsg = moveSingleNode(sm, parentChartNodeInfo, nodeInfo, prevNode, nextNode, parentNodeId, prevSort, nextSort, source)
  75. return
  76. }
  77. // moveSingleNode 移动节点
  78. func moveSingleNode(sm SingleNodeMoveInterface, parentChartNodeInfo, nodeInfo, prevNode, nextNode *models.NodeInfo, parentNodeId, prevSort, nextSort, source int) (newNodeInfo *models.NodeInfo, updateCol []string, err error, errMsg string) {
  79. updateCol = make([]string, 0)
  80. newNodeInfo = nodeInfo
  81. // 移动对象为分类, 判断分类是否存在
  82. oldParentId := nodeInfo.ParentId
  83. //oldLevel := nodeInfo.Level
  84. if oldParentId != parentNodeId {
  85. // todo 更新子分类对应的level
  86. }
  87. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  88. if nodeInfo.ParentId != parentNodeId && parentNodeId != 0 {
  89. if nodeInfo.Level != parentChartNodeInfo.Level+1 { //禁止层级调整
  90. errMsg = "不支持目录层级变更"
  91. err = errors.New("不支持目录层级变更")
  92. return
  93. }
  94. newNodeInfo.ParentId = parentChartNodeInfo.NodeId
  95. //nodeInfo.RootId = parentChartNodeInfo.RootId
  96. newNodeInfo.Level = parentChartNodeInfo.Level + 1
  97. newNodeInfo.ModifyTime = time.Now()
  98. //updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  99. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  100. } else if nodeInfo.ParentId != parentNodeId && parentNodeId == 0 {
  101. errMsg = "不支持目录层级变更"
  102. err = errors.New("不支持目录层级变更")
  103. return
  104. }
  105. if prevSort > 0 {
  106. //如果是移动在两个兄弟节点之间
  107. if nextSort > 0 {
  108. //下一个兄弟节点
  109. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  110. if prevSort == nextSort || prevSort == nodeInfo.Sort {
  111. //变更兄弟节点的排序
  112. updateSortStr := `sort + 2`
  113. //变更分类
  114. if prevNode != nil {
  115. _ = sm.UpdateNodeInfoSortByParentIdAndSource(parentNodeId, prevNode.NodeId, prevNode.Sort, updateSortStr, source)
  116. } else {
  117. _ = sm.UpdateNodeInfoSortByParentIdAndSource(parentNodeId, 0, prevSort, updateSortStr, source)
  118. }
  119. } else {
  120. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  121. if nextSort-prevSort == 1 {
  122. //变更兄弟节点的排序
  123. updateSortStr := `sort + 1`
  124. //变更分类
  125. if prevNode != nil {
  126. _ = sm.UpdateNodeInfoSortByParentIdAndSource(parentNodeId, prevNode.NodeId, prevSort, updateSortStr, source)
  127. } else {
  128. _ = sm.UpdateNodeInfoSortByParentIdAndSource(parentNodeId, 0, prevSort, updateSortStr, source)
  129. }
  130. }
  131. }
  132. }
  133. newNodeInfo.Sort = prevSort + 1
  134. newNodeInfo.ModifyTime = time.Now()
  135. updateCol = append(updateCol, "Sort", "ModifyTime")
  136. } else if prevNode == nil && nextNode == nil && parentNodeId > 0 {
  137. //处理只拖动到目录里,默认放到目录底部的情况
  138. var maxSort int
  139. maxSort, err = sm.GetNodeMaxSort(parentNodeId, source)
  140. if err != nil {
  141. errMsg = "移动失败"
  142. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  143. return
  144. }
  145. newNodeInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  146. newNodeInfo.ModifyTime = time.Now()
  147. updateCol = append(updateCol, "Sort", "ModifyTime")
  148. } else {
  149. // 拖动到父级分类的第一位
  150. firstClassify, tmpErr := sm.GetFirstNodeInfoByParentId(parentNodeId)
  151. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  152. errMsg = "移动失败"
  153. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  154. return
  155. }
  156. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  157. if firstClassify != nil && firstClassify.Sort == 0 {
  158. updateSortStr := ` sort + 1 `
  159. _ = sm.UpdateNodeInfoSortByParentIdAndSource(parentNodeId, firstClassify.NodeId-1, 0, updateSortStr, source)
  160. }
  161. newNodeInfo.Sort = 0 //那就是排在第一位
  162. newNodeInfo.ModifyTime = time.Now()
  163. updateCol = append(updateCol, "Sort", "ModifyTime")
  164. }
  165. // todo 更新层级
  166. if len(updateCol) > 0 {
  167. /*err = nodeInfo.Update(updateCol)
  168. if err != nil {
  169. errMsg = "移动失败"
  170. err = errors.New("修改失败,Err:" + err.Error())
  171. return
  172. }*/
  173. // todo 更新对应分类的root_id和层级
  174. }
  175. return
  176. }