/// <summary> /// 完成订单 /// </summary> public async Task CompleteOrder(EntityDto <int> input) { var order = await _orderRepository.FirstOrDefaultAsync(input.Id); if (order == null) { throw new UserFriendlyException("该订单不存在"); } if (order.State.HasValue && !order.State.Value) { throw new UserFriendlyException("该订单已取消"); } var customer = order.Customer; if (customer.Balance < order.TotalPrice) { throw new UserFriendlyException("该用户余额不足,请提醒充值"); } var cost = new CustomerCost() { Balance = customer.Balance, CustomerId = customer.Id }; customer.Balance -= order.TotalPrice; cost.Cost = order.TotalPrice; cost.CurrentBalance = customer.Balance; order.State = true; await _costRepository.InsertAsync(cost); }
/// <summary> /// 创建订单 /// </summary> /// <returns></returns> public async Task <OrderListDto> CreateOrder(CreateOrderInput input) { var customer = await _customerRepository.FirstOrDefaultAsync(c => c.Id == input.CustomerId); if (customer == null) { throw new UserFriendlyException("该账户不存在"); } var product = await _productRepository.FirstOrDefaultAsync(c => c.Id == input.ProductId); if (product == null) { throw new UserFriendlyException("该产品不存在"); } var price = product.CustomerPrices.First(c => c.CustomerId == customer.Id); var totalPrice = price.Price * input.Count; if (customer.Balance < totalPrice) { throw new UserFriendlyException("账户下余额不足,请充值后再试"); } var dto = new Order() { CustomerId = input.CustomerId, OrderNum = Guid.NewGuid().ToString("N"), State = null, PayState = false, Count = input.Count, TotalPrice = totalPrice, ProductId = product.Id, Price = price.Price, ProductName = product.ProductName, LevelOne = product.LevelOne.Name, LevelTwo = product.LevelTwo.Name, Profile = product.Profile }; if (!product.RequireForm) { var cost = new CustomerCost() { Balance = customer.Balance, CustomerId = customer.Id }; customer.Balance -= dto.TotalPrice; cost.Cost = dto.TotalPrice; cost.CurrentBalance = customer.Balance; await _costRepository.InsertAsync(cost); dto.PayState = true; } dto = await _ordeRepository.InsertAsync(dto); await CurrentUnitOfWork.SaveChangesAsync(); return(dto.MapTo <OrderListDto>()); }
/// <summary> /// 创建订单扩展信息 /// </summary> /// <returns></returns> public async Task ModifyForm(CustomerFormEditDto input) { var customer = await _customerRepository.FirstOrDefaultAsync(c => c.Id == input.CustomerId); if (customer == null) { throw new UserFriendlyException("该账户不存在"); } var order = await _ordeRepository.FirstOrDefaultAsync(input.OrderId); if (order == null) { throw new UserFriendlyException("订单信息不存在"); } var form = input.MapTo <CustomerForm>(); if (input.Id.HasValue) { await _profileRepository.DeleteAsync(c => c.FormId == form.Id); } await _formRepository.InsertOrUpdateAsync(form); if (!order.PayState) { if (customer.Balance < order.TotalPrice) { throw new UserFriendlyException("账户下余额不足,请充值后再试"); } var cost = new CustomerCost() { Balance = customer.Balance, CustomerId = customer.Id }; customer.Balance -= order.TotalPrice; cost.Cost = order.TotalPrice; cost.CurrentBalance = customer.Balance; await _costRepository.InsertAsync(cost); order.PayState = true; } await CurrentUnitOfWork.SaveChangesAsync(); order.FormId = form.Id; }
/// <summary> /// 完成订单 /// </summary> /// <returns></returns> public async Task CompleteOrder(CompleteOrderInput input) { var customer = await _customerRepository.FirstOrDefaultAsync(c => c.Id == input.CustomerId); if (customer == null) { throw new UserFriendlyException("该账户不存在"); } var order = await _ordeRepository.FirstOrDefaultAsync(c => c.OrderNum.Equals(input.OrderNum) && !c.State.HasValue); if (order == null) { throw new UserFriendlyException("该订单不存在"); } order.State = input.State; if (input.State) { if (customer.Balance < order.TotalPrice) { throw new UserFriendlyException("账户下余额不足,请充值后再试"); } var cost = new CustomerCost() { Balance = customer.Balance, CustomerId = customer.Id }; customer.Balance -= order.TotalPrice; cost.Cost = order.TotalPrice; cost.CurrentBalance = customer.Balance; await _costRepository.InsertAsync(cost); } else { order.CancelReason = input.CancelReason; } }