138 lines
4.3 KiB
C#
138 lines
4.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using KonSoft.Admin.Dtos;
|
||
using KonSoft.Admin.Entities;
|
||
using KonSoft.Admin.IApplicationServices;
|
||
using KonSoft.Admin.IRepositories;
|
||
using KonSoft.Admin.ValueObjects;
|
||
using Volo.Abp;
|
||
using Volo.Abp.Application.Dtos;
|
||
using Volo.Abp.Application.Services;
|
||
using Volo.Abp.Domain.Repositories;
|
||
|
||
namespace KonSoft.Admin.ApplicationServices;
|
||
|
||
public class OrderAppService(IOrderRepository orderRepository) : ApplicationService, IOrderAppService
|
||
{
|
||
private readonly IOrderRepository _orderRepository = orderRepository;
|
||
|
||
|
||
/// <summary>
|
||
/// 取消订单
|
||
/// </summary>
|
||
/// <param name="orderId"></param>
|
||
/// <param name="reason"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task CancelAsync(Guid orderId, string reason)
|
||
{
|
||
var order = await _orderRepository.GetAsync(o => o.Id == orderId);
|
||
order.Cancel(reason);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建订单
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task<OrderDto> CreateAsync(CreateOrderDto input)
|
||
{
|
||
// 生成订单号 TODO
|
||
var orderSN = "SN001";
|
||
var address = ObjectMapper.Map<AddressDto, AddressInfo>(input.Address);
|
||
var order = new Order(input.CustomerId, input.ServiceCategoryId, input.ServiceTime,
|
||
input.Amount, address, input.Remark);
|
||
|
||
await _orderRepository.InsertAsync(order);
|
||
return ObjectMapper.Map<Order, OrderDto>(order);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据订单ID删除订单
|
||
/// </summary>
|
||
/// <param name="id">订单ID</param>
|
||
public async Task DeleteAsync(params Guid[] ids)
|
||
{
|
||
// 根据ID删除订单
|
||
await _orderRepository.DeleteAsync(x => ids.Contains(x.Id));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改订单信息
|
||
/// </summary>
|
||
/// <param name="input">订单DTO对象</param>
|
||
public async Task EditAsync(OrderDto input)
|
||
{
|
||
// 根据ID查询订单,如果不存在则抛出异常【修改前端必然看到了,数据若查不到提示自定义异常信息】
|
||
var order = await _orderRepository.FindAsync(o => o.Id == input.Id) ??
|
||
throw new BusinessException("订单找不到").WithData("Id", input.Id);
|
||
|
||
// 将输入的DTO字段映射到订单实体
|
||
ObjectMapper.Map(input, order);
|
||
|
||
// 更新订单
|
||
//await _orderRepository.UpdateAsync(order);
|
||
}
|
||
|
||
public async Task PayServiceCallFeeAsync(Guid orderId, PayOrderDto input)
|
||
{
|
||
var order = await _orderRepository.FirstOrDefaultAsync(o => o.Id == orderId);
|
||
if (order == null) throw new BusinessException("未找到订单");
|
||
|
||
order.PayServiceCallFee();
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 支付
|
||
/// </summary>
|
||
/// <param name="orderId"></param>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public async Task PayAsync(Guid orderId, PayOrderDto input)
|
||
{
|
||
var order = await _orderRepository.FirstOrDefaultAsync(o => o.Id == orderId);
|
||
if (order == null) throw new BusinessException("未找到订单");
|
||
|
||
order.Pay();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 根据订单ID获取单个订单
|
||
/// </summary>
|
||
public async Task<OrderDto> GetAsync(Guid id)
|
||
{
|
||
// 根据ID从数据库查询订单
|
||
var order = await _orderRepository.GetAsync(o => o.Id == id);
|
||
|
||
// 转换为OrderDto返回
|
||
return ObjectMapper.Map<Order, OrderDto>(order);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取订单列表
|
||
/// </summary>
|
||
public async Task<PagedResultDto<OrderDto>> GetListAsync(OrderPagedResultRequestDto input)
|
||
{
|
||
//TODO 默认根据角色筛选对应订单,整合到一个接口中
|
||
var query = await _orderRepository.GetQueryableAsync();
|
||
var orders = query.PageBy(input).OrderBy(o => o.Id).ToList();
|
||
|
||
var totalCount = await _orderRepository.CountAsync();
|
||
|
||
var orderDtos = ObjectMapper.Map<List<Order>, List<OrderDto>>(orders);
|
||
|
||
return new PagedResultDto<OrderDto>(totalCount, orderDtos);
|
||
}
|
||
|
||
|
||
|
||
|
||
} |