基本功能已初步完善
This commit is contained in:
@@ -25,13 +25,18 @@ func AdminGetTrafficRank(c *gin.Context) {
|
||||
if endTime == 0 {
|
||||
endTime = time.Now().Unix()
|
||||
}
|
||||
periodDuration := endTime - startTime
|
||||
if periodDuration <= 0 {
|
||||
periodDuration = 86400
|
||||
}
|
||||
previousStart := startTime - periodDuration
|
||||
previousEnd := startTime
|
||||
|
||||
var result []gin.H
|
||||
if rankType == "user" {
|
||||
type userRank struct {
|
||||
ID int `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
Email string `json:"name"`
|
||||
ID int `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
}
|
||||
var ranks []userRank
|
||||
database.DB.Model(&model.StatUser{}).
|
||||
@@ -47,11 +52,15 @@ func AdminGetTrafficRank(c *gin.Context) {
|
||||
userIDs = append(userIDs, r.ID)
|
||||
}
|
||||
userEmails := loadUserEmailMap(userIDs)
|
||||
previousValues := loadUserPreviousTrafficMap(userIDs, previousStart, previousEnd)
|
||||
for _, r := range ranks {
|
||||
previousValue := previousValues[r.ID]
|
||||
result = append(result, gin.H{
|
||||
"id": fmt.Sprintf("%d", r.ID),
|
||||
"name": userEmails[r.ID],
|
||||
"value": r.Value,
|
||||
"id": fmt.Sprintf("%d", r.ID),
|
||||
"name": userEmails[r.ID],
|
||||
"value": r.Value,
|
||||
"previousValue": previousValue,
|
||||
"change": calculateGrowth(r.Value, previousValue),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
@@ -75,11 +84,15 @@ func AdminGetTrafficRank(c *gin.Context) {
|
||||
nodeIDs = append(nodeIDs, r.ID)
|
||||
}
|
||||
nodeNames := loadNodeNameMap(nodeIDs)
|
||||
previousValues := loadNodePreviousTrafficMap(nodeIDs, previousStart, previousEnd)
|
||||
for _, r := range ranks {
|
||||
previousValue := previousValues[r.ID]
|
||||
result = append(result, gin.H{
|
||||
"id": fmt.Sprintf("%d", r.ID),
|
||||
"name": nodeNames[r.ID],
|
||||
"value": r.Value,
|
||||
"id": fmt.Sprintf("%d", r.ID),
|
||||
"name": nodeNames[r.ID],
|
||||
"value": r.Value,
|
||||
"previousValue": previousValue,
|
||||
"change": calculateGrowth(r.Value, previousValue),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -116,6 +129,10 @@ func AdminGetOrderStats(c *gin.Context) {
|
||||
Find(&stats)
|
||||
|
||||
var list []gin.H
|
||||
var paidTotal int64
|
||||
var paidCount int64
|
||||
var commissionTotal int64
|
||||
var commissionCount int64
|
||||
for _, s := range stats {
|
||||
dateStr := time.Unix(s.RecordAt, 0).Format("2006-01-02")
|
||||
item := gin.H{
|
||||
@@ -127,16 +144,80 @@ func AdminGetOrderStats(c *gin.Context) {
|
||||
item["paid_total"] = s.PaidTotal
|
||||
item["paid_count"] = s.PaidCount
|
||||
item["commission_total"] = s.CommissionTotal
|
||||
item["commission_count"] = s.CommissionCount
|
||||
}
|
||||
paidTotal += s.PaidTotal
|
||||
paidCount += int64(s.PaidCount)
|
||||
commissionTotal += s.CommissionTotal
|
||||
commissionCount += int64(s.CommissionCount)
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
avgPaidAmount := int64(0)
|
||||
if paidCount > 0 {
|
||||
avgPaidAmount = paidTotal / paidCount
|
||||
}
|
||||
commissionRate := 0.0
|
||||
if paidTotal > 0 {
|
||||
commissionRate = float64(commissionTotal) / float64(paidTotal) * 100.0
|
||||
}
|
||||
|
||||
Success(c, gin.H{
|
||||
"list": list,
|
||||
"summary": gin.H{
|
||||
"start_date": time.Unix(startDate, 0).Format("2006-01-02"),
|
||||
"end_date": time.Unix(endDate, 0).Format("2006-01-02"),
|
||||
"paid_total": paidTotal,
|
||||
"paid_count": paidCount,
|
||||
"avg_paid_amount": avgPaidAmount,
|
||||
"commission_total": commissionTotal,
|
||||
"commission_count": commissionCount,
|
||||
"commission_rate": commissionRate,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func loadUserPreviousTrafficMap(ids []int, startTime int64, endTime int64) map[int]int64 {
|
||||
result := make(map[int]int64)
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
type userRank struct {
|
||||
ID int `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
}
|
||||
var ranks []userRank
|
||||
database.DB.Model(&model.StatUser{}).
|
||||
Select("user_id as id, SUM(u + d) as value").
|
||||
Where("user_id IN ? AND record_at >= ? AND record_at <= ?", ids, startTime, endTime).
|
||||
Group("user_id").
|
||||
Scan(&ranks)
|
||||
for _, rank := range ranks {
|
||||
result[rank.ID] = rank.Value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func loadNodePreviousTrafficMap(ids []int, startTime int64, endTime int64) map[int]int64 {
|
||||
result := make(map[int]int64)
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
type nodeRank struct {
|
||||
ID int `json:"id"`
|
||||
Value int64 `json:"value"`
|
||||
}
|
||||
var ranks []nodeRank
|
||||
database.DB.Model(&model.StatServer{}).
|
||||
Select("server_id as id, SUM(u + d) as value").
|
||||
Where("server_id IN ? AND record_at >= ? AND record_at <= ?", ids, startTime, endTime).
|
||||
Group("server_id").
|
||||
Scan(&ranks)
|
||||
for _, rank := range ranks {
|
||||
result[rank.ID] = rank.Value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func loadNodeNameMap(ids []int) map[int]string {
|
||||
result := make(map[int]string)
|
||||
|
||||
Reference in New Issue
Block a user