zhengyuxuan
2025-03-28 6803ee52d2061fb811440fd17aede81250f0a53b
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using LifePayment.Application.Contracts;
using LifePayment.Domain.LifePay;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using ZeroD.Util;
using LifePayment.Domain;
using static LifePayment.Domain.Shared.LifePaymentConstant;
 
namespace LifePayment.Application.LifePay
{
    public class LifePayOrderService : ApplicationService, ILifePayOrderService
    {
        private readonly IRepository<LifePayRechargeReceipts, Guid> _lifePayRechargeReceiptsRepository;
        private readonly IRepository<LifePayExpensesReceipts, Guid> _lifePayExpensesReceiptsRepository;
        private readonly IAliPayApi _aliPayApi;
        private readonly IWxPayApi _wxPayApi;
 
        public LifePayOrderService(
                              IRepository<LifePayRechargeReceipts, Guid> lifePayRechargeReceiptsRepository,
                              IRepository<LifePayExpensesReceipts, Guid> lifePayExpensesReceiptsRepository,
                              IAliPayApi aliPayApi,
                              IWxPayApi wxPayApi)
        {
            _lifePayRechargeReceiptsRepository = lifePayRechargeReceiptsRepository;
            _lifePayExpensesReceiptsRepository = lifePayExpensesReceiptsRepository;
            _aliPayApi = aliPayApi;
            _wxPayApi = wxPayApi;
        }
 
        /// <summary>
        /// 获取充值流水
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<LifePayRechargeReceiptsPageOutput<LifePayRechargeReceiptsListOutput>> GetLifePayRechargeReceiptsPage(LifePayRechargeReceiptsPageInput input)
        {
            var list = await _lifePayRechargeReceiptsRepository.Where(x => x.IsDeleted == false)
                .WhereIf(input.KeyWord.IsNotNullOrEmpty(), x => x.OrderNo.Contains(input.KeyWord))
                .WhereIf(input.CreationTimeBegin.HasValue, x => x.CreationTime >= input.CreationTimeBegin)
                .WhereIf(input.CreationTimeEnd.HasValue, x => x.CreationTime <= input.CreationTimeEnd)
                .Select(x => new LifePayRechargeReceiptsListOutput()
                {
                    Id = x.Id,
                    OrderNo = x.OrderNo,
                    RechargeAmount = x.RechargeAmount,
                    Remark = x.Remark,
                    Voucher = x.Voucher.GetOssPath(),
                    CreationTime = x.CreationTime,
                })
                .GetPageResult(input.PageModel);
 
            var total = await _lifePayRechargeReceiptsRepository.Where(x => x.IsDeleted == false).SumAsync(x => x.RechargeAmount);
            LifePayRechargeReceiptsPageOutput<LifePayRechargeReceiptsListOutput> result = new LifePayRechargeReceiptsPageOutput<LifePayRechargeReceiptsListOutput>();
            result.Data = list.Data;
            result.TotalRechargeAmount = total;
            return result;
        }
 
 
 
        /// <summary>
        /// 编辑充值流水
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddUpdatePayRechargeReceipts(AddUpdatePayRechargeReceiptsInput input)
        {
            CheckExtensions.IfTrueThrowUserFriendlyException(input.OrderNo == null, "请输入业务订单号");
            CheckExtensions.IfTrueThrowUserFriendlyException(input.RechargeAmount <= 0, "充值金额应大于0");
            CheckExtensions.IfTrueThrowUserFriendlyException(input.Voucher == null, "请提交充值凭证");
            var repeat = await _lifePayRechargeReceiptsRepository.Where(x => x.IsDeleted == false && x.OrderNo == input.OrderNo).FirstOrDefaultAsync();
            CheckExtensions.IfTrueThrowUserFriendlyException(repeat != null && repeat.Id != input.Id, "业务订单号重复");
            if (input.Id.HasValue)
            {
                var payRechargeReceipts = await _lifePayRechargeReceiptsRepository.Where(x => x.IsDeleted == false && x.Id == input.Id.Value).FirstOrDefaultAsync();
                payRechargeReceipts.OrderNo = input.OrderNo;
                payRechargeReceipts.RechargeAmount = input.RechargeAmount;
                payRechargeReceipts.Remark = input.Remark;
                payRechargeReceipts.Voucher = input.Voucher;
            }
            else
            {
                LifePayRechargeReceipts payRechargeReceipts = new LifePayRechargeReceipts()
                {
                    Id = Guid.NewGuid(),
                    OrderNo = input.OrderNo,
                    RechargeAmount = input.RechargeAmount,
                    Remark = input.Remark,
                    Voucher = input.Voucher,
                };
                await _lifePayRechargeReceiptsRepository.InsertAsync(payRechargeReceipts);
            }
        }
 
 
        //public async Task<LifePayExpensesReceiptsPageOutput<LifePayExpensesReceiptsListOutput>> GetLifePayExpensesReceiptsPage(LifePayExpensesReceiptsPageInput input)
        //{
        //    var list = await _lifePayExpensesReceiptsRepository.Where(x => x.IsDeleted == false)
        //        .WhereIf(input.KeyWord.IsNotNullOrEmpty(), x => x.OrderNo.Contains(input.KeyWord))
        //        .WhereIf(input.CreationTimeBegin.HasValue, x => x.CreationTime >= input.CreationTimeBegin)
        //        .WhereIf(input.CreationTimeEnd.HasValue, x => x.CreationTime <= input.CreationTimeEnd)
        //        .Select(x => new LifePayRechargeReceiptsListOutput()
        //        {
        //            Id = x.Id,
        //            OrderNo = x.OrderNo,
        //            RechargeAmount = x.RechargeAmount,
        //            Remark = x.Remark,
        //            Voucher = x.Voucher.GetOssPath(),
        //            CreationTime = x.CreationTime,
        //        })
        //        .GetPageResult(input.PageModel);
 
        //    var total = await _lifePayRechargeReceiptsRepository.Where(x => x.IsDeleted == false).SumAsync(x => x.RechargeAmount);
        //    LifePayRechargeReceiptsPageOutput<LifePayRechargeReceiptsListOutput> result = new LifePayRechargeReceiptsPageOutput<LifePayRechargeReceiptsListOutput>();
        //    result.Data = list.Data;
        //    result.TotalRechargeAmount = total;
        //    return result;
        //}
 
        /// <summary>
        /// 插入收支流水
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddLifePayExpensesReceipts(AddLifePayExpensesReceiptsInput input)
        {
            var repeat = await _lifePayExpensesReceiptsRepository.Where(x => x.ExpensesReceiptsType == input.ExpensesReceiptsType
            && x.OrderNo == input.OrderNo).FirstOrDefaultAsync();
            if (repeat == null)
            {
                var data = new LifePayExpensesReceipts()
                {
                    Id = Guid.NewGuid(),
                    OrderNo = input.OrderNo,
                    OutOrderNo = input.OutOrderNo,
                    LifePayType = input.LifePayType,
                    ExpensesReceiptsType = input.ExpensesReceiptsType,
                    Amount = input.Amount
                };
 
                switch (input.LifePayType)
                {
                    case LifePayTypeEnum.AliPay:
                        if (input.ExpensesReceiptsType == ExpensesReceiptsTypeEnum.Expenses)
                        {
                            var query = await _aliPayApi.OrderInQuiry(new OrderInQuiryInput() { OutTradeNo = input.OrderNo });
                            if (query.Code == AlipayResultCode.Success && query.TradeStatus == AlipayStatus.TRADESUCCESS)
                            {
                                await _lifePayExpensesReceiptsRepository.InsertAsync(data);
                            }
                        }
                        else
                        {
                            var query = await _aliPayApi.QueryAlipayTradeRefund(new OrderInQuiryInput() { OutTradeNo = input.OrderNo });
                            if (query.Code == AlipayResultCode.Success && query.RefundStatus == AlipayRefundStatus.Success)
                            {
                                await _lifePayExpensesReceiptsRepository.InsertAsync(data);
                            }
                        }
                        break;
                    case LifePayTypeEnum.WxPay:
                        if (input.ExpensesReceiptsType == ExpensesReceiptsTypeEnum.Expenses)
                        {
                            var query = await _wxPayApi.WxPayTradeQuery(input.OrderNo);
                            if (query.Code == AlipayResultCode.Success && query.Status == WxPayStatus.支付成功)
                            {
                                await _lifePayExpensesReceiptsRepository.InsertAsync(data);
                            }
                        }
                        else
                        {
                            var query = await _wxPayApi.WxPayDomesticRefundsQuery(input.OrderNo);
                            if (query.Code == AlipayResultCode.Success && query.RefundStatus == WxPayRefundStatus.退款成功)
                            {
                                await _lifePayExpensesReceiptsRepository.InsertAsync(data);
                            }
                        }
                        break;
                    default: break;
                }
                
                
            }
        }
    }
}