charge.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strconv"
  7. "time"
  8. "go-common/app/admin/main/growup/dao/resource"
  9. model "go-common/app/admin/main/growup/model/income"
  10. "go-common/library/log"
  11. )
  12. // ArchiveChargeStatis av_charge statis
  13. func (s *Service) ArchiveChargeStatis(c context.Context, categoryID []int64, typ, groupType int, fromTime, toTime int64) (data interface{}, err error) {
  14. table := setChargeTableByGroup(typ, groupType)
  15. from := getDateByGroup(groupType, time.Unix(fromTime, 0))
  16. to := getDateByGroup(groupType, time.Unix(toTime, 0))
  17. query := formatArchiveQuery(categoryID, from, to)
  18. archives, err := s.GetArchiveChargeStatis(c, table, query)
  19. if err != nil {
  20. log.Error("s.GetArchiveChargeStatis error(%v)", err)
  21. return
  22. }
  23. data = archiveChargeStatis(archives, from, to, groupType)
  24. return
  25. }
  26. func archiveChargeStatis(archs []*model.ArchiveChargeStatis, from, to time.Time, groupType int) interface{} {
  27. avsMap := make(map[string]*model.ArchiveChargeStatis)
  28. ctgyMap := make(map[string]bool)
  29. for _, arch := range archs {
  30. date := formatDateByGroup(arch.CDate.Time(), groupType)
  31. ctgykey := date + strconv.FormatInt(arch.CategroyID, 10)
  32. if val, ok := avsMap[date]; ok {
  33. val.Avs += arch.Avs
  34. if !ctgyMap[ctgykey] {
  35. val.Charge += arch.Charge
  36. ctgyMap[ctgykey] = true
  37. }
  38. } else {
  39. avsMap[date] = &model.ArchiveChargeStatis{
  40. Avs: arch.Avs,
  41. Charge: arch.Charge,
  42. }
  43. ctgyMap[ctgykey] = true
  44. }
  45. }
  46. charge, counts, xAxis := []string{}, []int64{}, []string{}
  47. // get result by date
  48. to = to.AddDate(0, 0, 1)
  49. for from.Before(to) {
  50. dateStr := formatDateByGroup(from, groupType)
  51. xAxis = append(xAxis, dateStr)
  52. if val, ok := avsMap[dateStr]; ok {
  53. charge = append(charge, fmt.Sprintf("%.2f", float64(val.Charge)/float64(100)))
  54. counts = append(counts, val.Avs)
  55. } else {
  56. charge = append(charge, "0")
  57. counts = append(counts, int64(0))
  58. }
  59. from = addDayByGroup(groupType, from)
  60. }
  61. return map[string]interface{}{
  62. "counts": counts,
  63. "charges": charge,
  64. "xaxis": xAxis,
  65. }
  66. }
  67. // ArchiveChargeSection get av_charge section
  68. func (s *Service) ArchiveChargeSection(c context.Context, categoryID []int64, typ, groupType int, fromTime, toTime int64) (data interface{}, err error) {
  69. table := setChargeTableByGroup(typ, groupType)
  70. from := getDateByGroup(groupType, time.Unix(fromTime, 0))
  71. to := getDateByGroup(groupType, time.Unix(toTime, 0))
  72. query := formatArchiveQuery(categoryID, from, to)
  73. archives, err := s.GetArchiveChargeStatis(c, table, query)
  74. if err != nil {
  75. log.Error("s.GetArchiveChargeStatis error(%v)", err)
  76. return
  77. }
  78. data = archiveChargeSection(archives, from, to, groupType)
  79. return
  80. }
  81. func archiveChargeSection(archs []*model.ArchiveChargeStatis, from, to time.Time, groupType int) interface{} {
  82. ret := make([]map[string]interface{}, 0)
  83. avsMap := make(map[string][]int64)
  84. for _, arch := range archs {
  85. date := formatDateByGroup(arch.CDate.Time(), groupType)
  86. if val, ok := avsMap[date]; ok {
  87. val[arch.MoneySection] += arch.Avs
  88. } else {
  89. avsMap[date] = make([]int64, 12)
  90. avsMap[date][arch.MoneySection] = arch.Avs
  91. ret = append(ret, map[string]interface{}{
  92. "date_format": date,
  93. "sections": avsMap[date],
  94. })
  95. }
  96. }
  97. return ret
  98. }
  99. // ArchiveChargeDetail archive charge details
  100. func (s *Service) ArchiveChargeDetail(c context.Context, aid int64, typ int) (archives []*model.ArchiveCharge, err error) {
  101. switch typ {
  102. case _video:
  103. archives, err = s.GetAvCharges(c, aid)
  104. case _column:
  105. archives, err = s.dao.GetColumnCharges(c, aid)
  106. case _bgm:
  107. archives, err = s.dao.GetBgmCharges(c, aid)
  108. default:
  109. err = fmt.Errorf("type error")
  110. }
  111. if err != nil {
  112. log.Error("s.GetArchives(%d) error(%v)", typ, err)
  113. return
  114. }
  115. err = s.archiveChargeDetail(c, archives, aid, typ)
  116. return
  117. }
  118. func (s *Service) archiveChargeDetail(c context.Context, archs []*model.ArchiveCharge, aid int64, typ int) (err error) {
  119. if len(archs) == 0 {
  120. return
  121. }
  122. // get up nickname
  123. nickname, err := resource.NameByMID(c, archs[0].MID)
  124. if err != nil {
  125. return
  126. }
  127. var table, query string
  128. switch typ {
  129. case _video:
  130. table, query = "av_charge_statis", fmt.Sprintf("av_id = %d", aid)
  131. case _column:
  132. table, query = "column_charge_statis", fmt.Sprintf("aid = %d", aid)
  133. case _bgm:
  134. table, query = "bgm_charge_statis", fmt.Sprintf("sid = %d", aid)
  135. }
  136. totalCharge, err := s.dao.GetTotalCharge(c, table, query)
  137. if err != nil {
  138. log.Error("s.GetTotalCharge error(%v)", err)
  139. return
  140. }
  141. sort.Slice(archs, func(i, j int) bool {
  142. return archs[i].Date > archs[j].Date
  143. })
  144. for _, arch := range archs {
  145. arch.TotalCharge = totalCharge
  146. arch.Nickname = nickname
  147. totalCharge -= arch.Charge
  148. }
  149. return
  150. }
  151. // BgmChargeDetail bgm charge detail
  152. func (s *Service) BgmChargeDetail(c context.Context, sid int64) (archives []*model.ArchiveCharge, err error) {
  153. archives = make([]*model.ArchiveCharge, 0)
  154. bgms, err := s.dao.GetBgmCharges(c, sid)
  155. if err != nil {
  156. log.Error("s.dao.GetBgmCharges error(%v)", err)
  157. return
  158. }
  159. avIDs := make(map[int64]struct{})
  160. for _, bgm := range bgms {
  161. avIDs[bgm.AvID] = struct{}{}
  162. }
  163. for avID := range avIDs {
  164. var avs []*model.ArchiveCharge
  165. avs, err = s.ArchiveChargeDetail(c, avID, _video)
  166. if err != nil {
  167. log.Error("s.ArchiveChargeDetail error(%v)", err)
  168. return
  169. }
  170. archives = append(archives, avs...)
  171. }
  172. return
  173. }
  174. // UpRatio up charge ratio
  175. func (s *Service) UpRatio(c context.Context, from, limit int64) (map[int64]int64, error) {
  176. return s.dao.UpRatio(c, from, limit)
  177. }
  178. // GetAvCharges get av charge by av id
  179. func (s *Service) GetAvCharges(c context.Context, avID int64) (avs []*model.ArchiveCharge, err error) {
  180. avs = make([]*model.ArchiveCharge, 0)
  181. for i := 1; i <= 12; i++ {
  182. var av []*model.ArchiveCharge
  183. av, err = s.dao.GetAvDailyCharge(c, i, avID)
  184. if err != nil {
  185. return
  186. }
  187. avs = append(avs, av...)
  188. }
  189. return
  190. }
  191. // GetArchiveChargeStatis get archive charge date statis
  192. func (s *Service) GetArchiveChargeStatis(c context.Context, table, query string) (archs []*model.ArchiveChargeStatis, err error) {
  193. offset, size := 0, 2000
  194. for {
  195. var arch []*model.ArchiveChargeStatis
  196. arch, err = s.dao.GetArchiveChargeStatis(c, table, query, offset, size)
  197. if err != nil {
  198. return
  199. }
  200. archs = append(archs, arch...)
  201. if len(arch) < size {
  202. break
  203. }
  204. offset += len(arch)
  205. }
  206. return
  207. }