creative_budget.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/growup/model"
  7. "go-common/library/log"
  8. )
  9. // CreativeBudget creative budget
  10. func (s *Service) CreativeBudget(c context.Context, date time.Time) (err error) {
  11. defer func() {
  12. GetTaskService().SetTaskStatus(c, TaskBudget, date.Format("2006-01-02"), err)
  13. }()
  14. err = GetTaskService().TaskReady(c, date.Format("2006-01-02"), TaskCreativeIncome)
  15. if err != nil {
  16. return
  17. }
  18. ups, err := s.GetUpIncome(c, "up_income", date.Format(_layout))
  19. if err != nil {
  20. log.Error("s.GetUpIncome(up_income) error(%v)", err)
  21. return
  22. }
  23. if len(ups) == 0 {
  24. err = fmt.Errorf("get 0 record from up_income")
  25. return
  26. }
  27. log.Info("daily ups(%d)", len(ups))
  28. monthStart := time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, time.Local)
  29. monthUps, err := s.GetUpIncome(c, "up_income_monthly", monthStart.Format(_layout))
  30. if err != nil {
  31. log.Error("s.GetUpIncome(up_income_monthly) error(%v)", err)
  32. return
  33. }
  34. if len(monthUps) == 0 {
  35. err = fmt.Errorf("get 0 record from up_income_monthly")
  36. return
  37. }
  38. log.Info("monthly ups(%d)", len(ups))
  39. preTotalExpense, err := s.dao.GetTotalExpenseByDate(c, date.AddDate(0, 0, -1).Format(_layout))
  40. if err != nil {
  41. log.Error("s.dao.GetTotalExpenseByDate error(%v)", err)
  42. return
  43. }
  44. log.Info("CreativeBudget ready date ok(%s)", date.Format(_layout))
  45. avDaily, cmDaily, bgmDaily := calCreativebudget(ups, date, preTotalExpense)
  46. avMonthly, cmMonthly, bgmMonthly := calCreativebudget(monthUps, date, preTotalExpense)
  47. avMonthly.TotalExpense = avDaily.TotalExpense
  48. cmMonthly.TotalExpense = cmDaily.TotalExpense
  49. bgmMonthly.TotalExpense = bgmDaily.TotalExpense
  50. // insert
  51. if avDaily.Expense > 0 {
  52. _, err = s.dao.InsertDailyExpense(c, avDaily)
  53. if err != nil {
  54. log.Error("s.dao.InsertDailyExpense error(%v)", err)
  55. return
  56. }
  57. _, err = s.dao.InsertMonthlyExpense(c, avMonthly)
  58. if err != nil {
  59. log.Error("s.dao.InsertMonthlyExpense error(%v)", err)
  60. return
  61. }
  62. }
  63. if cmDaily.Expense > 0 {
  64. _, err = s.dao.InsertDailyExpense(c, cmDaily)
  65. if err != nil {
  66. log.Error("s.dao.InsertDailyExpense error(%v)", err)
  67. return
  68. }
  69. _, err = s.dao.InsertMonthlyExpense(c, cmMonthly)
  70. if err != nil {
  71. log.Error("s.dao.InsertMonthlyExpense error(%v)", err)
  72. return
  73. }
  74. }
  75. if bgmDaily.Expense > 0 {
  76. _, err = s.dao.InsertDailyExpense(c, bgmDaily)
  77. if err != nil {
  78. log.Error("s.dao.InsertDailyExpense error(%v)", err)
  79. return
  80. }
  81. _, err = s.dao.InsertMonthlyExpense(c, bgmMonthly)
  82. if err != nil {
  83. log.Error("s.dao.InsertMonthlyExpense error(%v)", err)
  84. }
  85. }
  86. return
  87. }
  88. func calCreativebudget(ups []*model.UpIncome, date time.Time, preTotalExpense map[int64]int64) (avBudget, cmBudget, bgmBudget *model.BudgetExpense) {
  89. avBudget, cmBudget, bgmBudget = &model.BudgetExpense{}, &model.BudgetExpense{}, &model.BudgetExpense{}
  90. var (
  91. avExpense, cmExpense, bgmExpense int64
  92. avCount, cmCount, bgmCount int64
  93. upAvCount, upCmCount, upBgmCount int64
  94. )
  95. for _, up := range ups {
  96. if up.AvIncome > 0 {
  97. avCount += up.AvCount
  98. upAvCount++
  99. }
  100. // add up fix adjust to av
  101. avExpense += up.Income - up.ColumnIncome - up.BgmIncome // TODO up.AvIncome
  102. if up.ColumnIncome > 0 {
  103. cmExpense += up.ColumnIncome
  104. cmCount += up.ColumnCount
  105. upCmCount++
  106. }
  107. if up.BgmIncome > 0 {
  108. bgmExpense += up.BgmIncome
  109. bgmCount += up.BgmCount
  110. upBgmCount++
  111. }
  112. }
  113. if avCount > 0 && upAvCount > 0 {
  114. avBudget = &model.BudgetExpense{
  115. Expense: avExpense,
  116. AvCount: avCount,
  117. UpCount: upAvCount,
  118. UpAvgExpense: avExpense / upAvCount,
  119. AvAvgExpense: avExpense / avCount,
  120. Date: date,
  121. TotalExpense: preTotalExpense[0] + avExpense,
  122. CType: 0,
  123. }
  124. }
  125. if cmCount > 0 && upCmCount > 0 {
  126. cmBudget = &model.BudgetExpense{
  127. Expense: cmExpense,
  128. AvCount: cmCount,
  129. UpCount: upCmCount,
  130. UpAvgExpense: cmExpense / upCmCount,
  131. AvAvgExpense: cmExpense / cmCount,
  132. Date: date,
  133. TotalExpense: preTotalExpense[2] + cmExpense,
  134. CType: 2,
  135. }
  136. }
  137. if bgmCount > 0 && upBgmCount > 0 {
  138. bgmBudget = &model.BudgetExpense{
  139. Expense: bgmExpense,
  140. AvCount: bgmCount,
  141. UpCount: upBgmCount,
  142. UpAvgExpense: bgmExpense / upBgmCount,
  143. AvAvgExpense: bgmExpense / bgmCount,
  144. Date: date,
  145. TotalExpense: preTotalExpense[3] + bgmExpense,
  146. CType: 3,
  147. }
  148. }
  149. return
  150. }