using Exceptionless.Models; using Hg.Core.Entity.HgCrm; using Hg.Core.Entity.Views.DNG8; using Hg.Core.Entity.Views.DNZZ; using Hg.Core.Entity.Views.HGBM; using Hg.Core.Entity.Views.PTD2; using Hg.Core.Entity.Views.SHZZ; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using OfficeOpenXml.FormulaParsing.LexicalAnalysis; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace Hg.Internal.Domain { internal class CsvrCallRecordDomain : ICsvrCallRecordDomain { private readonly IBaseRepository _hgCrmRepository; private readonly IServiceProvider _serviceProvider; private readonly IHttpClient _httpClient; private readonly IMapper _mapper; private readonly IConfiguration _configuration; private readonly IRedisManager _redisManager; private readonly SystemConfig _systemConfig; public CsvrCallRecordDomain(IConfiguration configuration, IServiceProvider serviceProvider, IBaseRepository hgCrmRepository, IHttpClient httpClient, IMapper mapper, IRedisManager redisManager ) { _systemConfig = configuration.GetSection("SystemConfig").Get(); _serviceProvider = serviceProvider; _configuration = configuration; _hgCrmRepository = hgCrmRepository; _httpClient = httpClient; _mapper = mapper; _redisManager = redisManager; } public async Task CsvrCallRecordPage(CsvrCallRecordSearchPageDto search) { var query = CreateQuery(); if (string.IsNullOrEmpty(search.CompanyCode)) { var queryDNBB = CreateQuery(); var queryDNG8 = CreateQuery(); var queryDNYY = CreateQuery(); var queryDNZZ = CreateQuery(); var queryPTD2 = CreateQuery(); var querySHZZ = CreateQuery(); var queryQBJZ = CreateQuery(); var queryQBJX = CreateQuery(); query = query.Concat(queryDNBB) .Concat(queryDNG8) .Concat(queryDNYY) .Concat(queryDNZZ) .Concat(queryPTD2) .Concat(querySHZZ) .Concat(queryQBJZ) .Concat(queryQBJX); } else { query = search.CompanyCode == CompanyList.DNBB.GetDescription() ? CreateQuery () : search.CompanyCode == CompanyList.DNG8.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.DNYY.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.DNZZ.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.PTD2.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.SHZZ.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.QBJZ.GetDescription() ? CreateQuery() : search.CompanyCode == CompanyList.QBJX.GetDescription() ? CreateQuery() : CreateQuery(); } query = query .If(search.RecordId != null, x => x.Where(x => x.Id == search.RecordId)) .If(search.Eid != null && int.TryParse(search.Eid, out int eid), x => x.Where(x => x.Eid == int.Parse(search.Eid))) .If(search.Eid != null && !int.TryParse(search.Eid, out int eid2), x => x.Where(x => x.Uname.Contains(search.Eid))) .If(!string.IsNullOrEmpty(search.Resid), x => x.Where(x => x.Resid == search.Resid)) .If(!string.IsNullOrEmpty(search.UMID), x => x.Where(x => x.UmId == search.UMID)) .If(search.IsListened == IsListenType.未听, x => x.Where(x => x.ISCHECKED == (int)search.IsListened)) .If(search.IsListened == IsListenType.已听, x => x.Where(x => x.ISCHECKED == (int)search.IsListened)) .If(search.CallType.HasValue, x => x.Where(x => x.CallType == (int)search.CallType)) .If(!string.IsNullOrEmpty(search.CompanyCode), x => x.Where(x => x.CompanyCode == search.CompanyCode)) .If(search.LengthFrom != null, x => x.Where(x => x.TimeLength >= search.LengthFrom)) .If(search.LengthTo != null, x => x.Where(x => x.TimeLength <= search.LengthTo)) .If(search.TimeFrom != null, x => x.Where(x => x.Ctime >= search.TimeFrom)) .If(search.TimeTo != null, x => x.Where(x => x.Ctime <= search.TimeTo.Value.AddDays(1).AddMilliseconds(-1))) .OrderByDescending(x => x.Ctime); var total = await query.CountAsync(); if (search.Export.GetValueOrDefault()) { // todo 导出逻辑 var end = DateTime.Now.AddMonths(-1); if (total > 10000) { throw new ApiException("最多只能导出10000条录音数据"); } var exportData = await query.ToListAsync(); exportData = InitCsvrCallRecord(exportData); var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var fileName = $"人工录音列表 {DateTimeOffset.Now.ToUnixTimeMilliseconds()}.xlsx"; var fullName = Path.Combine(folder, fileName); ExcelHelper.Export(fullName, exportData); var tokenResult = await _httpClient.GetAsync(_systemConfig.GetFileServerToken()); var headers = new Dictionary { { "Authorization", $"Bearer {tokenResult}" } }; var result = await _httpClient.UploadFileAsync(_systemConfig.UploadFileServer(), fileName, fullName, headers); if (result.Success) { return ApiResult.Succeed(result.Url); } } var data = await query .Skip((search.PageIndex - 1) * search.PageSize) .Take(search.PageSize) .ToListAsync(); data = InitCsvrCallRecord(data); return new PageResult(search.PageIndex, search.PageSize, total, data); } private static List InitCsvrCallRecord(List data) { data.ForEach(x => { x.FileName = x.VideoUrl + x.FileName; var strIsTran = ""; if (x.IsTran.ToString() == "1") { strIsTran = "(转接)"; } if (x.CallType.ToString() == "1")//呼出 { x.Type = "呼出" + strIsTran; x.ServiceNumber = x.ServiceNumber;//主叫 x.TelNumberLast4 = x.TelNumberLast4;//被叫 } else { x.Type = "呼入" + strIsTran; x.ServiceNumber = x.TelNumberLast4;//被叫 x.TelNumberLast4 = x.ServiceNumber;//主叫 } }); return data; } private IQueryable CreateQuery() where TCsvrCallrecord : CsvrCallrecord where TBasInneruser : BAS_INNERUSER where TCacheOrdMemo : CacheOrdMemo where TOrdMemoContent : OrdMemoContent where TResCustomer:RES_CUSTOMER { var videoUrl = ""; var deptName = ""; var companyCode = ""; if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordHGBM)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.HGBM); deptName = _systemConfig.GetCompanyName(CompanyList.HGBM); companyCode = _systemConfig.GetCompanyCode(CompanyList.HGBM); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordDNBB)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.DNBB); deptName = _systemConfig.GetCompanyName(CompanyList.DNBB); companyCode = _systemConfig.GetCompanyCode(CompanyList.DNBB); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordDNG8)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.DNG8); deptName = _systemConfig.GetCompanyName(CompanyList.DNG8); companyCode = _systemConfig.GetCompanyCode(CompanyList.DNG8); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordDNYY)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.DNYY); deptName = _systemConfig.GetCompanyName(CompanyList.DNYY); companyCode = _systemConfig.GetCompanyCode(CompanyList.DNYY); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordDNZZ)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.DNZZ); deptName = _systemConfig.GetCompanyName(CompanyList.DNZZ); companyCode = _systemConfig.GetCompanyCode(CompanyList.DNZZ); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordPTD2)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.PTD2); deptName = _systemConfig.GetCompanyName(CompanyList.PTD2); companyCode = _systemConfig.GetCompanyCode(CompanyList.PTD2); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordSHZZ)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.SHZZ); deptName = _systemConfig.GetCompanyName(CompanyList.SHZZ); companyCode = _systemConfig.GetCompanyCode(CompanyList.SHZZ); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordQBJZ)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.QBJZ); deptName = _systemConfig.GetCompanyName(CompanyList.QBJZ); companyCode = _systemConfig.GetCompanyCode(CompanyList.QBJZ); } else if (typeof(TCsvrCallrecord) == typeof(CsvrCallrecordQBJX)) { videoUrl = _systemConfig.GetCompanyVideoUrl(CompanyList.QBJX); deptName = _systemConfig.GetCompanyName(CompanyList.QBJX); companyCode = _systemConfig.GetCompanyCode(CompanyList.QBJX); } var query = from x in _hgCrmRepository.GetRepository().Query() join y in _hgCrmRepository.GetRepository().Query() on x.SALESEID equals y.EID into xy from y in xy.DefaultIfEmpty() join b in _hgCrmRepository.GetRepository ().Query() on x.COID equals b.COID into xb from b in xb.DefaultIfEmpty() join c in _hgCrmRepository.GetRepository().Query() on b.MEMOCONTENTID equals c.CONTENTID into xc from c in xc.DefaultIfEmpty() join u in _hgCrmRepository.GetRepository().Query() on x.RESID equals u.RESID into um from u in um.DefaultIfEmpty() select new CsvrCallRecordDto { Id = x.PKID, RecordId = x.RECORDID, Resid = x.RESID, UmId= u.UMID, TimeLength = x.TIMELENGTH, TimeStart = x.TIMESTART, TimeEnd = x.TIMEEND, TelNumberLast4 = x.TELNUMBERLAST4, IsTran = x.ISTRAN, ServiceNumber = x.SERVICENUMBER, CallType = x.CALLTYPE, Uname = y.UNAME, Eid = y.EID, VideoUrl = videoUrl, FileName = x.FILENAME, Ctime = x.CTIME, ISCHECKED = x.ISCHECKED, DeptName = deptName, CompanyCode = companyCode, MemoContent = c.STRCONTENT }; return query; } public async Task> GetCompanyList() { return await Task.Run(() => { return _systemConfig.CompanyList; }); } } }