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.

115 lines
4.2 KiB

using Autofac;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Job;
using log4net;
using Service.Init;
using Service.Station;
namespace Service.Job;
[Scope]
public class CountDayOrderJob : AbstractCronJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CountDayOrderJob));
public readonly ChargeOrderService _ChargeOrderService=AppInfo.Container.Resolve<ChargeOrderService>();
public readonly SwapOrderService _SwapOrderService=AppInfo.Container.Resolve<SwapOrderService>();
public readonly ExStationDayRunResultService _ExStationDayRunResultService=AppInfo.Container.Resolve<ExStationDayRunResultService>();
protected override Task Handle()
{
// 获取 ChargeOrder 和 SwapOrder 列表
List<ChargeOrder> orders = _ChargeOrderService.DayChargeOrder();
List<SwapOrder> daySwapOrder = _SwapOrderService.DaySwapOrder();
// 创建 ExchangeStationDayRunResult 实例
ExchangeStationDayRunResult dayCount = new ExchangeStationDayRunResult();
// 计算平均充电时间(分钟)
double avgMinutes = orders
.Where(order => order.StartTime.HasValue && order.EndTime.HasValue)
.Select(order => (order.EndTime.Value - order.StartTime.Value).TotalMinutes)
.DefaultIfEmpty(0.0)
.Average();
// 计算平均换电时间(分钟)
double avgRepTime = daySwapOrder
.Where(order => order.SwapBeginTime.HasValue && order.SwapEndTime.HasValue)
.Select(order => (order.SwapEndTime.Value - order.SwapBeginTime.Value).TotalMinutes)
.DefaultIfEmpty(0.0)
.Average();
// 找到最早的换电时间
SwapOrder firstSwapOrder = daySwapOrder
.Where(order => order.SwapBeginTime.HasValue)
.OrderBy(order => order.SwapBeginTime)
.FirstOrDefault();
// 找到最晚的换电时间
SwapOrder lastSwapOrder = daySwapOrder
.Where(order => order.SwapEndTime.HasValue)
.OrderByDescending(order => order.SwapEndTime)
.FirstOrDefault();
// 统计充电次数
int chgCount = orders
.Where(order => !string.IsNullOrEmpty(order.Sn))
.Select(order => order.Sn)
.Distinct()
.Count();
// 统计换电次数
int totalSwapCount = daySwapOrder
.Where(order => !string.IsNullOrEmpty(order.Sn))
.Select(order => order.Sn)
.Distinct()
.Count();
// 计算总共换电多少分钟
double totalMinutes = daySwapOrder
.Where(order => order.SwapBeginTime.HasValue && order.SwapEndTime.HasValue)
.Sum(order => (order.SwapEndTime.Value - order.SwapBeginTime.Value).TotalMinutes);
// 赋值
dayCount.AvgChgTime = avgMinutes.ToString("F2");
dayCount.AvgRepTime = avgRepTime.ToString("F2");
dayCount.SwapDate = DateTime.Now.AddDays(-1).ToString();
dayCount.FristSwapTime = firstSwapOrder?.SwapBeginTime?.ToString() ?? null;
dayCount.StopTime = lastSwapOrder?.SwapEndTime?.ToString() ?? null;
dayCount.ChgCount = chgCount;
dayCount.ToltalSwapCount = totalSwapCount;
dayCount.ToltalSwapAllTime = Math.Round(totalMinutes, 2);
dayCount.State = StaticStationInfo.StationStatus;
var dateTime = DateTime.Now;
var formattedDateTime = dateTime.ToString("yyyy-MM-dd HH:mm");
// 查询当前分钟的数据
var existingData = _ExStationDayRunResultService.QueryListByClause(i =>
i.CreatedTime.HasValue && formattedDateTime.Equals(i.CreatedTime.Value.ToString("yyyy-MM-dd HH:mm")));
if (!existingData.Any())
{
_ExStationDayRunResultService.Insert(dayCount);
}
return Task.CompletedTask;
}
protected override string Key()
{
return "CountDayOrderJob";
}
protected override string Cron()
{
return "0 0 1 * * ?";
}
}