fix 优化查询
This commit is contained in:
@@ -28,18 +28,21 @@ public class BalanceService : IBalanceService
|
||||
private readonly ICurrentUser _currentUser;
|
||||
private readonly ITenantApi _tenantApi;
|
||||
private readonly ICanteenSettingService _canteenSettingService;
|
||||
private readonly RedisCache _redisCache;
|
||||
private readonly IBusinessPlacePermissionsApi _businessPlacePermissionsApi;
|
||||
|
||||
public BalanceService(IDbContext dbContext,
|
||||
ICurrentUser currentUser,
|
||||
ITenantApi tenantApi,
|
||||
ICanteenSettingService canteenSettingService,
|
||||
RedisCache redisCache,
|
||||
IBusinessPlacePermissionsApi businessPlacePermissionsApi)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_currentUser = currentUser;
|
||||
_tenantApi = tenantApi;
|
||||
_canteenSettingService = canteenSettingService;
|
||||
_redisCache = redisCache;
|
||||
_businessPlacePermissionsApi = businessPlacePermissionsApi;
|
||||
}
|
||||
|
||||
@@ -426,6 +429,13 @@ public class BalanceService : IBalanceService
|
||||
|
||||
public async Task<ConsumeNumberResponse> GetConsumeNumberAsync()
|
||||
{
|
||||
var cacheKey = $"GetConsumeNumberAsync:{_currentUser.GetStringTenantId()}";
|
||||
var cache = await _redisCache.StringGetAsync(cacheKey);
|
||||
if (cache.IsPresent())
|
||||
{
|
||||
return cache.ToObject<ConsumeNumberResponse>();
|
||||
}
|
||||
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.Type == DeviceTypeEnumType.Meal)
|
||||
.ToListAsync();
|
||||
@@ -447,7 +457,7 @@ public class BalanceService : IBalanceService
|
||||
var months = await GetConsumptionTrendsAsync(1);
|
||||
var days = await GetConsumptionTrendsAsync(2);
|
||||
|
||||
return new ConsumeNumberResponse
|
||||
var result = new ConsumeNumberResponse
|
||||
{
|
||||
Total = devices.Count,
|
||||
OffNumber = offCount.Count,
|
||||
@@ -457,6 +467,9 @@ public class BalanceService : IBalanceService
|
||||
Days = days,
|
||||
Months = months
|
||||
};
|
||||
|
||||
await _redisCache.StringSetAsync(cacheKey, result.ToJson(), TimeSpan.FromMinutes(30));
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Pagination<SpendingDetailResponse>> GetSpendingDetailsAsync(SpendingDetailRequest request)
|
||||
@@ -653,64 +666,71 @@ public class BalanceService : IBalanceService
|
||||
|
||||
private async Task<List<ConsumptionTrendResponse>> GetConsumptionTrendsAsync(int type)
|
||||
{
|
||||
//todo
|
||||
|
||||
//消费总额
|
||||
var totalAmount = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out && x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.SumAsync(x => SqlFunc.Abs(x.Amount));
|
||||
|
||||
var list = new List<ConsumptionTrendResponse>();
|
||||
if (type == 1)
|
||||
{
|
||||
//月与总额对比
|
||||
var months = GetMonthsOfYear();
|
||||
var last = months.Last().AddMonths(1).AddDays(-1);
|
||||
var data = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.CostDate >= months.First() && x.CostDate <= last)
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out && x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.GroupBy(x => SqlFunc.DateValue(x.CostDate, DateType.Month))
|
||||
.Select(x => new { CostDate = SqlFunc.DateValue(x.CostDate, DateType.Month), Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount)) })
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var month in months)
|
||||
var total = data.Sum(x => x.Amount);
|
||||
|
||||
return months.Select(m =>
|
||||
{
|
||||
var res = new ConsumptionTrendResponse
|
||||
var current = data.Where(x => x.CostDate == m.Month).ToList();
|
||||
return new ConsumptionTrendResponse
|
||||
{
|
||||
DateTime = month,
|
||||
CurrentAmount = 0,
|
||||
TotalAmount = totalAmount
|
||||
DateTime = m.ToString("yyyy-MM"),
|
||||
CurrentAmount = current.Sum(x => x.Amount),
|
||||
TotalAmount = total
|
||||
};
|
||||
list.Add(res);
|
||||
}
|
||||
}).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
//日与总额对比
|
||||
var days = GetDaysOfMonth();
|
||||
var days = GetDaysOfCurrentMonth();
|
||||
var data = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.CostDate >= days.First() && x.CostDate <= days.Last())
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out && x.Status == BalanceRecordStatusEnumType.Valid)
|
||||
.GroupBy(x => x.CostDate)
|
||||
.Select(x => new { x.CostDate, Amount = SqlFunc.AggregateSum(SqlFunc.Abs(x.Amount)) })
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var day in days)
|
||||
var total = data.Sum(x => x.Amount);
|
||||
|
||||
return days.Select(d =>
|
||||
{
|
||||
var res = new ConsumptionTrendResponse
|
||||
var current = data.FirstOrDefault(x => x.CostDate == d);
|
||||
return new ConsumptionTrendResponse
|
||||
{
|
||||
DateTime = day,
|
||||
CurrentAmount = 0,
|
||||
TotalAmount = totalAmount
|
||||
DateTime = d.ToString("yyyy-MM-dd"),
|
||||
CurrentAmount = current?.Amount ?? 0,
|
||||
TotalAmount = total
|
||||
};
|
||||
list.Add(res);
|
||||
}
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前年所有月
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private List<string> GetMonthsOfYear()
|
||||
private List<DateTime> GetMonthsOfYear()
|
||||
{
|
||||
var months = new List<string>();
|
||||
var months = new List<DateTime>();
|
||||
// 获取当前年份
|
||||
int year = DateTime.Now.Year;
|
||||
// 遍历12个月份并添加到列表中
|
||||
for (int month = 1; month <= 12; month++)
|
||||
for (var month = 1; month <= 12; month++)
|
||||
{
|
||||
var monthDate = new DateTime(year, month, 1);
|
||||
months.Add(monthDate.ToString("yyyy-MM"));
|
||||
months.Add(monthDate);
|
||||
}
|
||||
|
||||
return months;
|
||||
@@ -720,22 +740,18 @@ public class BalanceService : IBalanceService
|
||||
/// 获取当前月所有天
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private List<string> GetDaysOfMonth()
|
||||
private List<DateTime> GetDaysOfCurrentMonth()
|
||||
{
|
||||
var days = new List<string>();
|
||||
|
||||
// 获取当前年份和月份
|
||||
int year = DateTime.Now.Year;
|
||||
int month = DateTime.Now.Month;
|
||||
var days = new List<DateTime>();
|
||||
|
||||
// 获取当前月份的第一天和最后一天
|
||||
var firstDayOfMonth = new DateTime(year, month, 1);
|
||||
var firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
|
||||
var lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
|
||||
|
||||
// 遍历当前月的所有日期并添加到列表中
|
||||
for (DateTime date = firstDayOfMonth; date <= lastDayOfMonth; date = date.AddDays(1))
|
||||
{
|
||||
days.Add(date.ToString("yyyy-MM-dd"));
|
||||
days.Add(date);
|
||||
}
|
||||
|
||||
return days;
|
||||
|
||||
Reference in New Issue
Block a user