using DotNetCore.CAP;
using Hotline.Orders;
using Hotline.Orders.Notifications;
using Hotline.OrderTranspond;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Order;
using MapsterMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using XF.Domain.Repository;

namespace Hotline.Application.OrderApp.Handlers.OrderHandler
{
    // [Injection(AppScopes = EAppScope.YiBin)]
    public class TranspondCityNotifyHandler : INotificationHandler<OrderStartWorkflowNotify>
    {
        private readonly IMapper _mapper;
        private readonly ILogger<TranspondCityNotifyHandler> _logger;
        private readonly ICapPublisher _capPublisher;
        private readonly IOrderRepository _orderRepository;
        private readonly IRepository<TranspondCityRawData> _transpondCityRawDataRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="capPublisher"></param>
        /// <param name="orderRepository"></param>
        /// <param name="transpondCityRawDataRepository"></param>
        public TranspondCityNotifyHandler(
            IMapper mapper,
            ILogger<TranspondCityNotifyHandler> logger,
          ICapPublisher capPublisher,
          IOrderRepository orderRepository,
          IRepository<TranspondCityRawData> transpondCityRawDataRepository)
        {
            _mapper = mapper;
            _logger = logger;
            _capPublisher = capPublisher;
            _orderRepository = orderRepository;
            _transpondCityRawDataRepository = transpondCityRawDataRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(OrderStartWorkflowNotify notification, CancellationToken cancellationToken)
        {
            try
            {
                var id = notification.Id;
                var order = await _orderRepository.GetAsync(id, cancellationToken);
                if (order != null)
                {
                    //是否市州互转
                    if (order.Transpond.HasValue && order.Transpond.Value)
                    {
                        var orderDto = _mapper.Map<OrderDto>(order);
                        await _capPublisher.PublishAsync(Share.Mq.EventNames.HotlineOrderTranspondCity, orderDto);
                        //保存本地数据
                        TranspondCityRawData cityRawData = new TranspondCityRawData
                        {
                            OrderCode = order.No,
                            TransferOutTime = DateTime.Now,
                            CityName = order.TranspondCityName,
                            Direction = ETranspondDirection.Out
                        };

                        await _transpondCityRawDataRepository.AddAsync(cityRawData, cancellationToken);
                    }
                }

            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                throw;
            }
        }
    }
}
