ppt_report.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // 更新分类报告计数加个锁
  31. var classifyReportNumLock sync.Mutex
  32. // UpdateClassifyReportNum 更新分类报告计数
  33. func UpdateClassifyReportNum(classifyId int) (err error) {
  34. classifyReportNumLock.Lock()
  35. defer func() {
  36. if err != nil {
  37. utils.FileLog.Info(fmt.Sprintf("更新分类报告计数失败, %v", err))
  38. }
  39. classifyReportNumLock.Unlock()
  40. }()
  41. classifyItem, e := models.GetClassifyById(classifyId)
  42. if e != nil {
  43. err = fmt.Errorf("获取分类失败, %v", e)
  44. return
  45. }
  46. // 更新分类报告数
  47. var total int
  48. {
  49. reportOb := new(models.Report)
  50. var cond string
  51. switch classifyItem.Level {
  52. case 1:
  53. cond += ` AND classify_id_first = ?`
  54. case 2:
  55. cond += ` AND classify_id_second = ?`
  56. case 3:
  57. cond += ` AND classify_id_third = ?`
  58. }
  59. pars := make([]interface{}, 0)
  60. pars = append(pars, classifyId)
  61. count, e := reportOb.GetCountByCondition(cond, pars)
  62. if e != nil {
  63. err = fmt.Errorf("获取报告计数失败, %v", e)
  64. return
  65. }
  66. total += count
  67. }
  68. {
  69. pptOb := new(models.PptV2)
  70. cond := ` AND classify_id = ?`
  71. pars := make([]interface{}, 0)
  72. pars = append(pars, classifyId)
  73. count, e := pptOb.GetCountByCondition(cond, pars)
  74. if e != nil {
  75. err = fmt.Errorf("获取PPT报告计数失败, %v", e)
  76. return
  77. }
  78. total += count
  79. }
  80. classifyItem.ReportNum = total
  81. if e = classifyItem.UpdateClassify([]string{"ReportNum"}); e != nil {
  82. err = fmt.Errorf("更新分类报告计数失败, %v", e)
  83. return
  84. }
  85. // 获取所有分类, 更新父级, 无父级忽略
  86. if classifyItem.ParentId <= 0 {
  87. return
  88. }
  89. classifyOb := new(models.Classify)
  90. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  91. if e != nil {
  92. err = fmt.Errorf("获取报告列表失败, %v", e)
  93. return
  94. }
  95. classifyIdMap := make(map[int]*models.Classify)
  96. classifyParentChildMap := make(map[int][]*models.Classify)
  97. for _, v := range classifies {
  98. classifyIdMap[v.Id] = v
  99. if v.ParentId <= 0 {
  100. continue
  101. }
  102. if classifyParentChildMap[v.ParentId] == nil {
  103. classifyParentChildMap[v.ParentId] = make([]*models.Classify, 0)
  104. }
  105. classifyParentChildMap[v.ParentId] = append(classifyParentChildMap[v.ParentId], v)
  106. }
  107. // 递归获取需要更新的父级报告数
  108. updateClassifies := CountParentClassifyReportNumRecursive(classifies, classifyItem.ParentId, classifyIdMap, classifyParentChildMap)
  109. if len(updateClassifies) == 0 {
  110. return
  111. }
  112. for _, v := range updateClassifies {
  113. if e = v.UpdateClassify([]string{"ReportNum"}); e != nil {
  114. err = fmt.Errorf("更新父级分类报告计数失败, %v", e)
  115. return
  116. }
  117. }
  118. return
  119. }
  120. // CountParentClassifyReportNumRecursive 递归统计父级分类报告数
  121. func CountParentClassifyReportNumRecursive(list []*models.Classify, parentId int, classifyIdMap map[int]*models.Classify, classifyParentChildMap map[int][]*models.Classify) []*models.Classify {
  122. res := make([]*models.Classify, 0)
  123. for _, v := range list {
  124. // 找父级
  125. if v.Id != parentId {
  126. continue
  127. }
  128. parentItem := classifyIdMap[v.Id]
  129. if parentItem == nil {
  130. break
  131. }
  132. // 合计所有直系子分类报告数
  133. children := classifyParentChildMap[v.Id]
  134. if len(children) == 0 {
  135. break
  136. }
  137. var t int
  138. for _, child := range children {
  139. t += child.ReportNum
  140. }
  141. parentItem.ReportNum = t
  142. res = append(res, parentItem)
  143. // 继续向上统计父级
  144. if v.ParentId <= 0 {
  145. break
  146. }
  147. parents := CountParentClassifyReportNumRecursive(list, v.ParentId, classifyIdMap, classifyParentChildMap)
  148. if len(parents) == 0 {
  149. break
  150. }
  151. res = append(res, parents...)
  152. }
  153. return res
  154. }