You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
8.1 KiB

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