ppt_report.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package services
  2. import (
  3. "eta_gn/eta_api/models"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "sync"
  7. )
  8. // GetPptReportClassifyTreeRecursive 递归获取ppt报告分类树
  9. func GetPptReportClassifyTreeRecursive(list []*models.Classify, parentId int, classifyPpt map[int][]*models.PptReportItem) []*models.PptReportClassifyItem {
  10. res := make([]*models.PptReportClassifyItem, 0)
  11. for _, v := range list {
  12. if v.ParentId == parentId {
  13. t := new(models.PptReportClassifyItem)
  14. t.Id = v.Id
  15. t.ClassifyName = v.ClassifyName
  16. t.Sort = v.Sort
  17. t.ParentId = v.ParentId
  18. t.Enabled = v.Enabled
  19. t.Level = v.Level
  20. t.HasChild = v.HasChild
  21. t.ClassifyType = v.ClassifyType
  22. t.ReportNum = v.ReportNum
  23. t.PptList = classifyPpt[v.Id]
  24. t.Child = GetPptReportClassifyTreeRecursive(list, v.Id, classifyPpt)
  25. res = append(res, t)
  26. }
  27. }
  28. return res
  29. }
  30. func RecursiveFilterPptNoChildTreeClassify(list []*models.PptReportClassifyItem) []*models.PptReportClassifyItem {
  31. res := make([]*models.PptReportClassifyItem, 0)
  32. for _, v := range list {
  33. v.Child = RecursiveFilterPptNoChildTreeClassify(v.Child)
  34. if len(v.Child) == 0 && v.HasChild == 1 {
  35. continue
  36. }
  37. if len(v.Child) == 0 {
  38. v.Child = nil
  39. }
  40. res = append(res, v)
  41. }
  42. return res
  43. }
  44. // 更新分类报告计数加个锁
  45. var classifyReportNumLock sync.Mutex
  46. // UpdateClassifyReportNum 更新分类报告计数
  47. func UpdateClassifyReportNum(classifyId int) (err error) {
  48. classifyReportNumLock.Lock()
  49. defer func() {
  50. if err != nil {
  51. utils.FileLog.Info(fmt.Sprintf("更新分类报告计数失败, %v", err))
  52. }
  53. classifyReportNumLock.Unlock()
  54. }()
  55. classifyItem, e := models.GetClassifyById(classifyId)
  56. if e != nil {
  57. err = fmt.Errorf("获取分类失败, %v", e)
  58. return
  59. }
  60. // 更新分类报告数
  61. var total int
  62. {
  63. reportOb := new(models.Report)
  64. var cond string
  65. switch classifyItem.Level {
  66. case 1:
  67. cond += ` AND classify_id_first = ?`
  68. case 2:
  69. cond += ` AND classify_id_second = ?`
  70. case 3:
  71. cond += ` AND classify_id_third = ?`
  72. }
  73. pars := make([]interface{}, 0)
  74. pars = append(pars, classifyId)
  75. count, e := reportOb.GetCountByCondition(cond, pars)
  76. if e != nil {
  77. err = fmt.Errorf("获取报告计数失败, %v", e)
  78. return
  79. }
  80. total += count
  81. }
  82. {
  83. pptOb := new(models.PptV2)
  84. cond := ` AND classify_id = ?`
  85. pars := make([]interface{}, 0)
  86. pars = append(pars, classifyId)
  87. count, e := pptOb.GetCountByCondition(cond, pars)
  88. if e != nil {
  89. err = fmt.Errorf("获取PPT报告计数失败, %v", e)
  90. return
  91. }
  92. total += count
  93. }
  94. classifyItem.ReportNum = total
  95. if e = classifyItem.UpdateClassify([]string{"ReportNum"}); e != nil {
  96. err = fmt.Errorf("更新分类报告计数失败, %v", e)
  97. return
  98. }
  99. // 获取所有分类, 更新父级, 无父级忽略
  100. if classifyItem.ParentId <= 0 {
  101. return
  102. }
  103. classifyOb := new(models.Classify)
  104. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  105. if e != nil {
  106. err = fmt.Errorf("获取报告列表失败, %v", e)
  107. return
  108. }
  109. classifyIdMap := make(map[int]*models.Classify)
  110. classifyParentChildMap := make(map[int][]*models.Classify)
  111. for _, v := range classifies {
  112. classifyIdMap[v.Id] = v
  113. if v.ParentId <= 0 {
  114. continue
  115. }
  116. if classifyParentChildMap[v.ParentId] == nil {
  117. classifyParentChildMap[v.ParentId] = make([]*models.Classify, 0)
  118. }
  119. classifyParentChildMap[v.ParentId] = append(classifyParentChildMap[v.ParentId], v)
  120. }
  121. // 递归获取需要更新的父级报告数
  122. updateClassifies := CountParentClassifyReportNumRecursive(classifies, classifyItem.ParentId, classifyIdMap, classifyParentChildMap)
  123. if len(updateClassifies) == 0 {
  124. return
  125. }
  126. for _, v := range updateClassifies {
  127. if e = v.UpdateClassify([]string{"ReportNum"}); e != nil {
  128. err = fmt.Errorf("更新父级分类报告计数失败, %v", e)
  129. return
  130. }
  131. }
  132. return
  133. }
  134. // CountParentClassifyReportNumRecursive 递归统计父级分类报告数
  135. func CountParentClassifyReportNumRecursive(list []*models.Classify, parentId int, classifyIdMap map[int]*models.Classify, classifyParentChildMap map[int][]*models.Classify) []*models.Classify {
  136. res := make([]*models.Classify, 0)
  137. for _, v := range list {
  138. // 找父级
  139. if v.Id != parentId {
  140. continue
  141. }
  142. parentItem := classifyIdMap[v.Id]
  143. if parentItem == nil {
  144. break
  145. }
  146. // 合计所有直系子分类报告数
  147. children := classifyParentChildMap[v.Id]
  148. if len(children) == 0 {
  149. break
  150. }
  151. var t int
  152. for _, child := range children {
  153. t += child.ReportNum
  154. }
  155. parentItem.ReportNum = t
  156. res = append(res, parentItem)
  157. // 继续向上统计父级
  158. if v.ParentId <= 0 {
  159. break
  160. }
  161. parents := CountParentClassifyReportNumRecursive(list, v.ParentId, classifyIdMap, classifyParentChildMap)
  162. if len(parents) == 0 {
  163. break
  164. }
  165. res = append(res, parents...)
  166. }
  167. return res
  168. }