首页统计接口

master
tq 2 months ago
parent e01c2da4d3
commit f9bc422181

@ -0,0 +1,33 @@
namespace Entity.Api.Req;
/// <summary>
/// 用电量统计
/// </summary>
public class ChargeCountReq
{
/// <summary>
/// 时间类型 1:天 2:月 3
/// </summary>
public int TimeType { get; set; }
/// <summary>
/// 充电机编号
/// </summary>
public string? ChargerNo { get; set; }
/// <summary>
/// 开始时间
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
/// 结束时间
/// </summary>
public DateTime? EndTime { get; set; }
/// <summary>页码</summary>
public int PageNum { get; set; } = 1;
/// <summary>页数</summary>
public int PageSize { get; set; } = 10;
}

@ -0,0 +1,20 @@
namespace Entity.Api.Resp;
/// <summary>
/// 用电量统计结果返回
/// </summary>
public class ChargeCountResp
{
/// <summary>
/// 时间
/// </summary>
public string? Time { get; set; }
/// <summary>
/// 充电度数
/// </summary>
public decimal? ChargeCount { get; set; }
/// <summary>
/// 充电机编号
/// </summary>
public string? ChargerNo { get; set; }
}

@ -0,0 +1,38 @@
namespace Entity.Api.Resp;
/// <summary>
/// 首页换电次数统计
/// </summary>
public class SwapOrderCountMonthResp
{
/// <summary>
/// 月换电总量
/// </summary>
public int SwapCount { get; set; }
/// <summary>
/// 月服务车辆
/// </summary>
public int VehicleCount { get; set; }
/// <summary>
/// 月充电次数
/// </summary>
public int ChargeFrequency { get; set; }
/// <summary>
/// 月充电量
/// </summary>
public decimal? ChargeCount { get; set; }
/// <summary>
/// 今日换电次数
/// </summary>
public int DaySwapCount { get; set; }
/// <summary>
/// 今日充电量
/// </summary>
public decimal? DayChargeCount { get; set; }
}

@ -0,0 +1,20 @@
namespace Entity.Api.Resp;
/// <summary>
/// 每日换电订单统计
/// </summary>
public class SwapOrderCountResp
{
/// <summary>
/// 天
/// </summary>
public int Day { get; set; }
/// <summary>
/// 换电数量
/// </summary>
public int Count { get; set; }
/// <summary>
/// 充电度数
/// </summary>
public decimal? ChargeCount { get; set; }
}

@ -51,4 +51,18 @@ public class SwapVehicleResp
/// 刹车状态 "0:未拉手刹1已拉手刹 其它值无效" /// 刹车状态 "0:未拉手刹1已拉手刹 其它值无效"
/// </summary> /// </summary>
public byte? Break { get; set; } public byte? Break { get; set; }
/// <summary>
/// Desc:放电池仓位号 亏电包仓号
/// Default:
/// Nullable:True
/// </summary>
public int? DownBatteryBinNo { get; set; }
/// <summary>
/// Desc:取电池仓位号 满电包仓号
/// Default:
/// Nullable:True
/// </summary>
public int? UpBatteryBinNo { get; set; }
} }

@ -192,4 +192,184 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
return QueryListByClause(predicate); return QueryListByClause(predicate);
} }
public async Task<List<ChargeCountResp>> GetElectricityConsumptionByTime(ChargeCountReq req)
{
List<ChargeCountResp> list = await GetElectricityConsumption(req, false);
list = list.OrderBy(x => x.Time).ToList();
return list;
}
public async Task<PageResult<ChargeCountResp>> GetElectricityConsumptionByChargerNo(ChargeCountReq req)
{
List<ChargeCountResp> resp = await GetElectricityConsumption(req, true);
resp = resp.OrderBy(x => x.Time).ToList();
int skip = (req.PageNum - 1) * req.PageSize;
int take = req.PageSize;
List<ChargeCountResp> respList = resp.Skip(skip).Take(take).ToList();
PageResult<ChargeCountResp> result = new PageResult<ChargeCountResp>();
result.PageNum = req.PageNum;
result.PageSize = req.PageSize;
result.Rows = respList;
result.ToTal = resp.Count;
return result;
}
public async Task<List<ChargeCountResp>> GetElectricityConsumption(ChargeCountReq req,
bool groupByChargerNo = false)
{
// 设置时间范围
SetTime(req);
Expression<Func<ChargeOrder, bool>> predicate = x => x.EndTime >= req.StartTime && x.EndTime <= req.EndTime;
if (!string.IsNullOrEmpty(req.ChargerNo))
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.ChargerNo == req.ChargerNo;
predicate = Expression.Lambda<Func<ChargeOrder, bool>>(
Expression.AndAlso(predicate.Body, condition2Expr.Body),
predicate.Parameters[0]);
}
List<ChargeOrder> chargeOrders = await QueryListByClauseAsync(predicate);
List<ChargeCountResp> result = new List<ChargeCountResp>();
if (req.TimeType == 1)
{
// 按小时分组统计
if (groupByChargerNo)
{
// 按 ChargerNo 和小时分组
result = chargeOrders
.Where(order => order.EndTime.HasValue && !string.IsNullOrEmpty(order.ChargerNo))
.GroupBy(order => new { order.ChargerNo, Hour = order.EndTime.Value.Hour })
.Select(group => new ChargeCountResp
{
ChargerNo = group.Key.ChargerNo,
Time = group.Key.Hour.ToString("00") + ":00",
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
else
{
// 仅按小时分组
result = chargeOrders
.Where(order => order.EndTime.HasValue)
.GroupBy(order => order.EndTime.Value.Hour)
.Select(group => new ChargeCountResp
{
Time = group.Key.ToString("00") + ":00",
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
}
else if (req.TimeType == 2)
{
// 按天分组统计
if (groupByChargerNo)
{
// 按 ChargerNo 和日期分组
result = chargeOrders
.Where(order => order.EndTime.HasValue && !string.IsNullOrEmpty(order.ChargerNo))
.GroupBy(order => new { order.ChargerNo, Date = order.EndTime.Value.Date })
.Select(group => new ChargeCountResp
{
ChargerNo = group.Key.ChargerNo,
Time = group.Key.Date.ToString("yyyy-MM-dd"),
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
else
{
// 仅按日期分组
result = chargeOrders
.Where(order => order.EndTime.HasValue)
.GroupBy(order => order.EndTime.Value.Date)
.Select(group => new ChargeCountResp
{
Time = group.Key.ToString("yyyy-MM-dd"),
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
}
else if (req.TimeType == 3)
{
// 按月分组统计
if (groupByChargerNo)
{
// 按 ChargerNo、年和月分组
result = chargeOrders
.Where(order => order.EndTime.HasValue && !string.IsNullOrEmpty(order.ChargerNo))
.GroupBy(order => new { order.ChargerNo, order.EndTime.Value.Year, order.EndTime.Value.Month })
.Select(group => new ChargeCountResp
{
ChargerNo = group.Key.ChargerNo,
Time = group.Key.Year + "-" + group.Key.Month.ToString("00"),
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
else
{
// 仅按年和月分组
result = chargeOrders
.Where(order => order.EndTime.HasValue)
.GroupBy(order => new { order.EndTime.Value.Year, order.EndTime.Value.Month })
.Select(group => new ChargeCountResp
{
Time = group.Key.Year + "-" + group.Key.Month.ToString("00"),
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
}
return result;
}
private static void SetTime(ChargeCountReq req)
{
if (req.StartTime.HasValue && req.EndTime.HasValue)
{
return ;
}
if (req.TimeType < 1 || req.TimeType > 3)
{
// 时间类型传值错误
throw new ArgumentException("时间类型传值错误");
}
if (req.TimeType == 1)
{
// 今天的开始时间
req.StartTime = DateTime.Today;
// 今天的结束时间
req.EndTime = DateTime.Today.AddDays(1).AddSeconds(-1);
}
else if (req.TimeType == 2)
{
// 本月的开始时间
req.StartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
// 本月的结束时间 下个月减1秒
req.EndTime = req.StartTime.Value.AddMonths(1).AddSeconds(-1);
}
else if (req.TimeType == 3)
{
// 今年的开始时间
req.StartTime = new DateTime(DateTime.Now.Year, 1, 1);
// 今年的结束时间 明年减1秒
req.EndTime = req.StartTime.Value.AddYears(1).AddSeconds(-1);
}
}
} }

@ -39,6 +39,9 @@ public class MonitorService
public MoveBinRecordRepository MoveBinRecordRepository { get; set; } public MoveBinRecordRepository MoveBinRecordRepository { get; set; }
public SwapOrderBatteryRepository _SwapOrderBatteryRepository { get; set; }
[DisplayName("获取移仓分页")] [DisplayName("获取移仓分页")]
public async Task<PageResult<MoveBinRecord>> Page(PageMoveBinRecordReq input) public async Task<PageResult<MoveBinRecord>> Page(PageMoveBinRecordReq input)
{ {
@ -135,6 +138,13 @@ public class MonitorService
i.LastChargeFinishTime.ToDateTime().Ticks) i.LastChargeFinishTime.ToDateTime().Ticks)
.TotalMinutes > .TotalMinutes >
StaticStationInfo.SwapFinishChargeTime).Count(); StaticStationInfo.SwapFinishChargeTime).Count();
if (monitorScreenResp.VehicleInfo.OrderNo!=null)
{
SwapOrderBattery queryByClauseAsync = _SwapOrderBatteryRepository.QueryByClause(u => u.SwapOrderSn == monitorScreenResp.VehicleInfo.OrderNo);
monitorScreenResp.VehicleInfo.DownBatteryBinNo = queryByClauseAsync.DownBatteryBinNo;
monitorScreenResp.VehicleInfo.UpBatteryBinNo = queryByClauseAsync.UpBatteryBinNo;
}
//SwapMonitorScreenResp monitorScreenResp = new() //SwapMonitorScreenResp monitorScreenResp = new()
//{ //{

@ -25,6 +25,8 @@ public class SwapOrderService : BaseServices<SwapOrder>
public SwapOrderReportCloudRepository SwapOrderReportCloudRepository { get; set; } public SwapOrderReportCloudRepository SwapOrderReportCloudRepository { get; set; }
public SwapOrderMgr SwapOrderMgr { get; set; } public SwapOrderMgr SwapOrderMgr { get; set; }
public ChargeOrderRepository ChargeOrderRepository { get; set; }
public AppCustomerVehicleRepository _appCustomerVehicleRepository{ get; set; } public AppCustomerVehicleRepository _appCustomerVehicleRepository{ get; set; }
public SwapOrderService(SwapOrderRepository dal) public SwapOrderService(SwapOrderRepository dal)
{ {
@ -234,4 +236,141 @@ public class SwapOrderService : BaseServices<SwapOrder>
return QueryListByClause(predicate); return QueryListByClause(predicate);
} }
/// <summary>
/// 获取当前月份每一天的换电订单数量统计
/// </summary>
/// <returns>返回包含日期和对应订单数量的列表</returns>
public async Task<List<SwapOrderCountResp>> GetDaySwapOrderCount()
{
DateTime now = DateTime.Now;
int year = now.Year;
int month = now.Month;
// 这个月的开始时间
DateTime firstDayOfMonth = new DateTime(year, month, 1);
// 这个月的最后一秒
DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddSeconds(-1);
// 查询换电订单
List<SwapOrder> swapOrders = await BaseDal.Queryable()
.Where(order => order.SwapEndTime >= firstDayOfMonth && order.SwapEndTime <= lastDayOfMonth)
.ToListAsync();
// 查询充电订单
List<ChargeOrder> chargeOrders = await ChargeOrderRepository.Queryable()
.Where(order => order.EndTime >= firstDayOfMonth && order.EndTime <= lastDayOfMonth)
.ToListAsync();
// 获取这个月的天数
int daysInMonth = DateTime.DaysInMonth(year, month);
// 生成天数的列表
List<int> dayList = Enumerable.Range(1, daysInMonth).ToList();
// 根据天分组、统计分组数量
var swapOrderCounts = swapOrders
.Where(order => order.SwapEndTime.HasValue)
.GroupBy(order => order.SwapEndTime.Value.Day)
.Select(group => new
{
Day = group.Key,
Count = group.Count()
})
.ToList();
// 根据天分组、统计充电量
var chargeOrderCounts = chargeOrders
.Where(order => order.EndTime.HasValue)
.GroupBy(order => order.EndTime.Value.Day)
.Select(group => new
{
Day = group.Key,
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
List<SwapOrderCountResp> dailyOrderCounts = dayList.Select(day => new SwapOrderCountResp
{
Day = day,
Count = swapOrderCounts.FirstOrDefault(oc => oc.Day == day)?.Count ?? 0,
ChargeCount = chargeOrderCounts.FirstOrDefault(cc => cc.Day == day)?.ChargeCount ?? 0
}).ToList();
return dailyOrderCounts;
}
/// <summary>
/// 查询月换电次数、换电车辆、充电统计
/// </summary>
/// <returns></returns>
public async Task<SwapOrderCountMonthResp> GetMonthSwapOrderCount()
{
DateTime now = DateTime.Now;
int year = now.Year;
int month = now.Month;
// 本月的开始时间 (00:00:00) 和最后一秒 (23:59:59)
DateTime firstDayOfMonth = new DateTime(year, month, 1);
DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddSeconds(-1);
// 查询本月换电订单
List<SwapOrder> swapOrders = await GetSwapOrdersInRange(firstDayOfMonth, lastDayOfMonth);
// 查询本月充电订单
List<ChargeOrder> chargeOrders = await GetChargeOrdersInRange(firstDayOfMonth, lastDayOfMonth);
SwapOrderCountMonthResp resp = new SwapOrderCountMonthResp
{
// 本月换电订单总数
SwapCount = swapOrders.Count,
VehicleCount = swapOrders
.Where(order => !string.IsNullOrEmpty(order.VehicleNo))
.Select(order => order.VehicleNo)
.Distinct()
.Count(),
// 本月充电订单总数
ChargeFrequency = chargeOrders.Count,
// 本月充电电量总和
ChargeCount = chargeOrders
.Where(order => order.ElecCount.HasValue)
.Sum(order => order.ElecCount ?? 0)
};
// 获取今天的开始时间和最后一秒
DateTime todayStart = DateTime.Today;
DateTime todayEnd = todayStart.AddDays(1).AddSeconds(-1);
// 查询当天换电订单
List<SwapOrder> daySwapOrder = await GetSwapOrdersInRange(todayStart, todayEnd);
// 查询当天充电订单
List<ChargeOrder> dayChargeOrder = await GetChargeOrdersInRange(todayStart, todayEnd);
resp.DaySwapCount = daySwapOrder.Count;
resp.DayChargeCount = dayChargeOrder
.Where(order => order.ElecCount.HasValue)
.Sum(order => order.ElecCount ?? 0);
return resp;
}
private async Task<List<SwapOrder>> GetSwapOrdersInRange(DateTime startTime, DateTime endTime)
{
return await BaseDal.Queryable()
.Where(order => order.SwapEndTime >= startTime && order.SwapEndTime <= endTime)
.ToListAsync();
}
private async Task<List<ChargeOrder>> GetChargeOrdersInRange(DateTime startTime, DateTime endTime)
{
return await ChargeOrderRepository.Queryable()
.Where(order => order.EndTime >= startTime && order.EndTime <= endTime)
.ToListAsync();
}
} }

@ -0,0 +1,73 @@
using Entity.Api.Req;
using Entity.Api.Resp;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using Service.Station;
namespace WebStarter.Controllers;
/// <summary>
/// 首页统计
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class HomeController : ControllerBase
{
private readonly SwapOrderService _swapOrderService;
private readonly ChargeOrderService _chargeOrderService;
/// <summary>
/// 注入
/// </summary>
/// <param name="swapOrderService"></param>
/// <param name="chargeOrderService"></param>
public HomeController(SwapOrderService swapOrderService
,ChargeOrderService chargeOrderService
)
{
_swapOrderService = swapOrderService;
_chargeOrderService = chargeOrderService;
}
/// <summary>
/// 统计当月的每一天换电数量、充电量
/// </summary>
/// <returns></returns>
[HttpGet("GetDaySwapOrderCount")]
public async Task<Result<List<SwapOrderCountResp>>> GetDaySwapOrderCount()
{
return Result<List<SwapOrderCountResp>>.Success(await _swapOrderService.GetDaySwapOrderCount(), "成功");
}
/// <summary>
/// 查询月换电次数、换电车辆、充电统计
/// </summary>
/// <returns></returns>
[HttpGet("GetMonthSwapOrderCount")]
public async Task<Result<SwapOrderCountMonthResp>> GetMonthSwapOrderCount()
{
return Result<SwapOrderCountMonthResp>.Success(await _swapOrderService.GetMonthSwapOrderCount(), "成功");
}
/// <summary>
/// 用电量查询
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost("GetElectricityConsumption")]
public async Task<Result<List<ChargeCountResp>>> GetElectricityConsumption([FromBody] ChargeCountReq req)
{
return Result<List<ChargeCountResp>>.Success(await _chargeOrderService.GetElectricityConsumptionByTime(req), "成功");
}
/// <summary>
/// 用电量查询、根据充电机分组
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
[HttpPost("GetElectricityChargerNoConsumption")]
public async Task<Result<PageResult<ChargeCountResp>>> GetElectricityChargerNoConsumption([FromBody] ChargeCountReq req)
{
return Result<PageResult<ChargeCountResp>>.Success(await _chargeOrderService.GetElectricityConsumptionByChargerNo(req), "成功");
}
}
Loading…
Cancel
Save