using ContingencyManagement.Sdk;
using DataSharing.Repository;
using DotNetCore.CAP;
using Hotline.Share.Dtos.ContingencyManagement;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
using XF.Domain.Dependency;

namespace DataSharing.Application.Receivers.YiBin
{
    public class YjgljReceiver : ICapSubscribe, IScopeDependency
    {
        private readonly IMapper _mapper;
        private readonly ILogger<YjgljReceiver> _logger;
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="serviceProvider"></param>
        /// <param name="sharingConfigurationManager"></param>
        public YjgljReceiver(IMapper mapper,
            ILogger<YjgljReceiver> logger,
          IServiceProvider serviceProvider,
          ISharingConfigurationManager sharingConfigurationManager)
        {
            _mapper = mapper;
            _logger = logger;
            //_db = uow.Db;
            if (sharingConfigurationManager.IsCity(ConfigurationConsts.YiBin))
                _db = serviceProvider.GetRequiredService<ISugarUnitOfWork<ContingencyManagementDbContext>>().Db;
        }

        /// <summary>
        /// 接收应急管理数据
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [CapSubscribe(Hotline.Share.Mq.EventNames.HotlineOrderContingencyManagement)]
        public async Task<RecOrderStateDto> RecOrderData(ContingencyManagementOrdersDto dto, CancellationToken cancellationToken)
        {
            int rows = 0;
            try
            {
                //处理业务数据
                var data = await _db.Queryable<ContingencyManagementOrders>().Where(p => p.Id == dto.Id).FirstAsync(cancellationToken);
                if (data != null)
                {
                    data.No = dto.No;
                    data.Title = dto.ArrangeTitle;
                    data.Content = dto.ArrangeContent;
                    data.SourceChannel = dto.SourceChannel;
                    data.AcceptType = dto.AcceptType;
                    data.HotspotSpliceName = dto.HotspotSpliceName;
                    data.City = dto.City;
                    data.County = dto.County;
                    data.CreationTime = dto.CreationTime;
                    data.FiledTime = dto.FiledTime;
                    data.FileOpinion = dto.ArrangementOpinion;
                    data.ActualHandleOrgCode = dto.ActualHandleOrgCode;
                    data.ActualHandleOrgName = dto.ActualHandleOrgName;
                    data.AreaCode = string.IsNullOrEmpty(dto.AreaCode) == true ? "" : dto.AreaCode.Substring(0, 6);
                    rows = await _db.Updateable(data).ExecuteCommandAsync();
                }
                else
                {
                    data = new ContingencyManagementOrders()
                    {
                        Id = dto.Id,
                        No = dto.No,
                        Title = dto.ArrangeTitle,
                        Content = dto.ArrangeContent,
                        SourceChannel = dto.SourceChannel,
                        AcceptType = dto.AcceptType,
                        HotspotSpliceName = dto.HotspotSpliceName,
                        City = dto.City,
                        County = dto.County,
                        CreationTime = dto.CreationTime,
                        FiledTime = dto.FiledTime,
                        FileOpinion = dto.ArrangementOpinion,
                        ActualHandleOrgCode = dto.ActualHandleOrgCode,
                        ActualHandleOrgName = dto.ActualHandleOrgName,
                        AreaCode = string.IsNullOrEmpty(dto.AreaCode) == true ? "" : dto.AreaCode.Substring(0, 6)
                    };
                    rows = await _db.Insertable(data).ExecuteCommandAsync();
                }
            }
            catch (Exception)
            {

            }

            //组装参数
            RecOrderStateDto recOrderStateDto = new()
            {
                Id = dto.Id,
                State = rows > 0 ? "1" : "2"
            };
            return recOrderStateDto;
        }
    }
}
