You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
13 KiB

6 months ago
using Entity.Api.Req;
using Entity.Api.Resp;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Repository.Station;
using SqlSugar;
using System.Linq.Expressions;
using AutoMapper;
using Entity.Dto;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
using Repository.System.App;
using Service.Mgr;
6 months ago
namespace Service.Station;
6 months ago
/// <summary>
/// 充电订单
6 months ago
/// </summary>
[Scope("SingleInstance")]
public class SwapOrderService : BaseServices<SwapOrder>
{
public SwapOrderRepository swapOrderRepository { get; set; }
public SwapOrderReportCloudRepository SwapOrderReportCloudRepository { get; set; }
public SwapOrderMgr SwapOrderMgr { get; set; }
public ChargeOrderRepository ChargeOrderRepository { get; set; }
public AppCustomerVehicleRepository _appCustomerVehicleRepository{ get; set; }
6 months ago
public SwapOrderService(SwapOrderRepository dal)
{
BaseDal = dal;
}
/// <summary>
/// 查询需要导出换电订单
/// </summary>
/// <param name="swapOrder"></param>
/// <returns></returns>
public async Task<List<SwapOrder>> QuerySwapOrderListAsync(QuerySwapOrderPageReq swapOrder)
{
Expression<Func<SwapOrder, bool>> where = null;
ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u");
#region 构建动态查询树
where = queryTree(swapOrder, where, parameter);
#endregion
// 查询需要导出充电订单 不分页
return await swapOrderRepository.QuerySwapOrderList(where);
}
6 months ago
/// <summary>
/// 根据条件查询分页数据
6 months ago
/// </summary>
/// <param name="equipAlarmLevel"></param>
/// <returns></returns>
public PageResult<SwapOrderResp> QuerySwapOrder(QuerySwapOrderPageReq swapOrder)
{
//创建一个空的表达式树
6 months ago
Expression<Func<SwapOrder, bool>> where = null;
//// 定义参数表达式
6 months ago
ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u");
#region 构建动态查询树
where = queryTree(swapOrder, where, parameter);
#endregion
//查询
//var swapOrderResp = PageResult<SwapOrderResp>.ConvertPage(swapOrderRepository.QueryIPageByCause(swapOrder, where));
//for (int i = 0; i < swapOrderResp.Rows.Count; i++)
//{
// swapOrderResp.Rows[0].BatteryList.AddRange()
//}
//List<SwapOrderBatteryResp> BatteryList = new List<SwapOrderBatteryResp>();
//List<SwapOrderStepResp> StepList = new List<SwapOrderStepResp>();
return PageResult<SwapOrderResp>.ConvertPage(swapOrderRepository.QueryIPage(swapOrder, where));
}
public PageResult<SwapOrderResp> AppQuerySwapOrder(QuerySwapOrderPageReq swapOrder)
{
//创建一个空的表达式树
Expression<Func<SwapOrder, bool>> where = null;
//// 定义参数表达式
ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u");
where = queryTreeApp(swapOrder, where, parameter);
if (where==null)
{
return new PageResult<SwapOrderResp>();
}
return PageResult<SwapOrderResp>.ConvertPage(swapOrderRepository.QueryIPage(swapOrder, where));
}
private Expression<Func<SwapOrder, bool>>? queryTreeApp(QuerySwapOrderPageReq swapOrder,
Expression<Func<SwapOrder, bool>>? where, ParameterExpression parameter)
{
if (swapOrder.Time != null)
{
DateTime startTime = new DateTime(swapOrder.Time.Value.Year, swapOrder.Time.Value.Month, 1, 0, 0, 0);
DateTime endTime = startTime.AddMonths(1).AddSeconds(-1);
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.CreatedTime >= startTime && u.CreatedTime <= endTime;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
if (swapOrder.CustomerId.HasValue)
{
List<string> vinList = _appCustomerVehicleRepository.GetVinList(swapOrder.CustomerId);
if (vinList != null && vinList.Count > 0)
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => vinList.Contains(u.VehicleVin);
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
}
}
else
{
return null;
}
return where;
}
private Expression<Func<SwapOrder, bool>>? queryTree(QuerySwapOrderPageReq swapOrder,
Expression<Func<SwapOrder, bool>>? where, ParameterExpression parameter)
{
6 months ago
if (!string.IsNullOrEmpty(swapOrder.Sn))
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.Sn == swapOrder.Sn;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
6 months ago
}
6 months ago
if (!string.IsNullOrEmpty(swapOrder.VehicleNo))
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.VehicleNo == swapOrder.VehicleNo;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
6 months ago
}
if (swapOrder.CustomerId.HasValue)
{
List<string> vinList = _appCustomerVehicleRepository.GetVinList(swapOrder.CustomerId);
if (vinList != null && vinList.Count > 0)
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => vinList.Contains(u.VehicleVin);
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
}
}
6 months ago
if (!string.IsNullOrEmpty(swapOrder.VehicleMac))
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.VehicleMac == swapOrder.VehicleMac;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
6 months ago
}
6 months ago
if (!string.IsNullOrEmpty(swapOrder.VehicleVin))
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.VehicleVin == swapOrder.VehicleVin;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
6 months ago
}
if (swapOrder.SwapBeginTime != null && swapOrder.SwapEndTime != null)
6 months ago
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.SwapBeginTime >= swapOrder.SwapBeginTime && u.SwapBeginTime <= swapOrder.SwapEndTime;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
6 months ago
}
6 months ago
if (swapOrder.SwapResult != 0)
{
Expression<Func<SwapOrder, bool>> condition2Expr = u => u.SwapResult == swapOrder.SwapResult;
where = where == null
? condition2Expr
: Expression.Lambda<Func<SwapOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
6 months ago
}
return where;
6 months ago
}
/// <summary>
/// 获取每日换电订单
/// </summary>
/// <returns></returns>
public List<SwapOrder> DaySwapOrder()
{
// 获取今天的日期
DateTime today = DateTime.Today;
// 昨天 00:00
DateTime startOfYesterday = today.AddDays(-1);
// 昨天 23:59:59
DateTime endOfYesterday = startOfYesterday.AddDays(1).AddTicks(-1);
Expression<Func<SwapOrder, bool>> predicate = x => x.SwapBeginTime >= startOfYesterday && x.SwapBeginTime <= endOfYesterday;
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();
}
6 months ago
}