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.

135 lines
4.5 KiB

using Autofac;
using Entity.Constant;
using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac;
using log4net;
using Repository.Station;
using Service.Execute;
using Service.Execute.Api;
using Service.Execute.Model;
namespace Service.BusinessTask.MyTask;
/// <summary>
/// 充电结束上报云平台task
/// </summary>
public class SwapOrderReportCloudTask : AbstractTaskHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SwapOrderReportCloudTask));
private readonly SwapOrderReportCloudRepository _reportCloudRepository =
AppInfo.Container.Resolve<SwapOrderReportCloudRepository>();
private readonly SwapOrderRepository _swapOrderRepository =
AppInfo.Container.Resolve<SwapOrderRepository>();
private readonly SwapOrderBatteryRepository _swapOrderBatteryRepository =
AppInfo.Container.Resolve<SwapOrderBatteryRepository>();
private readonly BinInfoRepository _binInfoRepository =
AppInfo.Container.Resolve<BinInfoRepository>();
protected override int Interval()
{
return 20 * 1000;
}
protected override void Handle()
{
List<SwapOrderReportCloud> list = _reportCloudRepository.QueryListByClause(i => i.CloudReportStatus == 0);
if (list.Count <= 0)
{
return;
}
List<int> swapOrderIds = list.Select(i => i.SwapOrderId).ToList();
List<SwapOrder> swapOrders =
_swapOrderRepository.QueryListByClause(i => swapOrderIds.Contains(i.Id) && i.CloudReportStatus == 0);
if (swapOrders.Count <= 0)
{
return;
}
List<string> swapOrderSn = swapOrders.Select(i => i.Sn).ToList();
List<SwapOrderBattery> batterys =
_swapOrderBatteryRepository.QueryListByClause(i => swapOrderSn.Contains(i.SwapOrderSn));
if (batterys.Count <= 0)
{
return;
}
IDictionary<string, SwapOrderBattery> dictionary = batterys.ToDictionary(i => i.SwapOrderSn);
List<string> downBatteryBinNos = batterys.Select(i => i.DownBatteryBinNo.ToString()).ToList();
Dictionary<string, BinInfo> binInfosMap = _binInfoRepository
.QueryListByClause(i => downBatteryBinNos.Contains(i.No)).ToDictionary(i => i.No);
List<SwapOrderBattery> updateDbBattery = new List<SwapOrderBattery>();
List<SwapOrder> updateDbOrder = new List<SwapOrder>();
List<SwapOrderReportCloud> updateDbCloudReport = new List<SwapOrderReportCloud>();
Dictionary<int, SwapOrderReportCloud> swapOrderReportClouds = list.ToDictionary(i => i.SwapOrderId);
foreach (var swapOrder in swapOrders)
{
dictionary.TryGetValue(swapOrder.Sn, out SwapOrderBattery swapOrderBattery);
if (null == swapOrderBattery)
{
continue;
}
if (null == swapOrderBattery.DownBatteryNo)
{
//更新换下电池包
binInfosMap.TryGetValue(swapOrderBattery.DownBatteryBinNo.ToString(), out BinInfo info);
if (info.BatteryNo == null || info.BatteryNo == "")
{
continue;
}
swapOrderBattery.DownBatteryNo = info.BatteryNo;
swapOrderBattery.DownBatterySoc = info.Soc;
swapOrderBattery.DownBatterySoe = info.Soe;
updateDbBattery.Add(swapOrderBattery);
}
//TODO::获取本日第几次换电
var count = _swapOrderRepository.GetCount(i =>
i.SwapResult == 1 && i.SwapEndTime >= DateTime.Today && i.SwapEndTime < swapOrder.SwapEndTime);
var uploadSwapOrder = CloudApi.UploadSwapOrder(swapOrder, count++, swapOrderBattery, 1);
if (uploadSwapOrder == 0)
{
swapOrder.CloudReportStatus = 1;
updateDbOrder.Add(swapOrder);
swapOrderReportClouds.TryGetValue(swapOrder.Id, out SwapOrderReportCloud value);
value.CloudReportStatus = 1;
updateDbCloudReport.Add(value);
}
}
if (updateDbBattery.Count > 0)
{
_swapOrderBatteryRepository.Update(updateDbBattery);
}
if (updateDbOrder.Count > 0)
{
_swapOrderRepository.Update(updateDbOrder);
}
if (updateDbCloudReport.Count > 0)
{
_reportCloudRepository.Update(updateDbCloudReport);
}
}
protected override string Name()
{
return "SwapOrderReportCloudTask";
}
}