|
|
|
|
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;
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
|
|
|
|
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 * 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle()
|
|
|
|
|
{
|
|
|
|
|
// 统计每日运行
|
|
|
|
|
return;
|
|
|
|
|
// CountOrder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool Stoped()
|
|
|
|
|
{
|
|
|
|
|
return _stop;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
_stop = true;
|
|
|
|
|
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetStop()
|
|
|
|
|
{
|
|
|
|
|
_stop = false;
|
|
|
|
|
ScheduleNextRun();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public CountDayOrderTask()
|
|
|
|
|
{
|
|
|
|
|
ScheduleNextRun();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ScheduleNextRun()
|
|
|
|
|
{
|
|
|
|
|
if (_stop)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
|
DateTime nextRun = new DateTime(now.Year, now.Month, now.Day, 19, 21, 0);
|
|
|
|
|
if (now > nextRun)
|
|
|
|
|
{
|
|
|
|
|
nextRun = nextRun.AddDays(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeSpan timeToGo = nextRun - now;
|
|
|
|
|
_timer = new Timer(x =>
|
|
|
|
|
{
|
|
|
|
|
if (!_stop)
|
|
|
|
|
{
|
|
|
|
|
CountOrder();
|
|
|
|
|
ScheduleNextRun();
|
|
|
|
|
}
|
|
|
|
|
}, null, timeToGo, Timeout.InfiniteTimeSpan);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("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;
|
|
|
|
|
_ExStationDayRunResultService.Insert(dayCount);
|
|
|
|
|
}
|
|
|
|
|
}
|