date_statis.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package charge
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "time"
  9. model "go-common/app/job/main/growup/model/charge"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. )
  13. // SectionEntries section entries
  14. type SectionEntries struct {
  15. daily []*model.DateStatis
  16. weekly []*model.DateStatis
  17. monthly []*model.DateStatis
  18. }
  19. func initChargeSections(charge, tagID int64, date xtime.Time) []*model.DateStatis {
  20. chargeSections := make([]*model.DateStatis, 12)
  21. chargeSections[0] = initChargeSection(0, 1, 0, charge, tagID, date)
  22. chargeSections[1] = initChargeSection(1, 5, 1, charge, tagID, date)
  23. chargeSections[2] = initChargeSection(5, 10, 2, charge, tagID, date)
  24. chargeSections[3] = initChargeSection(10, 20, 3, charge, tagID, date)
  25. chargeSections[4] = initChargeSection(20, 50, 4, charge, tagID, date)
  26. chargeSections[5] = initChargeSection(50, 100, 5, charge, tagID, date)
  27. chargeSections[6] = initChargeSection(100, 200, 6, charge, tagID, date)
  28. chargeSections[7] = initChargeSection(200, 500, 7, charge, tagID, date)
  29. chargeSections[8] = initChargeSection(500, 1000, 8, charge, tagID, date)
  30. chargeSections[9] = initChargeSection(1000, 3000, 9, charge, tagID, date)
  31. chargeSections[10] = initChargeSection(3000, 5000, 10, charge, tagID, date)
  32. chargeSections[11] = initChargeSection(5000, math.MaxInt32, 11, charge, tagID, date)
  33. return chargeSections
  34. }
  35. func initChargeSection(min, max, section, charge, tagID int64, date xtime.Time) *model.DateStatis {
  36. var tips string
  37. if max == math.MaxInt32 {
  38. tips = fmt.Sprintf("\"%d+\"", min)
  39. } else {
  40. tips = fmt.Sprintf("\"%d~%d\"", min, max)
  41. }
  42. return &model.DateStatis{
  43. MinCharge: min,
  44. MaxCharge: max,
  45. MoneySection: section,
  46. MoneyTips: tips,
  47. Charge: charge,
  48. CategoryID: tagID,
  49. CDate: date,
  50. }
  51. }
  52. func (s *Service) handleDateStatis(c context.Context, sourceCh chan []*model.Archive, date time.Time, table string) (sections []*model.DateStatis, err error) {
  53. // delete
  54. if table != "" {
  55. _, err = s.dao.DelStatisTable(c, table, date.Format(_layout))
  56. if err != nil {
  57. log.Error("s.dao.DelChargeStatisTable error(%v)", err)
  58. return
  59. }
  60. }
  61. // add
  62. sections = s.handleArchives(c, sourceCh, date)
  63. return
  64. }
  65. // HandleAv handle archive_charge_daily_statis, archive_charge_weekly_statis, archive_charge_monthly_statis
  66. func (s *Service) handleArchives(c context.Context, archiveCh chan []*model.Archive, date time.Time) (sections []*model.DateStatis) {
  67. archiveTagMap := make(map[int64]map[int64]int64) // key TagID, value map[int64]int64 -> key avId, value charge
  68. tagChargeMap := make(map[int64]int64) // key TagID, value TagID total Charge
  69. handleArchive(archiveCh, archiveTagMap, tagChargeMap, date)
  70. sections = make([]*model.DateStatis, 0)
  71. for tagID, archives := range archiveTagMap {
  72. section := countDateStatis(archives, tagChargeMap[tagID], tagID, date)
  73. sections = append(sections, section...)
  74. }
  75. return
  76. }
  77. func handleArchive(archiveCh chan []*model.Archive, archiveTagMap map[int64]map[int64]int64, tagChargeMap map[int64]int64, startDate time.Time) {
  78. for archives := range archiveCh {
  79. for _, ac := range archives {
  80. if !startDate.After(ac.Date.Time()) {
  81. tagChargeMap[ac.TagID] += ac.IncCharge
  82. if _, ok := archiveTagMap[ac.TagID]; !ok {
  83. archiveTagMap[ac.TagID] = make(map[int64]int64)
  84. }
  85. archiveTagMap[ac.TagID][ac.ID] += ac.IncCharge
  86. }
  87. }
  88. }
  89. }
  90. func countDateStatis(charges map[int64]int64, totalCharge, tagID int64, date time.Time) (sections []*model.DateStatis) {
  91. if len(charges) == 0 {
  92. return
  93. }
  94. sections = initChargeSections(totalCharge, tagID, xtime.Time(date.Unix()))
  95. for _, charge := range charges {
  96. for _, section := range sections {
  97. min, max := section.MinCharge*100, section.MaxCharge*100
  98. if charge >= min && charge < max {
  99. section.Count++
  100. }
  101. }
  102. }
  103. return
  104. }
  105. func (s *Service) dateStatisInsert(c context.Context, avChargeSection []*model.DateStatis, table string) (rows int64, err error) {
  106. var buf bytes.Buffer
  107. for _, row := range avChargeSection {
  108. buf.WriteString("(")
  109. buf.WriteString(strconv.FormatInt(row.Count, 10))
  110. buf.WriteByte(',')
  111. buf.WriteString(strconv.FormatInt(row.MoneySection, 10))
  112. buf.WriteByte(',')
  113. buf.WriteString(row.MoneyTips)
  114. buf.WriteByte(',')
  115. buf.WriteString(strconv.FormatInt(row.Charge, 10))
  116. buf.WriteByte(',')
  117. buf.WriteString(strconv.FormatInt(row.CategoryID, 10))
  118. buf.WriteByte(',')
  119. buf.WriteString("'" + row.CDate.Time().Format(_layout) + "'")
  120. buf.WriteString(")")
  121. buf.WriteByte(',')
  122. }
  123. if buf.Len() > 0 {
  124. buf.Truncate(buf.Len() - 1)
  125. }
  126. vals := buf.String()
  127. buf.Reset()
  128. rows, err = s.dao.InsertStatisTable(c, table, vals)
  129. return
  130. }