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

namespace DataSharing.YiBin.ZzptPlatformsYB
{
    /// <summary>
    /// 综治平台
    /// </summary>
    public class ZzptService : IZzptService
    {
        private readonly IMapper _mapper;
        private readonly ILogger<ZzptService> _logger;
        private readonly IInitPushDataService _initPushDataService;

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

        /// <summary>
        /// 综治平台--推送工单信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task InitZzptPlatformsAsync(OrderDto dto, CancellationToken cancellationToken)
        {
            string fromGender = dto.FromGender switch
            {
                EGender.Female => "2",
                EGender.Male => "1",
                _ => "0",
            };
            string AcceptTypeCode = dto.AcceptTypeCode switch
            {
                "10" => "1",
                "35" => "2",
                "20" => "3",
                "15" => "4",
                "1" => "5",
                _ => "6",
            };
            Dictionary<string, string> dicParam = new()
            {
                { "code", dto.No },
                { "departmentNo", dto.AreaCode },
                { "userName", dto.FromName },
                { "gender", fromGender },
                { "email", "" },
                { "phome", dto.Contact },
                { "address", dto.FullAddress },
                { "type", AcceptTypeCode },
                { "title", dto.Title },
                { "content", dto.Content },
                { "occurDate", Convert.ToDateTime(dto.CreationTime).ToString("yyyy-MM-dd HH:mm:ss") },
                { "remark", "" }
            };

            //写入待推送数据
            var request = JsonSerializer.Serialize(dicParam, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions);
            await _initPushDataService.InitDsSendTaskOtherPlatforms("InitZzptPlatformsAsync", YbEnterpriseDefaults.ZzptPlatformsSendOrder, dto.ProvinceNo,
                request, request, EPlatformSource.ZZPT, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// 推送办理信息
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task InitZzptPlatformsDetailAsync(OrderFlowDto dto, CancellationToken cancellationToken)
        {
            Dictionary<string, string> dicParam = new()
            {
                { "code", dto.Order.ExternalId },
                { "action", "9" },
                { "user", dto.Order.ActualHandlerName },
                { "date", dto.Order.ActualHandleTime == null ? "" : dto.Order.ActualHandleTime.Value.ToString("yyyy-MM-dd HH:mm:ss") },
                { "result", dto.Order.ActualOpinion }
            };

            //写入待推送数据
            var request = JsonSerializer.Serialize(dicParam, ProvinceJsonSerializerOptions.UnStandardDatetimeJsonOptions);
            await _initPushDataService.InitDsSendTaskOtherPlatforms("InitZzptPlatformsDetailAsync", YbEnterpriseDefaults.ZzptPlatformsSendHandle, dto.Order.ProvinceNo,
                request, request, EPlatformSource.ZZPT, cancellationToken: cancellationToken);
        }


    }
}
