up_income.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "time"
  7. model "go-common/app/admin/main/growup/model/income"
  8. "go-common/app/admin/main/growup/service"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. // UpIncomeList up income list
  13. func (s *Service) UpIncomeList(c context.Context, mids []int64, typ, groupType int, fromTime, toTime, minIncome, maxIncome int64, from, limit int) (upIncome []*model.UpIncome, total int, err error) {
  14. table := setUpTableByGroup(groupType)
  15. _, incomeType := getUpInfoByType(typ)
  16. fromDate := getDateByGroup(groupType, time.Unix(fromTime, 0))
  17. toDate := getDateByGroup(groupType, time.Unix(toTime, 0))
  18. typeField := getUpFieldByType(typ)
  19. query := formatUpQuery(mids, fromDate, toDate, incomeType)
  20. if maxIncome != 0 || minIncome != 0 {
  21. query = fmt.Sprintf("%s AND %s >= %d AND %s <= %d", query, incomeType, minIncome, incomeType, maxIncome)
  22. }
  23. total, err = s.dao.UpIncomeCount(c, table, query)
  24. if err != nil {
  25. log.Error("s.dao.UpIncomeCount error(%v)", err)
  26. return
  27. }
  28. upIncome, err = s.dao.GetUpIncomeBySort(c, table, typeField, incomeType, query, from, limit)
  29. if err != nil {
  30. log.Error("s.dao.GetUpIncomeBySort error(%v)", err)
  31. return
  32. }
  33. if len(upIncome) == 0 {
  34. return
  35. }
  36. for _, up := range upIncome {
  37. mids = append(mids, up.MID)
  38. }
  39. nicknames, err := s.dao.ListUpInfo(c, mids)
  40. if err != nil {
  41. log.Error("s.dao.ListUpInfo error(%v)", err)
  42. return
  43. }
  44. var breachType []int64
  45. switch typ {
  46. case _video:
  47. breachType = []int64{0}
  48. case _column:
  49. breachType = []int64{2}
  50. case _bgm:
  51. breachType = []int64{3}
  52. case _up:
  53. breachType = []int64{0, 1, 2, 3}
  54. }
  55. breachs, err := s.dao.GetAvBreachByMIDs(c, mids, breachType)
  56. if err != nil {
  57. log.Error("s.dao.GetAvBreachByMIDs error(%v)", err)
  58. return
  59. }
  60. upIncomeList(upIncome, nicknames, breachs, typ, groupType)
  61. return
  62. }
  63. func upIncomeList(upIncome []*model.UpIncome, nicknames map[int64]string, breachs []*model.AvBreach, typ, groupType int) {
  64. midDateBreach := make(map[int64]map[string]int64) // map[mid][date] = money
  65. for _, b := range breachs {
  66. dateFormat := formatDateByGroup(b.CDate.Time(), groupType)
  67. if _, ok := midDateBreach[b.MID]; !ok {
  68. midDateBreach[b.MID] = make(map[string]int64)
  69. }
  70. midDateBreach[b.MID][dateFormat] += b.Money
  71. }
  72. for _, up := range upIncome {
  73. up.Nickname = nicknames[up.MID]
  74. up.DateFormat = formatDateByGroup(up.Date.Time(), groupType)
  75. up.ExtraIncome = up.Income - up.BaseIncome
  76. if _, ok := midDateBreach[up.MID]; ok {
  77. up.Breach = midDateBreach[up.MID][up.DateFormat]
  78. }
  79. switch typ {
  80. case _video:
  81. up.Count = up.AvCount
  82. case _column:
  83. up.Count = up.ColumnCount
  84. case _bgm:
  85. up.Count = up.BgmCount
  86. case _up:
  87. up.Count = up.AvCount + up.ColumnCount + up.BgmCount
  88. }
  89. }
  90. }
  91. // UpIncomeListExport up income list
  92. func (s *Service) UpIncomeListExport(c context.Context, mids []int64, typ, groupType int, fromTime, toTime, minIncome, maxIncome int64, from, limit int) (res []byte, err error) {
  93. ups, _, err := s.UpIncomeList(c, mids, typ, groupType, fromTime, toTime, minIncome, maxIncome, from, limit)
  94. if err != nil {
  95. log.Error("s.UpIncomeList error(%v)", err)
  96. return
  97. }
  98. records := formatUpIncome(ups)
  99. res, err = service.FormatCSV(records)
  100. if err != nil {
  101. log.Error("FormatCSV error(%v)")
  102. }
  103. return
  104. }
  105. // UpIncomeStatis up income statis
  106. func (s *Service) UpIncomeStatis(c context.Context, mids []int64, typ, groupType int, fromTime, toTime int64) (data interface{}, err error) {
  107. from := getDateByGroup(groupType, time.Unix(fromTime, 0))
  108. to := getDateByGroup(groupType, time.Unix(toTime, 0))
  109. if groupType == _groupDay && len(mids) == 0 {
  110. return s.upStatisDaily(c, typ, from, to)
  111. }
  112. return s.upIncomeStatisDate(c, mids, typ, groupType, from, to)
  113. }
  114. func (s *Service) upStatisDaily(c context.Context, typ int, from, to time.Time) (data interface{}, err error) {
  115. table, _ := getUpInfoByType(typ)
  116. statis, err := s.dao.GetUpDailyStatis(c, table, from.Format(_layout), to.Format(_layout))
  117. if err != nil {
  118. log.Error("s.dao.GetIncomeDailyStatis error(%v)", err)
  119. return
  120. }
  121. dateMap := make(map[string]*model.UpStatisRsp)
  122. for _, sta := range statis {
  123. date := sta.Date.Time().Format(_layout)
  124. if val, ok := dateMap[date]; ok {
  125. val.Ups += sta.Ups
  126. } else {
  127. dateMap[date] = &model.UpStatisRsp{
  128. Income: sta.Income,
  129. Ups: sta.Ups,
  130. }
  131. }
  132. }
  133. data = calUpStatis(dateMap, 1, from, to)
  134. return
  135. }
  136. func (s *Service) upIncomeStatisDate(c context.Context, mids []int64, typ, groupType int, from, to time.Time) (data interface{}, err error) {
  137. table := setUpTableByGroup(groupType)
  138. _, incomeType := getUpInfoByType(typ)
  139. query := formatUpQuery(mids, from, to, incomeType)
  140. upIncome, err := s.GetUpIncome(c, table, incomeType, query)
  141. if err != nil {
  142. log.Error("s.GetUpIncome error(%v)", err)
  143. return
  144. }
  145. sort.Slice(upIncome, func(i, j int) bool {
  146. return upIncome[i].Date < upIncome[j].Date
  147. })
  148. dateMap := make(map[string]*model.UpStatisRsp)
  149. for _, up := range upIncome {
  150. date := formatDateByGroup(up.Date.Time(), groupType)
  151. if val, ok := dateMap[date]; ok {
  152. val.Income += up.Income
  153. val.Ups++
  154. } else {
  155. dateMap[date] = &model.UpStatisRsp{
  156. Income: up.Income,
  157. Ups: 1,
  158. }
  159. }
  160. }
  161. data = calUpStatis(dateMap, groupType, from, to)
  162. return
  163. }
  164. func calUpStatis(dateMap map[string]*model.UpStatisRsp, groupType int, from, to time.Time) interface{} {
  165. incomes, ups, xAxis := []string{}, []int{}, []string{}
  166. to = to.AddDate(0, 0, 1)
  167. for from.Before(to) {
  168. dateStr := formatDateByGroup(from, groupType)
  169. xAxis = append(xAxis, dateStr)
  170. if val, ok := dateMap[dateStr]; ok {
  171. incomes = append(incomes, fmt.Sprintf("%.2f", float64(val.Income)/float64(100)))
  172. ups = append(ups, val.Ups)
  173. } else {
  174. incomes = append(incomes, "0")
  175. ups = append(ups, 0)
  176. }
  177. from = addDayByGroup(groupType, from)
  178. }
  179. return map[string]interface{}{
  180. "counts": ups,
  181. "incomes": incomes,
  182. "xaxis": xAxis,
  183. }
  184. }
  185. // GetUpIncome get
  186. func (s *Service) GetUpIncome(c context.Context, table, incomeType, query string) (upIncomes []*model.UpIncome, err error) {
  187. var id int64
  188. limit := 2000
  189. for {
  190. upIncome, err := s.dao.GetUpIncome(c, table, incomeType, query, id, limit)
  191. if err != nil {
  192. return upIncomes, err
  193. }
  194. upIncomes = append(upIncomes, upIncome...)
  195. if len(upIncome) < limit {
  196. break
  197. }
  198. id = upIncome[len(upIncome)-1].ID
  199. }
  200. return
  201. }
  202. func formatUpQuery(mids []int64, fromTime, toTime time.Time, incomeType string) string {
  203. query := "date >= '" + fromTime.Format("2006-01-02") + "'"
  204. query += " AND "
  205. query += "date <= '" + toTime.Format("2006-01-02") + "'"
  206. if len(mids) != 0 {
  207. query += " AND "
  208. query += "mid in (" + xstr.JoinInts(mids) + ")"
  209. }
  210. query += fmt.Sprintf(" AND %s > 0", incomeType)
  211. return query
  212. }