using System.Linq.Expressions; using Entity.Api.Req; using Entity.DbModel.Station; using Entity.DbModel.System.App; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using log4net; using Repository.Station; using Service.Init; using Swapping.Business.Common; namespace Service.Station; [Scope("SingleInstance")] public class AmtOrderInfoService : BaseServices { private static readonly ILog Log = LogManager.GetLogger(typeof(AmtOrderInfoService)); private AmtOrderInfoRepository _amtOrderInfoRepository; public BinInfoRepository _binInfoRepository { get; set; } public AmtOrderInfoService(AmtOrderInfoRepository dal) { _amtOrderInfoRepository = dal; BaseDal = dal; } public PageResult AppQuerySwapOrder(QueryAmtOrderInfoPageReq req) { //创建一个空的表达式树 Expression> where = null; //// 定义参数表达式 ParameterExpression parameter = Expression.Parameter(typeof(AmtOrderInfo), "u"); where = queryTreeApp(req, where, parameter); if (where == null) { return new PageResult(); } return PageResult.ConvertPage(_amtOrderInfoRepository.QueryIPage(req, where)); } /// /// 添加预约订单 /// /// /// public virtual async Task> InsertAmtOrder(AddAmtOrderInfoReq input) { if (input == null || !input.CustomerId.HasValue) { return Result.Fail("CustomerId为null"); } BinInfo binInfo = await _binInfoRepository.QueryByClauseAsync(u => u.No == input.BinNos); if (binInfo==null||binInfo.AmtLock==1||binInfo.Status==0||binInfo.CanSwapFlag==0) { return Result.Fail("仓位不满足换电,请更换仓位预约"); } var time = DateTime.Now; DateTime halfHourAgo = time.AddMinutes(-30); DateTime halfHourLater = time.AddMinutes(30); // 查询最近半小时内的预约订单 var recentOrders = await _amtOrderInfoRepository.QueryByClauseAsync(u => u.CustomerId == input.CustomerId && u.CreatedTime > halfHourAgo && u.Status == 1); if (recentOrders != null) { return Result.Fail("您已有预约订单"); } // 换电站编码 var stationNo = StaticStationInfo.StationNo; // 生成订单号 var orderNo = SwapOrderNoGenerator.GenerateOrderNo(stationNo); input.OrderNo = orderNo; input.sn = stationNo; input.Status = 1; input.CreatedTime = time; input.StartTime = time; input.EndTime = halfHourLater; input.BatteryNos = binInfo.BatteryNo; input.Batnum = 1; // 插入新订单 await _amtOrderInfoRepository.InsertAsync(input); // 查询新插入的订单 var amtOrder = await _amtOrderInfoRepository.QueryByClauseAsync(u => u.OrderNo == orderNo); if (amtOrder!=null) { await _binInfoRepository.UpdateAsync(i => i.AmtLock == 1,i => i.No == input.BinNos); return Result.Success("新增预约订单id: "+amtOrder.Id); } return Result.Fail("新增预约订单失败"); } public virtual async Task> CancelAmtOrder(DeleteAmtOrderInfoReq req) { var amtOrder = await _amtOrderInfoRepository.QueryByClauseAsync(u => u.Id == req.Id); amtOrder.Status = 2; if (await _amtOrderInfoRepository.UpdateAsync(amtOrder)) { await _binInfoRepository.UpdateAsync(i => i.AmtLock == 1, i => i.ChargerNo == amtOrder.BinNos); } else { return Result.Fail("失败"); } return Result.Success("成功取消"); } private Expression>? queryTreeApp(QueryAmtOrderInfoPageReq req, Expression>? where, ParameterExpression parameter) { if (req.Time != null) { DateTime startTime = new DateTime(req.Time.Value.Year, req.Time.Value.Month, 1, 0, 0, 0); DateTime endTime = startTime.AddMonths(1).AddSeconds(-1); Expression> condition2Expr = u => u.CreatedTime >= startTime && u.CreatedTime <= endTime; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (req.CustomerId.HasValue) { Expression> condition2Expr = u => u.CustomerId == req.CustomerId; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } else { return null; } return where; } }