package statistics
import (
"fmt"
"server/common"
"server/db"
"server/modules/backend/app"
"server/modules/backend/models"
utils "server/modules/backend/util"
"server/modules/backend/values"
"time"
"github.com/gin-gonic/gin"
"github.com/liangdas/mqant/log"
)
// WithdrawListData 获取退出统计数据
func WithdrawListData ( c * gin . Context ) {
a := app . NewApp ( c )
defer func ( ) {
a . Response ( )
} ( )
req := new ( values . WithdrawDataReq )
if ! a . S ( req ) {
return
}
start := req . Start
end := req . End
if start > end {
a . Code = values . CodeParam
a . Msg = "查询时间不合法"
return
}
resp := values . WithdrawDataResp { }
var ret [ ] values . OneWithdrawData
var oneDay int64 = 24 * 60 * 60
s , e := utils . GetQueryUnix ( req . Start , req . End )
skipStart := s + int64 ( ( req . Page - 1 ) * req . Num ) * oneDay
skipEnd := s + int64 ( req . Page * req . Num ) * oneDay
if skipEnd > e {
skipEnd = e
}
for i := skipEnd ; i > skipStart ; i -= oneDay {
j := i - oneDay
t := time . Unix ( j , 0 ) . Format ( "2006/01/02" )
// t1 := time.Unix(i, 0).Format("2006/01/02")
// log.Debug("i:%v,j:%v,t:%v,t1:%v", i, j, t, t1)
m := & common . RechargeOrder { }
one := values . OneWithdrawData { }
one . Date = t
// one.Active = models.GetActive(&j, &i, false, false)
con := fmt . Sprintf ( ` create_time > %d and create_time < %d and event = %v ` , j , i , common . CurrencyEventWithDraw )
one . WithdrawNum = db . Mysql ( ) . DistinctCount ( m , con , "uid" )
one . WithdrawTotal = db . Mysql ( ) . Count ( m , con )
con1 := con + fmt . Sprintf ( " and status = %v" , common . StatusROrderFinish )
one . SuccessCount = db . Mysql ( ) . Count ( m , con1 )
// con2 := con + fmt.Sprintf(" and status = %v", common.StatusROrderRefuse)
// one.RefuseCount = db.Mysql().Count(m, con2)
con3 := con + fmt . Sprintf ( " and status = %v" , common . StatusROrderPay )
one . PayingCount = db . Mysql ( ) . Count ( m , con3 )
con4 := con + fmt . Sprintf ( " and status = %v" , common . StatusROrderFail )
one . FailCount = db . Mysql ( ) . Count ( m , con4 )
one . WithdrawPer = utils . GetPer ( one . WithdrawTotal , one . Active )
one . SuccessPer = utils . GetPer ( one . SuccessCount , one . WithdrawTotal )
one . WithdrawAmount = db . Mysql ( ) . Sum ( m , con1 , "amount" )
one . WithdrawCost = db . Mysql ( ) . Sum ( m , con1 , "withdraw_cash" )
one . DayRecharge = models . GetRechargeTotal ( & j , & i , false )
// 退出成功的金额
one . WithdrawSuccessAmount = getWithDrawAmountBySql ( j , i , common . StatusROrderFinish )
// 打款中的金额
one . PayingAmount = getWithDrawAmountBySql ( j , i , common . StatusROrderCreate )
//re := &common.RechargeInfo{UID: req.UID}
//db.Mysql().Get(re)
ret = append ( ret , one )
}
resp . List = ret
a . Data = resp
}
// 退出成功金额
func getWithDrawAmountBySql ( s , e int64 , event int ) int64 {
var Amount int64
amountTotal := ` SELECT SUM(amount) as RechargeAmount FROM withdraw_order WHERE event = %v AND status = %v AND u.create_time >= %d AND u.create_time < %d `
err := db . Mysql ( ) . QueryBySql ( fmt . Sprintf ( amountTotal , common . CurrencyEventWithDraw , event , s , e ) , & Amount )
if err != nil {
log . Error ( "查询用户总退出失败, error : [%s]" , err . Error ( ) )
}
return Amount
}