using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using XYY.Data.Model.Standard.Tracking;
using XYY.Service.Standard.TrackingService;
using XYY.Common.GRPC.Standard;
namespace XYY.gRpc.Tracking
{
    public class TrackingDbService : Db.DbBase
    {

        private readonly ILogger<SeventeenService> _logger;
        private readonly ITrackingService iTrackingService;

        public TrackingDbService(ILogger<SeventeenService> logger, ITrackingService iTrackingService)
        {
            _logger = logger;
            this.iTrackingService = iTrackingService;
        }

        public override async Task<AsyncDbPushStatusResult> AsyncDbPushStatus(PostPushDataResult request, ServerCallContext context)
        {
            var dict = request.Items.ToDictionary(x => x.TrackingNumber, x => x.FailMessage);
            var result = await this.iTrackingService.AsyncUpdateStatus(dict, request.Auth);
            return new AsyncDbPushStatusResult
            {
                IsSuccess = result.success,
                Message = result.message
            };
        }



        public override async Task<BaseRes> AddTracking(SeventeenConvertItem request, ServerCallContext context)
        {
            List<PostTrackingRequest> postTrackings = new List<PostTrackingRequest>();
            var d = request.FromGrpc<PostTrackingRequest>();
            d.List = new List<PostTrackingRequestItem>();
            if (request.List.Count > 0)
            {
                foreach (var item in request.List)
                {
                    d.List.Add(item.FromGrpc<PostTrackingRequestItem>());
                }
            }
            postTrackings.Add(d);
            var result = await iTrackingService.Insert(postTrackings);
            if (!result.success)
                _logger.LogError("api:{api} res:{res} req:{req}", "Tracking/AddTracking,", result.message, request);
            return result.ToGrpc<BaseRes>();
        }

        public override async Task<SupplementLogList> GetWaitSupplementTracking(Google.Protobuf.WellKnownTypes.Empty request, ServerCallContext context)
        {
            var list = await iTrackingService.GetWaitSupplementTracking();
            SupplementLogList supplementLog = new SupplementLogList();
            list.ForEach(x => x.ErrorMsg = x.ErrorMsg ?? string.Empty);
            //考虑长度问题，我们每次仅返回5000条
            supplementLog.List.AddRange(list.Select(x => x.ToGrpc<SupplementLog>()));
            return supplementLog;
        }

        public override async Task<SupplementLogList> AddTrackingTrajectory(SupplementLogList request, ServerCallContext context)
        {
            List<Logistics_Trace_SupplementLog> postTrackings = new List<Logistics_Trace_SupplementLog>();
            if (request.List.Count > 0)
            {
                foreach (var item in request.List)
                {
                    postTrackings.Add(item.FromGrpc<Logistics_Trace_SupplementLog>());
                }
            }
            SupplementLogList supplementLogList = new SupplementLogList();
            var result = await iTrackingService.AddTrackingTrajectory(postTrackings);
            foreach (var item in result)
            {
                supplementLogList.List.Add(item.ToGrpc<SupplementLog>());
            }
            return supplementLogList;
        }
        public override async Task<BaseRes> AddTrackingTrajectoryErrors(SupplementLogList request, ServerCallContext context)
        {
            List<Logistics_Trace_SupplementLog> postTrackings = new List<Logistics_Trace_SupplementLog>();
            if (request.List.Count > 0)
            {
                foreach (var item in request.List)
                {
                    postTrackings.Add(item.FromGrpc<Logistics_Trace_SupplementLog>());
                }
                postTrackings.ForEach(x => x.CreateTime = DateTime.Now);
            }
            var result = await iTrackingService.AddTrackingTrajectoryErrors(postTrackings);
            if (!result.success)
                _logger.LogError("api:{api} res:{res} req:{req}", "Tracking/AddTrackingTrajectoryErrors,", result.message, request);
            return result.ToGrpc<BaseRes>();
        }

    }
}
