using Entity.Api.Req; using Entity.Api.Resp; using Entity.DbModel.Station; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using Repository.Station; using SqlSugar; using System.Linq.Expressions; using AutoMapper; using Entity.Dto; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Mvc; using Repository.System.App; using Service.Mgr; namespace Service.Station; /// /// 充电订单 /// [Scope("SingleInstance")] public class SwapOrderService : BaseServices { public SwapOrderRepository swapOrderRepository { get; set; } public SwapOrderReportCloudRepository SwapOrderReportCloudRepository { get; set; } public SwapOrderMgr SwapOrderMgr { get; set; } public AppCustomerVehicleRepository _appCustomerVehicleRepository{ get; set; } public SwapOrderService(SwapOrderRepository dal) { BaseDal = dal; } /// /// 查询需要导出换电订单 /// /// /// public async Task> QuerySwapOrderListAsync(QuerySwapOrderPageReq swapOrder) { Expression> where = null; ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u"); #region 构建动态查询树 where = queryTree(swapOrder, where, parameter); #endregion // 查询需要导出充电订单 不分页 return await swapOrderRepository.QuerySwapOrderList(where); } /// /// 根据条件查询分页数据 /// /// /// public PageResult QuerySwapOrder(QuerySwapOrderPageReq swapOrder) { //创建一个空的表达式树 Expression> where = null; //// 定义参数表达式 ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u"); #region 构建动态查询树 where = queryTree(swapOrder, where, parameter); #endregion //查询 //var swapOrderResp = PageResult.ConvertPage(swapOrderRepository.QueryIPageByCause(swapOrder, where)); //for (int i = 0; i < swapOrderResp.Rows.Count; i++) //{ // swapOrderResp.Rows[0].BatteryList.AddRange() //} //List BatteryList = new List(); //List StepList = new List(); return PageResult.ConvertPage(swapOrderRepository.QueryIPage(swapOrder, where)); } public PageResult AppQuerySwapOrder(QuerySwapOrderPageReq swapOrder) { //创建一个空的表达式树 Expression> where = null; //// 定义参数表达式 ParameterExpression parameter = Expression.Parameter(typeof(SwapOrder), "u"); where = queryTreeApp(swapOrder, where, parameter); if (where==null) { return new PageResult(); } return PageResult.ConvertPage(swapOrderRepository.QueryIPage(swapOrder, where)); } private Expression>? queryTreeApp(QuerySwapOrderPageReq swapOrder, Expression>? where, ParameterExpression parameter) { if (swapOrder.Time != null) { DateTime startTime = new DateTime(swapOrder.Time.Value.Year, swapOrder.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 (swapOrder.CustomerId.HasValue) { List vinList = _appCustomerVehicleRepository.GetVinList(swapOrder.CustomerId); if (vinList != null && vinList.Count > 0) { Expression> condition2Expr = u => vinList.Contains(u.VehicleVin); where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } } else { return null; } return where; } private Expression>? queryTree(QuerySwapOrderPageReq swapOrder, Expression>? where, ParameterExpression parameter) { if (!string.IsNullOrEmpty(swapOrder.Sn)) { Expression> condition2Expr = u => u.Sn == swapOrder.Sn; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(swapOrder.VehicleNo)) { Expression> condition2Expr = u => u.VehicleNo == swapOrder.VehicleNo; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (swapOrder.CustomerId.HasValue) { List vinList = _appCustomerVehicleRepository.GetVinList(swapOrder.CustomerId); if (vinList != null && vinList.Count > 0) { Expression> condition2Expr = u => vinList.Contains(u.VehicleVin); where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } } if (!string.IsNullOrEmpty(swapOrder.VehicleMac)) { Expression> condition2Expr = u => u.VehicleMac == swapOrder.VehicleMac; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(swapOrder.VehicleVin)) { Expression> condition2Expr = u => u.VehicleVin == swapOrder.VehicleVin; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (swapOrder.SwapBeginTime != null && swapOrder.SwapEndTime != null) { Expression> condition2Expr = u => u.CreatedTime >= swapOrder.SwapBeginTime && u.CreatedTime <= swapOrder.SwapEndTime; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (swapOrder.SwapResult != 0) { Expression> condition2Expr = u => u.SwapResult == swapOrder.SwapResult; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } return where; } /// /// 获取每日换电订单 /// /// public List DaySwapOrder() { // 获取今天的日期 DateTime today = DateTime.Today; // 昨天 00:00 DateTime startOfYesterday = today.AddDays(-1); // 昨天 23:59:59 DateTime endOfYesterday = startOfYesterday.AddDays(1).AddTicks(-1); Expression> predicate = x => x.SwapBeginTime >= startOfYesterday && x.SwapBeginTime <= endOfYesterday; return QueryListByClause(predicate); } }