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.

609 lines
24 KiB

using System.Linq.Expressions;
using AutoMapper;
6 months ago
using Entity.Api.Req;
using Entity.Api.Resp;
using Entity.DbModel.Station;
using Entity.Dto;
using Entity.Dto.Req;
using Entity.Dto.Resp;
6 months ago
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
6 months ago
using HybirdFrameworkCore.Utils;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
using Repository.Station;
6 months ago
namespace Service.Station;
/// <summary>
6 months ago
/// 充电订单
6 months ago
/// </summary>
[Scope("SingleInstance")]
public class ChargeOrderService : BaseServices<ChargeOrder>
{
ChargeOrderRepository chargeOrderRepository;
SwapOrderBatteryRepository _swapOrderBatteryRepository;
public ChargeOrderService(ChargeOrderRepository dal,SwapOrderBatteryRepository swapOrderBatteryRepository)
6 months ago
{
chargeOrderRepository = dal;
_swapOrderBatteryRepository = swapOrderBatteryRepository;
6 months ago
BaseDal = dal;
}
/// <summary>
6 months ago
/// 根据条件查询分页数据
6 months ago
/// </summary>
/// <param name="equipAlarmLevel"></param>
/// <returns></returns>
public PageResult<ChargeOrderResp> QueryChargeOrder(QueryChargeOrderReq chargeOrder)
{
6 months ago
6 months ago
//创建一个空的表达式树
6 months ago
Expression<Func<ChargeOrder, bool>> where = null;
6 months ago
//// 定义参数表达式
6 months ago
ParameterExpression parameter = Expression.Parameter(typeof(ChargeOrder), "u");
#region 构建动态查询树
where = queryTree(chargeOrder, where, parameter);
#endregion
//查询
return PageResult<ChargeOrderResp>.ConvertPage(chargeOrderRepository.QueryIPage(chargeOrder, where));
}
/// <summary>
/// 导出充电订单
/// </summary>
4 months ago
/// <param name="chargeOrder"></param>
/// <param name="language"></param>
/// <returns></returns>
4 months ago
public async Task<IActionResult> ExportChargeOrder(QueryChargeOrderReq chargeOrder,string language)
{
// 查询订单
List<ChargeOrder> chargeOrders = await QueryChargeOrderListAsync(chargeOrder);
4 months ago
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ChargeOrderDto, ChargeOrderExportResp>().ReverseMap();
cfg.CreateMap<ChargeOrderDto2, ChargeOrderExportResp>().ReverseMap();
cfg.CreateMap<ChargeOrderExportResp, ChargeOrder>().ReverseMap();
4 months ago
});
IMapper mapper = config.CreateMapper();
List<ChargeOrderExportResp> chargeOrderExportList = mapper.Map<List<ChargeOrderExportResp>>(chargeOrders);
if (chargeOrderExportList.Count > 0)
{
List<string> orderSnList = chargeOrderExportList.Select(resp => resp.SwapOrderSn).ToList();
List<SwapOrderBattery> swapOrderBatteryList = await _swapOrderBatteryRepository
.Queryable()
.In(swapOrderBattery => swapOrderBattery.SwapOrderSn, orderSnList)
.ToListAsync();
var swapOrderBatteryMap = new Dictionary<string, SwapOrderBattery>();
foreach (var swapOrderBattery in swapOrderBatteryList)
{
if (!string.IsNullOrEmpty(swapOrderBattery.SwapOrderSn)) swapOrderBatteryMap.Add(swapOrderBattery.SwapOrderSn, swapOrderBattery);
}
foreach (var order in chargeOrderExportList)
{
if (order.SwapOrderSn!=null&&swapOrderBatteryMap.TryGetValue(order.SwapOrderSn, out var swapOrderBattery))
{
try
{
int startSoc = order.StartSoc ?? 0;
int stopSoc = order.StopSoc ?? 0;
decimal downNominalEnergy = swapOrderBattery.DownNominalEnergy ?? 0;
order.StartBattery = downNominalEnergy > 0 ? (int?)(startSoc * downNominalEnergy) : 0;
order.StopBattery = downNominalEnergy > 0 ? (int?)(stopSoc * downNominalEnergy) : 0;
}
catch (Exception ex)
{
order.StartBattery = 0;
order.StopBattery = 0;
}
}
}
}
4 months ago
if (language == "en")
{
List<ChargeOrderDto2> list2 = mapper.Map<List<ChargeOrderDto2>>(chargeOrderExportList);
2 months ago
foreach (var chargeOrderDto in list2)
{
if (!string.IsNullOrEmpty(chargeOrderDto.ChargerNo))
{
try
{
string lastTwoDigits = chargeOrderDto.ChargerNo.Substring(chargeOrderDto.ChargerNo.Length - 2);
if (int.TryParse(lastTwoDigits, out int binNo))
{
chargeOrderDto.BinNO = binNo.ToString();
}
}
catch (Exception e)
{
chargeOrderDto.BinNO = "-1";
}
2 months ago
}
}
4 months ago
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_ChargeOrders.xlsx" };
}
else
{
List<ChargeOrderDto> list = mapper.Map<List<ChargeOrderDto>>(chargeOrderExportList);
2 months ago
foreach (var chargeOrderDto in list)
{
if (!string.IsNullOrEmpty(chargeOrderDto.ChargerNo))
{
try
{
string lastTwoDigits = chargeOrderDto.ChargerNo.Substring(chargeOrderDto.ChargerNo.Length - 2);
if (int.TryParse(lastTwoDigits, out int binNo))
{
chargeOrderDto.BinNO = binNo.ToString();
}
}
catch (Exception e)
{
chargeOrderDto.BinNO = "-1";
}
2 months ago
}
}
4 months ago
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "充电订单.xlsx" };
}
}
public async Task<IActionResult> ExportElectricityChargerNoConsumption(ChargeCountReq req,string language)
{
// 查询订单
List<ChargeCountResp> resp = await GetElectricityConsumptionExport(req, false);
if (resp.Count>0)
{
resp = resp.OrderBy(x => x.Time).ToList();
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ChargeCountDto, ChargeCountResp>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
if (language == "en")
{
List<ChargeCountDto> list2 = mapper.Map<List<ChargeCountDto>>(resp);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list2);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_chargeCount.xlsx" };
}
else
{
List<ChargeCountDto> list = mapper.Map<List<ChargeCountDto>>(resp);
IExcelExporter excelExporter = new ExcelExporter();
var res = await excelExporter.ExportAsByteArray(list);
return new FileStreamResult(new MemoryStream(res), "application/octet-stream")
{ FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "充电量统计.xlsx" };
}
}
/// <summary>
/// 查询需要导出订单
/// </summary>
/// <param name="chargeOrder"></param>
/// <returns></returns>
public async Task<List<ChargeOrder>> QueryChargeOrderListAsync(QueryChargeOrderReq chargeOrder)
{
Expression<Func<ChargeOrder, bool>> where = null;
ParameterExpression parameter = Expression.Parameter(typeof(ChargeOrder), "u");
6 months ago
6 months ago
#region 构建动态查询树
where = queryTree(chargeOrder, where, parameter);
#endregion
// 查询需要导出充电订单 不分页
return await chargeOrderRepository.QueryChargeOrderList(where);
}
private static Expression<Func<ChargeOrder, bool>>? queryTree(QueryChargeOrderReq chargeOrder, Expression<Func<ChargeOrder, bool>>? where, ParameterExpression parameter)
{
6 months ago
if (chargeOrder.Id != 0)
{
Expression<Func<ChargeOrder, bool>> condition1Expr = u => u.Id == chargeOrder.Id;
where = condition1Expr;
}
if (!string.IsNullOrEmpty(chargeOrder.Sn))
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.Sn == chargeOrder.Sn;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
if (!string.IsNullOrEmpty(chargeOrder.BatteryNo))
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.BatteryNo == chargeOrder.BatteryNo;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
6 months ago
if (!string.IsNullOrEmpty(chargeOrder.ChargerNo))
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.ChargerNo == chargeOrder.ChargerNo;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
// 构建时间范围条件
if (chargeOrder.StartTime != null && chargeOrder.StartTime != DateTime.MinValue && chargeOrder.EndTime != null && chargeOrder.EndTime != DateTime.MinValue)
6 months ago
{
Expression<Func<ChargeOrder, bool>> conditionExpr = u => u.StartTime >= chargeOrder.StartTime && u.StartTime <= chargeOrder.EndTime;
where = conditionExpr;
6 months ago
}
if (chargeOrder.ChargeTimeCount!=null)
6 months ago
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.ChargeTimeCount == chargeOrder.ChargeTimeCount;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
if (chargeOrder.StopReason != null)
6 months ago
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.StopReason == chargeOrder.StopReason;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
/*if (!string.IsNullOrEmpty(chargeOrder.ElecPriceModelVersion))
6 months ago
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.ElecPriceModelVersion == chargeOrder.ElecPriceModelVersion;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}*/
6 months ago
if (!string.IsNullOrEmpty(chargeOrder.SwapOrderSn))
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.SwapOrderSn == chargeOrder.SwapOrderSn;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
if (chargeOrder.CloudReportStatus != null)
6 months ago
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.CloudReportStatus == chargeOrder.CloudReportStatus;
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
if (!string.IsNullOrEmpty(chargeOrder.CloudChargeOrder))
6 months ago
{
Expression<Func<ChargeOrder, bool>> condition2Expr = u => u.CloudChargeOrder == chargeOrder.CloudChargeOrder;
6 months ago
where = where == null ? condition2Expr : Expression.Lambda<Func<ChargeOrder, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter);
}
return where;
6 months ago
}
6 months ago
/// <summary>
/// 获取每日充电订单
6 months ago
/// </summary>
/// <returns></returns>
public List<ChargeOrder> DayChargeOrder()
6 months ago
{
// 获取今天的日期
DateTime today = DateTime.Today;
6 months ago
// 昨天 00:00
DateTime startOfYesterday = today.AddDays(-1);
// 昨天 23:59:59
DateTime endOfYesterday = startOfYesterday.AddDays(1).AddTicks(-1);
Expression<Func<ChargeOrder, bool>> predicate = x => x.StartTime >= startOfYesterday && x.StartTime <= endOfYesterday;
6 months ago
return QueryListByClause(predicate);
6 months ago
}
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;
}
public async Task<List<ChargeCountResp>> GetElectricityConsumptionExport(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, DateHour = order.EndTime.Value.ToString("yyyy-MM-dd HH") })
.Select(group => new ChargeCountResp
{
ChargerNo = group.Key.ChargerNo,
Time = group.Key.DateHour + ":00", // 保留年月日和小时
ChargeCount = group.Sum(order => order.ElecCount ?? 0)
})
.ToList();
}
else
{
// 仅按小时分组
result = chargeOrders
.Where(order => order.EndTime.HasValue)
.GroupBy(order => order.EndTime.Value.ToString("yyyy-MM-dd HH"))
.Select(group => new ChargeCountResp
{
Time = group.Key + ":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);
}
}
6 months ago
}