138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using Apimanager_backend.Data;
|
|
using Apimanager_backend.Dtos;
|
|
using Apimanager_backend.Exceptions;
|
|
using Apimanager_backend.Models;
|
|
using Apimanager_backend.Tools;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Apimanager_backend.Services
|
|
{
|
|
public class OrderService:IOrderService
|
|
{
|
|
private ApiContext _apiContext;
|
|
private IMapper mapper;
|
|
private ILogger<OrderService> logger;
|
|
public OrderService(ApiContext apiContext,IMapper mapper,ILogger<OrderService> logger)
|
|
{
|
|
_apiContext = apiContext;
|
|
this.mapper = mapper;
|
|
this.logger = logger;
|
|
}
|
|
#region 创建订单
|
|
/// <summary>
|
|
/// 创建订单
|
|
/// </summary>
|
|
/// <param name="order">订单信息</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<Order> CreateOrderAsync(OrderDto order)
|
|
{
|
|
var transaction = await _apiContext.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
Order orderInfo = mapper.Map<Order>(order);
|
|
//初始化信息
|
|
orderInfo.Status = OrderStatus.Pending;
|
|
orderInfo.CreatedAt = DateTime.Now;
|
|
orderInfo.OrderNumber = OrderNumberGenerator.Generate(order.UserId);
|
|
_apiContext.Orders.Add(orderInfo);
|
|
await _apiContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
return orderInfo;
|
|
}catch(Exception e)
|
|
{
|
|
//异常回滚
|
|
await transaction.RollbackAsync();
|
|
logger.LogError(e.Message);
|
|
throw new BaseException(4006,"支付系统错误");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
public async Task<int> GetOrderNumAsync(int? userId = null)
|
|
{
|
|
try
|
|
{
|
|
int num = 0;
|
|
if (userId != null)
|
|
{
|
|
num = await _apiContext.Orders.CountAsync(x => x.UserId == userId);
|
|
}
|
|
else
|
|
{
|
|
num = await _apiContext.Orders.CountAsync();
|
|
}
|
|
return num;
|
|
}catch(Exception e)
|
|
{
|
|
logger.LogError(e, "查询订单数量:{Message}", e.Message);
|
|
throw new BaseException(1005,"服务器内部错误");
|
|
}
|
|
|
|
}
|
|
|
|
#region 获取订单列表
|
|
/// <summary>
|
|
/// 获取订单列表
|
|
/// </summary>
|
|
/// <param name="pageIndex">索引</param>
|
|
/// <param name="pageSize">页大小</param>
|
|
/// <param name="desc">是否倒序</param>
|
|
/// <param name="userId">指定用户订单</param>
|
|
/// <returns></returns>
|
|
public async Task<List<Order>> GetOrdersAsync(int pageIndex, int pageSize, bool desc, int? userId)
|
|
{
|
|
IQueryable<Order> query = _apiContext.Orders.Include(x => x.User).AsQueryable().OrderBy(x => x.Id);
|
|
if(userId != null)
|
|
{
|
|
query = query.Where(x => x.UserId == userId);
|
|
}
|
|
//倒序
|
|
if (desc)
|
|
{
|
|
query.OrderDescending();
|
|
}
|
|
//分页
|
|
query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
|
return await query.ToListAsync();
|
|
}
|
|
#endregion
|
|
|
|
#region 更新订单状态
|
|
public async Task<bool> UpdateOrderAsync(OrderDto order)
|
|
{
|
|
var transaction = await _apiContext.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
var data = await _apiContext.Orders.SingleOrDefaultAsync(x => x.OrderNumber == order.OrderNumber || x.ThirdPartyOrderId == order.ThirdPartyOrderId);
|
|
if (data == null)
|
|
{
|
|
throw new BaseException(4003, "订单未找到");
|
|
}
|
|
data.UpdatedAt = DateTime.Now;
|
|
data.Status = order.Status;
|
|
data.PaiAt = order.Status == OrderStatus.Completed ? DateTime.Now : null;
|
|
var user = await _apiContext.Users.FirstOrDefaultAsync(x => x.Id == data.UserId);
|
|
if (user == null)
|
|
{
|
|
throw new BaseException(2004, "用户未找到");
|
|
}
|
|
user.Balance += data.Amount;
|
|
_apiContext.Orders.Update(data);
|
|
_apiContext.Users.Update(user);
|
|
await _apiContext.SaveChangesAsync();
|
|
await transaction.CommitAsync();
|
|
return true;
|
|
}catch(Exception e)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
logger.LogError(e.Message);
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|