using Hg.Core.Domain.Dto.Statistcs; using Microsoft.Extensions.DependencyInjection; using MySqlConnector; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hg.Core.Domain { internal class StatisticsDomain : IStatisticsDomain { private readonly IBaseRepository _repository; private readonly IHttpClient _httpClient; private readonly IMapper _mapper; private readonly ICacheDomain _cacheDomain; private readonly IConfiguration _configuration; private readonly SystemConfig _systemConfig; private readonly IProductDomain _productDomain; private readonly IServiceProvider _serviceProvider; public StatisticsDomain(IConfiguration configuration, IBaseRepository repository, IHttpClient httpClient, IMapper mapper, ICacheDomain cacheDomain, IProductDomain productDomain, IInneruserDomain inneruserDomain, IServiceProvider serviceProvider) { _systemConfig = configuration.GetSection("SystemConfig").Get(); _configuration = configuration; _repository = repository; _httpClient = httpClient; _mapper = mapper; _cacheDomain = cacheDomain; _productDomain = productDomain; _serviceProvider = serviceProvider; } public async Task GetMonthOutboundData(string year, string month) { var date = $"{year}-{month}"; var result = new List(); var starDate = DateTime.Parse(date).ToString("yyyy-MM-dd"); var endDate = DateTime.Parse(date).AddMonths(1).ToString("yyyy-MM-dd"); var param = new MySqlParameter[] { new MySqlParameter(){ DbType=System.Data.DbType.String,Value=starDate,ParameterName="starDate"}, new MySqlParameter(){ DbType=System.Data.DbType.String,Value=endDate,ParameterName="endDate"} }; var sql = ""; using (var scope = _serviceProvider.CreateAsyncScope()) { var dncmsbaseRepository = scope.ServiceProvider.GetRequiredService>(); sql = @"select 'AI 回访' as typeName, count(distinct resid) as total FROM `WX_SZZYORDER` AS `w` LEFT JOIN `WX_ComplianceConfirm` AS `w0` ON `w`.`SZZYORDERID`=`w0`.`SzzyOrderId` where ORDERSTATUS IN ( '200','220','205','90','80') and ctime >= @starDate and ctime < @endDate and ai_hgrecord_status in (5) and w0.SzzyOrderId is null;"; result.Add(await dncmsbaseRepository.ExecuteSqlToEntityAsync(sql, param)); } using (var scope = _serviceProvider.CreateAsyncScope()) { var dncmsbaseRepository = scope.ServiceProvider.GetRequiredService>(); sql = @"select '人工回访' as typeName, count(distinct resid) as total FROM `WX_SZZYORDER` AS `w` LEFT JOIN `WX_ComplianceConfirm` AS `w0` ON `w`.`SZZYORDERID`=`w0`.`SzzyOrderId` where ORDERSTATUS IN ( '200','220','205','90','80') and ctime >= @starDate and ctime < @endDate and ai_hgrecord_status in (9) and w0.SzzyOrderId is null;"; result.Add(await dncmsbaseRepository.ExecuteSqlToEntityAsync(sql, param)); } using (var scope = _serviceProvider.CreateAsyncScope()) { var dncmsbaseRepository = scope.ServiceProvider.GetRequiredService>(); sql = @"select '电子回访' as typeName, count(distinct resid) as total FROM `WX_SZZYORDER` AS `w` LEFT JOIN `WX_ComplianceConfirm` AS `w0` ON `w`.`SZZYORDERID`=`w0`.`SzzyOrderId` where ORDERSTATUS IN ( '200','220','205','90','80') and ctime >= @starDate and ctime < @endDate and w0.SzzyOrderId is not null;"; result.Add(await dncmsbaseRepository.ExecuteSqlToEntityAsync(sql, param)); } var total = result.Sum(x => x.Total); foreach (var item in result) { item.Proportion = item.Total > 0 && total > 0 ? $"{item.Total * 100M / total:0.00}%" : "0%"; } return new { date, result }; } } }