ql_apimanager_backend/Apimanager_backend/Services/SystemInfoService.cs

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
}
}