UavRefundApplicationService.cs
13.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.UavRefundApplication;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys;
using NCC.Extend.Entitys.Dto.UavRefundApplication;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Extend.Entitys.Enums;
namespace NCC.Extend.UavRefundApplication
{
/// <summary>
/// 退款申请表服务
/// </summary>
[ApiDescriptionSettings(Tag = "退款申请表服务", Name = "UavRefundApplication", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavRefundApplicationService : IUavRefundApplicationService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<UavRefundApplicationEntity> _uavRefundApplicationRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
/// <summary>
/// 初始化一个<see cref="UavRefundApplicationService"/>类型的新实例
/// </summary>
public UavRefundApplicationService(
ISqlSugarRepository<UavRefundApplicationEntity> uavRefundApplicationRepository,
IUserManager userManager)
{
_uavRefundApplicationRepository = uavRefundApplicationRepository;
_db = _uavRefundApplicationRepository.Context;
_userManager = userManager;
}
#region 获取退款申请表
/// <summary>
/// 获取退款申请表
/// </summary>
/// <param name="id">参数</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
var entity = await _db.Queryable<UavRefundApplicationEntity>().FirstAsync(p => p.Id == id);
var output = entity.Adapt<UavRefundApplicationInfoOutput>();
return output;
}
#endregion
#region 获取退款申请表列表
/// <summary>
/// 获取退款申请表列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable<UavRefundApplicationEntity>()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.WhereIF(!string.IsNullOrEmpty(input.userId), p => p.UserId.Contains(input.userId))
.WhereIF(!string.IsNullOrEmpty(input.userPhone), p => p.UserPhone.Contains(input.userPhone))
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UavRefundApplicationListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 新建退款申请表
/// <summary>
/// 新建退款申请表
/// </summary>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPost("")]
public async Task Create([FromBody] UavRefundApplicationCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt<UavRefundApplicationEntity>();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 更新退款申请表
/// <summary>
/// 更新退款申请表
/// </summary>
/// <param name="id">主键</param>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavRefundApplicationUpInput input)
{
var entity = input.Adapt<UavRefundApplicationEntity>();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除退款申请表
/// <summary>
/// 删除退款申请表
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable<UavRefundApplicationEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable<UavRefundApplicationEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
#region 支付宝小程序用户申请退款
/// <summary>
/// 支付宝小程序用户申请退款
/// </summary>
/// <param name="input">退款申请参数</param>
/// <returns></returns>
[HttpPost("ApplyRefund")]
public async Task<dynamic> ApplyRefund([FromBody] UavRefundApplicationCrInputByUser input)
{
try
{
//判断是否申请过退款
var refundApplication = await _db.Queryable<UavRefundApplicationEntity>().Where(p => p.OrderNo == input.OrderNo).FirstAsync();
if (refundApplication != null)
{
if (refundApplication.RefundStatus == RefundStatusEnum.待处理.GetHashCode() || refundApplication.RefundStatus == RefundStatusEnum.已同意.GetHashCode() || refundApplication.RefundStatus == RefundStatusEnum.已退款.GetHashCode())
{
return new { code = 500, message = "该订单已申请过退款,请勿重复申请" };
}
}
// 查询订单信息
var orderInfo = await _db.Queryable<UavOrderEntity>().FirstAsync(x => x.OrderNo == input.OrderNo);
if (orderInfo == null)
{
return new { code = 500, message = "订单不存在" };
}
// 验证订单状态
if (orderInfo.Status != UavOrderStatusEnum.已完成.GetHashCode()) // 已完成状态
{
return new { code = 500, message = "只有已完成的订单才能申请退款" };
}
// 验证申请人是否为订单所有者
if (orderInfo.UserAccount != _userManager.UserId)
{
return new { code = 500, message = "无权操作此订单" };
}
// 验证退款金额
if (input.ApplyAmount > orderInfo.ActualAmount)
{
return new { code = 500, message = "退款金额不能大于实际支付金额" };
}
// 创建退款申请
var entity = new UavRefundApplicationEntity
{
Id = YitIdHelper.NextId().ToString(),
OrderNo = input.OrderNo,
UserId = orderInfo.UserAccount,
UserPhone = orderInfo.UserPhone,
ApplyAmount = input.ApplyAmount,
ApplyTime = DateTime.Now,
AgentId = orderInfo.AgentId,
RefundReason = input.RefundReason,
RefundStatus = RefundStatusEnum.待处理.GetHashCode(), // 待处理
Remark = input.Remark
};
// 开启事务
var result = await _db.UseTranAsync(async () =>
{
// 插入退款申请记录
await _db.Insertable(entity).ExecuteCommandAsync();
});
if (!result.IsSuccess)
{
return new { code = 500, message = "申请退款失败,请重试" };
}
return new { code = 200, message = "退款申请提交成功", data = entity.Id };
}
catch (Exception ex)
{
return new { code = 500, message = $"申请退款失败:{ex.Message}" };
}
}
#endregion
#region 获取当前登录用户申请退款列表
/// <summary>
/// 获取当前登录用户申请退款列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("GetUserRefundList")]
public async Task<dynamic> GetUserRefundList([FromQuery] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable<UavRefundApplicationEntity>()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.Where(p => p.UserId == _userManager.UserId)
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UavRefundApplicationListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 获取当前代理商的退款申请列表
/// <summary>
///获取当前代理商的退款申请列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetAgentRefundList")]
public async Task<dynamic> GetAgentRefundList([FromBody] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable<UavRefundApplicationEntity>()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.Where(p => p.AgentId == _userManager.UserId)
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UavRefundApplicationListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 拒绝用户退款
/// <summary>
/// 拒绝用户退款
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("RejectRefund")]
public async Task<dynamic> RejectRefund([FromBody] UavRefundApplicationUpInput input)
{
var info = _db.Queryable<UavRefundApplicationEntity>().Where(x => x.Id == input.id).First();
if (info != null)
{
info.RefundStatus = RefundStatusEnum.已拒绝.GetHashCode();
info.HandleResult = input.handleResult;
info.HandleTime = DateTime.Now;
info.HandleUser = _userManager.UserId;
var isOk = await _db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
return "退款申请已拒绝";
}
else
{
return "无此退款申请信息";
}
}
#endregion
}
}