using Hotline.Caching.Interfaces;
using Hotline.DI;
using Hotline.JudicialManagement;
using Hotline.JudicialManagement.Notifies;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Enums.JudicialManagement;
using MapsterMapper;
using MediatR;
using XF.Domain.Repository;

namespace Hotline.Application.JudicialManagement
{
    [Injection(AppScopes = EAppScope.YiBin)]
    public class JudicialManagementOrderNotifyHandler : INotificationHandler<JudicialManagementOrderNotify>
    {
        private readonly IRepository<EnforcementHotspot> _enforcementHotspotRepository;
        private readonly ISystemDicDataCacheManager _sysDicDataCacheManager;
        private readonly ISystemSettingCacheManager _systemSettingCacheManager;
        private readonly IRepository<JudicialManagementOrders> _judicialManagementOrdersRepository;
        private readonly IRepository<EnforcementOrdersHandler> _enforcementOrdersHandlerRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enforcementHotspotRepository"></param>
        /// <param name="sysDicDataCacheManager"></param>
        /// <param name="systemSettingCacheManager"></param>
        /// <param name="judicialManagementOrdersRepository"></param>
        /// <param name="enforcementOrdersHandlerRepository"></param>
        /// <param name="mapper"></param>
        public JudicialManagementOrderNotifyHandler(
           IRepository<EnforcementHotspot> enforcementHotspotRepository,
           ISystemDicDataCacheManager sysDicDataCacheManager,
           ISystemSettingCacheManager systemSettingCacheManager,
           IRepository<JudicialManagementOrders> judicialManagementOrdersRepository,
           IRepository<EnforcementOrdersHandler> enforcementOrdersHandlerRepository,
           IMapper mapper)
        {
            _enforcementHotspotRepository = enforcementHotspotRepository;
            _sysDicDataCacheManager = sysDicDataCacheManager;
            _systemSettingCacheManager = systemSettingCacheManager;
            _judicialManagementOrdersRepository = judicialManagementOrdersRepository;
            _enforcementOrdersHandlerRepository = enforcementOrdersHandlerRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(JudicialManagementOrderNotify notification, CancellationToken cancellationToken)
        {
            var order = notification.dto;

            //如果没开启则不处理
            var isOpenJudicialManagement = _systemSettingCacheManager.GetSetting(SettingConstants.IsOpenJudicialManagement)?.SettingValue[0];
            if (isOpenJudicialManagement != "true")
                return;

            //排除咨询件
            if (order.AcceptTypeCode != "35")
                return;

            //排除非电话来源
            if (order.SourceChannelCode != "RGDH")
                return;

            //如果部门为空排除
            if (string.IsNullOrEmpty(order.OrgLevelOneName) || string.IsNullOrEmpty(order.OrgLevelOneCode))
                return;

            bool isAdd = false;
            //如果是行政执法类工单，写入数据
            if (order.IsEnforcementOrder.HasValue && order.IsEnforcementOrder.Value)
                isAdd = true;

            //如果是特定热点写入数据
            if (await _enforcementHotspotRepository.AnyAsync(p => p.Id == order.HotspotId, cancellationToken))
                isAdd = true;

            //如果是特定部门办理写入数据
            var JudicialManagementOrg = _sysDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.JudicialManagementOrg).Where(p => p.DicDataValue.Trim() == order.OrgLevelOneCode);

            if (JudicialManagementOrg != null && JudicialManagementOrg.Any())
                isAdd = true;


            //需要添加
            if (isAdd)
            {
                var orderData = await _judicialManagementOrdersRepository.GetAsync(p => p.Id == order.Id, cancellationToken);
                if (orderData == null)
                {
                    orderData = new JudicialManagementOrders();
                    //处理工单
                    _mapper.Map(order, orderData);
                    orderData.Id = order.Id;
                    orderData.IsEnforcementOrder = order.IsEnforcementOrder.HasValue ? order.IsEnforcementOrder.Value : false;
                    orderData.IsTheClueTrue = null;
                    orderData.IsPassTheBuckOrder = false;
                    orderData.OrderSoure = EOrderSoure.Hotline;
                    List<Kv> kvs = new()
                    {
                        new Kv { Key = order.ActualHandleOrgName, Value = order.ActualHandleOrgCode }
                    };
                    orderData.EnforcementOrdersHandler = kvs;
                    await _judicialManagementOrdersRepository.AddAsync(orderData, cancellationToken);

                    //处理部门
                    EnforcementOrdersHandler enforcementOrdersHandler = new()
                    {
                        OrderId = order.Id,
                        OrderNo = order.No,
                        OrderSoure = EOrderSoure.Hotline,
                        OrgCode = order.ActualHandleOrgCode,
                        OrgName = order.ActualHandleOrgName
                    };
                    await _enforcementOrdersHandlerRepository.AddAsync(enforcementOrdersHandler, cancellationToken);
                }
            }
        }
    }
}
