| | |
| | | using Volo.Abp.EventBus.Distributed; |
| | | using static Nest.FileSystemStats; |
| | | using Microsoft.EntityFrameworkCore; |
| | | using System.Threading.Channels; |
| | | using ZeroD.Util; |
| | | |
| | | namespace LifePayment.Application.LifePay |
| | | { |
| | |
| | | private readonly IRepository<LifePayOrder, Guid> _lifePayOrderRepository; |
| | | private readonly IRepository<LifePayUser, Guid> _lifePayUserRepository; |
| | | private readonly IRepository<DallyStatistics, Guid> _dallyStatisticsRepository; |
| | | private readonly IRepository<LifePayChannles, Guid> _lifePayChannlesRep; |
| | | |
| | | public StatisticsService( |
| | | IRepository<LifePayOrder, Guid> lifePayOrderRepository, |
| | | IRepository<LifePayUser, Guid> lifePayUserRepository, |
| | | IRepository<DallyStatistics, Guid> dallyStatisticsRepository) |
| | | IRepository<DallyStatistics, Guid> dallyStatisticsRepository, |
| | | IRepository<LifePayChannles, Guid> lifePayChannlesRep) |
| | | { |
| | | _lifePayOrderRepository = lifePayOrderRepository; |
| | | _lifePayUserRepository = lifePayUserRepository; |
| | | _dallyStatisticsRepository = dallyStatisticsRepository; |
| | | _lifePayChannlesRep = lifePayChannlesRep; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="channleId"></param> |
| | | /// <returns></returns> |
| | | public async Task<TopStatisticsOutput> GetTopStatistics(string channleId = "") |
| | | public async Task<TopStatisticsOutput> GetTopStatistics(List<string> channleList) |
| | | { |
| | | var today = DateTime.Now.Date; |
| | | var statistics = await _dallyStatisticsRepository.Where(x => x.CreationTime.Date == today) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId) |
| | | .FirstOrDefaultAsync(); |
| | | if (statistics == null) |
| | | .WhereIf(channleList.Count() > 0, x => channleList.Contains(x.ChannelId)) |
| | | .ToListAsync(); |
| | | if (statistics == null || statistics.Count() == 0) |
| | | { |
| | | /// 累计收款:统计平台账户下订单创建时间在昨天及之前收到的【用户支付成功的金额-退款给用户的金额】; |
| | | var accumulatedReceipts = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.PayStatus != LifePayStatusEnum.未支付) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount) - await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款).SumAsync(x => (x.RefundPrice ?? 0)); |
| | | /// 昨日收款:统计平台账户下订单创建时间在昨天收到的【用户支付成功的金额-退款给用户的金额】; |
| | | var receiptsYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.PayStatus == LifePayStatusEnum.已支付) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount) - await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款).SumAsync(x => (x.RefundPrice ?? 0)); |
| | | /// 累计收入:统计平台账户下订单状态为“已完成”且订单创建时间在昨天及之前收到的【用户实付金额-平台扣款金额-部分退款金额】; |
| | | var accumulatedIncome = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount - (x.PlatformDeductionAmount ?? 0) - (x.RefundPrice ?? 0)); |
| | | /// 累计下单:统计平台中订单下单时间在昨天及之前时间的订单记录; |
| | | var accumulatedOrders = await _lifePayOrderRepository.Where(x => x.CreationTime < today).WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日下单:统计平台中订单下单时间在昨天的订单记录; |
| | | var ordersNumYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日成功:统计平台中订单状态为“已完成/部分充值成功”且订单下单时间在昨天的订单记录; |
| | | var yesterdaySuccess = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成) |
| | | .WhereIf(string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日失败:统计平台中订单状态为“充值失败/已退款”且订单下单时间在昨天的订单记录; |
| | | var yesterdayFail = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已退款) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 累计用户 |
| | | var accumulatedUsers = await _lifePayUserRepository.WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.CreationChannleNum == channleId).CountAsync(); |
| | | /// 昨日活跃用户 |
| | | var yesterdayActiveUsers = await _lifePayUserRepository.WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.CreationChannleNum == channleId).Where(x => x.LastLoginTime >= today.AddDays(-1)).CountAsync(); |
| | | |
| | | var entity = new DallyStatistics() |
| | | TopStatisticsOutput topStatisticsOutput = new TopStatisticsOutput() { }; |
| | | var allChannle = await _lifePayChannlesRep.Where(x => x.IsDeleted == false).ToListAsync(); |
| | | foreach (var item in allChannle) |
| | | { |
| | | Id = GuidGenerator.Create(), |
| | | CreationTime = DateTime.Now, |
| | | Amount = 0, |
| | | AccumulatedReceipts = accumulatedReceipts??0, |
| | | AccumulatedIncome = accumulatedIncome ?? 0, |
| | | ReceiptsYesterday = receiptsYesterday ?? 0, |
| | | AccumulatedOrders = accumulatedOrders, |
| | | OrdersNumYesterday = ordersNumYesterday, |
| | | YesterdaySuccess = yesterdaySuccess, |
| | | YesterdayFail = yesterdayFail, |
| | | AccumulatedUsers = accumulatedUsers, |
| | | YesterdayActiveUsers = yesterdayActiveUsers, |
| | | ChannelId = channleId |
| | | }; |
| | | await _dallyStatisticsRepository.InsertAsync(entity); |
| | | var entity = await TopStatistics(item.ChannlesNum, today); |
| | | if (channleList.Count() == 0 || channleList.Contains(item.ChannlesNum)) |
| | | { |
| | | topStatisticsOutput.Amount += entity.Amount; |
| | | topStatisticsOutput.AccumulatedReceipts += entity.AccumulatedReceipts; |
| | | topStatisticsOutput.AccumulatedIncome += entity.AccumulatedIncome; |
| | | topStatisticsOutput.ReceiptsYesterday += entity.ReceiptsYesterday; |
| | | topStatisticsOutput.AccumulatedOrders += entity.AccumulatedOrders; |
| | | topStatisticsOutput.OrdersNumYesterday += entity.OrdersNumYesterday; |
| | | topStatisticsOutput.YesterdaySuccess += entity.YesterdaySuccess; |
| | | topStatisticsOutput.YesterdayFail += entity.YesterdayFail; |
| | | topStatisticsOutput.AccumulatedUsers += entity.AccumulatedUsers; |
| | | topStatisticsOutput.YesterdayActiveUsers += entity.YesterdayActiveUsers; |
| | | } |
| | | } |
| | | |
| | | TopStatisticsOutput topStatisticsOutput = new TopStatisticsOutput() |
| | | { |
| | | Amount = entity.Amount, |
| | | AccumulatedReceipts = entity.AccumulatedReceipts, |
| | | AccumulatedIncome = entity.AccumulatedIncome, |
| | | ReceiptsYesterday = entity.ReceiptsYesterday, |
| | | AccumulatedOrders = entity.AccumulatedOrders, |
| | | OrdersNumYesterday = entity.OrdersNumYesterday, |
| | | YesterdaySuccess = entity.YesterdaySuccess, |
| | | YesterdayFail = entity.YesterdayFail, |
| | | AccumulatedUsers = entity.AccumulatedUsers, |
| | | YesterdayActiveUsers = entity.YesterdayActiveUsers, |
| | | }; |
| | | |
| | | return topStatisticsOutput; |
| | | } |
| | | else |
| | | { |
| | | TopStatisticsOutput topStatisticsOutput = new TopStatisticsOutput() |
| | | { |
| | | Amount = statistics.Amount, |
| | | AccumulatedReceipts = statistics.AccumulatedReceipts, |
| | | AccumulatedIncome = statistics.AccumulatedIncome, |
| | | ReceiptsYesterday = statistics.ReceiptsYesterday, |
| | | AccumulatedOrders = statistics.AccumulatedOrders, |
| | | OrdersNumYesterday = statistics.OrdersNumYesterday, |
| | | YesterdaySuccess = statistics.YesterdaySuccess, |
| | | YesterdayFail = statistics.YesterdayFail, |
| | | AccumulatedUsers = statistics.AccumulatedUsers, |
| | | YesterdayActiveUsers = statistics.YesterdayActiveUsers, |
| | | Amount = statistics.Sum(s => s.Amount), |
| | | AccumulatedReceipts = statistics.Sum(s => s.AccumulatedReceipts), |
| | | AccumulatedIncome = statistics.Sum(s => s.AccumulatedIncome), |
| | | ReceiptsYesterday = statistics.Sum(s => s.ReceiptsYesterday), |
| | | AccumulatedOrders = statistics.Sum(s => s.AccumulatedOrders), |
| | | OrdersNumYesterday = statistics.Sum(s => s.OrdersNumYesterday), |
| | | YesterdaySuccess = statistics.Sum(s => s.YesterdaySuccess), |
| | | YesterdayFail = statistics.Sum(s => s.YesterdayFail), |
| | | AccumulatedUsers = statistics.Sum(s => s.AccumulatedUsers), |
| | | YesterdayActiveUsers = statistics.Sum(s => s.YesterdayActiveUsers), |
| | | }; |
| | | return topStatisticsOutput; |
| | | } |
| | | } |
| | | |
| | | //public async Task<ReceiptsListOutPut> GetReceiptsList(string channleId = "") |
| | | //public async Task<ReceiptsListOutPut> GetReceiptsList(List<string> channleList) |
| | | //{ |
| | | // var today = DateTime.Now.Date; |
| | | |
| | | //} |
| | | |
| | | private async Task<DallyStatistics> TopStatistics(string channleId, DateTime today) |
| | | { |
| | | /// 累计收款:统计平台账户下订单创建时间在昨天及之前收到的【用户支付成功的金额-退款给用户的金额】; |
| | | var accumulatedReceipts = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.PayStatus != LifePayStatusEnum.未支付) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount) - |
| | | await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => (x.RefundPrice ?? 0)); |
| | | /// 昨日收款:统计平台账户下订单创建时间在昨天收到的【用户支付成功的金额-退款给用户的金额】; |
| | | var receiptsYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.PayStatus == LifePayStatusEnum.已支付) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount) - |
| | | await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => (x.RefundPrice ?? 0)); |
| | | /// 累计收入:统计平台账户下订单状态为“已完成”且订单创建时间在昨天及之前收到的【用户实付金额-平台扣款金额-部分退款金额】; |
| | | var accumulatedIncome = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).SumAsync(x => x.PayAmount - (x.PlatformDeductionAmount ?? 0) - (x.RefundPrice ?? 0)); |
| | | /// 累计下单:统计平台中订单下单时间在昨天及之前时间的订单记录; |
| | | var accumulatedOrders = await _lifePayOrderRepository.Where(x => x.CreationTime < today).WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日下单:统计平台中订单下单时间在昨天的订单记录; |
| | | var ordersNumYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日成功:统计平台中订单状态为“已完成/部分充值成功”且订单下单时间在昨天的订单记录; |
| | | var yesterdaySuccess = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成) |
| | | .WhereIf(string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 昨日失败:统计平台中订单状态为“充值失败/已退款”且订单下单时间在昨天的订单记录; |
| | | var yesterdayFail = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已退款) |
| | | .WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.ChannelId == channleId).CountAsync(); |
| | | /// 累计用户 |
| | | var accumulatedUsers = await _lifePayUserRepository.WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.CreationChannleNum == channleId).CountAsync(); |
| | | /// 昨日活跃用户 |
| | | var yesterdayActiveUsers = await _lifePayUserRepository.WhereIf(!string.IsNullOrWhiteSpace(channleId), x => x.CreationChannleNum == channleId).Where(x => x.LastLoginTime >= today.AddDays(-1)).CountAsync(); |
| | | |
| | | |
| | | var entity = new DallyStatistics() |
| | | { |
| | | Id = GuidGenerator.Create(), |
| | | CreationTime = DateTime.Now, |
| | | Amount = 0, |
| | | AccumulatedReceipts = accumulatedReceipts ?? 0, |
| | | AccumulatedIncome = accumulatedIncome ?? 0, |
| | | ReceiptsYesterday = receiptsYesterday ?? 0, |
| | | AccumulatedOrders = accumulatedOrders, |
| | | OrdersNumYesterday = ordersNumYesterday, |
| | | YesterdaySuccess = yesterdaySuccess, |
| | | YesterdayFail = yesterdayFail, |
| | | AccumulatedUsers = accumulatedUsers, |
| | | YesterdayActiveUsers = yesterdayActiveUsers, |
| | | ChannelId = channleId |
| | | }; |
| | | await _dallyStatisticsRepository.InsertAsync(entity); |
| | | return entity; |
| | | } |
| | | } |
| | | } |