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.

338 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Autofac;
using AutoMapper;
using Common.Util;
using Entity.Constant;
using Entity.DbModel.Station;
using Entity.Dto;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using log4net;
using NewLife.Serialization;
using Newtonsoft.Json;
using Repository.Station;
using Service.Execute.Api;
using Service.Execute.Model;
using Service.Init;
using Service.Plc.Client;
using Service.Station;
using SqlSugar;
namespace Service.Execute.Utils;
[Scope("SingleInstance")]
public class CommonMgr
{
private static readonly ILog _log = LogManager.GetLogger(typeof(CommonMgr));
public SwapOrderStepService _swapOrderStepService { get; set; }
public SwapOrderRepository _swapOrderRepository { get; set; }
public BinInfoRepository _binInfoRepository { get; set; }
public SwapAmtOrderRepository _amtOrderRepository { get; set; }
public SwapOrderBatteryRepository _swapOrderBatteryRepository { get; set; }
public SwapOrderReportCloudRepository _swapOrderReportCloudRepository { get; set; }
public MoveBinRecordRepository MoveBinRecordRepository { get; set; }
public BatteryGroupRepository BatteryGroupRepository { get; set; }
/// <summary>
/// 新增小步状态
/// </summary>
public void InsertStep(InfoEnum.BusinessSwappingStep step, SwappingStateMachine machine)
{
var stepModel = new StepModel
{
StepName = BaseEnumExtensions.GetDescription(step),
StepNo = (int)step,
StartTime = DateTime.Now,
Status = 1
};
machine.StepModel[stepModel.StepNo.ToString()] = stepModel;
_swapOrderStepService.InsertSwapStepForSwapMain(step,
machine.StepSort++, machine.SwapOrder.Sn);
}
/// <summary>
/// 新增换电订单
/// </summary>
/// <param name="swapOrder"></param>
/// <returns></returns>
public SwapOrder SaveOrder(SwapOrder swapOrder)
{
swapOrder.SwapWay = (int)StationConstant.StationWay.Auto;
swapOrder.CloudReportStatus = 0;
swapOrder.SwapWay = 1;
swapOrder.CreatedTime = DateTime.Now;
_swapOrderRepository.Insert(swapOrder);
return swapOrder;
}
public void SaveSwapBattery(SwappingStateMachine machine)
{
List<BinInfo> upBin = machine.SwapOrderBatteryInfo.UpBinInfo;
List<BinInfo> inBin = machine.SwapOrderBatteryInfo.InBinInfo;
List<SwapOrderBattery> swapOrderBatteryList = new List<SwapOrderBattery>();
for (int i = 0; i < upBin.Count; i++)
{
SwapOrderBattery swapOrderBattery = new SwapOrderBattery()
{
SwapOrderSn = machine.SwapOrder.Sn,
UpBatterySoc = upBin[i].Soc,
UpBatteryNo = upBin[i].BatteryNo,
UpBatterySoe = upBin[i].Soe,
UpBatteryBinNo = int.Parse(upBin[i].No),
DownBatteryBinNo = int.Parse(inBin[i].No),
DownBatteryNo = inBin[i].BatteryNo,
DownBatterySoc = inBin[i].Soc,
DownBatterySoe = inBin[i].Soe,
CreatedTime = DateTime.Now
};
swapOrderBatteryList.Add(swapOrderBattery);
}
_swapOrderBatteryRepository.Insert(swapOrderBatteryList);
}
/// <summary>
/// 1.锁仓 锁两个
/// 2.更改预约单
/// </summary>
public void LockBinAndUpdateAmt(SwapOrderBatteryInfo orderBatteryInfo)
{
//
var configBinInfo =
new MapperConfiguration(cfg => cfg.CreateMap<BinInfo, BinInfo>().ReverseMap());
IMapper mapperBinInfo = configBinInfo.CreateMapper();
List<BinInfo> dbBinInfo = mapperBinInfo.Map<List<BinInfo>>(orderBatteryInfo.UpBinInfo);
dbBinInfo[0].AmtLock = (int)InfoEnum.AmtBatLockStatus.Lock;
dbBinInfo[1].AmtLock = (int)InfoEnum.AmtBatLockStatus.Lock;
_binInfoRepository.Update(dbBinInfo);
if (!orderBatteryInfo.isAmt)
{
return;
}
var configAmt =
new MapperConfiguration(cfg => cfg.CreateMap<SwapAmtOrder, SwapAmtOrder>().ReverseMap());
IMapper mapperAmt = configAmt.CreateMapper();
SwapAmtOrder swapAmtOrder = mapperAmt.Map<SwapAmtOrder>(orderBatteryInfo.swapAmtOrder);
swapAmtOrder.Status = (int)InfoEnum.AmtOrderStatus.Swapping;
_amtOrderRepository.Update(swapAmtOrder);
}
/// 1.仓位状态:启动
/// 2.电池在位
/// 3.充电状态:不在充电中
/// 4.未锁定
/// 5.最后一次结束充电时间> ()
/// 6.soc >()
public SwapOrderBatteryInfo SelectPackNotArm(SwappingStateMachine machine)
{
SwapOrderBatteryInfo orderBatteryInfo = new SwapOrderBatteryInfo();
UpBin(orderBatteryInfo);
_log.Info($"UpBin orderBatteryInfo={JsonConvert.SerializeObject(orderBatteryInfo)}");
if (orderBatteryInfo.CanSwap != InfoEnum.SelectBinStatusInfo.Success)
{
return orderBatteryInfo;
}
InBin(orderBatteryInfo, machine);
_log.Info($"InBin orderBatteryInfo={JsonConvert.SerializeObject(orderBatteryInfo)}");
return orderBatteryInfo;
}
/// <summary>
/// 选包
/// 新增条件1仓号1-8,2电池编码对应
/// </summary>
/// <param name="swapAmtOrder"></param>
/// <param name="machine"></param>
/// <returns></returns>
public SwapOrderBatteryInfo SelectPackArm(SwapAmtOrder swapAmtOrder, SwappingStateMachine machine)
{
SwapOrderBatteryInfo orderBatteryInfo = new SwapOrderBatteryInfo();
orderBatteryInfo.swapAmtOrder = swapAmtOrder;
orderBatteryInfo.isAmt = true;
BinInfo UpBin = _binInfoRepository.QueryByClause(i => i.No.Equals(swapAmtOrder.AmtBinNoList));
bool CanSwap = UpBin.Exists == 1 && UpBin.Status == 1 && UpBin.ChargeStatus == 2
&& UpBin.AmtLock == (int)InfoEnum.AmtBatLockStatus.Lock && new TimeSpan(DateTime.Now.Ticks -
UpBin.LastChargeFinishTime.ToDateTime()
.Ticks)
.TotalMinutes > StaticStationInfo.SwapFinishChargeTime &&
UpBin.Soc > StaticStationInfo.SwapSoc;
//第二块电池信息去第一块电池编号+10
int no = Convert.ToInt32(UpBin.No) + 10;
BinInfo UpBin2 = _binInfoRepository.QueryByClause(i => i.No == no.ToString());
if (!CanSwap)
{
orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.AmtError;
return orderBatteryInfo;
}
orderBatteryInfo.UpBinInfo=new List<BinInfo> { UpBin , UpBin2 };
InBin(orderBatteryInfo, machine);
return orderBatteryInfo;
}
/// <summary>
/// 取电池判断
/// 不能选正在执行移仓的电池
/// </summary>
/// <param name="orderBatteryInfo"></param>
public void UpBin(SwapOrderBatteryInfo orderBatteryInfo)
{
SelectPackDto selectPack =
_binInfoRepository.SelectPack(StaticStationInfo.SwapSoc, StaticStationInfo.SwapFinishChargeTime,
QueryMoveBinNo()[0],BatteryGroupRepository);
if (selectPack != null)
{
//第二块电池信息去第一块电池编号+10
int no = Convert.ToInt32(selectPack.BinInfo.No) + 10;
BinInfo UpBin2 = _binInfoRepository.QueryByClause(i => i.No == no.ToString());
orderBatteryInfo.UpBinInfo = new List<BinInfo>() { selectPack.BinInfo, UpBin2 };
orderBatteryInfo.CanSwap = selectPack.Info;
}
}
private List<string> QueryMoveBinNo()
{
string moveUp = "";
string moveIn = "";
//当前有移仓任务
if (PlcMgr.PlcClient?.ReadTaskNo() == 6)
{
MoveBinRecord queryByClause = MoveBinRecordRepository.QueryByClause(i => i.Status == 0 || i.Status == 1,
i => i.CreatedTime,
OrderByType.Desc);
if (queryByClause != null)
{
moveUp = queryByClause?.UpBinNo;
moveIn = queryByClause?.InBinNo;
}
}
return new List<string>()
{
moveUp, moveIn
};
}
/// <summary>
/// 放电池判断:取出来的电池仓位能不能放
/// </summary>
/// <param name="orderBatteryInfo"></param>
public void InBin(SwapOrderBatteryInfo orderBatteryInfo, SwappingStateMachine machine)
{
List<BinInfo> list = orderBatteryInfo.UpBinInfo;
if (list.Count <= 0)
{
orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.LessOfEmptyBin;
return;
}
var value = QueryMoveBinNo()[1];
if (!string.IsNullOrWhiteSpace(value))
{
list = list.Where(i => i.No != value).ToList();
}
if (list.Count <= 0)
{
orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.LessOfEmptyBin;
return;
}
//第二块电池信息去第一块电池编号+10
int no = Convert.ToInt32(list[0].No) + 10;
BinInfo UpBin2 = _binInfoRepository.QueryByClause(i => i.No == no.ToString());
orderBatteryInfo.InBinInfo = new List<BinInfo>() { list[0], UpBin2 };
orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success;
}
/// <summary>
/// 查询预约单
/// </summary>
/// <param name="machine"></param>
/// <returns></returns>
public SwapAmtOrder? QueryAmtOrder(SwappingStateMachine machine)
{
return
_amtOrderRepository.QueryByClause(i =>
i.Status == (int)InfoEnum.AmtOrderStatus.Success && i.CarNo.Equals(machine.BoxCarInfoModel.CarNo));
}
/// <summary>
/// 更新订单
/// </summary>
/// <param name="machine"></param>
public void UpdateSwapOrder(SwappingStateMachine machine)
{
SwapOrder swapOrder = _swapOrderRepository.QueryById(machine.SwapOrder.Id);
machine.SwapOrder.CloudReportStatus = swapOrder.CloudReportStatus;
_swapOrderRepository.Update(machine.SwapOrder);
}
/// <summary>
/// 更新预约订单
/// </summary>
/// <param name="machine"></param>
public void UpdateAmtOrder(SwappingStateMachine machine)
{
_amtOrderRepository.Update(machine.SwapOrderBatteryInfo.swapAmtOrder);
}
/// <summary>
/// 换电成功关于binInfo表的处理
/// </summary>
public void UpdateBinInfoForSwapSuccess(SwappingStateMachine machine)
{
SwapOrderBatteryInfo? machineSwapOrderBatteryInfo = machine.SwapOrderBatteryInfo;
if (machineSwapOrderBatteryInfo != null)
{
var inBinInfo = machineSwapOrderBatteryInfo.InBinInfo;
//修改入仓顺序
BinInfo binInfo = _binInfoRepository
.QueryListByClause(i => i.BatteryEnterSeq != null, "battery_enter_seq desc").First();
inBinInfo[0].BatteryEnterSeq = binInfo.BatteryEnterSeq + 1;
inBinInfo[0].InTime = DateTime.Now;
inBinInfo[1].InTime = DateTime.Now;
_binInfoRepository.Update(inBinInfo);
}
}
/// <summary>
/// 换电完成新增上报云平台记录
/// </summary>
/// <param name="machine"></param>
public void InsertCloudReportForSwapSuccess(SwappingStateMachine machine)
{
SwapOrder? machineSwapOrder = machine.SwapOrder;
_swapOrderReportCloudRepository.Insert(new SwapOrderReportCloud()
{
SwapOrderSn = machineSwapOrder.Sn,
SwapOrderId = machineSwapOrder.Id,
CloudReportStatus = 0,
Vin = machineSwapOrder.VehicleVin,
Vtm = Convert.ToSingle(machine.BoxCarInfoModel?.ElecMsg?.TotalMile),
});
}
}