using SJZY.Track.Application.Dtos.ProvideServices; using SJZY.Track.Domain.Aggregates.OrderAggregate; namespace SJZY.Track.Application.Services.ProvideServices; /// /// 订单关联服务 /// [ApiExplorerSettings(GroupName = "提供接口")] public class OrderLinkService : ApplicationService { private readonly IOrderRepository _orderRepository; private readonly IBasicQuery _orderLinkQuery; public OrderLinkService(IBasicQuery orderLinkQuery, IOrderRepository orderRepository) { _orderLinkQuery = orderLinkQuery; _orderRepository = orderRepository; } /// /// 关联订单 /// /// /// [HttpPost("Order/Link")] public async Task LinkOrderAsync(LinkOrderInput input) { if (!await _orderRepository.IsAnyAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo)) { throw new BusinessException(message: $"单号:{input.OrderNo}不存在"); } if (input.LinkOrderNos.Count < 1 || input.LinkOrderType == 0) return true; // 单号去重 input.LinkOrderNos = input.LinkOrderNos.Distinct().ToList(); // 解绑 if (!input.IsLink) return await UnLinkOrderAsync(input); // 关联订单 return await DoLinkOrderAsync(input); } private async Task DoLinkOrderAsync(LinkOrderInput input) { // 获取已关联的订单 var linkOrderNos = await _orderLinkQuery.GetListAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo && x.LinkOrderType == input.LinkOrderType, x => x.LinkOrderNo); // 计算出未关联的订单 var noLinkOrderNos = input.LinkOrderNos.Except(linkOrderNos); // 过滤掉不存在的订单 var canLinkOrderNos = await _orderRepository.GetListAsync(x => x.OrderType == input.LinkOrderType && noLinkOrderNos.Contains(x.OrderNo), x => x.OrderNo); await _orderRepository.LinkOrderAsync(canLinkOrderNos.Select(x => new OrderLinkEntity { OrderNo = input.OrderNo, OrderType = input.OrderType, LinkOrderNo = x, LinkOrderType = input.LinkOrderType }).ToList()); return true; } private async Task UnLinkOrderAsync(LinkOrderInput input) { var unLinkIds = await _orderLinkQuery.GetListAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo && x.LinkOrderType == input.LinkOrderType && input.LinkOrderNos.Contains(x.LinkOrderNo), x => x.Id); // real del await _orderRepository.UnLinkByIdsAsync(unLinkIds); return true; } }