|
@@ -1,4 +1,11 @@
|
|
|
-using Central.Control.WebApi.EFDbContext;
|
|
|
+using Ant.Core.Utils;
|
|
|
+using Ant.Core.WebApi.Enum;
|
|
|
+using Ant.Core.WebApi.Model;
|
|
|
+using Central.Control.WebApi.DbEntity;
|
|
|
+using Central.Control.WebApi.EFDbContext;
|
|
|
+using Central.Control.WebApi.Enum;
|
|
|
+using Central.Control.WebApi.Models.Request;
|
|
|
+using Central.Control.WebApi.Models.Response;
|
|
|
using Central.Control.WebApi.Service.Interface;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
@@ -13,19 +20,259 @@ namespace Central.Control.WebApi.Service
|
|
|
public class OrderService: IOrderService
|
|
|
{
|
|
|
private readonly IDbContext _dbContent;
|
|
|
+ private readonly IUserService _userService;
|
|
|
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
/// <param name="dbContent"></param>
|
|
|
public OrderService(
|
|
|
- IDbContext dbContent)
|
|
|
+ IDbContext dbContent,
|
|
|
+ IUserService userService)
|
|
|
{
|
|
|
_dbContent = dbContent;
|
|
|
+ _userService = userService;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 下单
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="req"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public ApiResult<OrderResponseDto> Order(OrderRequestDto req)
|
|
|
+ {
|
|
|
+ #region 1、验证参数
|
|
|
+ if (req == null
|
|
|
+ || req.OrderInfo == null
|
|
|
+ || req.OrderInfo.Count() == 0)
|
|
|
+ {
|
|
|
+ return new ApiResult<OrderResponseDto>(ApiStatusCode.InvalidParameter, "请求参数不正确");
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 2、验证相应数据和库存
|
|
|
+ var productIds = req.OrderInfo.Select(p => p.ProductId).ToList();
|
|
|
+ var products = _dbContent.Set<YW_Product>().Where(p => p.IsDelete == 0 && p.Sale == Enum.SaleEnum.On && productIds.Contains(p.Id)).ToList();
|
|
|
+ if (products.Count() <= 0)
|
|
|
+ {
|
|
|
+ return new ApiResult<OrderResponseDto>(ApiStatusCode.RecordNotFound, "未找到下单的商品");
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 3、库存验证
|
|
|
+ var session = _userService.GetLoginSession();
|
|
|
+ var orderId = IdGenerator.NewId();
|
|
|
+ List<YW_OrderDetails> orderDetailAdds = new List<YW_OrderDetails>();
|
|
|
+ List<YW_OrderProcess> orderProcessAdds = new List<YW_OrderProcess>();
|
|
|
+
|
|
|
+ foreach (var o in req.OrderInfo)
|
|
|
+ {
|
|
|
+ var currentProduct = products.FirstOrDefault(p => p.Id == o.ProductId);
|
|
|
+ if (currentProduct == null)
|
|
|
+ {
|
|
|
+ return new ApiResult<OrderResponseDto>(ApiStatusCode.RecordNotFound, $"{o.ProductId}商品不存在");
|
|
|
+ }
|
|
|
+ if (currentProduct.Stock < o.Count)
|
|
|
+ {
|
|
|
+ return new ApiResult<OrderResponseDto>(ApiStatusCode.Forbidden, $"{currentProduct.Name}商品库存不足,请修改后重新下单");
|
|
|
+ }
|
|
|
+ // 扣库存
|
|
|
+ currentProduct.Stock = currentProduct.Stock - o.Count;
|
|
|
+
|
|
|
+ // orderDetails
|
|
|
+ orderDetailAdds.Add(new YW_OrderDetails()
|
|
|
+ {
|
|
|
+ Id = IdGenerator.NewId(),
|
|
|
+ OrderId = orderId,
|
|
|
+ ProductId = currentProduct.Id,
|
|
|
+ Name = currentProduct.Name,
|
|
|
+ Code = currentProduct.Code,
|
|
|
+ Img = currentProduct.Img,
|
|
|
+ Info = currentProduct.Info,
|
|
|
+ Price = currentProduct.Price,
|
|
|
+ BuyingPrice = currentProduct.BuyingPrice,
|
|
|
+ Count = o.Count,
|
|
|
+ CreateBY = session.UserId
|
|
|
+ });
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 4、下单
|
|
|
+ YW_Order order = new YW_Order()
|
|
|
+ {
|
|
|
+ Id = orderId,
|
|
|
+ DeviceId = _userService.GetLoginSession().Id,
|
|
|
+ Price = orderDetailAdds.Sum(p => p.Price * p.Count),
|
|
|
+ PayStatus = PayStatusEnum.UnPay,
|
|
|
+ OrderStatus = OrderStatusEnum.UnPay,
|
|
|
+ PaySerialId = Snowflake.Instance().GetId().ToString(),
|
|
|
+ CreateBY = session.Id
|
|
|
+ };
|
|
|
+ YW_OrderProcess orderProcess = new YW_OrderProcess()
|
|
|
+ {
|
|
|
+ Id = IdGenerator.NewId(),
|
|
|
+ OrderId = order.Id,
|
|
|
+ DeviceId = session.UserId,
|
|
|
+ CurrentOrderStatus = OrderStatusEnum.UnPay,
|
|
|
+ Message = "下单成功",
|
|
|
+ CreateBY = session.Id
|
|
|
+ };
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 5、存库
|
|
|
+ _dbContent.Set<YW_Order>().Add(order);
|
|
|
+ _dbContent.Set<YW_OrderProcess>().Add(orderProcess);
|
|
|
+ _dbContent.Set<YW_OrderDetails>().AddRange(orderDetailAdds);
|
|
|
+
|
|
|
+ // 数据库执行 库存已经改过了,会自动保存
|
|
|
+ _dbContent.SaveChanges();
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ return new ApiResult<OrderResponseDto>(new OrderResponseDto()
|
|
|
+ {
|
|
|
+ OrderId = order.Id,
|
|
|
+ Price = order.Price
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取待烧烤的商品列表
|
|
|
+ /// <returns></returns>
|
|
|
+ public ApiResult<List<QueueOrderResponseDto>> GetQueueOrders()
|
|
|
+ {
|
|
|
+ var orders = _dbContent.Set<YW_Order>()
|
|
|
+ .Where(p => p.IsDelete == 0
|
|
|
+ && p.PayStatus == PayStatusEnum.Paid
|
|
|
+ && p.OrderStatus == OrderStatusEnum.Paid)
|
|
|
+ .OrderBy(p => p.CreateDT).ToList();
|
|
|
+ var orderIds = orders.Select(p => p.Id).ToList();
|
|
|
+
|
|
|
+ var orderDetails = _dbContent.Set<YW_OrderDetails>().Where(p => p.IsDelete == 0 && orderIds.Contains(p.OrderId)).ToList();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ List<QueueOrderResponseDto> result = new List<QueueOrderResponseDto>();
|
|
|
+ orders.ForEach(item => {
|
|
|
+ QueueOrderResponseDto resultItem = new QueueOrderResponseDto()
|
|
|
+ {
|
|
|
+ OrderId = item.Id,
|
|
|
+ Price = item.Price,
|
|
|
+ OrderDetails = orderDetails
|
|
|
+ .Where(p => p.OrderId == item.Id)
|
|
|
+ .Select(p => SafeClone<YW_OrderDetails, QueueOrderDetailsDto>.Trans(p))
|
|
|
+ .ToList()
|
|
|
+ };
|
|
|
+ resultItem.OrderDetails?.ForEach(p => p.Img = $"{ProductService.ImagePrefix}{p.Img}");
|
|
|
|
|
|
+ result.Add(resultItem);
|
|
|
+ });
|
|
|
+ return new ApiResult<List<QueueOrderResponseDto>>(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 回写流程状态
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="req"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public ApiResult OrderProcess(OrderProcessRequestDto req)
|
|
|
+ {
|
|
|
+ if (req == null)
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "请求参数不正确");
|
|
|
+ }
|
|
|
+ var order = _dbContent.Set<YW_Order>().FirstOrDefault(p => p.Id == req.OrderId);
|
|
|
+ if (order == null)
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "未找到相应订单");
|
|
|
+ }
|
|
|
+ if (order.PayStatus != PayStatusEnum.Paid)
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "未支付订单不能回写流程");
|
|
|
+ }
|
|
|
+ if (order.OrderStatus == OrderStatusEnum.UnPay
|
|
|
+ || order.OrderStatus == OrderStatusEnum.Cancel)
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "未支付或已取消订单不能回写流程");
|
|
|
+ }
|
|
|
+ var seesion = _userService.GetLoginSession();
|
|
|
+ // 修改订单状态
|
|
|
+ order.OrderStatus = req.OrderStatus;
|
|
|
+ // 记录订单流程
|
|
|
+ YW_OrderProcess orderProcess = new YW_OrderProcess()
|
|
|
+ {
|
|
|
+ Id = IdGenerator.NewId(),
|
|
|
+ OrderId = req.OrderId,
|
|
|
+ DeviceId = seesion.UserId,
|
|
|
+ CurrentOrderStatus = req.OrderStatus,
|
|
|
+ Message = req.Message,
|
|
|
+ CreateBY = seesion.UserId
|
|
|
+ };
|
|
|
+ _dbContent.Set<YW_OrderProcess>().Add(orderProcess);
|
|
|
+ _dbContent.SaveChanges();
|
|
|
|
|
|
+ return new ApiResult();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 支付回写(雏形,需要根据实际需要接入支付宝或微信)
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="req"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public ApiResult PayWriteBack(PayWriteBackRequestDto req)
|
|
|
+ {
|
|
|
+ if (req == null
|
|
|
+ || string.IsNullOrWhiteSpace(req.PaySerialId))
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "请求参数不正确");
|
|
|
+ }
|
|
|
+ var order = _dbContent.Set<YW_Order>().FirstOrDefault(p => p.PaySerialId == req.PaySerialId);
|
|
|
+ if (order == null)
|
|
|
+ {
|
|
|
+ return new ApiResult(ApiStatusCode.InvalidParameter, "未找到相应订单");
|
|
|
+ }
|
|
|
+ var status = GetStatusByPayWriteBack(req.PayStatus);
|
|
|
+ // 修改订单状态
|
|
|
+ order.PayStatus = status.Item1;
|
|
|
+ order.OrderStatus = status.Item2;
|
|
|
+ // 记录订单流程
|
|
|
+ YW_PayCall payCall = new YW_PayCall()
|
|
|
+ {
|
|
|
+ Id = IdGenerator.NewId(),
|
|
|
+ OrderId = order.Id,
|
|
|
+ PayStatus = status.Item1,
|
|
|
+ PaySerialId = req.PaySerialId,
|
|
|
+ PayAccount = req.PayAccount,
|
|
|
+ CreateBY = req.PayAccount
|
|
|
+ };
|
|
|
+ _dbContent.Set<YW_PayCall>().Add(payCall);
|
|
|
+ _dbContent.SaveChanges();
|
|
|
+
|
|
|
+ return new ApiResult();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="payStatusBack"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private Tuple<PayStatusEnum, OrderStatusEnum> GetStatusByPayWriteBack(int payStatusBack)
|
|
|
+ {
|
|
|
+ // 临时:payStatus 0失败,1成功
|
|
|
+ PayStatusEnum payStatus = PayStatusEnum.Fail;
|
|
|
+ OrderStatusEnum orderStatus = OrderStatusEnum.UnPay;
|
|
|
+ switch (payStatusBack)
|
|
|
+ {
|
|
|
+ case 1:// 成功
|
|
|
+ payStatus = PayStatusEnum.Paid;
|
|
|
+ orderStatus = OrderStatusEnum.Paid;
|
|
|
+ break;
|
|
|
+ default:// 失败
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new Tuple<PayStatusEnum, OrderStatusEnum>(payStatus, orderStatus);
|
|
|
+ }
|
|
|
|
|
|
+ // public Tuple<bool,string> ProcessOrderStock(int )
|
|
|
}
|
|
|
}
|