using DataSharing.Province;
using DataSharing.SendTask;
using DataSharing.Share.Dtos;
using DataSharing.Share.Dtos.YiBin.Enterprise;
using DataSharing.Share.Enums;
using Hotline.Share.Dtos.File;
using Hotline.Share.Dtos.Order;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using XF.Domain.Exceptions;

namespace DataSharing.YiBin.Enterprise
{
    /// <summary>
    /// 企业专班
    /// </summary>
    public class EnterpriseService : IEnterpriseService
    {
        private readonly IMapper _mapper;
        private readonly ILogger<EnterpriseService> _logger;
        private readonly IInitPushDataService _initPushDataService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="initPushDataService"></param>
        public EnterpriseService(IMapper mapper,
            ILogger<EnterpriseService> logger,
            IInitPushDataService initPushDataService)
        {
            _mapper = mapper;
            _logger = logger;
            _initPushDataService = initPushDataService;
        }

        /// <summary>
        /// 企业专班数据提交
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task InitEnterpriseDataAsync(OrderDto dto, CancellationToken cancellationToken)
        {
            var enterpriseData = _mapper.Map<PushWorkOrder>(dto);
            enterpriseData.AcceptTime = Convert.ToDateTime(enterpriseData.AcceptTime).ToString("yyyy-MM-dd HH:mm:ss");
            enterpriseData.ExpirationTime = string.IsNullOrEmpty(enterpriseData.ExpirationTime) == true ? "" : Convert.ToDateTime(enterpriseData.ExpirationTime).ToString("yyyy-MM-dd HH:mm:ss");
            enterpriseData.IncidentTime = string.IsNullOrEmpty(enterpriseData.IncidentTime) == true ? Convert.ToDateTime(enterpriseData.AcceptTime).ToString("yyyy-MM-dd HH:mm:ss") : Convert.ToDateTime(enterpriseData.IncidentTime).ToString("yyyy-MM-dd HH:mm:ss");

            //验证接收的数据是否正确
            string strResult = enterpriseData.Validate();
            if (!string.IsNullOrEmpty(strResult))
                throw new UserFriendlyException(strResult);

            var fileJson = _mapper.Map<List<FileJson>>(dto.FileJson);

            //将待推送数据写入待推送表
            var request = JsonSerializer.Serialize(enterpriseData, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions);
            await _initPushDataService.InitDsSendTaskOtherPlatforms("EnterprisePushWorkOrder", YbEnterpriseDefaults.PathEnterprisesWorkOrder, dto.ProvinceNo,
                request, request, EPlatformSource.Enterprise, fileJson: fileJson, cancellationToken: cancellationToken);

        }

        /// <summary>
        /// 企业服务撤单
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task EnterpriseRevokeOrderAsync(PublishSpecialDto dto, CancellationToken cancellationToken)
        {
            CancelWorkOrder dsDataWork = new()
            {
                WorkOrderNo = dto.Order.No,
                Reason = dto.Special.Reason
            };

            //将待推送数据写入待推送表
            //  await _initPushDataService.InitPushDataAsync("EnterpriseCancelWorkOrder", YbEnterpriseDefaults.PathEnterprisesCancelWorkOrder, System.Text.Json.JsonSerializer.Serialize(dsDataWork), dto.Order.ProvinceNo, platformSource: EPlatformSource.Enterprise, cancellationToken: cancellationToken);
            var request = JsonSerializer.Serialize(dsDataWork, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions);
            await _initPushDataService.InitDsSendTaskOtherPlatforms("EnterpriseCancelWorkOrder", YbEnterpriseDefaults.PathEnterprisesCancelWorkOrder, dto.Order.ProvinceNo,
                request, request, EPlatformSource.Enterprise, cancellationToken: cancellationToken);
        }
    }
}
