feat 日/月消费走势接口
This commit is contained in:
@@ -142,4 +142,24 @@ public class BalancesController : ControllerBase
|
||||
{
|
||||
return await _balanceService.GetConsumeNumberAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 消费走势
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("consumptiontrends")]
|
||||
public async Task<List<ConsumptionTrendResponse>> GetConsumptionTrendsAsync([FromQuery] ConsumptionTrendRequest request)
|
||||
{
|
||||
return await _balanceService.GetConsumptionTrendsAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 餐补走势
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("supplementtrend")]
|
||||
public async Task<List<ConsumptionTrendResponse>> GetSupplementTrendsAsync([FromQuery] ConsumptionTrendRequest request)
|
||||
{
|
||||
return await _balanceService.GetSupplementTrendsAsync(request);
|
||||
}
|
||||
}
|
||||
|
||||
12
Src/Dlhis.Canteen/Order/Request/ConsumptionTrendRequest.cs
Normal file
12
Src/Dlhis.Canteen/Order/Request/ConsumptionTrendRequest.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Request
|
||||
{
|
||||
public class ConsumptionTrendRequest
|
||||
{
|
||||
[Required]
|
||||
[EnumDataType(typeof(ConsumptionTrendEnumType))]
|
||||
public ConsumptionTrendEnumType Type { get; set; }
|
||||
}
|
||||
}
|
||||
20
Src/Dlhis.Canteen/Order/Response/ConsumptionTrendResponse.cs
Normal file
20
Src/Dlhis.Canteen/Order/Response/ConsumptionTrendResponse.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Dlhis.Canteen.Order.Response
|
||||
{
|
||||
public class ConsumptionTrendResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间
|
||||
/// </summary>
|
||||
public string DateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当日/月消费总额
|
||||
/// </summary>
|
||||
public decimal CurrentAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消费总额
|
||||
/// </summary>
|
||||
public decimal TotalAmount { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Dlhis.Canteen.Order.Request;
|
||||
using Dlhis.Canteen.Order.Response;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Services;
|
||||
|
||||
@@ -25,4 +26,8 @@ public interface IBalanceService : IScopeDependency
|
||||
Task<Pagination<BalanceRecordAnalysisRespones>> BalanceRecordAnalysisAsync(BalanceRecordAnalysisRequest request);
|
||||
|
||||
Task<ConsumeNumberResponse> GetConsumeNumberAsync();
|
||||
|
||||
Task<List<ConsumptionTrendResponse>> GetConsumptionTrendsAsync(ConsumptionTrendRequest request);
|
||||
|
||||
Task<List<ConsumptionTrendResponse>> GetSupplementTrendsAsync(ConsumptionTrendRequest request);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,9 @@ using Kurisu.Core.User.Abstractions;
|
||||
using Kurisu.SqlSugar.Attributes;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Dlhis.Canteen.Order.Services.Implements;
|
||||
|
||||
@@ -584,6 +586,104 @@ public class BalanceService : IBalanceService
|
||||
return res;
|
||||
|
||||
}
|
||||
public async Task<List<ConsumptionTrendResponse>> GetConsumptionTrendsAsync(ConsumptionTrendRequest request)
|
||||
{
|
||||
var bs = await _dbContext.Queryable<BalanceRecordEntity>()
|
||||
.Where(x => x.Type == BalanceRecordTypeEnumType.Out)
|
||||
.ToListAsync();
|
||||
//消费总额
|
||||
var totalAmount = Math.Round(Math.Abs(bs.Sum(x => x.Amount)), 2);
|
||||
|
||||
var list = new List<ConsumptionTrendResponse>();
|
||||
if (request.Type == ConsumptionTrendEnumType.Month)
|
||||
{
|
||||
//月与总额对比
|
||||
var months = GetMonthsOfYear();
|
||||
|
||||
foreach (var month in months)
|
||||
{
|
||||
var currentAmount = Math.Round(Math.Abs(bs.Where(x => x.CostDateTime.ToString("yyyy-MM") == month)
|
||||
.Sum(x => x.Amount)), 2);
|
||||
|
||||
var res = new ConsumptionTrendResponse
|
||||
{
|
||||
DateTime = month,
|
||||
CurrentAmount = currentAmount,
|
||||
TotalAmount = totalAmount
|
||||
};
|
||||
list.Add(res);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//日与总额对比
|
||||
var days = GetDaysOfMonth();
|
||||
|
||||
foreach (var day in days)
|
||||
{
|
||||
var currentAmount = Math.Round(Math.Abs(bs.Where(x => x.CostDateTime.ToString("yyyy-MM-dd") == day)
|
||||
.Sum(x => x.Amount)), 2);
|
||||
|
||||
var res = new ConsumptionTrendResponse
|
||||
{
|
||||
DateTime = day,
|
||||
CurrentAmount = currentAmount,
|
||||
TotalAmount = totalAmount
|
||||
};
|
||||
list.Add(res);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public async Task<List<ConsumptionTrendResponse>> GetSupplementTrendsAsync(ConsumptionTrendRequest request)
|
||||
{
|
||||
//todo
|
||||
return new List<ConsumptionTrendResponse>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前年所有月
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private List<string> GetMonthsOfYear()
|
||||
{
|
||||
List<string> months = new List<string>();
|
||||
// 获取当前年份
|
||||
int year = DateTime.Now.Year;
|
||||
// 遍历12个月份并添加到列表中
|
||||
for (int month = 1; month <= 12; month++)
|
||||
{
|
||||
DateTime monthDate = new DateTime(year, month, 1);
|
||||
months.Add(monthDate.ToString("yyyy-MM"));
|
||||
}
|
||||
return months;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前月所有天
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private List<string> GetDaysOfMonth()
|
||||
{
|
||||
List<string> days = new List<string>();
|
||||
|
||||
// 获取当前年份和月份
|
||||
int year = DateTime.Now.Year;
|
||||
int month = DateTime.Now.Month;
|
||||
|
||||
// 获取当前月份的第一天和最后一天
|
||||
DateTime firstDayOfMonth = new DateTime(year, month, 1);
|
||||
DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
|
||||
|
||||
// 遍历当前月的所有日期并添加到列表中
|
||||
for (DateTime date = firstDayOfMonth; date <= lastDayOfMonth; date = date.AddDays(1))
|
||||
{
|
||||
days.Add(date.ToString("yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
|
||||
//************************************************************** help methods ******************************************************************
|
||||
|
||||
24
Src/Dlhis.Entity/Canteen/Enums/ConsumptionTrendEnumType.cs
Normal file
24
Src/Dlhis.Entity/Canteen/Enums/ConsumptionTrendEnumType.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dlhis.Entity.Canteen.Enums
|
||||
{
|
||||
public enum ConsumptionTrendEnumType
|
||||
{
|
||||
/// <summary>
|
||||
/// 日
|
||||
/// </summary>
|
||||
[Description("日")]
|
||||
Day = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 月
|
||||
/// </summary>
|
||||
[Description("月")]
|
||||
Month = 2,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user