using Autofac; using AutoMapper; using Entity.Constant; using Entity.DbModel.Station; using Entity.Dto; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using Repository.Station; using Service.Execute.Model; using Service.Init; using Service.Station; namespace Service.Execute.Utils; [Scope("SingleInstance")] public class 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 void InsertStep(InfoEnum.BusinessSwappingStep step, SwappingStateMachine machine) { machine.StepModel.Add(new() { StepName = BaseEnumExtensions.GetDescription(step), StepNo = (int)step, StartTime = DateTime.Now }); _swapOrderStepService.InsertSwapStepForSwapMain(step, machine.StepSort++, machine.SwapOrder.Sn); } /// /// 新增换电订单 /// /// /// public SwapOrder SaveOrder(SwapOrder swapOrder) { swapOrder.CloudReportStatus = 0; _swapOrderRepository.Insert(swapOrder); return swapOrder; } public void SaveSwapBattery(SwappingStateMachine machine) { BinInfo UpBin = machine.SwapOrderBatteryInfo.UpBinInfo; BinInfo InBin = machine.SwapOrderBatteryInfo.InBinInfo; SwapOrderBattery swapOrderBattery = new SwapOrderBattery() { SwapOrderSn = machine.SwapOrder.Sn, UpBatterySoc = UpBin.Soc, UpBatteryNo = UpBin.BatteryNo, UpBatterySoe = UpBin.Soe, UpBatteryBinNo = int.Parse(UpBin.No), DownBatteryBinNo = int.Parse(InBin.No), }; swapOrderBattery= _swapOrderBatteryRepository.Insert(swapOrderBattery); } /// /// 1.锁仓 /// 2.更改预约单 /// public void LockBinAndUpdateAmt(SwapOrderBatteryInfo orderBatteryInfo) { var configBinInfo = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap()); IMapper mapperBinInfo = configBinInfo.CreateMapper(); BinInfo dbBinInfo = mapperBinInfo.Map(orderBatteryInfo.UpBinInfo); dbBinInfo.AmtLock = (int)InfoEnum.AmtBatLockStatus.Lock; _binInfoRepository.Update(dbBinInfo); if (!orderBatteryInfo.isAmt) { return; } var configAmt = new MapperConfiguration(cfg => cfg.CreateMap().ReverseMap()); IMapper mapperAmt = configAmt.CreateMapper(); SwapAmtOrder swapAmtOrder = mapperAmt.Map(orderBatteryInfo.swapAmtOrder); swapAmtOrder.Status = (int)InfoEnum.AmtOrderStatus.Swapping; _amtOrderRepository.Update(swapAmtOrder); } /// 1.仓位状态:启动 /// 2.电池在位 /// 3.充电状态:不在充电中 /// 4.未锁定 /// 5.最后一次结束充电时间> () /// 6.soc >() /** * && i.ChargeStatus == 2 && i.AmtLock == "0" && new TimeSpan(DateTime.Now.Ticks - i.LastChargeFinishTime.ToDateTime().Ticks) .TotalMinutes > StaticStationInfo.SwapFinishChargeTime && i.Soc > StaticStationInfo.Soc */ public SwapOrderBatteryInfo SelectPackNotArm() { SwapOrderBatteryInfo orderBatteryInfo = new SwapOrderBatteryInfo(); UpBin(orderBatteryInfo); if (orderBatteryInfo.CanSwap != InfoEnum.SelectBinStatusInfo.Success) { return orderBatteryInfo; } InBin(orderBatteryInfo); return orderBatteryInfo; } public SwapOrderBatteryInfo SelectPackArm(SwapAmtOrder swapAmtOrder) { 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; if (!CanSwap) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.AmtError; return orderBatteryInfo; } orderBatteryInfo.UpBinInfo = UpBin; InBin(orderBatteryInfo); return orderBatteryInfo; } /// /// 取电池判断 /// /// public void UpBin(SwapOrderBatteryInfo orderBatteryInfo) { SelectPackDto selectPack = _binInfoRepository.SelectPack(StaticStationInfo.SwapSoc, StaticStationInfo.SwapFinishChargeTime); orderBatteryInfo.UpBinInfo = selectPack.BinInfo; orderBatteryInfo.CanSwap = selectPack.Info; } /// /// 放电池判断:取出来的电池仓位能不能放 /// /// public void InBin(SwapOrderBatteryInfo orderBatteryInfo) { List list = _binInfoRepository.QueryListByClause(i => i.CacheBinFlag==0 && i.Status == 1 && i.AmtLock == (int)InfoEnum.AmtBatLockStatus.UnLock && i.Exists == 0); if (list.Count <= 0) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.LessOfEmptyBin; return; } orderBatteryInfo.InBinInfo = list[0]; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; } /// /// 查询预约单 /// /// /// public SwapAmtOrder? QueryAmtOrder(SwappingStateMachine machine) { return _amtOrderRepository.QueryByClause(i => i.Status == (int)InfoEnum.AmtOrderStatus.Success && i.CarNo.Equals(machine.BoxCarInfoModel.CarNo)); } /// /// 更新订单 /// /// public void UpdateSwapOrder(SwappingStateMachine machine) { _swapOrderRepository.Update(machine.SwapOrder); } /// /// 更新预约订单 /// /// public void UpdateAmtOrder(SwappingStateMachine machine) { _amtOrderRepository.Update(machine.SwapOrderBatteryInfo.swapAmtOrder); } /// /// 换电成功关于binInfo表的处理 /// public void UpdateBinInfoForSwapSuccess(SwappingStateMachine machine) { SwapOrderBatteryInfo? machineSwapOrderBatteryInfo = machine.SwapOrderBatteryInfo; if (machineSwapOrderBatteryInfo != null) { var inBinInfo = machineSwapOrderBatteryInfo.InBinInfo; inBinInfo.Soc = -1; inBinInfo.Soe = -1; inBinInfo.BatteryNo = "-1"; //修改入仓顺序 BinInfo binInfo = _binInfoRepository.QueryListByClause(i=>i.BatteryEnterSeq!=null,"battery_enter_seq desc").First(); inBinInfo.BatteryEnterSeq = binInfo.BatteryEnterSeq + 1; _binInfoRepository.Update(inBinInfo); var upBinInfo = machineSwapOrderBatteryInfo.UpBinInfo; upBinInfo.Soc = -1; upBinInfo.Soe = -1; upBinInfo.BatteryNo = "-1"; _binInfoRepository.Update(upBinInfo); } } /// /// 换电完成新增上报云平台记录 /// /// public void InsertCloudReportForSwapSuccess(SwappingStateMachine machine) { SwapOrder? machineSwapOrder = machine.SwapOrder; _swapOrderReportCloudRepository.Insert(new SwapOrderReportCloud() { SwapOrderSn = machineSwapOrder.Sn, SwapOrderId = machineSwapOrder.Id, CloudReportStatus = 0, Vin = machineSwapOrder.VehicleVin }); } }