income_date_statis.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package income
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "time"
  9. incomeD "go-common/app/job/main/growup/dao/income"
  10. model "go-common/app/job/main/growup/model/income"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. var (
  15. _avIncomeDailyStatis = "av_income_daily_statis"
  16. _avIncomeWeeklyStatis = "av_income_weekly_statis"
  17. _avIncomeMonthlyStatis = "av_income_monthly_statis"
  18. _cmIncomeDailyStatis = "column_income_daily_statis"
  19. _cmIncomeWeeklyStatis = "column_income_weekly_statis"
  20. _cmIncomeMonthlyStatis = "column_income_monthly_statis"
  21. _bgmIncomeDailyStatis = "bgm_income_daily_statis"
  22. _bgmIncomeWeeklyStatis = "bgm_income_weekly_statis"
  23. _bgmIncomeMonthlyStatis = "bgm_income_monthly_statis"
  24. )
  25. // SectionEntries section entries
  26. type SectionEntries struct {
  27. avDaily []*model.DateStatis
  28. avWeekly []*model.DateStatis
  29. avMonthly []*model.DateStatis
  30. }
  31. // DateStatis income date statistics
  32. type DateStatis struct {
  33. dao *incomeD.Dao
  34. }
  35. // NewDateStatis new income date statistics service
  36. func NewDateStatis(dao *incomeD.Dao) *DateStatis {
  37. return &DateStatis{dao: dao}
  38. }
  39. func initIncomeSections(income, tagID int64, date xtime.Time) []*model.DateStatis {
  40. incomeSections := make([]*model.DateStatis, 12)
  41. incomeSections[0] = initIncomeSection(0, 1, 0, income, tagID, date)
  42. incomeSections[1] = initIncomeSection(1, 5, 1, income, tagID, date)
  43. incomeSections[2] = initIncomeSection(5, 10, 2, income, tagID, date)
  44. incomeSections[3] = initIncomeSection(10, 30, 3, income, tagID, date)
  45. incomeSections[4] = initIncomeSection(30, 50, 4, income, tagID, date)
  46. incomeSections[5] = initIncomeSection(50, 100, 5, income, tagID, date)
  47. incomeSections[6] = initIncomeSection(100, 200, 6, income, tagID, date)
  48. incomeSections[7] = initIncomeSection(200, 500, 7, income, tagID, date)
  49. incomeSections[8] = initIncomeSection(500, 1000, 8, income, tagID, date)
  50. incomeSections[9] = initIncomeSection(1000, 3000, 9, income, tagID, date)
  51. incomeSections[10] = initIncomeSection(3000, 5000, 10, income, tagID, date)
  52. incomeSections[11] = initIncomeSection(5000, math.MaxInt32, 11, income, tagID, date)
  53. return incomeSections
  54. }
  55. func initIncomeSection(min, max, section, income, tagID int64, date xtime.Time) *model.DateStatis {
  56. var tips string
  57. if max == math.MaxInt32 {
  58. tips = fmt.Sprintf("\"%d+\"", min)
  59. } else {
  60. tips = fmt.Sprintf("\"%d~%d\"", min, max)
  61. }
  62. return &model.DateStatis{
  63. MinIncome: min,
  64. MaxIncome: max,
  65. MoneySection: section,
  66. MoneyTips: tips,
  67. Income: income,
  68. CategoryID: tagID,
  69. CDate: date,
  70. }
  71. }
  72. func (s *DateStatis) handleDateStatis(c context.Context, archiveCh chan []*model.ArchiveIncome, date time.Time, table string) (incomeSections []*model.DateStatis, err error) {
  73. // delete
  74. if table != "" {
  75. _, err = s.dao.DelIncomeStatisTable(c, table, date.Format(_layout))
  76. if err != nil {
  77. log.Error("s.dao.DelIncomeStatisTable error(%v)", err)
  78. return
  79. }
  80. }
  81. // add
  82. incomeSections = s.handleArchives(c, archiveCh, date)
  83. return
  84. }
  85. // handleArchives handle archive_income_daily_statis, archive_income_weekly_statis, archive_income_monthly_statis
  86. func (s *DateStatis) handleArchives(c context.Context, archiveCh chan []*model.ArchiveIncome, date time.Time) (incomeSections []*model.DateStatis) {
  87. archTagMap := make(map[int64]map[int64]int64) // key TagID, value map[int64]int64 -> key aid, value income
  88. tagIncomeMap := make(map[int64]int64) // key TagID, value TagID total income
  89. for archive := range archiveCh {
  90. handleArchive(archive, archTagMap, tagIncomeMap, date)
  91. }
  92. incomeSections = make([]*model.DateStatis, 0)
  93. for tagID, avMap := range archTagMap {
  94. incomeSection := countIncomeDailyStatis(avMap, tagIncomeMap[tagID], tagID, date)
  95. incomeSections = append(incomeSections, incomeSection...)
  96. }
  97. return
  98. }
  99. func handleArchive(archives []*model.ArchiveIncome, archTagMap map[int64]map[int64]int64, tagIncomeMap map[int64]int64, startDate time.Time) {
  100. if archives == nil {
  101. return
  102. }
  103. if archTagMap == nil {
  104. archTagMap = make(map[int64]map[int64]int64)
  105. }
  106. if tagIncomeMap == nil {
  107. tagIncomeMap = make(map[int64]int64)
  108. }
  109. for _, archive := range archives {
  110. if !startDate.After(archive.Date.Time()) {
  111. tagIncomeMap[archive.TagID] += archive.Income
  112. if _, ok := archTagMap[archive.TagID]; !ok {
  113. archTagMap[archive.TagID] = make(map[int64]int64)
  114. }
  115. archTagMap[archive.TagID][archive.AID] += archive.Income
  116. }
  117. }
  118. }
  119. func (s *DateStatis) handleDateUp(c context.Context, upStatisCh chan []*model.UpIncome, date time.Time) (upSections, upAvSections, upCmSections, upBgmSections []*model.DateStatis, err error) {
  120. _, err = s.dao.DelIncomeStatisTable(c, "up_income_daily_statis", date.Format(_layout))
  121. if err != nil {
  122. log.Error("s.dao.DelIncomeStatisTable error(%v)", err)
  123. return
  124. }
  125. _, err = s.dao.DelIncomeStatisTable(c, "up_av_daily_statis", date.Format(_layout))
  126. if err != nil {
  127. log.Error("s.dao.DelIncomeStatisTable error(%v)", err)
  128. return
  129. }
  130. _, err = s.dao.DelIncomeStatisTable(c, "up_column_daily_statis", date.Format(_layout))
  131. if err != nil {
  132. log.Error("s.dao.DelIncomeStatisTable error(%v)", err)
  133. return
  134. }
  135. _, err = s.dao.DelIncomeStatisTable(c, "up_bgm_daily_statis", date.Format(_layout))
  136. if err != nil {
  137. log.Error("s.dao.DelIncomeStatisTable error(%v)", err)
  138. return
  139. }
  140. upMap := make(map[int64]int64)
  141. upAvMap := make(map[int64]int64)
  142. upCmMap := make(map[int64]int64)
  143. upBgmMap := make(map[int64]int64)
  144. var upTotal, avTotal, cmTotal, bgmTotal int64
  145. for up := range upStatisCh {
  146. up, av, cm, bgm := handleUp(up, upMap, upAvMap, upCmMap, upBgmMap, date)
  147. upTotal += up
  148. avTotal += av
  149. cmTotal += cm
  150. bgmTotal += bgm
  151. }
  152. upSections = countIncomeDailyStatis(upMap, upTotal, 0, date)
  153. upAvSections = countIncomeDailyStatis(upAvMap, avTotal, 0, date)
  154. upCmSections = countIncomeDailyStatis(upCmMap, cmTotal, 0, date)
  155. upBgmSections = countIncomeDailyStatis(upBgmMap, bgmTotal, 0, date)
  156. return
  157. }
  158. func handleUp(upIncomes []*model.UpIncome, upMap, upAvMap, upCmMap, upBgmMap map[int64]int64, startDate time.Time) (income, avIncome, cmIncome, bgmIncome int64) {
  159. if len(upIncomes) == 0 {
  160. return
  161. }
  162. for _, upIncome := range upIncomes {
  163. if startDate.Equal(upIncome.Date.Time()) {
  164. income += upIncome.Income
  165. avIncome += upIncome.AvIncome
  166. cmIncome += upIncome.ColumnIncome
  167. bgmIncome += upIncome.BgmIncome
  168. upMap[upIncome.MID] += upIncome.Income
  169. if upIncome.AvIncome > 0 {
  170. upAvMap[upIncome.MID] += upIncome.AvIncome
  171. }
  172. if upIncome.ColumnIncome > 0 {
  173. upCmMap[upIncome.MID] += upIncome.ColumnIncome
  174. }
  175. if upIncome.BgmIncome > 0 {
  176. upBgmMap[upIncome.MID] += upIncome.BgmIncome
  177. }
  178. }
  179. }
  180. return
  181. }
  182. func (s *DateStatis) incomeDateStatisInsert(c context.Context, incomeSection []*model.DateStatis, table string) (rows int64, err error) {
  183. var buf bytes.Buffer
  184. for _, row := range incomeSection {
  185. buf.WriteString("(")
  186. buf.WriteString(strconv.FormatInt(row.Count, 10))
  187. buf.WriteByte(',')
  188. buf.WriteString(strconv.FormatInt(row.MoneySection, 10))
  189. buf.WriteByte(',')
  190. buf.WriteString(row.MoneyTips)
  191. buf.WriteByte(',')
  192. buf.WriteString(strconv.FormatInt(row.Income, 10))
  193. buf.WriteByte(',')
  194. buf.WriteString(strconv.FormatInt(row.CategoryID, 10))
  195. buf.WriteByte(',')
  196. buf.WriteString("'" + row.CDate.Time().Format(_layout) + "'")
  197. buf.WriteString(")")
  198. buf.WriteByte(',')
  199. }
  200. if buf.Len() > 0 {
  201. buf.Truncate(buf.Len() - 1)
  202. }
  203. vals := buf.String()
  204. buf.Reset()
  205. rows, err = s.dao.InsertIncomeStatisTable(c, table, vals)
  206. return
  207. }
  208. func countIncomeDailyStatis(incomes map[int64]int64, totalIncome, tagID int64, date time.Time) (incomeSections []*model.DateStatis) {
  209. if len(incomes) == 0 {
  210. return
  211. }
  212. incomeSections = initIncomeSections(totalIncome, tagID, xtime.Time(date.Unix()))
  213. for _, income := range incomes {
  214. for _, section := range incomeSections {
  215. min, max := section.MinIncome*100, section.MaxIncome*100
  216. if income >= min && income < max {
  217. section.Count++
  218. }
  219. }
  220. }
  221. return
  222. }
  223. func (s *DateStatis) upIncomeDailyStatisInsert(c context.Context, upIncomeSection []*model.DateStatis, table string) (rows int64, err error) {
  224. var buf bytes.Buffer
  225. for _, row := range upIncomeSection {
  226. buf.WriteString("(")
  227. buf.WriteString(strconv.FormatInt(row.Count, 10))
  228. buf.WriteByte(',')
  229. buf.WriteString(strconv.FormatInt(row.MoneySection, 10))
  230. buf.WriteByte(',')
  231. buf.WriteString(row.MoneyTips)
  232. buf.WriteByte(',')
  233. buf.WriteString(strconv.FormatInt(row.Income, 10))
  234. buf.WriteByte(',')
  235. buf.WriteString("'" + row.CDate.Time().Format(_layout) + "'")
  236. buf.WriteString(")")
  237. buf.WriteByte(',')
  238. }
  239. if buf.Len() > 0 {
  240. buf.Truncate(buf.Len() - 1)
  241. }
  242. vals := buf.String()
  243. buf.Reset()
  244. rows, err = s.dao.InsertUpIncomeDailyStatis(c, table, vals)
  245. return
  246. }