bi_provider_service_get_pay.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package access_log
  4. import (
  5. "context"
  6. "gd_statistics/apis"
  7. "gd_statistics/errors"
  8. "strconv"
  9. )
  10. func sortApiPayDetailInfo(array []apis.ProviderServicePayDetail) []apis.ProviderServicePayDetail {
  11. length := len(array)
  12. for i := 0; i < length; i++ {
  13. for j := i + 1; j < length; j++ {
  14. if array[i].Charge < array[j].Charge {
  15. tmp := array[i]
  16. array[i] = array[j]
  17. array[j] = tmp
  18. }
  19. }
  20. }
  21. return array
  22. }
  23. func sortApiPayInfo(array []apis.ProviderServicePayInfo) []apis.ProviderServicePayInfo {
  24. length := len(array)
  25. for i := 0; i < length; i++ {
  26. for j := i + 1; j < length; j++ {
  27. if array[i].Charge < array[j].Charge {
  28. tmp := array[i]
  29. array[i] = array[j]
  30. array[j] = tmp
  31. }
  32. }
  33. }
  34. for i, v := range array {
  35. array[i].Detail = sortApiPayDetailInfo(v.Detail)
  36. }
  37. return array
  38. }
  39. func apiServiceGetPayInfo(ctx context.Context, req *apis.BiGetProviderServicePayInfoReq, start, end int64, isToday bool) ([]apis.ProviderServicePayInfo, error) {
  40. mreq := apis.BiGetProviderReportReq{}
  41. mreply := apis.BiGetProviderReportReply{}
  42. mreq.IsAll = true
  43. mreq.TodayEndTimestamp = req.TodayEndTimestamp
  44. mreq.Date = getDatesFromTimestamp(start, end)
  45. var err error
  46. if isToday {
  47. mreply.Infos, err = getTodayProviderReportFromHour(&mreq, start, end)
  48. if err != nil {
  49. return nil, err
  50. }
  51. } else {
  52. err = BiGetProviderReport(ctx, &mreq, &mreply)
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. m := map[string]map[string]*apis.BiProviderReportInfo{}
  58. for i, v := range mreply.Infos {
  59. if _, ok := m[v.ProviderName]; ok == false {
  60. item := map[string]*apis.BiProviderReportInfo{}
  61. item[v.ProviderApiName] = &mreply.Infos[i]
  62. m[v.ProviderName] = item
  63. continue
  64. }
  65. if _, ok := m[v.ProviderName][v.ProviderApiName]; ok == false {
  66. m[v.ProviderName][v.ProviderApiName] = &mreply.Infos[i]
  67. continue
  68. }
  69. m[v.ProviderName][v.ProviderApiName].Charge += v.Charge
  70. m[v.ProviderName][v.ProviderApiName].Total += v.Total
  71. }
  72. ret := []apis.ProviderServicePayInfo{}
  73. for providerName, apiMap := range m {
  74. item := apis.ProviderServicePayInfo{}
  75. item.ProviderName = providerName
  76. for _, api := range apiMap {
  77. apiItem := apis.ProviderServicePayDetail{}
  78. item.Charge += api.Charge
  79. apiItem.Charge = api.Charge
  80. apiItem.Valid = api.Total
  81. apiItem.ChargeRate = strconv.FormatFloat(float64(100*api.Charge/api.Total), 'f', 2, 64) + "%"
  82. apiItem.ProviderApiName = api.ProviderApiName
  83. item.Detail = append(item.Detail, apiItem)
  84. }
  85. ret = append(ret, item)
  86. }
  87. for i, v := range ret {
  88. for k, d := range ret[i].Detail {
  89. if v.Charge == 0 {
  90. ret[i].Detail[k].Rate = "0%"
  91. } else {
  92. ret[i].Detail[k].Rate = strconv.FormatFloat(float64(100*d.Charge/v.Charge), 'f', 2, 64) + "%"
  93. }
  94. }
  95. }
  96. return ret, nil
  97. }
  98. func BiGetProviderServicePayInfo(ctx context.Context, req *apis.BiGetProviderServicePayInfoReq, reply *apis.BiGetProviderServicePayInfoReply) (err error) {
  99. // 参数预处理
  100. if req.PageNumber <= 0 {
  101. req.PageNumber = 1
  102. }
  103. if req.PageSize <= 0 {
  104. req.PageSize = pageSize
  105. }
  106. reply.PageNumber = req.PageNumber
  107. reply.PageSize = req.PageSize
  108. offset := (req.PageNumber - 1) * req.PageSize
  109. limit := req.PageSize
  110. start := int64(0)
  111. end := int64(0)
  112. isToday := false
  113. // 今日,昨日,本周,本月
  114. switch req.TimeType {
  115. case 0:
  116. isToday = true
  117. start, end = getTodayTimestamp()
  118. case 1:
  119. start, end = getYesterdayTimestamp()
  120. case 2:
  121. start, end = getWeekTimestamp()
  122. case 3:
  123. start, end = getMonthTimestamp()
  124. default:
  125. return errors.ArgsError
  126. }
  127. // 获取所有信息
  128. infos, err := apiServiceGetPayInfo(ctx, req, start, end, isToday)
  129. if err != nil {
  130. return err
  131. }
  132. reply.Total = int64(len(infos))
  133. infos = sortApiPayInfo(infos)
  134. reply.Infos = infos
  135. // 分页
  136. switch {
  137. case reply.Total-int64(offset) <= 0:
  138. reply.Infos = []apis.ProviderServicePayInfo{}
  139. case reply.Total-int64(offset) <= int64(limit):
  140. reply.Infos = reply.Infos[offset:]
  141. case reply.Total-int64(offset) > int64(limit):
  142. reply.Infos = reply.Infos[offset : offset+limit]
  143. }
  144. return nil
  145. }