using DotNetCore.CAP; using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Events; using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate; using SJZY.Track.Domain.Consts; using SJZY.Track.Domain.Queries; using SJZY.UserCenter.HttpApi.Services; using Volo.Abp.Http.Client.DynamicProxying; namespace SJZY.Track.Application.Services.ProvideServices; /// /// 轨迹服务 /// [ApiExplorerSettings(GroupName = "提供接口")] [Route("/ProviderService")] public partial class TrackService : ApplicationService, ITrackService { private readonly IOrderRepository _orderRepository; private readonly IProvideQueries _provideQueries; private readonly ICapPublisher _capPublisher; private readonly IHttpClientProxy _requestCurrentUserService; public TrackService(IOrderRepository orderRepository, IProvideQueries provideQueries, ICapPublisher capPublisher, ITrackDetailsRepository trackDetailsRepository, IHttpClientProxy requestCurrentUserService) { _orderRepository = orderRepository; _provideQueries = provideQueries; _provideQueries = provideQueries; _capPublisher = capPublisher; _requestCurrentUserService = requestCurrentUserService; _trackDetailsRepository = trackDetailsRepository; } /// /// 新增、更新轨迹(存在就更新) /// /// /// /// [HttpPost("Track/Upsert")] public async Task UpsertTrackAsync(UpsertTrackInput input) { var order = await _orderRepository.GetFirstAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo, x => new { x.Id, x.SubSystemId, x.OrderNo, x.OrderType, x.CustomId }); if (order == default) { throw new BusinessException(message: $"订单:{input.OrderNo}不存在"); } var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync(); if (userInfo.Code != "200" || userInfo.Data is null) { //TODO: log || ex throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo?.Msg}"); } var name = userInfo.Data.UserName; // 新增/更新轨迹 var @event = ObjectMapper.Map(input); @event.SubSystemId = order.SubSystemId; @event.OrderIdentifiers = new[] { new OrderIdentifier { OrderId = order.Id, OrderNo = order.OrderNo, OrderType = order.OrderType, } }; @event.OperationMan = name; await _capPublisher.PublishAsync(GlobalConst.TrackUpsertSubscribe, @event); return new UnifyResultDto(true, true); } /// /// 批量新增、更新轨迹 /// /// /// /// [HttpPost("Track/BatchUpsert")] public async Task BatchUpsertTrackAsync(BatchUpsertTrackInput input) { input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空 if (input.OrderNos.Count == 0) { throw new BusinessException(message: "订单一个都不存在"); } var orders = await _orderRepository.GetListAsync(x => x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo), x => new { x.Id, x.OrderNo, x.SubSystemId, x.CustomId }); var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync(); if (userInfo.Code != "200" || userInfo.Data is null) { //TODO: log || ex throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo?.Msg}"); } var name = userInfo.Data.UserName; foreach (var group in orders.GroupBy(x => new { x.SubSystemId, x.CustomId })) { // 新增/更新轨迹 var @event = new TrackUpsertEvent { SubSystemId = group.Key.SubSystemId, CustomId = group.Key.CustomId, OrderIdentifiers = group.Select(order => new OrderIdentifier { OrderId = order.Id, OrderNo = order.OrderNo, OrderType = input.OrderType, }).ToArray(), IsAdd = input.IsAdd, TrackCode = input.TrackCode, TrackPlace = input.TrackPlace, TrackTime = input.TrackTime!.Value, TrackTimeZone = input.TrackTimeZone!.Value, OperationMan = name }; await _capPublisher.PublishAsync(GlobalConst.TrackUpsertSubscribe, @event); } return new UnifyResultDto(true, true); } /// /// 删除轨迹 /// /// /// /// [HttpPost("Track/Delete")] public async Task DeleteTrackAsync(DeleteTrackInput input) { var order = await _orderRepository.GetFirstAsync(x => x.OrderType == input.OrderType && x.OrderNo == input.OrderNo, x => new { x.Id, x.SubSystemId }); if (order == default) { throw new BusinessException(message: $"订单:{input.OrderNo}不存在"); } var codes = await _provideQueries.GetCanDeleteTrackCodesAsync(new List { order.Id }, input.TrackCodes); if (codes.Count == 0) { throw new BusinessException(message: "没有一个轨迹代码是可删的"); } var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync(); if (userInfo.Code != "200" || userInfo.Data is null) { //TODO: log || ex throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo?.Msg}"); } var name = userInfo.Data.UserName; // 删除轨迹 var @event = new TrackDeleteEvent { OrderIds = new[] { order.Id }, TrackCodes = codes.Select(x => x.TrackCode).ToList(), SubSystemId = order.SubSystemId, OperationMan = name }; await _capPublisher.PublishAsync(GlobalConst.TrackDeleteSubscribe, @event); return new UnifyResultDto(true, true); } /// /// 批量删除轨迹 /// /// /// /// [HttpPost("Track/BatchDelete")] public async Task BatchDeleteTrackAsync(BatchDeleteTrackInput input) { var orders = await _orderRepository.GetListAsync(x => x.OrderType == input.OrderType && input.OrderNos.Contains(x.OrderNo) , x => new { x.Id, x.SubSystemId }); var codes = await _provideQueries.GetCanDeleteTrackCodesAsync( orders.Select(x => x.Id).ToList(), input.TrackCodes ); if (codes.Count == 0) { throw new BusinessException(message: "没有一个轨迹代码是可删的"); } var userInfo = await _requestCurrentUserService.Service.GetCurrentUserBasicInfoAsync(); if (userInfo.Code != "200" || userInfo.Data is null) { //TODO: log || ex throw new BusinessException(message: $"调用用户中心,获取用户信息失败:{userInfo?.Msg}"); } var name = userInfo.Data.UserName; // 删除轨迹 foreach (var group in codes.GroupBy(x => x.OrderId)) { var @event = new TrackDeleteEvent { OrderIds = group.Select(x => x.OrderId).ToArray(), TrackCodes = input.TrackCodes, SubSystemId = orders.First(x => x.Id == group.Key).SubSystemId, OperationMan = name }; await _capPublisher.PublishAsync(GlobalConst.TrackDeleteSubscribe, @event); } return new UnifyResultDto(true, true); } }