using DotNetCore.CAP;
using Hotline.ContingencyManagement;
using Hotline.ContingencyManagement.Notifies;
using Hotline.DI;
using Hotline.EventBus;
using Hotline.Share.Dtos.ContingencyManagement;
using MapsterMapper;
using MediatR;
using XF.Domain.Repository;

namespace Hotline.Application.ContingencyManagement
{
    [Injection(AppScopes = EAppScope.YiBin)]
    public class ContingencyManagementNotifyHandler : INotificationHandler<ContingencyManagementNotify>
    {
        private readonly IRepository<ContingencyManagementOrders> _contingencyManagementOrdersRepository;
        private readonly IRepository<ContingencyManagementHotspot> _contingencyManagementHotspotRepository;
        private readonly IMapper _mapper;
        private readonly ICapPublisher _capPublisher;

        public ContingencyManagementNotifyHandler(
              IRepository<ContingencyManagementOrders> contingencyManagementOrdersRepository,
              IRepository<ContingencyManagementHotspot> contingencyManagementHotspotRepository,
              IMapper mapper,
              ICapPublisher capPublisher)
        {
            _contingencyManagementOrdersRepository = contingencyManagementOrdersRepository;
            _contingencyManagementHotspotRepository = contingencyManagementHotspotRepository;
            _mapper = mapper;
            _capPublisher = capPublisher;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(ContingencyManagementNotify notification, CancellationToken cancellationToken)
        {
            var order = notification.dto;
            //应急管理局业务处理
            //是否是指定热点
            if (await _contingencyManagementHotspotRepository.AnyAsync(p => p.Id == order.HotspotId, cancellationToken))
            {
                var orderData = await _contingencyManagementOrdersRepository.GetAsync(p => p.Id == order.Id, cancellationToken);
                //不存在新增
                if (orderData == null)
                {
                    orderData = new ContingencyManagementOrders();
                    orderData = _mapper.Map<ContingencyManagementOrders>(order);
                    orderData.Id = order.Id;
                    orderData.ArrangementOpinion = notification.ArrangementOpinion;
                    orderData.ArrangeTitle = notification.ArrangeTitle;
                    orderData.ArrangeContent = notification.ArrangeContent;
                    orderData.PushState = "0";
                    orderData.CreationTime = order.CreationTime;
                    await _contingencyManagementOrdersRepository.AddAsync(orderData, cancellationToken);
                }
                else
                {
                    //存在修改
                    _mapper.Map(order, orderData);
                    orderData.ArrangementOpinion = notification.ArrangementOpinion;
                    orderData.ArrangeTitle = notification.ArrangeTitle;
                    orderData.ArrangeContent = notification.ArrangeContent;
                    orderData.PushState = "0";
                    orderData.CreationTime = order.CreationTime;
                    await _contingencyManagementOrdersRepository.UpdateAsync(orderData, cancellationToken);
                }
                //推送消息
                var pushaData = _mapper.Map<ContingencyManagementOrdersDto>(orderData);
                pushaData.Id = orderData.Id;
                //推送消息
                await _capPublisher.PublishAsync(Hotline.Share.Mq.EventNames.HotlineOrderContingencyManagement,
                 pushaData, Hotline.Share.Mq.EventNames.HotlineOrderContingencyManagementState);
            }
        }
    }
}
