report_chapter.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package services
  2. import (
  3. "eta_gn/eta_api/models"
  4. "eta_gn/eta_api/models/report"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // MoveReportChapter
  10. // @Description: 移动报告章节
  11. // @author: Roc
  12. // @datetime 2024-06-06 09:48:52
  13. // @param req *models.ReportChapterMoveReq
  14. // @return err error
  15. // @return errMsg string
  16. func MoveReportChapter(req *models.ReportChapterMoveReq) (err error, errMsg string) {
  17. ob := new(models.ReportChapter)
  18. reportChapterId := req.ReportChapterId
  19. prevReportChapterId := req.PrevReportChapterId
  20. nextReportChapterId := req.NextReportChapterId
  21. //如果有传入 上一个兄弟节点分类id
  22. var (
  23. reportChapter *models.ReportChapter
  24. prevReportChapter *models.ReportChapter
  25. nextReportChapter *models.ReportChapter
  26. prevSort int
  27. nextSort int
  28. )
  29. // 移动对象为分类, 判断权限
  30. reportChapter, err = ob.GetReportChapterById(reportChapterId)
  31. if err != nil {
  32. if utils.IsErrNoRow(err) {
  33. errMsg = "当前报告章节不存在"
  34. err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
  35. return
  36. }
  37. errMsg = "移动失败"
  38. err = fmt.Errorf("获取章节信息失败,Err:" + err.Error())
  39. return
  40. } else if reportChapter.ReportChapterId == 0 {
  41. errMsg = "当前报告章节不存在"
  42. err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
  43. return
  44. }
  45. if prevReportChapterId > 0 {
  46. prevReportChapter, err = ob.GetReportChapterById(prevReportChapterId)
  47. if err != nil {
  48. errMsg = "移动失败"
  49. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  50. return
  51. }
  52. prevSort = prevReportChapter.Sort
  53. }
  54. if nextReportChapterId > 0 {
  55. //下一个兄弟节点
  56. nextReportChapter, err = ob.GetReportChapterById(nextReportChapterId)
  57. if err != nil {
  58. errMsg = "移动失败"
  59. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  60. return
  61. }
  62. nextSort = nextReportChapter.Sort
  63. }
  64. err, errMsg = moveReportChapter(reportChapter, prevReportChapter, nextReportChapter, reportChapter.ReportId, prevSort, nextSort)
  65. return
  66. }
  67. // moveReportChapter
  68. // @Description: 移动章节分类
  69. // @author: Roc
  70. // @datetime 2024-06-06 09:48:46
  71. // @param reportChapter *models.ReportChapter
  72. // @param prevReportChapter *models.ReportChapter
  73. // @param nextReportChapter *models.ReportChapter
  74. // @param reportId int
  75. // @param prevSort int
  76. // @param nextSort int
  77. // @return err error
  78. // @return errMsg string
  79. func moveReportChapter(reportChapter, prevReportChapter, nextReportChapter *models.ReportChapter, reportId int, prevSort, nextSort int) (err error, errMsg string) {
  80. ob := new(models.ReportChapter)
  81. updateCol := make([]string, 0)
  82. if prevSort > 0 {
  83. //如果是移动在两个兄弟节点之间
  84. if nextSort > 0 {
  85. //下一个兄弟节点
  86. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  87. if prevSort == nextSort || prevSort == reportChapter.Sort {
  88. //变更兄弟节点的排序
  89. updateSortStr := `sort + 2`
  90. //变更分类
  91. if prevReportChapter != nil {
  92. _ = ob.UpdateReportChapterSortByReportId(reportId, prevReportChapter.ReportChapterId, prevReportChapter.Sort, updateSortStr)
  93. } else {
  94. _ = ob.UpdateReportChapterSortByReportId(reportId, 0, prevSort, updateSortStr)
  95. }
  96. } else {
  97. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  98. if nextSort-prevSort == 1 {
  99. //变更兄弟节点的排序
  100. updateSortStr := `sort + 1`
  101. //变更分类
  102. if prevReportChapter != nil {
  103. _ = ob.UpdateReportChapterSortByReportId(reportId, prevReportChapter.ReportChapterId, prevSort, updateSortStr)
  104. } else {
  105. _ = ob.UpdateReportChapterSortByReportId(reportId, 0, prevSort, updateSortStr)
  106. }
  107. }
  108. }
  109. }
  110. reportChapter.Sort = prevSort + 1
  111. reportChapter.ModifyTime = time.Now()
  112. updateCol = append(updateCol, "Sort", "ModifyTime")
  113. } else if prevReportChapter == nil && nextReportChapter == nil {
  114. //处理只拖动到目录里,默认放到目录底部的情况
  115. var maxSort int
  116. maxSort, err = ob.GetMaxSortByReportId(reportId)
  117. if err != nil {
  118. errMsg = "移动失败"
  119. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  120. return
  121. }
  122. reportChapter.Sort = maxSort + 1 //那就是排在组内最后一位
  123. reportChapter.ModifyTime = time.Now()
  124. updateCol = append(updateCol, "Sort", "ModifyTime")
  125. } else {
  126. // 拖动到父级分类的第一位
  127. firstReportChapter, tmpErr := ob.GetFirstReportChapterByReportId(reportId)
  128. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  129. errMsg = "移动失败"
  130. err = fmt.Errorf("获取当前报告下,且排序数相同 的排序第一条的数据失败,Err:" + tmpErr.Error())
  131. return
  132. }
  133. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  134. if firstReportChapter != nil && firstReportChapter.ReportChapterId > 0 && firstReportChapter.Sort == 0 {
  135. updateSortStr := ` sort + 1 `
  136. _ = ob.UpdateReportChapterSortByReportId(reportId, firstReportChapter.ReportChapterId-1, 0, updateSortStr)
  137. }
  138. reportChapter.Sort = 0 //那就是排在第一位
  139. reportChapter.ModifyTime = time.Now()
  140. updateCol = append(updateCol, "Sort", "ModifyTime")
  141. }
  142. //更新
  143. if len(updateCol) > 0 {
  144. err = reportChapter.UpdateChapter(updateCol)
  145. if err != nil {
  146. errMsg = "移动失败"
  147. err = fmt.Errorf("修改失败,Err:" + err.Error())
  148. return
  149. }
  150. }
  151. return
  152. }
  153. // CheckChapterAuthByAdminIdList
  154. // @Description: 根据管理员id列表,判断当前用户是否有章节权限
  155. // @author: Roc
  156. // @datetime 2024-06-13 11:03:10
  157. // @param adminId int
  158. // @param createAdminId int
  159. // @param grantAdminIdList []int
  160. // @return isAuth bool
  161. func CheckChapterAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
  162. // 如果是自己创建的报告,那么就有权限
  163. if adminId == createAdminId {
  164. isAuth = true
  165. return
  166. }
  167. // 如果是授权用户,那么就有权限
  168. if utils.IsCheckInList(grantAdminIdList, adminId) {
  169. isAuth = true
  170. return
  171. }
  172. return
  173. }
  174. // CheckChapterAuthByReportChapterInfo
  175. // @Description: 根据报告章节信息,判断当前用户是否有章节权限
  176. // @author: Roc
  177. // @datetime 2024-06-13 11:10:46
  178. // @param adminId int
  179. // @param createAdminId int
  180. // @param reportChapterInfo *models.ReportChapter
  181. // @return isAuth bool
  182. // @return err error
  183. func CheckChapterAuthByReportChapterInfo(adminId, createAdminId int, reportChapterInfo *models.ReportChapter) (isAuth bool, err error) {
  184. // 如果是自己创建的报告,那么就有权限
  185. if adminId == createAdminId {
  186. isAuth = true
  187. return
  188. }
  189. chapterGrantObj := report.ReportChapterGrant{}
  190. chapterGrantList, err := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
  191. if err != nil {
  192. return
  193. }
  194. for _, v := range chapterGrantList {
  195. if v.AdminId == adminId {
  196. isAuth = true
  197. return
  198. }
  199. }
  200. return
  201. }