using System.Linq.Expressions; using AutoMapper; using Entity.Api.Req; using Entity.Api.Resp; using Entity.DbModel.Station; using Entity.Dto; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Utils; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Mvc; using Repository.Station; namespace Service.Station; /// /// 充电订单 /// [Scope("SingleInstance")] public class ChargeOrderService : BaseServices { ChargeOrderRepository chargeOrderRepository; public ChargeOrderService(ChargeOrderRepository dal) { chargeOrderRepository = dal; BaseDal = dal; } /// /// 根据条件查询分页数据 /// /// /// public PageResult QueryChargeOrder(QueryChargeOrderReq chargeOrder) { //创建一个空的表达式树 Expression> where = null; //// 定义参数表达式 ParameterExpression parameter = Expression.Parameter(typeof(ChargeOrder), "u"); #region 构建动态查询树 where = queryTree(chargeOrder, where, parameter); #endregion //查询 return PageResult.ConvertPage(chargeOrderRepository.QueryIPage(chargeOrder, where)); } /// /// 导出充电订单 /// /// /// /// public async Task ExportChargeOrder(QueryChargeOrderReq chargeOrder,string language) { // 查询订单 List chargeOrders = await QueryChargeOrderListAsync(chargeOrder); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); if (language == "en") { List list2 = mapper.Map>(chargeOrders); IExcelExporter excelExporter = new ExcelExporter(); var res = await excelExporter.ExportAsByteArray(list2); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_ChargeOrders.xlsx" }; } else { List list = mapper.Map>(chargeOrders); IExcelExporter excelExporter = new ExcelExporter(); var res = await excelExporter.ExportAsByteArray(list); return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "充电订单.xlsx" }; } } /// /// 查询需要导出订单 /// /// /// public async Task> QueryChargeOrderListAsync(QueryChargeOrderReq chargeOrder) { Expression> where = null; ParameterExpression parameter = Expression.Parameter(typeof(ChargeOrder), "u"); #region 构建动态查询树 where = queryTree(chargeOrder, where, parameter); #endregion // 查询需要导出充电订单 不分页 return await chargeOrderRepository.QueryChargeOrderList(where); } private static Expression>? queryTree(QueryChargeOrderReq chargeOrder, Expression>? where, ParameterExpression parameter) { if (chargeOrder.Id != 0) { Expression> condition1Expr = u => u.Id == chargeOrder.Id; where = condition1Expr; } if (!string.IsNullOrEmpty(chargeOrder.Sn)) { Expression> condition2Expr = u => u.Sn == chargeOrder.Sn; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(chargeOrder.BatteryNo)) { Expression> condition2Expr = u => u.BatteryNo == chargeOrder.BatteryNo; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(chargeOrder.ChargerNo)) { Expression> condition2Expr = u => u.ChargerNo == chargeOrder.ChargerNo; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } // 构建时间范围条件 if (chargeOrder.StartTime != null && chargeOrder.StartTime != DateTime.MinValue && chargeOrder.EndTime != null && chargeOrder.EndTime != DateTime.MinValue) { Expression> conditionExpr = u => u.CreatedTime >= chargeOrder.StartTime && u.CreatedTime <= chargeOrder.EndTime; where = conditionExpr; } if (chargeOrder.ChargeTimeCount!=null) { Expression> condition2Expr = u => u.ChargeTimeCount == chargeOrder.ChargeTimeCount; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (chargeOrder.StopReason != null) { Expression> condition2Expr = u => u.StopReason == chargeOrder.StopReason; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(chargeOrder.ElecPriceModelVersion)) { Expression> condition2Expr = u => u.ElecPriceModelVersion == chargeOrder.ElecPriceModelVersion; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(chargeOrder.SwapOrderSn)) { Expression> condition2Expr = u => u.SwapOrderSn == chargeOrder.SwapOrderSn; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (chargeOrder.CloudReportStatus != null) { Expression> condition2Expr = u => u.CloudReportStatus == chargeOrder.CloudReportStatus; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } if (!string.IsNullOrEmpty(chargeOrder.CloudChargeOrder)) { Expression> condition2Expr = u => u.CloudChargeOrder == chargeOrder.CloudChargeOrder; where = where == null ? condition2Expr : Expression.Lambda>(Expression.AndAlso(where.Body, condition2Expr.Body), parameter); } return where; } /// /// 获取每日充电订单 /// /// public List DayChargeOrder() { // 获取今天的日期 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.StartTime >= startOfYesterday && x.StartTime <= endOfYesterday; return QueryListByClause(predicate); } }