zhengyuxuan
2025-03-25 c43a23fc14c317b3a79fe5d8000ae17b9610e3ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using LifePayment.Application.Contracts;
using LifePayment.Domain.LifePay;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using LifePayment.Domain;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using static Nest.FileSystemStats;
using Microsoft.EntityFrameworkCore;
 
namespace LifePayment.Application.LifePay
{
    public class StatisticsService : ApplicationService, IStatisticsService
    {
        private readonly IRepository<LifePayOrder, Guid> _lifePayOrderRepository;
        private readonly IRepository<LifePayUser, Guid> _lifePayUserRepository;
        private readonly IRepository<DallyStatistics, Guid> _dallyStatisticsRepository;
 
        public StatisticsService(
                              IRepository<LifePayOrder, Guid> lifePayOrderRepository,
                              IRepository<LifePayUser, Guid> lifePayUserRepository,
                            IRepository<DallyStatistics, Guid> dallyStatisticsRepository)
        {
            _lifePayOrderRepository = lifePayOrderRepository;
            _lifePayUserRepository = lifePayUserRepository;
            _dallyStatisticsRepository = dallyStatisticsRepository;
        }
 
        public async Task<TopStatisticsOutput> GetTopStatistics()
        {
            var today = DateTime.Now.Date;
            var statistics = await _dallyStatisticsRepository.Where(x => x.CreationTime.Date == today).FirstOrDefaultAsync();
            if (statistics == null)
            {
                /// 累计收款:统计平台账户下订单创建时间在昨天及之前收到的【用户支付成功的金额-退款给用户的金额】;
                var accumulatedReceipts = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.PayStatus == LifePayStatusEnum.已支付).SumAsync(x => x.PayAmount) - await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款).SumAsync(x => x.RefundPrice);
                /// 昨日收款:统计平台账户下订单创建时间在昨天收到的【用户支付成功的金额-退款给用户的金额】;
                var receiptsYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.PayStatus == LifePayStatusEnum.已支付).SumAsync(x => x.PayAmount) - await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款).SumAsync(x => x.PayAmount);
                /// 累计收入:统计平台账户下订单状态为“已完成”且订单创建时间在昨天及之前收到的【用户实付金额-平台扣款金额-部分退款金额】;
                var accumulatedIncome = await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成).SumAsync(x => x.PayAmount - x.PlatformDeductionAmount) -
                    await _lifePayOrderRepository.Where(x => x.CreationTime < today && x.LifePayRefundStatus == LifePayRefundStatusEnum.已退款).SumAsync(x => x.RefundPrice);
                /// 累计下单:统计平台中订单下单时间在昨天及之前时间的订单记录;
                var accumulatedOrders = await _lifePayOrderRepository.Where(x => x.CreationTime < today).CountAsync();
                /// 昨日下单:统计平台中订单下单时间在昨天的订单记录;
                var ordersNumYesterday = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today).CountAsync();
                /// 昨日成功:统计平台中订单状态为“已完成/部分充值成功”且订单下单时间在昨天的订单记录;
                var yesterdaySuccess = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已完成).CountAsync();
                /// 昨日失败:统计平台中订单状态为“充值失败/已退款”且订单下单时间在昨天的订单记录;
                var yesterdayFail = await _lifePayOrderRepository.Where(x => x.CreationTime >= today.AddDays(-1) && x.CreationTime < today && x.LifePayOrderStatus == LifePayOrderStatusEnum.已退款).CountAsync();
                /// 累计用户
                var accumulatedUsers = await _lifePayUserRepository.CountAsync();
                /// 昨日活跃用户
                var yesterdayActiveUsers = await _lifePayUserRepository.Where(x => x.LastLoginTime >= today.AddDays(-1)).CountAsync();
 
                var entity = new DallyStatistics()
                {
                    Id = GuidGenerator.Create(),
                    CreationTime = DateTime.Now,
                    Amount = 0,
                    AccumulatedReceipts = accumulatedReceipts.HasValue? accumulatedReceipts.Value:0,
                    AccumulatedIncome = accumulatedIncome.HasValue ? accumulatedIncome.Value : 0,
                    ReceiptsYesterday = receiptsYesterday,
                    AccumulatedOrders = accumulatedOrders,
                    OrdersNumYesterday = ordersNumYesterday,
                    YesterdaySuccess = yesterdaySuccess,
                    YesterdayFail = yesterdayFail,
                    AccumulatedUsers = accumulatedUsers,
                    YesterdayActiveUsers = yesterdayActiveUsers
                };
                await _dallyStatisticsRepository.InsertAsync(entity);
 
                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,
                    ReceiptsYesterday = statistics.ReceiptsYesterday,
                    AccumulatedOrders = statistics.AccumulatedOrders,
                    OrdersNumYesterday = statistics.OrdersNumYesterday,
                    YesterdaySuccess = statistics.YesterdaySuccess,
                    YesterdayFail = statistics.YesterdayFail,
                    AccumulatedUsers = statistics.AccumulatedUsers,
                };
                return topStatisticsOutput;
            }
        }
    }
}