Files
hm-server/app/main/api/internal/logic/admin_order/admingetrevenuestatisticslogic.go
2025-12-29 18:29:24 +08:00

85 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 AdminGetRevenueStatisticsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAdminGetRevenueStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetRevenueStatisticsLogic {
return &AdminGetRevenueStatisticsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AdminGetRevenueStatisticsLogic) AdminGetRevenueStatistics(req *types.AdminGetRevenueStatisticsReq) (resp *types.AdminGetRevenueStatisticsResp, err error) {
// 获取今日的开始和结束时间
today := time.Now()
startOfDay := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location())
endOfDay := startOfDay.Add(24 * time.Hour)
// 构建查询条件
builder := l.svcCtx.OrderModel.SelectBuilder()
// 查询总收入金额status=paid表示已支付
totalRevenueBuilder := builder.Where("status = ?", "paid")
totalRevenueAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalRevenueBuilder, "amount")
if err != nil {
logx.Errorf("查询总收入金额失败: %v", err)
return nil, fmt.Errorf("查询总收入金额失败: %w", err)
}
// 查询今日收入金额status=paid表示已支付且支付时间为今日
todayRevenueBuilder := builder.Where("status = ? AND pay_time >= ? AND pay_time < ?", "paid", startOfDay, endOfDay)
todayRevenueAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, todayRevenueBuilder, "amount")
if err != nil {
logx.Errorf("查询今日收入金额失败: %v", err)
return nil, fmt.Errorf("查询今日收入金额失败: %w", err)
}
// 查询总成本金额status=paid表示已支付
totalCostBuilder := builder.Where("status = ?", "paid")
totalCostAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, totalCostBuilder, "sales_cost")
if err != nil {
logx.Errorf("查询总成本金额失败: %v", err)
return nil, fmt.Errorf("查询总成本金额失败: %w", err)
}
// 查询今日成本金额status=paid表示已支付且支付时间为今日
todayCostBuilder := builder.Where("status = ? AND pay_time >= ? AND pay_time < ?", "paid", startOfDay, endOfDay)
todayCostAmount, err := l.svcCtx.OrderModel.FindSum(l.ctx, todayCostBuilder, "sales_cost")
if err != nil {
logx.Errorf("查询今日成本金额失败: %v", err)
return nil, fmt.Errorf("查询今日成本金额失败: %w", err)
}
// 计算总利润 = 总收入 - 总成本
totalProfitAmount := totalRevenueAmount - totalCostAmount
// 计算今日利润 = 今日收入 - 今日成本
todayProfitAmount := todayRevenueAmount - todayCostAmount
// 构建响应
resp = &types.AdminGetRevenueStatisticsResp{
TotalRevenueAmount: totalRevenueAmount,
TodayRevenueAmount: todayRevenueAmount,
TotalProfitAmount: totalProfitAmount,
TodayProfitAmount: todayProfitAmount,
}
return resp, nil
}