123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package access_log
- import (
- "context"
- "gd_statistics/apis"
- "gd_statistics/errors"
- "fmt"
- "sort"
- "strings"
- "github.com/astaxie/beego/orm"
- )
- func sortProvider(array []apis.ProviderCount) []apis.ProviderCount {
- length := len(array)
- for i := 0; i < length; i++ {
- for j := i + 1; j < length; j++ {
- if array[i].Charge < array[j].Charge {
- tmp := array[i]
- array[i] = array[j]
- array[j] = tmp
- } else if array[i].Charge == array[j].Charge {
- nameArray := []string{
- array[i].ProviderApiName,
- array[j].ProviderApiName,
- }
- sort.Strings(nameArray)
- if array[i].ProviderApiName != nameArray[0] {
- tmp := array[i]
- array[i] = array[j]
- array[j] = tmp
- }
- }
- }
- }
- return array
- }
- func getPayDistributionByHour(o orm.Ormer, req *apis.BiGetPayDistributionReq, start, end int64) (*apis.BiGetPayDistributionReply, error) {
- ret := apis.BiGetPayDistributionReply{}
- if start == 0 || end == 0 {
- return &ret, nil
- }
- sql := fmt.Sprintf("select sum(charge) as charge from t_gd_report_hour where hour >= %d and hour < %d", start, end)
- if len(req.ApiId) > 0 {
- sql = fmt.Sprintf("%s and api_id in(%s)", sql, strings.Replace(strings.Trim(fmt.Sprint(req.ApiId), "[]"), " ", ",", -1))
- }
- if req.MerchantId > 0 {
- sql = fmt.Sprintf("%s and merchant_id=%d", sql, req.MerchantId)
- }
- err := o.Raw(sql).QueryRow(&ret.MerchantSumCharge)
- if err != nil && err != orm.ErrNoRows {
- return nil, errors.DataBaseError
- }
- 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)
- if len(req.ApiId) > 0 {
- sql = fmt.Sprintf("%s and api_id in(%s)", sql, strings.Replace(strings.Trim(fmt.Sprint(req.ApiId), "[]"), " ", ",", -1))
- }
- if req.MerchantId > 0 {
- sql = fmt.Sprintf("%s and merchant_id=%d", sql, req.MerchantId)
- }
- sql = fmt.Sprintf("%s group by provider_api_id", sql)
- _, err = o.Raw(sql).QueryRows(&ret.PayInfos)
- if err != nil && err != orm.ErrNoRows {
- return nil, errors.DataBaseError
- }
- return &ret, nil
- }
- func getPayDistributionNormal(o orm.Ormer, req *apis.BiGetPayDistributionReq, start, end int64) (*apis.BiGetPayDistributionReply, error) {
- ret := apis.BiGetPayDistributionReply{}
- if start == 0 || end == 0 {
- return &ret, nil
- }
- mreq := apis.LogQueryUserAccessCountExportReq{}
- mreq.MerchantId = req.MerchantId
- mreq.StartTimestamp = start
- mreq.EndTimestamp = end
- mreply := apis.LogQueryUserAccessCountExportReply{}
- mreq.TabName = "t_gd_access_log_day"
- if len(req.ApiId) != 0 {
- mreq.ApiIdList = append(mreq.ApiIdList, req.ApiId...)
- }
- err := LogQueryUserAccessCountExport(context.Background(), &mreq, &mreply)
- if err != nil {
- return nil, err
- }
- for _, v := range mreply.LogQueryUserAcessCount {
- ret.MerchantSumCharge += v.Charge
- }
- vreq := apis.LogQueryProviderCountExportReq{}
- vreq.MerchantId = req.MerchantId
- vreq.ApiIds = req.ApiId
- vreq.StartTimestamp = start
- vreq.EndTimestamp = end
- vreply := apis.LogQueryProviderCountExportReply{}
- vreq.TabName = "t_gd_thirdpart_log_day"
- err = LogQueryProviderCountExport(context.Background(), &vreq, &vreply)
- if err != nil {
- return nil, err
- }
- ret.PayInfos = make([]apis.ProviderCount, len(vreply.LogQueryProviderCount))
- for i, v := range vreply.LogQueryProviderCount {
- ret.PayInfos[i].ProviderName = v.ProviderName
- ret.PayInfos[i].ProviderApiName = v.ProviderApiName
- ret.PayInfos[i].Charge = int(v.Charge)
- ret.PayInfos[i].Count = int(v.Total)
- }
- return &ret, nil
- }
- func BiGetPayDistribution(ctx context.Context, req *apis.BiGetPayDistributionReq, reply *apis.BiGetPayDistributionReply) error {
- if req.TodayEndTimestamp > 0 && req.TodayEndTimestamp < req.EndTimestamp {
- req.EndTimestamp = req.TodayEndTimestamp
- }
- o := orm.NewOrm()
- tabs := []string{
- "t_gd_report_hour",
- "t_gd_provider_report_hour",
- }
- hourStart, hourEnd, otherStart, otherEnd, err := parseHourTimestamp(o, req.StartTimestamp, req.EndTimestamp, tabs)
- if err != nil {
- return err
- }
- hourInfos, err := getPayDistributionByHour(o, req, hourStart, hourEnd)
- if err != nil {
- return err
- }
- otherInfos, err := getPayDistributionNormal(o, req, otherStart, otherEnd)
- if err != nil {
- return err
- }
- reply.MerchantSumCharge = hourInfos.MerchantSumCharge + otherInfos.MerchantSumCharge
- m := map[string]*apis.ProviderCount{}
- for i, v := range hourInfos.PayInfos {
- reply.ProviderSumCharge += int64(v.Charge)
- key := fmt.Sprintf("%s-%s", v.ProviderName, v.ProviderApiName)
- if _, ok := m[key]; ok == false {
- m[key] = &hourInfos.PayInfos[i]
- }
- }
- for i, v := range otherInfos.PayInfos {
- reply.ProviderSumCharge += int64(v.Charge)
- key := fmt.Sprintf("%s-%s", v.ProviderName, v.ProviderApiName)
- if _, ok := m[key]; ok == false {
- m[key] = &otherInfos.PayInfos[i]
- continue
- }
- m[key].Count += v.Count
- m[key].Charge += v.Charge
- }
- i := 0
- reply.PayInfos = make([]apis.ProviderCount, len(m))
- for _, v := range m {
- reply.PayInfos[i] = *v
- i++
- }
- reply.PayInfos = sortProvider(reply.PayInfos)
- return nil
- }
|