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.PlcAfrica.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 SwapOrderStepRepository SwapOrderStepRepository { get; set; } public BinInfo BinInfo1 = new BinInfo() { Id = 1, // No = "1", // Code = "1", // Name = "1", Exists = 1, // BatteryNo = "124243510021", // ChargerNo = "EAXF000100000001", ChargerGunNo = "1", WaterCoolNo = "1", ElecPluginFlag = 1, ElecPluginStatus = "", WaterPluginFlag = "", AmtLock = 0, Soc = 100, Soe = 100, Soh = 100, BatteryEnterSeq = 1, ChargeStatus = 4, Status = 1, CreatedBy = "", CreatedTime = DateTime.Now, UpdatedBy = "", LastChargeFinishTime = DateTime.Now, UpdatedTime = DateTime.Now, CacheBinFlag = 0, CanSwapFlag = 1, CanChargeFlag = 1, InTime = DateTime.Now, NominalEnergy = (Decimal)373.2, }; public BinInfo BinInfo2 = new BinInfo() { Id = 2, // No = "2", // Code = "2", // Name = "2", Exists = 1, BatteryNo = "124243510022", // ChargerNo = "EAXF000100000001", ChargerGunNo = "1", WaterCoolNo = "1", ElecPluginFlag = 1, ElecPluginStatus = "", WaterPluginFlag = "", AmtLock = 0, Soc = 100, Soe = 100, Soh = 100, BatteryEnterSeq = 1, ChargeStatus = 4, Status = 1, CreatedBy = "", CreatedTime = DateTime.Now, UpdatedBy = "", LastChargeFinishTime = DateTime.Now, UpdatedTime = DateTime.Now, CacheBinFlag = 0, CanSwapFlag = 1, CanChargeFlag = 1, InTime = DateTime.Now, NominalEnergy = (Decimal)373.2, }; public BinInfo BinInfo3 = new BinInfo() { Id = 3, // No = "3", // Code = "3", // Name = "3", Exists = 1, BatteryNo = "124243510023", // ChargerNo = "EAXF000100000003", ChargerGunNo = "1", WaterCoolNo = "1", ElecPluginFlag = 1, ElecPluginStatus = "", WaterPluginFlag = "", AmtLock = 0, Soc = 100, Soe = 100, Soh = 100, BatteryEnterSeq = 1, ChargeStatus = 4, Status = 1, CreatedBy = "", CreatedTime = DateTime.Now, UpdatedBy = "", LastChargeFinishTime = DateTime.Now, UpdatedTime = DateTime.Now, CacheBinFlag = 0, CanSwapFlag = 1, CanChargeFlag = 1, InTime = DateTime.Now, NominalEnergy = (Decimal)373.2, }; public BinInfo BinInfo4 = new BinInfo() { Id = 4, // No = "4", // Code = "4", // Name = "4", Exists = 1, BatteryNo = "124243510024", // ChargerNo = "EAXF000100000001", ChargerGunNo = "1", WaterCoolNo = "1", ElecPluginFlag = 1, ElecPluginStatus = "", WaterPluginFlag = "", AmtLock = 0, Soc = 100, Soe = 100, Soh = 100, BatteryEnterSeq = 1, ChargeStatus = 4, Status = 1, CreatedBy = "", CreatedTime = DateTime.Now, UpdatedBy = "", LastChargeFinishTime = DateTime.Now, UpdatedTime = DateTime.Now, CacheBinFlag = 0, CanSwapFlag = 1, CanChargeFlag = 1, InTime = DateTime.Now, NominalEnergy = (Decimal)373.2, }; /// /// 新增小步状态 /// public void InsertStep(InfoEnum.BusinessSwappingStep step, SwappingStateMachine machine, string desc = "", string param = null, int type = (int)SwapConstant.StepType.AUTO) { 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, desc, param, type); } /// /// 新增换电订单 /// /// /// public SwapOrder SaveOrder(SwapOrder swapOrder) { swapOrder.SwapWay = (int)StationConstant.StationWay.Auto; swapOrder.CloudReportStatus = 0; swapOrder.CreatedTime = DateTime.Now; _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, //TODO::添加 换上电池的容量 UpBatteryBinNo = int.Parse(upBin.No), UpNominalEnergy = upBin.NominalEnergy, DownBatteryBinNo = int.Parse(inBin.No), DownBatteryNo = inBin.BatteryNo, DownBatterySoc = inBin.Soc, DownBatterySoe = inBin.Soe, CreatedTime = DateTime.Now }; _swapOrderBatteryRepository.Insert(swapOrderBattery); } //查询是否有手动操作 public bool QueryHasManual(String swapOrderSn) { return SwapOrderStepRepository.GetCount(i => i.SwapOrderSn == swapOrderSn && i.StepType == (int)SwapConstant.StepType.MANUAL) > 0; } /// /// 1.锁仓 /// 2.更改预约单 /// public void LockBinAndUpdateAmt(SwapOrderBatteryInfo orderBatteryInfo) { orderBatteryInfo.UpBinInfo.AmtLock = (int)InfoEnum.AmtBatLockStatus.Lock; _binInfoRepository.Update(i=>new() { AmtLock=(int)InfoEnum.AmtBatLockStatus.Lock, },i=>i.Id==orderBatteryInfo.UpBinInfo.Id); orderBatteryInfo.InBinInfo.AmtLock = (int)InfoEnum.AmtBatLockStatus.Lock; _binInfoRepository.Update(i=>new() { AmtLock=(int)InfoEnum.AmtBatLockStatus.Lock, },i=>i.Id==orderBatteryInfo.InBinInfo.Id); 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.更改预约单 /// public void LockBinAndUpdateAmts(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.UnLock; _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.SwapFinish; _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(SwappingStateMachine machine) { SwapOrderBatteryInfo orderBatteryInfo = new SwapOrderBatteryInfo(); UpBinOnly(machine, orderBatteryInfo); _log.Info($"UpBin orderBatteryInfo={JsonConvert.SerializeObject(orderBatteryInfo)}"); if (orderBatteryInfo.CanSwap != InfoEnum.SelectBinStatusInfo.Success) { return orderBatteryInfo; } InBinOnly(orderBatteryInfo, machine); _log.Info($"InBin orderBatteryInfo={JsonConvert.SerializeObject(orderBatteryInfo)}"); if (orderBatteryInfo.CanSwap != InfoEnum.SelectBinStatusInfo.Success) { return orderBatteryInfo; } //手动选包 ManualSelectPack(machine, orderBatteryInfo); if (orderBatteryInfo.CanSwap == InfoEnum.SelectBinStatusInfo.Success) { orderBatteryInfo.InBinInfo.BatteryNo = "-1"; orderBatteryInfo.InBinInfo.Soc = -1; orderBatteryInfo.InBinInfo.Soe = -1; orderBatteryInfo.InBinInfo.Soh = -1; var carInfoBatteryInfos = machine.BoxCarInfoModel?.CarInfo?.BatteryInfos; if (carInfoBatteryInfos != null && carInfoBatteryInfos.Count > 0) { orderBatteryInfo.InBinInfo.BatteryNo = carInfoBatteryInfos[0].BatteryNo; // orderBatteryInfo.InBinInfo.Soc = carInfoBatteryInfos[0].Soc; orderBatteryInfo.InBinInfo.Soe = carInfoBatteryInfos[0].Soe; orderBatteryInfo.InBinInfo.Soh = carInfoBatteryInfos[0].Soh; } } return orderBatteryInfo; } 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; if (!CanSwap) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.AmtError; return orderBatteryInfo; } orderBatteryInfo.UpBinInfo = UpBin; InBin(orderBatteryInfo, machine); return orderBatteryInfo; } /// /// 取电池判断 /// 不能选正在执行移仓的电池 /// /// public void UpBin(SwappingStateMachine machine, SwapOrderBatteryInfo orderBatteryInfo) { SelectPackDto selectPack = _binInfoRepository.SelectPack(StaticStationInfo.SwapSoc, StaticStationInfo.SwapFinishChargeTime, QueryMoveBinNo()[0]); orderBatteryInfo.CanUpBin = selectPack.CanSwapBinInfo; orderBatteryInfo.UpBinInfo = selectPack.BinInfo; orderBatteryInfo.CanSwap = selectPack.Info; } public void UpBinOnly(SwappingStateMachine machine, SwapOrderBatteryInfo orderBatteryInfo) { if (PlcAfricaMgr.PlcAfricaMsg.Loader1SeatSensor1.Value) { List CanUpBin = new(); CanUpBin.Add(BinInfo1); CanUpBin.Add(BinInfo2); CanUpBin.Add(BinInfo3); CanUpBin.Add(BinInfo4); orderBatteryInfo.CanUpBin = CanUpBin; orderBatteryInfo.UpBinInfo = BinInfo1; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; } else if (PlcAfricaMgr.PlcAfricaMsg.Loader2SeatSensor1.Value) { List CanUpBin = new(); CanUpBin.Add(BinInfo1); CanUpBin.Add(BinInfo2); CanUpBin.Add(BinInfo3); CanUpBin.Add(BinInfo4); orderBatteryInfo.CanUpBin = CanUpBin; orderBatteryInfo.UpBinInfo = BinInfo2; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; } else if (PlcAfricaMgr.PlcAfricaMsg.Loader3SeatSensor1.Value) { List CanUpBin = new(); CanUpBin.Add(BinInfo1); CanUpBin.Add(BinInfo2); CanUpBin.Add(BinInfo3); CanUpBin.Add(BinInfo4); orderBatteryInfo.CanUpBin = CanUpBin; orderBatteryInfo.UpBinInfo = BinInfo3; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; } /*else if (PlcAfricaMgr.PlcAfricaMsg.Loader4SeatSensor1.Value) { List CanUpBin = new(); CanUpBin.Add(BinInfo1); CanUpBin.Add(BinInfo2); CanUpBin.Add(BinInfo3); CanUpBin.Add(BinInfo4); orderBatteryInfo.CanUpBin = CanUpBin; orderBatteryInfo.UpBinInfo = BinInfo4; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; }*/ } private List QueryMoveBinNo() { string moveUp = ""; string moveIn = ""; /*//当前有移仓任务 if (PlcMgr.PlcClient?.ReadTaskNo() == 2) { 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() { moveUp, moveIn }; } public void ceshi() { } /// /// 放电池判断:取出来的电池仓位能不能放 /// /// public void InBin(SwapOrderBatteryInfo orderBatteryInfo, SwappingStateMachine machine) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.WaitSelectDownBin; SelectPackDto selectPackDto = _binInfoRepository.SelectPackInBin(); orderBatteryInfo.CanDownBin = selectPackDto.CanSwapBinInfo; orderBatteryInfo.InBinInfo = selectPackDto.BinInfo; orderBatteryInfo.CanSwap = selectPackDto.Info; } public void InBinOnly(SwapOrderBatteryInfo orderBatteryInfo, SwappingStateMachine machine) { List CanDownBin = new List(); CanDownBin.Add(BinInfo1); CanDownBin.Add(BinInfo2); CanDownBin.Add(BinInfo3); CanDownBin.Add(BinInfo4); orderBatteryInfo.CanDownBin = CanDownBin; orderBatteryInfo.InBinInfo = orderBatteryInfo.UpBinInfo; orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; } //手动选包 public void ManualSelectPack(SwappingStateMachine machine, SwapOrderBatteryInfo orderBatteryInfo) { if ((int)SwapConstant.AutoOrManual.Auto == StaticStationInfo.SelectPackManually) { return; } //手动等待选包 orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.WaitManually; orderBatteryInfo.UpBinInfo = null; orderBatteryInfo.InBinInfo = null; if (machine.SwapOrderBatteryInfo?.UpBinInfo == null || machine.SwapOrderBatteryInfo?.InBinInfo == null) { _log.Info("等待手动选包.........."); //TODO:: 增加播报 return; } if (orderBatteryInfo.CanUpBin.Count > 0) { BinInfo? binInfo = orderBatteryInfo.CanUpBin.First(i => i.Id == machine.SwapOrderBatteryInfo?.UpBinInfo.Id); if (binInfo != null) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.WaitSelectDownBin; orderBatteryInfo.UpBinInfo = binInfo; } } if (orderBatteryInfo.CanDownBin.Count > 0) { BinInfo? binInfo = orderBatteryInfo.CanDownBin.First(i => i.Id == machine.SwapOrderBatteryInfo?.InBinInfo.Id); if (binInfo != null) { orderBatteryInfo.CanSwap = InfoEnum.SelectBinStatusInfo.Success; orderBatteryInfo.InBinInfo = binInfo; } } } /// /// 查询预约单 /// /// /// 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(BuildParamForSwap(machine)); } //赋值换电相关的数据,防止更新掉其他任务插入的数据 private SwapOrder BuildParamForSwap(SwappingStateMachine machine) { var old = machine.SwapOrder; SwapOrder swapOrder = _swapOrderRepository.QueryById(machine.SwapOrder.Id); swapOrder.SwapWay = old.SwapWay; swapOrder.SwapResult = old.SwapResult; swapOrder.Remark = old.Remark; swapOrder.CloudSn = old.CloudSn; swapOrder.FailReason = old.FailReason; swapOrder.SwapEndTime = old.SwapEndTime; swapOrder.VehicleLeaveTime = old.VehicleLeaveTime; swapOrder.SwapBeginTime = old.SwapBeginTime; swapOrder.VehicleEnterTime = old.VehicleEnterTime; return 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; //修改入仓顺序 BinInfo binInfo = _binInfoRepository .QueryListByClause(i => i.BatteryEnterSeq != null, "battery_enter_seq desc").First(); inBinInfo.BatteryEnterSeq = binInfo.BatteryEnterSeq + 1; inBinInfo.InTime = DateTime.Now; _binInfoRepository.Update( i => new BinInfo() { BatteryEnterSeq = inBinInfo.BatteryEnterSeq, InTime = inBinInfo.InTime }, it => it.Id == inBinInfo.Id); var upBinInfo = machineSwapOrderBatteryInfo.UpBinInfo; upBinInfo.Soc = -1; upBinInfo.Soe = -1; upBinInfo.BatteryNo = "-1"; _binInfoRepository.Update( i => new BinInfo() { Soc = -1, Soe = -1, BatteryNo = "-1" }, it => it.Id == upBinInfo.Id); } } /// /// 换电完成新增上报云平台记录 /// /// 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), }); } }