using DataSharing.Province.SendTask.ProvinceOther;
using DataSharing.Province.SendTask.SubmitCaseInfo;
using DataSharing.Province.SendTask.SubmitCaseProcess;
using DataSharing.Province.SendTask.SubmitCaseRecord;
using DataSharing.Province.SendTask.SubmitCaseResult;
using DataSharing.SendTask.OtherPlatforms;
using DataSharing.Share.Dtos.Province.HuiJu;
using DataSharing.Share.Enums;
using Hotline.Share.Dtos.File;
using Hotline.Share.Enums.CallCenter;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace DataSharing.SendTask
{
    /// <summary>
    /// 初始化推送数据
    /// </summary>
    public class InitPushDataService : IInitPushDataService, IScopeDependency
    {
        #region 注入
        private readonly IMapper _mapper;
        private readonly ILogger<InitPushDataService> _logger;
        private readonly IRepository<DsSendTaskSubmitCaseInfo> _sendTaskSubmitCaseInfoRepository;
        private readonly IRepository<DsSendTaskSubmitCaseProcess> _sendTaskSubmitCaseProcessRepository;
        private readonly IRepository<DsSendTaskSubmitCaseRecord> _sendTaskSubmitCaseRecordRepository;
        private readonly IRepository<DsSendTaskSubmitCaseResult> _sendTaskSubmitCaseResultRepository;
        private readonly IRepository<DsSendTaskProvinceOther> _sendTaskProvinceOtherRepository;
        private readonly IRepository<DsSendTaskOtherPlatforms> _sendTaskOtherPlatformsRepository;
        private readonly IRepository<DsWaitSendTaskSubmitCaseInfo> _waitSendTaskSubmitCaseInfoRepository;
        private readonly IRepository<DsWaitSendTaskSubmitCaseProcess> _waitSendTaskSubmitCaseProcessRepository;
        private readonly IRepository<DsWaitSendTaskSubmitCaseRecord> _waitSendTaskSubmitCaseRecordRepository;
        private readonly IRepository<DsWaitSendTaskSubmitCaseResult> _waitSendTaskSubmitCaseResultRepository;
        private readonly IRepository<DsWaitSendTaskProvinceOther> _waitSendTaskProvinceOtherRepository;
        private readonly IRepository<DsWaitSendTaskOtherPlatforms> _waitSendTaskOtherPlatformsRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="sendTaskSubmitCaseInfoRepository"></param>
        /// <param name="sendTaskSubmitCaseProcessRepository"></param>
        /// <param name="sendTaskSubmitCaseRecordRepository"></param>
        /// <param name="sendTaskSubmitCaseResultRepository"></param>
        /// <param name="sendTaskProvinceOtherRepository"></param>
        /// <param name="sendTaskOtherPlatformsRepository"></param>
        /// <param name="waitSendTaskSubmitCaseInfoRepository"></param>
        /// <param name="waitSendTaskSubmitCaseProcessRepository"></param>
        /// <param name="waitSendTaskSubmitCaseRecordRepository"></param>
        /// <param name="waitSendTaskSubmitCaseResultRepository"></param>
        /// <param name="waitSendTaskProvinceOtherRepository"></param>
        /// <param name="waitSendTaskOtherPlatformsRepository"></param>
        public InitPushDataService(IMapper mapper,
            ILogger<InitPushDataService> logger,
            IRepository<DsSendTaskSubmitCaseInfo> sendTaskSubmitCaseInfoRepository,
          IRepository<DsSendTaskSubmitCaseProcess> sendTaskSubmitCaseProcessRepository,
          IRepository<DsSendTaskSubmitCaseRecord> sendTaskSubmitCaseRecordRepository,
           IRepository<DsSendTaskSubmitCaseResult> sendTaskSubmitCaseResultRepository,
           IRepository<DsSendTaskProvinceOther> sendTaskProvinceOtherRepository,
           IRepository<DsSendTaskOtherPlatforms> sendTaskOtherPlatformsRepository,
            IRepository<DsWaitSendTaskSubmitCaseInfo> waitSendTaskSubmitCaseInfoRepository,
          IRepository<DsWaitSendTaskSubmitCaseProcess> waitSendTaskSubmitCaseProcessRepository,
          IRepository<DsWaitSendTaskSubmitCaseRecord> waitSendTaskSubmitCaseRecordRepository,
           IRepository<DsWaitSendTaskSubmitCaseResult> waitSendTaskSubmitCaseResultRepository,
           IRepository<DsWaitSendTaskProvinceOther> waitSendTaskProvinceOtherRepository,
           IRepository<DsWaitSendTaskOtherPlatforms> waitSendTaskOtherPlatformsRepository)
        {
            _mapper = mapper;
            _logger = logger;
            _sendTaskSubmitCaseInfoRepository = sendTaskSubmitCaseInfoRepository;
            _sendTaskSubmitCaseProcessRepository = sendTaskSubmitCaseProcessRepository;
            _sendTaskSubmitCaseRecordRepository = sendTaskSubmitCaseRecordRepository;
            _sendTaskSubmitCaseResultRepository = sendTaskSubmitCaseResultRepository;
            _sendTaskProvinceOtherRepository = sendTaskProvinceOtherRepository;
            _sendTaskOtherPlatformsRepository = sendTaskOtherPlatformsRepository;
            _waitSendTaskSubmitCaseInfoRepository = waitSendTaskSubmitCaseInfoRepository;
            _waitSendTaskSubmitCaseProcessRepository = waitSendTaskSubmitCaseProcessRepository;
            _waitSendTaskSubmitCaseRecordRepository = waitSendTaskSubmitCaseRecordRepository;
            _waitSendTaskSubmitCaseResultRepository = waitSendTaskSubmitCaseResultRepository;
            _waitSendTaskProvinceOtherRepository = waitSendTaskProvinceOtherRepository;
            _waitSendTaskOtherPlatformsRepository = waitSendTaskOtherPlatformsRepository;
        }
        #endregion

        /// <summary>
        /// 组装工单受理数据
        /// </summary>
        /// <param name="orderId">工单编号</param>
        /// <param name="provinceNo">省编号</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="generationTime">生成时间（保存工单创建时间或者通话记录生成时间）</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskSubmitCaseInfo(string orderId,string provinceNo, string request, SubmitCaseInfo requestData,
            DateTime? generationTime = null, CancellationToken cancellationToken = default)
        {
            //写入待推送表
            DsWaitSendTaskSubmitCaseInfo waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = "SubmitCaseInfo",
                HttpMethod = "Post",
                Path = "submit_case_info",
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = EPathType.HuiJu,
                ExpiredTime = requestData.TfdBackTimeBf,
                CaseSource=requestData.CaseSource,
                OrderId= orderId
            };
            waitTask.Id = await _waitSendTaskSubmitCaseInfoRepository.AddAsync(waitTask, cancellationToken);

            var sendTask = _mapper.Map<DsSendTaskSubmitCaseInfo>(waitTask);
            sendTask.Id = waitTask.Id;

            //写入推送主表
            return await _sendTaskSubmitCaseInfoRepository.AddAsync(sendTask, cancellationToken);
        }

        /// <summary>
        /// 组装服务工单处理过程
        /// </summary>
        /// <param name="provinceNo">省编号</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="generationTime">生成时间（保存工单创建时间或者通话记录生成时间）</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskSubmitCaseProcess(string provinceNo, string request, SubmitCaseProcessInfo requestData,
            DateTime? generationTime = null, CancellationToken cancellationToken = default)
        {
            //写入待推送表
            DsWaitSendTaskSubmitCaseProcess waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = "SubmitCaseProcess",
                HttpMethod = "Post",
                Path = "submit_case_process",
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = EPathType.HuiJu,
                CaseId = requestData.CaseId
            };
            waitTask.Id = await _waitSendTaskSubmitCaseProcessRepository.AddAsync(waitTask, cancellationToken);

            var sendTask = _mapper.Map<DsSendTaskSubmitCaseProcess>(waitTask);
            sendTask.Id = waitTask.Id;
            //写入推送主表
            return await _sendTaskSubmitCaseProcessRepository.AddAsync(sendTask, cancellationToken);
        }

        /// <summary>
        /// 组装电话记录
        /// </summary>
        /// <param name="provinceNo">省编号</param>
        ///  <param name="callId">通话记录ID</param>
        ///   <param name="callLogId">通话记录本地ID</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="generationTime">生成时间（保存工单创建时间或者通话记录生成时间）</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskSubmitCaseRecord(string provinceNo, string callId,string callLogId, string request, SubmitCaseRecordInfo requestData,
            DateTime? generationTime = null, CancellationToken cancellationToken = default)
        {
            //写入待推送表
            DsWaitSendTaskSubmitCaseRecord waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = "SubmitCaseRecord",
                HttpMethod = "Post",
                Path = "submit_case_record",
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = EPathType.HuiJu,
                CallId = callId,
                CallDirection = requestData.IsPhoneCall == "1" ? "1" : "0",
                OnState = requestData.PhoneStatus == "10" ? "1" : "2",
                CallLogId = callLogId,
            };
            waitTask.Id = await _waitSendTaskSubmitCaseRecordRepository.AddAsync(waitTask, cancellationToken);
            var sendTask = _mapper.Map<DsSendTaskSubmitCaseRecord>(waitTask);
            sendTask.Id = waitTask.Id;
            //写入推送主表
            return await _sendTaskSubmitCaseRecordRepository.AddAsync(sendTask, cancellationToken);
        }

        /// <summary>
        /// 组装服务工单处理结果
        /// </summary>
        /// <param name="provinceNo">省编号</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="generationTime">生成时间（保存工单创建时间或者通话记录生成时间）</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskSubmitCaseResult(string provinceNo, string request, SubmitCaseResultInfo requestData,
            DateTime? generationTime = null, CancellationToken cancellationToken = default)
        {
            //写入待推送表
            DsWaitSendTaskSubmitCaseResult waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = "SubmitCaseResult",
                HttpMethod = "Post",
                Path = "submit_case_result",
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = EPathType.HuiJu,
                FiledTime = requestData.FinishTime
            };

            waitTask.Id = await _waitSendTaskSubmitCaseResultRepository.AddAsync(waitTask, cancellationToken);
            var sendTask = _mapper.Map<DsSendTaskSubmitCaseResult>(waitTask);
            sendTask.Id = waitTask.Id;
            //写入推送主表
            return await _sendTaskSubmitCaseResultRepository.AddAsync(sendTask, cancellationToken);
        }

        /// <summary>
        /// 省平台其他业务推送任务数据
        /// </summary>
        /// <param name="taskType">任务类型</param>
        /// <param name="path">请求地址</param>
        /// <param name="provinceNo">省编号</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="httpMethod">请求方式</param>
        /// <param name="pathType">接口类型，汇聚、协同</param>
        /// <param name="fileJson">附件</param>
        /// <param name="generationTime">生成时间</param>
        /// <param name="visitTime">回访时间（只有回访才有数据）</param>
        /// <param name="subjectResultSatify">回访满意度（只有回访才有数据）</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskProvinceOther(string taskType, string path, string provinceNo, string request, string requestData,
            string httpMethod = "Post", EPathType pathType = EPathType.Other, List<FileJson>? fileJson = null,
            DateTime? generationTime = null,DateTime? visitTime=null,string? subjectResultSatify=null, CancellationToken cancellationToken = default)
        {
            DsWaitSendTaskProvinceOther waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = taskType,
                HttpMethod = httpMethod,
                Path = path,
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = pathType,
                FileJson = fileJson,
                VisitTime= visitTime,
                SubjectResultSatify= subjectResultSatify
            };

            waitTask.Id = await _waitSendTaskProvinceOtherRepository.AddAsync(waitTask, cancellationToken);
            var sendTask = _mapper.Map<DsSendTaskProvinceOther>(waitTask);
            sendTask.Id = waitTask.Id;
            //写入推送主表
            return await _sendTaskProvinceOtherRepository.AddAsync(sendTask, cancellationToken);
        }

        /// <summary>
        /// 其他平台推送任务数据
        /// </summary>
        /// <param name="taskType">任务类型</param>
        /// <param name="path">请求地址</param>
        /// <param name="provinceNo">省编号</param>
        /// <param name="request">请求参数</param>
        /// <param name="requestData">请求参数</param>
        ///  <param name="platformSource">推送平台</param>
        /// <param name="httpMethod">请求方式</param>
        /// <param name="pathType">接口类型，汇聚、协同</param>
        /// <param name="fileJson">附件</param>
        /// <param name="generationTime">生成时间</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> InitDsSendTaskOtherPlatforms(string taskType, string path, string provinceNo, string request, string requestData,
            EPlatformSource platformSource, string httpMethod = "Post", EPathType pathType = EPathType.Other, List<FileJson>? fileJson = null,
            DateTime? generationTime = null, CancellationToken cancellationToken = default)
        {
            DsWaitSendTaskOtherPlatforms waitTask = new()
            {
                ProvinceNo = provinceNo,
                TaskType = taskType,
                HttpMethod = httpMethod,
                Path = path,
                Request = request,
                RequestData = requestData,
                GenerationTime = generationTime,
                PathType = pathType,
                FileJson = fileJson,
                PlatformSource = platformSource
            };

            waitTask.Id = await _waitSendTaskOtherPlatformsRepository.AddAsync(waitTask, cancellationToken);
            var sendTask = _mapper.Map<DsSendTaskOtherPlatforms>(waitTask);
            sendTask.Id = waitTask.Id;
            //写入推送主表
            return await _sendTaskOtherPlatformsRepository.AddAsync(sendTask, cancellationToken);
        }
    }
}
