log_bi_pay_distribution.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "fmt"
  9. "sort"
  10. "strings"
  11. "github.com/astaxie/beego/orm"
  12. )
  13. func sortProvider(array []apis.ProviderCount) []apis.ProviderCount {
  14. length := len(array)
  15. for i := 0; i < length; i++ {
  16. for j := i + 1; j < length; j++ {
  17. if array[i].Charge < array[j].Charge {
  18. tmp := array[i]
  19. array[i] = array[j]
  20. array[j] = tmp
  21. } else if array[i].Charge == array[j].Charge {
  22. nameArray := []string{
  23. array[i].ProviderApiName,
  24. array[j].ProviderApiName,
  25. }
  26. sort.Strings(nameArray)
  27. if array[i].ProviderApiName != nameArray[0] {
  28. tmp := array[i]
  29. array[i] = array[j]
  30. array[j] = tmp
  31. }
  32. }
  33. }
  34. }
  35. return array
  36. }
  37. func getPayDistributionByHour(o orm.Ormer, req *apis.BiGetPayDistributionReq, start, end int64) (*apis.BiGetPayDistributionReply, error) {
  38. ret := apis.BiGetPayDistributionReply{}
  39. if start == 0 || end == 0 {
  40. return &ret, nil
  41. }
  42. sql := fmt.Sprintf("select sum(charge) as charge from t_gd_report_hour where hour >= %d and hour < %d", start, end)
  43. if len(req.ApiId) > 0 {
  44. sql = fmt.Sprintf("%s and api_id in(%s)", sql, strings.Replace(strings.Trim(fmt.Sprint(req.ApiId), "[]"), " ", ",", -1))
  45. }
  46. if req.MerchantId > 0 {
  47. sql = fmt.Sprintf("%s and merchant_id=%d", sql, req.MerchantId)
  48. }
  49. err := o.Raw(sql).QueryRow(&ret.MerchantSumCharge)
  50. if err != nil && err != orm.ErrNoRows {
  51. return nil, errors.DataBaseError
  52. }
  53. sql = fmt.Sprintf("select sum(charge) as charge, provider_api_name, provider_name, sum(total) as count from t_gd_provider_report_hour where hour >= %d and hour < %d", start, end)
  54. if len(req.ApiId) > 0 {
  55. sql = fmt.Sprintf("%s and api_id in(%s)", sql, strings.Replace(strings.Trim(fmt.Sprint(req.ApiId), "[]"), " ", ",", -1))
  56. }
  57. if req.MerchantId > 0 {
  58. sql = fmt.Sprintf("%s and merchant_id=%d", sql, req.MerchantId)
  59. }
  60. sql = fmt.Sprintf("%s group by provider_api_id", sql)
  61. _, err = o.Raw(sql).QueryRows(&ret.PayInfos)
  62. if err != nil && err != orm.ErrNoRows {
  63. return nil, errors.DataBaseError
  64. }
  65. return &ret, nil
  66. }
  67. func getPayDistributionNormal(o orm.Ormer, req *apis.BiGetPayDistributionReq, start, end int64) (*apis.BiGetPayDistributionReply, error) {
  68. ret := apis.BiGetPayDistributionReply{}
  69. if start == 0 || end == 0 {
  70. return &ret, nil
  71. }
  72. mreq := apis.LogQueryUserAccessCountExportReq{}
  73. mreq.MerchantId = req.MerchantId
  74. mreq.StartTimestamp = start
  75. mreq.EndTimestamp = end
  76. mreply := apis.LogQueryUserAccessCountExportReply{}
  77. mreq.TabName = "t_gd_access_log_day"
  78. if len(req.ApiId) != 0 {
  79. mreq.ApiIdList = append(mreq.ApiIdList, req.ApiId...)
  80. }
  81. err := LogQueryUserAccessCountExport(context.Background(), &mreq, &mreply)
  82. if err != nil {
  83. return nil, err
  84. }
  85. for _, v := range mreply.LogQueryUserAcessCount {
  86. ret.MerchantSumCharge += v.Charge
  87. }
  88. vreq := apis.LogQueryProviderCountExportReq{}
  89. vreq.MerchantId = req.MerchantId
  90. vreq.ApiIds = req.ApiId
  91. vreq.StartTimestamp = start
  92. vreq.EndTimestamp = end
  93. vreply := apis.LogQueryProviderCountExportReply{}
  94. vreq.TabName = "t_gd_thirdpart_log_day"
  95. err = LogQueryProviderCountExport(context.Background(), &vreq, &vreply)
  96. if err != nil {
  97. return nil, err
  98. }
  99. ret.PayInfos = make([]apis.ProviderCount, len(vreply.LogQueryProviderCount))
  100. for i, v := range vreply.LogQueryProviderCount {
  101. ret.PayInfos[i].ProviderName = v.ProviderName
  102. ret.PayInfos[i].ProviderApiName = v.ProviderApiName
  103. ret.PayInfos[i].Charge = int(v.Charge)
  104. ret.PayInfos[i].Count = int(v.Total)
  105. }
  106. return &ret, nil
  107. }
  108. func BiGetPayDistribution(ctx context.Context, req *apis.BiGetPayDistributionReq, reply *apis.BiGetPayDistributionReply) error {
  109. if req.TodayEndTimestamp > 0 && req.TodayEndTimestamp < req.EndTimestamp {
  110. req.EndTimestamp = req.TodayEndTimestamp
  111. }
  112. o := orm.NewOrm()
  113. tabs := []string{
  114. "t_gd_report_hour",
  115. "t_gd_provider_report_hour",
  116. }
  117. hourStart, hourEnd, otherStart, otherEnd, err := parseHourTimestamp(o, req.StartTimestamp, req.EndTimestamp, tabs)
  118. if err != nil {
  119. return err
  120. }
  121. hourInfos, err := getPayDistributionByHour(o, req, hourStart, hourEnd)
  122. if err != nil {
  123. return err
  124. }
  125. otherInfos, err := getPayDistributionNormal(o, req, otherStart, otherEnd)
  126. if err != nil {
  127. return err
  128. }
  129. reply.MerchantSumCharge = hourInfos.MerchantSumCharge + otherInfos.MerchantSumCharge
  130. m := map[string]*apis.ProviderCount{}
  131. for i, v := range hourInfos.PayInfos {
  132. reply.ProviderSumCharge += int64(v.Charge)
  133. key := fmt.Sprintf("%s-%s", v.ProviderName, v.ProviderApiName)
  134. if _, ok := m[key]; ok == false {
  135. m[key] = &hourInfos.PayInfos[i]
  136. }
  137. }
  138. for i, v := range otherInfos.PayInfos {
  139. reply.ProviderSumCharge += int64(v.Charge)
  140. key := fmt.Sprintf("%s-%s", v.ProviderName, v.ProviderApiName)
  141. if _, ok := m[key]; ok == false {
  142. m[key] = &otherInfos.PayInfos[i]
  143. continue
  144. }
  145. m[key].Count += v.Count
  146. m[key].Charge += v.Charge
  147. }
  148. i := 0
  149. reply.PayInfos = make([]apis.ProviderCount, len(m))
  150. for _, v := range m {
  151. reply.PayInfos[i] = *v
  152. i++
  153. }
  154. reply.PayInfos = sortProvider(reply.PayInfos)
  155. return nil
  156. }