english_classify.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package services
  2. import (
  3. "eta/eta_api/models"
  4. "eta/eta_api/models/report_approve"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // MoveEnglishReportClassify 移动分类
  10. func MoveEnglishReportClassify(req models.EnglishClassifyMoveReq) (err error, errMsg string) {
  11. classifyId := req.ClassifyId
  12. prevClassifyId := req.PrevClassifyId
  13. nextClassifyId := req.NextClassifyId
  14. //如果有传入 上一个兄弟节点分类id
  15. var (
  16. classifyInfo *models.EnglishClassify
  17. prevClassify *models.EnglishClassify
  18. nextClassify *models.EnglishClassify
  19. prevSort int
  20. nextSort int
  21. )
  22. // 移动对象为分类, 判断权限
  23. classifyInfo, err = models.GetEnglishReportClassifyById(classifyId)
  24. if err != nil {
  25. if err.Error() == utils.ErrNoRow() {
  26. errMsg = "分类不存在, 请刷新页面"
  27. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  28. return
  29. }
  30. errMsg = "移动失败"
  31. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  32. return
  33. } else if classifyInfo.Id == 0 {
  34. errMsg = "分类不存在, 请刷新页面"
  35. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  36. return
  37. }
  38. parentClassifyId := classifyInfo.ParentId
  39. if prevClassifyId > 0 {
  40. prevClassify, err = models.GetEnglishReportClassifyById(prevClassifyId)
  41. if err != nil {
  42. if err.Error() == utils.ErrNoRow() {
  43. errMsg = "上一个分类不存在, 请刷新页面"
  44. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  45. return
  46. }
  47. errMsg = "移动失败"
  48. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  49. return
  50. }
  51. prevSort = prevClassify.Sort
  52. }
  53. if nextClassifyId > 0 {
  54. //下一个兄弟节点
  55. nextClassify, err = models.GetEnglishReportClassifyById(nextClassifyId)
  56. if err != nil {
  57. if err.Error() == utils.ErrNoRow() {
  58. errMsg = "下一个分类不存在, 请刷新页面"
  59. err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
  60. return
  61. }
  62. errMsg = "移动失败"
  63. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  64. return
  65. }
  66. nextSort = nextClassify.Sort
  67. }
  68. err, errMsg = moveEnglishReportClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort)
  69. return
  70. }
  71. // moveEnglishReportClassify 移动分类
  72. func moveEnglishReportClassify(classifyInfo, prevClassify, nextClassify *models.EnglishClassify, parentId, prevSort, nextSort int) (err error, errMsg string) {
  73. ob := new(models.EnglishClassify)
  74. updateCol := make([]string, 0)
  75. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  76. if classifyInfo.ParentId != parentId {
  77. errMsg = "移动失败"
  78. err = fmt.Errorf("不支持目录层级变更")
  79. return
  80. }
  81. if prevSort > 0 {
  82. //如果是移动在两个兄弟节点之间
  83. if nextSort > 0 {
  84. //下一个兄弟节点
  85. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  86. if prevSort == nextSort || prevSort == classifyInfo.Sort {
  87. //变更兄弟节点的排序
  88. updateSortStr := `sort + 2`
  89. //变更分类
  90. if prevClassify != nil {
  91. _ = models.UpdateEnglishClassifySortByParentId(parentId, prevClassify.Id, prevClassify.Sort, updateSortStr)
  92. } else {
  93. _ = models.UpdateEnglishClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  94. }
  95. } else {
  96. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  97. if nextSort-prevSort == 1 {
  98. //变更兄弟节点的排序
  99. updateSortStr := `sort + 1`
  100. //变更分类
  101. if prevClassify != nil {
  102. _ = models.UpdateEnglishClassifySortByParentId(parentId, prevClassify.Id, prevSort, updateSortStr)
  103. } else {
  104. _ = models.UpdateEnglishClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
  105. }
  106. }
  107. }
  108. }
  109. classifyInfo.Sort = prevSort + 1
  110. classifyInfo.ModifyTime = time.Now()
  111. updateCol = append(updateCol, "Sort", "ModifyTime")
  112. } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
  113. //处理只拖动到目录里,默认放到目录底部的情况
  114. var maxSort int
  115. maxSort, err = ob.GetMaxSortByParentId(parentId)
  116. if err != nil {
  117. errMsg = "移动失败"
  118. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  119. return
  120. }
  121. classifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  122. classifyInfo.ModifyTime = time.Now()
  123. updateCol = append(updateCol, "Sort", "ModifyTime")
  124. } else {
  125. // 拖动到父级分类的第一位
  126. firstPermission, tmpErr := ob.GetFirstClassifyByParentId(parentId)
  127. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  128. errMsg = "移动失败"
  129. err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  130. return
  131. }
  132. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  133. if firstPermission != nil && firstPermission.Id != 0 && firstPermission.Sort == 0 {
  134. updateSortStr := ` sort + 1 `
  135. _ = models.UpdateEnglishClassifySortByParentId(parentId, firstPermission.Id-1, 0, updateSortStr)
  136. }
  137. classifyInfo.Sort = 0 //那就是排在第一位
  138. classifyInfo.ModifyTime = time.Now()
  139. updateCol = append(updateCol, "Sort", "ModifyTime")
  140. }
  141. //更新
  142. if len(updateCol) > 0 {
  143. err = classifyInfo.UpdateEnglishClassify(updateCol)
  144. if err != nil {
  145. errMsg = "移动失败"
  146. err = fmt.Errorf("修改失败,Err:" + err.Error())
  147. return
  148. }
  149. }
  150. return
  151. }
  152. func MarkEnableDeleteEnlishClassify(rootClassifyList, childClassifyList []*models.EnglishClassifyList) (err error) {
  153. if len(rootClassifyList) == 0 && len(childClassifyList) == 0 {
  154. return
  155. }
  156. // 合并分类列表
  157. allClassifyList := append(rootClassifyList, childClassifyList...)
  158. classifyIds := make([]int, len(allClassifyList))
  159. rootClassifyMap := make(map[int]*models.EnglishClassifyList)
  160. for i, v := range allClassifyList {
  161. classifyIds[i] = v.Id
  162. if v.RootId == v.Id {
  163. rootClassifyMap[v.Id] = v
  164. }
  165. }
  166. // 检查是否有报告
  167. reportList, err := models.GetExistEnglishReportClassifyIdByClassifyIds(classifyIds)
  168. if err != nil {
  169. return
  170. }
  171. // 查询该分类是否关联了审批流
  172. flowOb := new(report_approve.ReportApproveFlow)
  173. flowCond := fmt.Sprintf(` AND %s = ? `, report_approve.ReportApproveFlowCols.ReportType)
  174. flowPars := make([]interface{}, 0)
  175. flowPars = append(flowPars, report_approve.FlowReportTypeEnglish)
  176. flowList, err := flowOb.GetItemsByCondition(flowCond, flowPars, []string{"classify_first_id", "classify_second_id", "classify_third_id"}, "")
  177. if err != nil {
  178. return
  179. }
  180. // 查询路演视频分类
  181. videoClassify, err := models.GetAllEnglishVideoClassify()
  182. if err != nil {
  183. return
  184. }
  185. disableDeleteClassifyMap := make(map[int]struct{})
  186. // 记录关联报告的分类
  187. for _, v := range reportList {
  188. if v.ClassifyIdFirst > 0 {
  189. disableDeleteClassifyMap[v.ClassifyIdFirst] = struct{}{}
  190. }
  191. if v.ClassifyIdSecond > 0 {
  192. disableDeleteClassifyMap[v.ClassifyIdSecond] = struct{}{}
  193. }
  194. }
  195. // 记录关联审批流的分类
  196. for _, v := range flowList {
  197. if v.ClassifyFirstId > 0 {
  198. disableDeleteClassifyMap[v.ClassifyFirstId] = struct{}{}
  199. }
  200. if v.ClassifySecondId > 0 {
  201. disableDeleteClassifyMap[v.ClassifySecondId] = struct{}{}
  202. }
  203. if v.ClassifyThirdId > 0 {
  204. disableDeleteClassifyMap[v.ClassifyThirdId] = struct{}{}
  205. }
  206. }
  207. for _, v := range videoClassify {
  208. if v.ClassifyIdFirst > 0 {
  209. disableDeleteClassifyMap[v.Id] = struct{}{}
  210. }
  211. if v.ClassifyIdSecond > 0 {
  212. disableDeleteClassifyMap[v.Id] = struct{}{}
  213. }
  214. }
  215. markedClassifyMap := make(map[int]struct{})
  216. for _, v := range allClassifyList {
  217. if _, ok := markedClassifyMap[v.Id]; ok {
  218. continue
  219. }
  220. if _, ok := disableDeleteClassifyMap[v.Id]; !ok {
  221. v.IsEnableDelete = 1
  222. } else {
  223. rootClassifyMap[v.RootId].IsEnableDelete = 0
  224. markedClassifyMap[v.RootId] = struct{}{}
  225. }
  226. markedClassifyMap[v.Id] = struct{}{}
  227. }
  228. return
  229. }