71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using Apimanager_backend.Data;
|
|
using Apimanager_backend.Dtos;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Apimanager_backend.Services
|
|
{
|
|
public class SystemInfoService : ISystemInfoService
|
|
{
|
|
private readonly ApiContext _context;
|
|
private readonly IMapper _mapper;
|
|
public SystemInfoService(ApiContext apiContext,IMapper mapper)
|
|
{
|
|
_context = apiContext;
|
|
_mapper = mapper;
|
|
}
|
|
public async Task<SystemInfoDto> GetSystemInfoAsync()
|
|
{
|
|
return new SystemInfoDto()
|
|
{
|
|
UserCount = await GetUserCount(),
|
|
OrderCount = await GetOrderCount(),
|
|
CallCount = await GetCallCount(),
|
|
Money = await GetMoney(),
|
|
RecentOrder = await GetRecentOrder(),
|
|
RecentUser = await GetRecentUser()
|
|
};
|
|
}
|
|
#region 获取用户数量
|
|
private async Task<int> GetUserCount()
|
|
{
|
|
return await _context.Users.CountAsync();
|
|
}
|
|
#endregion
|
|
#region 获取订单数量
|
|
private async Task<int> GetOrderCount()
|
|
{
|
|
return await _context.Orders.CountAsync();
|
|
}
|
|
#endregion
|
|
#region 获取API调用次数
|
|
private async Task<int> GetCallCount()
|
|
{
|
|
return await _context.CallLogs.CountAsync();
|
|
}
|
|
#endregion
|
|
#region 获取最近一周用户
|
|
private async Task<List<UserInfoDto>> GetRecentUser()
|
|
{
|
|
DateTime dateTime = DateTime.Now.AddDays(-7);
|
|
var users = _context.Users.Where(x => x.CreatedAt >= dateTime);
|
|
return _mapper.Map<List<UserInfoDto>>(await users.ToListAsync());
|
|
}
|
|
#endregion
|
|
#region 获取最近一周订单
|
|
private async Task<List<OrderDto>> GetRecentOrder()
|
|
{
|
|
DateTime dateTime = DateTime.Now.AddDays(-7);
|
|
var orders = _context.Orders.Include(x => x.User).Where(x => x.CreatedAt >= dateTime);
|
|
return _mapper.Map<List<OrderDto>>(await orders.ToListAsync());
|
|
}
|
|
#endregion
|
|
#region 获取金额数
|
|
public async Task<decimal> GetMoney()
|
|
{
|
|
return await _context.Orders.Where(x => x.OrderType == Models.OrderType.Recharge).SumAsync(x => x.Amount);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|