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.

148 lines
5.1 KiB

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<AmtOrderInfo>
{
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<AmtOrderInfo> AppQuerySwapOrder(QueryAmtOrderInfoPageReq req)
{
//创建一个空的表达式树
Expression<Func<AmtOrderInfo, bool>> where = null;
//// 定义参数表达式
ParameterExpression parameter = Expression.Parameter(typeof(AmtOrderInfo), "u");
where = queryTreeApp(req, where, parameter);
if (where == null)
{
return new PageResult<AmtOrderInfo>();
}
return PageResult<AmtOrderInfo>.ConvertPage(_amtOrderInfoRepository.QueryIPage(req, where));
}
/// <summary>
/// 添加预约订单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<Result<string>> InsertAmtOrder(AddAmtOrderInfoReq input)
{
if (input == null || !input.CustomerId.HasValue)
{
return Result<string>.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<string>.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<string>.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<string>.Success("新增预约订单id: "+amtOrder.Id);
}
return Result<string>.Fail("新增预约订单失败");
}
public virtual async Task<Result<string>> 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<string>.Fail("失败");
}
return Result<string>.Success("成功取消");
}
private Expression<Func<AmtOrderInfo, bool>>? queryTreeApp(QueryAmtOrderInfoPageReq req,
Expression<Func<AmtOrderInfo, bool>>? 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<Func<AmtOrderInfo, bool>> condition2Expr =
u => u.CreatedTime >= startTime && u.CreatedTime <= endTime;
where = where == null
? condition2Expr
: Expression.Lambda<Func<AmtOrderInfo, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
}
if (req.CustomerId.HasValue)
{
Expression<Func<AmtOrderInfo, bool>> condition2Expr = u => u.CustomerId == req.CustomerId;
where = where == null
? condition2Expr
: Expression.Lambda<Func<AmtOrderInfo, bool>>(Expression.AndAlso(where.Body, condition2Expr.Body),
parameter);
}
else
{
return null;
}
return where;
}
}