//using DotNetCore.CAP;
//using Hotline.CallCenter.Calls;
//using Hotline.Orders;
//using Hotline.Repository.SqlSugar;
//using Hotline.Share.Dtos.CallCenter;
//using Hotline.Share.Dtos.Order;
//using Hotline.Share.Mq;
//using MapsterMapper;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;
//using Microsoft.Extensions.Logging;
//using SqlSugar;

//namespace Hotline.Application.CallCenter
//{
//    public class CallRecordManager : BackgroundService
//    {
//        private readonly IServiceScopeFactory _serviceScopeFactory;


//        public CallRecordManager(IServiceScopeFactory serviceScopeFactory, ISugarUnitOfWork<WexDbContext> uowWex)
//        {
//            _serviceScopeFactory = serviceScopeFactory;
//        }

//        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
//        {
//            //20秒扫描一次威尔信数据库
//            using var sc = _serviceScopeFactory.CreateScope();
//            var _logger = sc.ServiceProvider.GetService<ILogger<CallRecordManager>>();
//            var time = TimeSpan.FromSeconds(20);
//            while (!stoppingToken.IsCancellationRequested)
//            {
//                using var scope = _serviceScopeFactory.CreateScope();
//                var _wexCallRecordRepository = scope.ServiceProvider.GetService<IWexCallRecordRepository>();
//                var _uowWex = scope.ServiceProvider.GetService<ISugarUnitOfWork<WexDbContext>>();
//                var _mapper = scope.ServiceProvider.GetService<IMapper>();
//                var _capPublisher = scope.ServiceProvider.GetService<ICapPublisher>();
//                var _orderRepository = scope.ServiceProvider.GetService<IOrderRepository>();
//                var wexRes = _uowWex?.Db.Ado.SqlQuery<WexRecord>("select * from view_telinfo_full where IsSynch=@IsSynch and IsOk = @IsOk and CallType<>'会议' ", new { IsSynch = 0, IsOk = 1 });
//                if (wexRes != null)
//                {
//                    List<CallConnectOrderDto> list = new List<CallConnectOrderDto>();
//                    foreach (var item in wexRes)
//                    {
//                        var wexCallRecord = new WexCallRecord();
//                        wexCallRecord.CallId = item.CallId;
//                        //呼叫方向
//                        switch (item.CallType)
//                        {
//                            case "外呼":
//                                wexCallRecord.Direction = Share.Enums.CallCenter.ECallDirection.Out;
//                                break;
//                            case "来电":
//                                wexCallRecord.Direction = Share.Enums.CallCenter.ECallDirection.In;
//                                break;
//                            case "中继":
//                                wexCallRecord.Direction = Share.Enums.CallCenter.ECallDirection.In;
//                                break;
//                            case "留言":
//                                wexCallRecord.Direction = Share.Enums.CallCenter.ECallDirection.In;
//                                break;
//                            case "黑名单":
//                                wexCallRecord.Direction = Share.Enums.CallCenter.ECallDirection.In;
//                                break;
//                            default:
//                                break;
//                        }
//                        if (wexCallRecord.Direction == Share.Enums.CallCenter.ECallDirection.Out)
//                        {
//                            wexCallRecord.CPN = item.TelPhone;
//                            wexCallRecord.CDPN = item.TrunkNum;
//                        }
//                        else
//                        {
//                            wexCallRecord.CPN = item.TrunkNum;
//                            wexCallRecord.CDPN = item.TelPhone;
//                        }
//                        wexCallRecord.StaffNo = item.GongHao;
//                        wexCallRecord.TelNo = item.FenJi;
//                        wexCallRecord.AreaName = item.AreaName;
//                        wexCallRecord.InIvrTime = item.BeginIvrTime;
//                        wexCallRecord.OutIvrTime = item.EndIvrTime;
//                        wexCallRecord.InQueueTime = item.BeginQueueTime;
//                        wexCallRecord.OutQueueTime = item.EndQueueTime;
//                        wexCallRecord.BeginTime = item.CallTime;
//                        wexCallRecord.BeginRingTime = item.BeginRingTime;
//                        wexCallRecord.EndRingTime = item.EndRingTime;
//                        wexCallRecord.RingTimes = int.Parse(item.RingTimes);
//                        wexCallRecord.AnsweredTime = item.BeginTalking;
//                        wexCallRecord.ByeTime = item.EndTalking;
//                        wexCallRecord.TalkTime = int.Parse(item.TalkLength);
//                        wexCallRecord.RecordUrl = item.RecordUrl;
//                        wexCallRecord.RecordName = item.RecordName;
//                        wexCallRecord.OtherRecordUrl = item.OtherRecordUrl;
//                        wexCallRecord.OtherRecordName = item.OtherRecordName;
//                        wexCallRecord.OtherSourceRecord = item.OtherSourceRecord;
//                        wexCallRecord.SourceRecord = item.SourceRecord;
//                        wexCallRecord.TrunkLine = item.TrunkNum;
//                        switch (item.NoAnswerResult)
//                        {
//                            case "客户先挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "外呼挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "员工先挂机":
//                                if (wexCallRecord.Direction == Share.Enums.CallCenter.ECallDirection.In)
//                                {
//                                    wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.To;
//                                }
//                                else
//                                {
//                                    wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                }
//                                break;
//                            case "坐席振铃挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.To;
//                                break;
//                            case "对端振铃挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "自助语音挂机":
//                                //TODO 确定不了是自动挂机 还是来电挂机(默认为来电挂机)
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "排队挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "中继转接挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "来电挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "开始转人工挂机":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "电话广播":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            case "播放工号":
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                            default:
//                                wexCallRecord.EndBy = Share.Enums.CallCenter.EEndBy.From;
//                                break;
//                        }
//                        switch (item.Result)
//                        {
//                            case "成功":
//                                wexCallRecord.OnState = Share.Enums.CallCenter.EOnState.On;
//                                break;
//                            case "失败":
//                                wexCallRecord.OnState = Share.Enums.CallCenter.EOnState.NoOn;
//                                break;
//                            case "接听":
//                                wexCallRecord.OnState = Share.Enums.CallCenter.EOnState.On;
//                                break;
//                            case "未接":
//                                wexCallRecord.OnState = Share.Enums.CallCenter.EOnState.NoOn;
//                                break;
//                            default:
//                                break;
//                        }
//                        wexCallRecord.Result = item.Result;
//                        wexCallRecord.EvaluateResult = item.AssessName;

//                        var id = await _wexCallRecordRepository.AddAsync(wexCallRecord);
//                        if (!string.IsNullOrEmpty(id))
//                        {
//                            //执行更新来源数据库已下载数据
//                            _uowWex?.Db.Ado.ExecuteCommand("update telinfo set IsSynch=1 where GUID=@CallId", new { CallId = wexCallRecord.CallId });

//                            var callRecord = _mapper.Map<CallRecordDto>(wexCallRecord);
//                            var callConnectionOrder = new CallConnectOrderDto() { CallRecord = callRecord };
//                            var order = await _orderRepository.GetAsync(x => x.CallId == wexCallRecord.CallId);
//                            if (order != null)
//                            {
//                                var orderDto = _mapper.Map<OrderDto>(order);
//                                callConnectionOrder.Order = orderDto;
//                            }
//                            list.Add(callConnectionOrder);
//                        }
//                    }
//                    //推省上
//                    if (list.Count > 0)
//                    {
//                        await _capPublisher.PublishAsync(EventNames.HotlineCallBye, list);
//                    }
//                }
//                else
//                {
//                    await Task.Delay(time, stoppingToken);
//                }
//            }
//        }


//        public class WexRecord
//        {
//            [SugarColumn(ColumnName = "CallID")]
//            public string CallId { get; set; }

//            [SugarColumn(ColumnName = "CallType")]
//            public string CallType { get; set; }

//            [SugarColumn(ColumnName = "Telphone")]
//            public string TelPhone { get; set; }

//            [SugarColumn(ColumnName = "TrunkLinePhone")]
//            public string TrunkNum { get; set; }

//            [SugarColumn(ColumnName = "GongHao")]
//            public string GongHao { get; set; }

//            [SugarColumn(ColumnName = "FenJi")]
//            public string FenJi { get; set; }

//            [SugarColumn(ColumnName = "AreaName")]
//            public string AreaName { get; set; }

//            [SugarColumn(ColumnName = "BeginIvrTime")]
//            public DateTime? BeginIvrTime { get; set; }

//            [SugarColumn(ColumnName = "EndIvrTime")]
//            public DateTime? EndIvrTime { get; set; }

//            [SugarColumn(ColumnName = "BeginQueueTime")]
//            public DateTime? BeginQueueTime { get; set; }

//            [SugarColumn(ColumnName = "EndQueueTime")]
//            public DateTime? EndQueueTime { get; set; }

//            [SugarColumn(ColumnName = "BeginRingTime")]
//            public DateTime? BeginRingTime { get; set; }

//            [SugarColumn(ColumnName = "EndRingTime")]
//            public DateTime? EndRingTime { get; set; }

//            [SugarColumn(ColumnName = "RingTimes")]
//            public string RingTimes { get; set; }

//            [SugarColumn(ColumnName = "CallTime")]
//            public DateTime? CallTime { get; set; }

//            [SugarColumn(ColumnName = "BeginTalking")]
//            public DateTime? BeginTalking { get; set; }

//            [SugarColumn(ColumnName = "EndTalking")]
//            public DateTime? EndTalking { get; set; }

//            /// <summary>
//            /// 通话时长
//            /// </summary>
//            [SugarColumn(ColumnName = "TalkLenth")]
//            public string TalkLength { get; set; }

//            [SugarColumn(ColumnName = "RecordUrl")]
//            public string RecordUrl { get; set; }

//            [SugarColumn(ColumnName = "RecordName")]
//            public string RecordName { get; set; }

//            [SugarColumn(ColumnName = "SourceRecord")]
//            public string SourceRecord { get; set; }

//            [SugarColumn(ColumnName = "OtherRecordUrl")]
//            public string OtherRecordUrl { get; set; }

//            [SugarColumn(ColumnName ="OtherRecordName")]
//            public string OtherRecordName { get; set; }

//            [SugarColumn(ColumnName = "OtherSourceRecord")]
//            public string OtherSourceRecord { get; set; }

//            [SugarColumn(ColumnName = "noAnswerResult")]
//            public string NoAnswerResult { get; set; }

//            [SugarColumn(ColumnName = "Result")]
//            public string Result { get; set; }

//            [SugarColumn(ColumnName = "AssessName")]
//            public string AssessName { get; set; }
//        }
//    }
//}
