fix add
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user