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.

126 lines
3.8 KiB

5 months ago
using Entity.Constant;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.AutoTask;
using log4net;
using Service.Init;
using Service.Station;
namespace Service.MyTask;
/// <summary>
/// 统计每日运行
/// </summary>
[Scope]
public class CountDayOrderTask : ITask
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CountDayOrderTask));
private volatile bool _stop;
public ChargeOrderService _ChargeOrderService { get; set; }
public SwapOrderService _SwapOrderService { get; set; }
public ExStationDayRunResultService _ExStationDayRunResultService { get; set; }
public string Name()
{
return "CountDayOrderTask";
}
public int Interval()
{
return 1000 * 86400;
}
public void Handle()
{
// 统计每日运行
CountOrder();
}
public bool Stoped()
{
return _stop;
}
public void Stop()
{
_stop = true;
}
public void ResetStop()
{
_stop = false;
}
public void CountOrder()
{
// 获取 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();
dayCount.AvgRepTime = avgRepTime.ToString();
dayCount.SwapDate = DateTime.Now.ToString();
dayCount.FristSwapTime = firstSwapOrder?.SwapBeginTime?.ToString() ?? null;
dayCount.StopTime = lastSwapOrder?.SwapEndTime?.ToString() ?? null;
dayCount.ChgCount = chgCount;
dayCount.ToltalSwapCount = totalSwapCount;
dayCount.ToltalSwapAllTime = totalMinutes;
dayCount.State = StaticStationInfo.StationStatus;
_ExStationDayRunResultService.Insert(dayCount);
}
}