budgetstatistics.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/growup/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. )
  11. // BudgetDayStatistics budget day statistics.
  12. func (s *Service) BudgetDayStatistics(c context.Context, ctype, from, limit int) (total int, infos []*model.BudgetDayStatistics, err error) {
  13. infos = make([]*model.BudgetDayStatistics, 0)
  14. latelyDate, err := s.dao.GetLatelyExpenseDate(c, "daily", ctype)
  15. if err == sql.ErrNoRows {
  16. err = nil
  17. return
  18. }
  19. if err != nil {
  20. log.Error("s.BudgetDayGraph dao.GetLatelyExpenseDate error(%v)", err)
  21. return
  22. }
  23. beginDate := time.Date(latelyDate.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  24. total, err = s.dao.GetDayExpenseCount(c, beginDate, ctype)
  25. if err != nil {
  26. log.Error("s.dao.GetDayExpenseCount error(%v)", err)
  27. return
  28. }
  29. if total == 0 {
  30. return
  31. }
  32. infos, err = s.dao.GetAllDayExpenseInfo(c, beginDate, ctype, from, limit)
  33. if err != nil {
  34. log.Error("s.dao.GetAllDayExpenseInfo error(%v)", err)
  35. return
  36. }
  37. var annualBudget, dayBudget int64
  38. switch ctype {
  39. case _video:
  40. annualBudget, dayBudget = s.conf.Budget.Video.AnnualBudget, s.conf.Budget.Video.DayBudget
  41. case _column:
  42. annualBudget, dayBudget = s.conf.Budget.Column.AnnualBudget, s.conf.Budget.Column.DayBudget
  43. case _bgm:
  44. annualBudget, dayBudget = s.conf.Budget.Bgm.AnnualBudget, s.conf.Budget.Bgm.DayBudget
  45. }
  46. for _, info := range infos {
  47. info.ExpenseRatio = strconv.FormatFloat(float64(info.TotalExpense)/float64(annualBudget), 'f', 2, 32)
  48. info.DayRatio = strconv.FormatFloat(float64(info.DayExpense)/float64(dayBudget), 'f', 2, 32)
  49. }
  50. return
  51. }
  52. // BudgetDayGraph get day graph.
  53. func (s *Service) BudgetDayGraph(c context.Context, ctype int) (ratioInfo *model.BudgetRatio, err error) {
  54. ratioInfo = new(model.BudgetRatio)
  55. switch ctype {
  56. case _video:
  57. ratioInfo.Year, ratioInfo.Budget = s.conf.Budget.Video.Year, s.conf.Budget.Video.AnnualBudget
  58. case _column:
  59. ratioInfo.Year, ratioInfo.Budget = s.conf.Budget.Column.Year, s.conf.Budget.Column.AnnualBudget
  60. case _bgm:
  61. ratioInfo.Year, ratioInfo.Budget = s.conf.Budget.Bgm.Year, s.conf.Budget.Bgm.AnnualBudget
  62. }
  63. latelyDate, err := s.dao.GetLatelyExpenseDate(c, "daily", ctype)
  64. if err == sql.ErrNoRows {
  65. err = nil
  66. return
  67. }
  68. if err != nil {
  69. log.Error("s.BudgetDayGraph dao.GetLatelyExpenseDate error(%v)", err)
  70. return
  71. }
  72. t := time.Date(latelyDate.Year(), latelyDate.Month(), latelyDate.Day(), 0, 0, 0, 0, time.Local)
  73. totalExpense, err := s.dao.GetDayTotalExpenseInfo(c, t, ctype)
  74. if err != nil {
  75. log.Error("s.BudgetDayGraph dao.GetDayTotalExpenseInfo error(%v)", err)
  76. return
  77. }
  78. ratioInfo.ExpenseRatio = strconv.FormatFloat(float64(totalExpense)/float64(ratioInfo.Budget), 'f', 2, 32)
  79. ratioInfo.DayRatio = strconv.FormatFloat(float64(getGoneDays(latelyDate)*100)/float64(getYearDays(latelyDate.Year())), 'f', 2, 32)
  80. return
  81. }
  82. // BudgetMonthStatistics budget month statistics
  83. func (s *Service) BudgetMonthStatistics(c context.Context, ctype, from, limit int) (total int, infos []*model.BudgetMonthStatistics, err error) {
  84. latelyDate, err := s.dao.GetLatelyExpenseDate(c, "monthly", ctype)
  85. if err == sql.ErrNoRows {
  86. err = nil
  87. return
  88. }
  89. if err != nil {
  90. log.Error("s.BudgetMonthStatistics dao.GetLatelyExpenseDate error(%v)", err)
  91. return
  92. }
  93. var month, beginMonth string
  94. if int(latelyDate.Month()) < 10 {
  95. month = fmt.Sprintf("%d-0%d", latelyDate.Year(), latelyDate.Month())
  96. } else {
  97. month = fmt.Sprintf("%d-%d", latelyDate.Year(), latelyDate.Month())
  98. }
  99. beginMonth = fmt.Sprintf("%d-01", latelyDate.Year())
  100. total, err = s.dao.GetMonthExpenseCount(c, month, beginMonth, ctype)
  101. if err != nil {
  102. log.Error("s.dao.GetMonthExpenseCount error(%v)", err)
  103. return
  104. }
  105. infos, err = s.dao.GetAllMonthExpenseInfo(c, month, beginMonth, ctype, from, limit)
  106. if err != nil {
  107. log.Error("s.BudgetMonthStatistics dao.GetAllMonthExpenseInfo error(%v)", err)
  108. return
  109. }
  110. if len(infos) <= 0 {
  111. infos = make([]*model.BudgetMonthStatistics, 0)
  112. }
  113. var dayBudget int64
  114. switch ctype {
  115. case _video:
  116. dayBudget = s.conf.Budget.Video.DayBudget
  117. case _column:
  118. dayBudget = s.conf.Budget.Column.DayBudget
  119. case _bgm:
  120. dayBudget = s.conf.Budget.Bgm.DayBudget
  121. }
  122. for _, info := range infos {
  123. info.ExpenseRatio = strconv.FormatFloat(float64(info.MonthExpense)/float64(int(dayBudget)*getMonthDays(time.Unix(int64(info.Date), 0))), 'f', 2, 32)
  124. date := time.Unix(int64(info.Date), 0)
  125. info.Month = strconv.Itoa(date.Year())
  126. info.Month += "-"
  127. if int(date.Month()) < 10 {
  128. info.Month += "0"
  129. }
  130. info.Month += strconv.Itoa(int(date.Month()))
  131. }
  132. return
  133. }
  134. func getMonthDays(date time.Time) (count int) {
  135. begin := time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.Local)
  136. end := time.Date(date.Year(), date.Month()+1, 1, 0, 0, 0, 0, time.Local)
  137. return int(end.Sub(begin).Hours() / 24)
  138. }
  139. func getYearDays(year int) (count int) {
  140. yearBegin := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
  141. yearEnd := time.Date(year+1, 1, 1, 0, 0, 0, 0, time.Local)
  142. return int(yearEnd.Sub(yearBegin).Hours() / 24)
  143. }
  144. func getGoneDays(date time.Time) (count int) {
  145. yearBegin := time.Date(date.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  146. return int(date.Sub(yearBegin).Hours()/24) + 1
  147. }