using Entity.DbModel.Station;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.AutoTask;
using log4net;
using Repository.Station;
using Service.Init;
namespace Service.MyTask;
///
/// 换电订单算费用
///
[Scope]
public class SwapOrderSettleAnAccountTask : ITask
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SwapOrderSettleAnAccountTask));
private volatile bool _stop;
private SwapOrderBatteryRepository SwapOrderBatteryRepository { get; set; }
private SwapOrderRepository SwapOrderRepository { get; set; }
public SwapOrderSettleAnAccountTask(SwapOrderBatteryRepository swapOrderBatteryRepository,
SwapOrderRepository swapOrderRepository)
{
SwapOrderBatteryRepository = swapOrderBatteryRepository;
SwapOrderRepository = swapOrderRepository;
}
public string Name()
{
return "SwapOrderSettleAnAccountTask";
}
public int Interval()
{
return 1000 * 10;
}
public void Handle()
{
Log.Info("Begin SwapOrderSettleAnAccountTask");
List listSwapOrderBattery = SwapOrderBatteryRepository.QueryListByClause(i =>
i.DownNominalEnergy >0 &&
i.UpNominalEnergy >0 &&
i.UpBatterySoc >0 &&
i.DownBatterySoc >0 &&
i.SettleAnAccountFlag == null);
if (listSwapOrderBattery.Count > 0)
{
List list = listSwapOrderBattery
.Where(resp => !string.IsNullOrEmpty(resp.SwapOrderSn))
.Select(resp => resp.SwapOrderSn)
.ToList();
List swapOrderList = SwapOrderRepository
.QueryListByClause(swapOrder => list.Contains(swapOrder.Sn));
if (swapOrderList.Count <= 0)
{
return;
}
// 电费单价
decimal electricityTotalFee = StaticStationInfo.ElectricityTotalFee;
// 换电服务费用
decimal serviceTotalFee = StaticStationInfo.ServiceTotalFee;
// 附加费用
decimal appendTotalFee = StaticStationInfo.AppendTotalFee;
List updateSwapOrderBatteryList = new List();
List updateSwapOrderList = new List();
foreach (var swapOrderBattery in listSwapOrderBattery)
{
// 换上的满电包soc*满电包的电池标量
decimal? upNominalEnergy = swapOrderBattery.UpBatterySoc/100 * swapOrderBattery.UpNominalEnergy;
// 换下的亏电包soc*亏电包的电池标量
decimal? downNominalEnergy = swapOrderBattery.DownBatterySoc/100 * swapOrderBattery.DownNominalEnergy;
// 电量费用
decimal? electricCharge = (upNominalEnergy - downNominalEnergy) * electricityTotalFee;
if (upNominalEnergy != null && downNominalEnergy != null && electricCharge != null)
{
var matchingOrder = swapOrderList.FirstOrDefault(order => order.Sn == swapOrderBattery.SwapOrderSn);
if (matchingOrder != null)
{
matchingOrder.ServiceTotalFee = serviceTotalFee;
matchingOrder.ElectricityTotalFee = electricCharge;
matchingOrder.AppendTotalFee = appendTotalFee;
matchingOrder.TotalFee = matchingOrder.ServiceTotalFee + matchingOrder.ElectricityTotalFee +
matchingOrder.AppendTotalFee;
// 标记换电订单已经算了订单价钱
swapOrderBattery.SettleAnAccountFlag = 1;
updateSwapOrderBatteryList.Add(swapOrderBattery);
updateSwapOrderList.Add(matchingOrder);
}
}
}
if (updateSwapOrderList.Count > 0 && updateSwapOrderBatteryList.Count>0)
{
SwapOrderRepository.UpdateIgnoreNullColumns(updateSwapOrderList);
SwapOrderBatteryRepository.UpdateIgnoreNullColumns(updateSwapOrderBatteryList);
}
}
}
public bool Stoped()
{
return _stop;
}
public void Stop()
{
_stop = true;
}
public void ResetStop()
{
_stop = false;
}
}