report_chapter.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. func MoveReportChapter(req *models.ReportChapterMoveReq) (err error, errMsg string) {
  10. ob := new(models.ReportChapter)
  11. reportChapterId := req.ReportChapterId
  12. prevReportChapterId := req.PrevReportChapterId
  13. nextReportChapterId := req.NextReportChapterId
  14. var (
  15. reportChapter *models.ReportChapter
  16. prevReportChapter *models.ReportChapter
  17. nextReportChapter *models.ReportChapter
  18. prevSort int
  19. nextSort int
  20. )
  21. reportChapter, err = ob.GetReportChapterById(reportChapterId)
  22. if err != nil {
  23. if utils.IsErrNoRow(err) {
  24. errMsg = "当前报告章节不存在"
  25. err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
  26. return
  27. }
  28. errMsg = "移动失败"
  29. err = fmt.Errorf("获取章节信息失败,Err:" + err.Error())
  30. return
  31. } else if reportChapter.ReportChapterId == 0 {
  32. errMsg = "当前报告章节不存在"
  33. err = fmt.Errorf("获取报告章节信息失败,Err:" + err.Error())
  34. return
  35. }
  36. if prevReportChapterId > 0 {
  37. prevReportChapter, err = ob.GetReportChapterById(prevReportChapterId)
  38. if err != nil {
  39. errMsg = "移动失败"
  40. err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  41. return
  42. }
  43. prevSort = prevReportChapter.Sort
  44. }
  45. if nextReportChapterId > 0 {
  46. nextReportChapter, err = ob.GetReportChapterById(nextReportChapterId)
  47. if err != nil {
  48. errMsg = "移动失败"
  49. err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  50. return
  51. }
  52. nextSort = nextReportChapter.Sort
  53. }
  54. err, errMsg = moveReportChapter(reportChapter, prevReportChapter, nextReportChapter, reportChapter.ReportId, prevSort, nextSort)
  55. return
  56. }
  57. func moveReportChapter(reportChapter, prevReportChapter, nextReportChapter *models.ReportChapter, reportId int, prevSort, nextSort int) (err error, errMsg string) {
  58. ob := new(models.ReportChapter)
  59. updateCol := make([]string, 0)
  60. if prevSort > 0 {
  61. if nextSort > 0 {
  62. if prevSort == nextSort || prevSort == reportChapter.Sort {
  63. updateSortStr := `sort + 2`
  64. if prevReportChapter != nil {
  65. _ = ob.UpdateReportChapterSortByReportId(reportId, prevReportChapter.ReportChapterId, prevReportChapter.Sort, updateSortStr)
  66. } else {
  67. _ = ob.UpdateReportChapterSortByReportId(reportId, 0, prevSort, updateSortStr)
  68. }
  69. } else {
  70. if nextSort-prevSort == 1 {
  71. updateSortStr := `sort + 1`
  72. if prevReportChapter != nil {
  73. _ = ob.UpdateReportChapterSortByReportId(reportId, prevReportChapter.ReportChapterId, prevSort, updateSortStr)
  74. } else {
  75. _ = ob.UpdateReportChapterSortByReportId(reportId, 0, prevSort, updateSortStr)
  76. }
  77. }
  78. }
  79. }
  80. reportChapter.Sort = prevSort + 1
  81. reportChapter.ModifyTime = time.Now()
  82. updateCol = append(updateCol, "Sort", "ModifyTime")
  83. } else if prevReportChapter == nil && nextReportChapter == nil {
  84. var maxSort int
  85. maxSort, err = ob.GetMaxSortByReportId(reportId)
  86. if err != nil {
  87. errMsg = "移动失败"
  88. err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
  89. return
  90. }
  91. reportChapter.Sort = maxSort + 1 //那就是排在组内最后一位
  92. reportChapter.ModifyTime = time.Now()
  93. updateCol = append(updateCol, "Sort", "ModifyTime")
  94. } else {
  95. firstReportChapter, tmpErr := ob.GetFirstReportChapterByReportId(reportId)
  96. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  97. errMsg = "移动失败"
  98. err = fmt.Errorf("获取当前报告下,且排序数相同 的排序第一条的数据失败,Err:" + tmpErr.Error())
  99. return
  100. }
  101. if firstReportChapter != nil && firstReportChapter.ReportChapterId > 0 && firstReportChapter.Sort == 0 {
  102. updateSortStr := ` sort + 1 `
  103. _ = ob.UpdateReportChapterSortByReportId(reportId, firstReportChapter.ReportChapterId-1, 0, updateSortStr)
  104. }
  105. reportChapter.Sort = 0 //那就是排在第一位
  106. reportChapter.ModifyTime = time.Now()
  107. updateCol = append(updateCol, "Sort", "ModifyTime")
  108. }
  109. if len(updateCol) > 0 {
  110. err = reportChapter.UpdateChapter(updateCol)
  111. if err != nil {
  112. errMsg = "移动失败"
  113. err = fmt.Errorf("修改失败,Err:" + err.Error())
  114. return
  115. }
  116. }
  117. return
  118. }
  119. func CheckChapterAuthByAdminIdList(adminId, createAdminId int, grantAdminIdList []int) (isAuth bool) {
  120. if adminId == createAdminId {
  121. isAuth = true
  122. return
  123. }
  124. if utils.IsCheckInList(grantAdminIdList, adminId) {
  125. isAuth = true
  126. return
  127. }
  128. return
  129. }
  130. func CheckChapterAuthByReportChapterInfo(adminId, createAdminId int, reportChapterInfo *models.ReportChapter) (isAuth bool, err error) {
  131. if adminId == createAdminId {
  132. isAuth = true
  133. return
  134. }
  135. chapterGrantObj := report.ReportChapterGrant{}
  136. chapterGrantList, err := chapterGrantObj.GetGrantListById(reportChapterInfo.ReportChapterId)
  137. if err != nil {
  138. return
  139. }
  140. for _, v := range chapterGrantList {
  141. if v.AdminId == adminId {
  142. isAuth = true
  143. return
  144. }
  145. }
  146. return
  147. }