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(); public readonly SwapOrderService _SwapOrderService=AppInfo.Container.Resolve(); public readonly ExStationDayRunResultService _ExStationDayRunResultService=AppInfo.Container.Resolve(); protected override Task Handle() { // 获取 ChargeOrder 和 SwapOrder 列表 List orders = _ChargeOrderService.DayChargeOrder(); List 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.RunStartTime = DateTime.Now.AddDays(-1).ToString(); dayCount.RunEndTime = DateTime.Now.ToString(); dayCount.ToltalTimeCount = 24 * 60; 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 * * ?"; } }