|
|
|
@ -5,6 +5,7 @@ using Entity.Api.Resp;
|
|
|
|
|
using Entity.DbModel.Station;
|
|
|
|
|
using Entity.Dto;
|
|
|
|
|
using Entity.Dto.Req;
|
|
|
|
|
using Entity.Dto.Resp;
|
|
|
|
|
using HybirdFrameworkCore.Autofac.Attribute;
|
|
|
|
|
using HybirdFrameworkCore.Entity;
|
|
|
|
|
using HybirdFrameworkCore.Utils;
|
|
|
|
@ -20,9 +21,11 @@ namespace Service.Station;
|
|
|
|
|
public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
{
|
|
|
|
|
ChargeOrderRepository chargeOrderRepository;
|
|
|
|
|
public ChargeOrderService(ChargeOrderRepository dal)
|
|
|
|
|
SwapOrderBatteryRepository _swapOrderBatteryRepository;
|
|
|
|
|
public ChargeOrderService(ChargeOrderRepository dal,SwapOrderBatteryRepository swapOrderBatteryRepository)
|
|
|
|
|
{
|
|
|
|
|
chargeOrderRepository = dal;
|
|
|
|
|
_swapOrderBatteryRepository = swapOrderBatteryRepository;
|
|
|
|
|
BaseDal = dal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -62,19 +65,74 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<ChargeOrderDto, ChargeOrder>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<ChargeOrderDto2, ChargeOrder>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<ChargeOrderDto, ChargeOrderExportResp>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<ChargeOrderDto2, ChargeOrderExportResp>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<ChargeOrderExportResp, ChargeOrder>().ReverseMap();
|
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (language == "en")
|
|
|
|
|
{
|
|
|
|
|
List<ChargeOrderDto2> list2 = mapper.Map<List<ChargeOrderDto2>>(chargeOrders);
|
|
|
|
|
List<ChargeOrderDto2> list2 = mapper.Map<List<ChargeOrderDto2>>(chargeOrderExportList);
|
|
|
|
|
foreach (var chargeOrderDto in list2)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(chargeOrderDto.ChargerNo))
|
|
|
|
|
{
|
|
|
|
|
chargeOrderDto.BinNO = 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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IExcelExporter excelExporter = new ExcelExporter();
|
|
|
|
@ -84,12 +142,23 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
List<ChargeOrderDto> list = mapper.Map<List<ChargeOrderDto>>(chargeOrders);
|
|
|
|
|
List<ChargeOrderDto> list = mapper.Map<List<ChargeOrderDto>>(chargeOrderExportList);
|
|
|
|
|
foreach (var chargeOrderDto in list)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(chargeOrderDto.ChargerNo))
|
|
|
|
|
{
|
|
|
|
|
chargeOrderDto.BinNO = 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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IExcelExporter excelExporter = new ExcelExporter();
|
|
|
|
@ -102,7 +171,11 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
public async Task<IActionResult> ExportElectricityChargerNoConsumption(ChargeCountReq req,string language)
|
|
|
|
|
{
|
|
|
|
|
// 查询订单
|
|
|
|
|
List<ChargeCountResp> resp = await GetElectricityConsumption(req, false);
|
|
|
|
|
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();
|
|
|
|
@ -174,7 +247,7 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
// 构建时间范围条件
|
|
|
|
|
if (chargeOrder.StartTime != null && chargeOrder.StartTime != DateTime.MinValue && chargeOrder.EndTime != null && chargeOrder.EndTime != DateTime.MinValue)
|
|
|
|
|
{
|
|
|
|
|
Expression<Func<ChargeOrder, bool>> conditionExpr = u => u.CreatedTime >= chargeOrder.StartTime && u.CreatedTime <= chargeOrder.EndTime;
|
|
|
|
|
Expression<Func<ChargeOrder, bool>> conditionExpr = u => u.StartTime >= chargeOrder.StartTime && u.StartTime <= chargeOrder.EndTime;
|
|
|
|
|
where = conditionExpr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -380,6 +453,124 @@ public class ChargeOrderService : BaseServices<ChargeOrder>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|