This commit is contained in:
2025-12-29 16:13:55 +08:00
parent 8e1319ac39
commit d2d3e589f0
14 changed files with 544 additions and 26 deletions

View File

@@ -0,0 +1,100 @@
package admin_agent
import (
"context"
"fmt"
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
"github.com/Masterminds/squirrel"
"github.com/zeromicro/go-zero/core/logx"
)
type AdminGetAgentLinkProductStatisticsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAdminGetAgentLinkProductStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetAgentLinkProductStatisticsLogic {
return &AdminGetAgentLinkProductStatisticsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AdminGetAgentLinkProductStatisticsLogic) AdminGetAgentLinkProductStatistics(req *types.AdminGetAgentLinkProductStatisticsReq) (resp *types.AdminGetAgentLinkProductStatisticsResp, err error) {
// 构建查询
query := squirrel.Select(
"p.product_name",
"COUNT(al.id) as link_count",
).
From("agent_link al").
Join("product p ON al.product_id = p.id").
Where(squirrel.Eq{"al.del_state": 0}).
Where(squirrel.Eq{"p.del_state": 0}).
GroupBy("p.product_name").
OrderBy("link_count DESC")
// 执行查询
sql, args, err := query.ToSql()
if err != nil {
return nil, err
}
type Result struct {
ProductName string `db:"product_name"`
LinkCount int64 `db:"link_count"`
}
var results []Result
// 使用模型的方法执行查询
// 通过反射获取底层的QueryRowsNoCacheCtx方法避免直接类型断言
if agentLinkModel, ok := l.svcCtx.AgentLinkModel.(interface {
QueryRowsNoCacheCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
}); ok {
err = agentLinkModel.QueryRowsNoCacheCtx(l.ctx, &results, sql, args...)
} else {
// 如果无法使用模型的方法,则使用原始的连接方式(安全地获取连接)
if cachedConn, ok := l.svcCtx.AgentLinkModel.(interface {
GetConn() interface{}
}); ok {
conn := cachedConn.GetConn()
if sqlxConn, ok := conn.(interface {
QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
}); ok {
err = sqlxConn.QueryRowsCtx(l.ctx, &results, sql, args...)
} else {
return nil, fmt.Errorf("无法获取数据库连接")
}
} else {
return nil, fmt.Errorf("无法获取数据库连接")
}
}
if err != nil {
return nil, err
}
// 处理空结果
if len(results) == 0 {
return &types.AdminGetAgentLinkProductStatisticsResp{
Items: []types.AgentLinkProductStatisticsItem{},
}, nil
}
// 转换为返回结果
items := make([]types.AgentLinkProductStatisticsItem, 0, len(results))
for _, r := range results {
items = append(items, types.AgentLinkProductStatisticsItem{
ProductName: r.ProductName,
LinkCount: r.LinkCount,
})
}
return &types.AdminGetAgentLinkProductStatisticsResp{
Items: items,
}, nil
}

View File

@@ -50,10 +50,26 @@ func (l *AdminGetWithdrawalStatisticsLogic) AdminGetWithdrawalStatistics(req *ty
return nil, fmt.Errorf("查询今日提现金额失败: %w", err)
}
// 查询总实际到账金额status=2表示成功
totalActualAmount, err := l.svcCtx.AgentWithdrawalModel.FindSum(l.ctx, totalBuilder, "actual_amount")
if err != nil {
logx.Errorf("查询总实际到账金额失败: %v", err)
return nil, fmt.Errorf("查询总实际到账金额失败: %w", err)
}
// 查询总扣税金额status=2表示成功
totalTaxAmount, err := l.svcCtx.AgentWithdrawalModel.FindSum(l.ctx, totalBuilder, "tax_amount")
if err != nil {
logx.Errorf("查询总扣税金额失败: %v", err)
return nil, fmt.Errorf("查询总扣税金额失败: %w", err)
}
// 构建响应
resp = &types.AdminGetWithdrawalStatisticsResp{
TotalWithdrawalAmount: totalWithdrawalAmount,
TodayWithdrawalAmount: todayWithdrawalAmount,
TotalActualAmount: totalActualAmount,
TotalTaxAmount: totalTaxAmount,
}
return resp, nil

View File

@@ -0,0 +1,87 @@
package admin_order
import (
"context"
"fmt"
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type AdminGetOrderSourceStatisticsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAdminGetOrderSourceStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetOrderSourceStatisticsLogic {
return &AdminGetOrderSourceStatisticsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AdminGetOrderSourceStatisticsLogic) AdminGetOrderSourceStatistics(req *types.AdminGetOrderSourceStatisticsReq) (resp *types.AdminGetOrderSourceStatisticsResp, err error) {
// 查询所有有产品ID的订单
builder := l.svcCtx.OrderModel.SelectBuilder()
builder = builder.Where("product_id IS NOT NULL")
// 获取所有符合条件的订单
orders, err := l.svcCtx.OrderModel.FindAll(l.ctx, builder, "id DESC")
if err != nil {
logx.Errorf("查询订单列表失败: %v", err)
return nil, fmt.Errorf("查询订单列表失败: %w", err)
}
logx.Infof("获取到订单数量: %d", len(orders))
// 统计每个产品的订单数量
productCountMap := make(map[int64]int64)
for _, order := range orders {
productCountMap[order.ProductId]++
}
// 构建返回结果
items := make([]types.OrderSourceStatisticsItem, 0, len(productCountMap))
for productId, count := range productCountMap {
// 获取产品信息
product, err := l.svcCtx.ProductModel.FindOne(l.ctx, productId)
if err != nil {
logx.Errorf("查询产品信息失败 productId=%d, err=%v", productId, err)
// 如果查询失败,使用默认值
items = append(items, types.OrderSourceStatisticsItem{
ProductName: "未知产品",
OrderCount: count,
})
continue
}
items = append(items, types.OrderSourceStatisticsItem{
ProductName: product.ProductName,
OrderCount: count,
})
}
// 按订单数量降序排序
for i := 0; i < len(items)-1; i++ {
for j := i + 1; j < len(items); j++ {
if items[i].OrderCount < items[j].OrderCount {
items[i], items[j] = items[j], items[i]
}
}
}
logx.Infof("查询到订单来源统计数据: %d 个产品,订单总数: %d", len(items), len(orders))
return &types.AdminGetOrderSourceStatisticsResp{
Items: items,
}, nil
}
// 定义结果结构体
type OrderSourceResult struct {
ProductName string `db:"product_name"`
OrderCount int64 `db:"order_count"`
}

View File

@@ -0,0 +1,106 @@
package admin_order
import (
"context"
"fmt"
"time"
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type AdminGetOrderStatisticsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAdminGetOrderStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetOrderStatisticsLogic {
return &AdminGetOrderStatisticsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AdminGetOrderStatisticsLogic) AdminGetOrderStatistics(req *types.AdminGetOrderStatisticsReq) (resp *types.AdminGetOrderStatisticsResp, err error) {
// 获取当前时间
now := time.Now()
var startTime, endTime time.Time
// 根据时间维度设置时间范围和格式
switch req.Dimension {
case "day":
// 日当月1号到今天
startTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
endTime = time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, now.Location())
case "month":
// 月今年1月到当月
startTime = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
endTime = time.Date(now.Year(), now.Month(), 1, 23, 59, 59, 999999999, now.Location()).AddDate(0, 1, -1)
case "year":
// 年过去5年
startTime = time.Date(now.Year()-5, 1, 1, 0, 0, 0, 0, now.Location())
endTime = time.Date(now.Year(), 12, 31, 23, 59, 59, 999999999, now.Location())
case "all":
// 全部:所有时间,但按日统计
startTime = time.Date(2020, 1, 1, 0, 0, 0, 0, now.Location()) // 假设从2020年开始
endTime = now
default:
// 默认为日
startTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
endTime = time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, now.Location())
}
// 构建查询条件
builder := l.svcCtx.OrderModel.SelectBuilder().
Where("create_time >= ? AND create_time <= ?", startTime, endTime).
Where("status = ?", "paid") // 只统计已支付的订单
// 查询所有符合条件的订单
orders, err := l.svcCtx.OrderModel.FindAll(l.ctx, builder, "create_time ASC")
if err != nil {
logx.Errorf("查询订单统计数据失败: %v", err)
return nil, fmt.Errorf("查询订单统计数据失败: %w", err)
}
// 按日期分组统计
dateMap := make(map[string]*types.OrderStatisticsItem)
for _, order := range orders {
var dateKey string
if req.Dimension == "year" {
dateKey = order.CreateTime.Format("2006")
} else if req.Dimension == "month" {
dateKey = order.CreateTime.Format("2006-01")
} else {
dateKey = order.CreateTime.Format("2006-01-02")
}
if item, exists := dateMap[dateKey]; exists {
item.Count++
item.Amount += order.Amount
} else {
dateMap[dateKey] = &types.OrderStatisticsItem{
Date: dateKey,
Count: 1,
Amount: order.Amount,
}
}
}
// 转换为切片
items := make([]types.OrderStatisticsItem, 0, len(dateMap))
for date := range dateMap {
items = append(items, *dateMap[date])
}
// 构建响应
resp = &types.AdminGetOrderStatisticsResp{
Items: items,
}
return resp, nil
}