RiderWithdrawServiceImpl.java
12.9 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
312
313
314
315
316
317
318
319
320
321
322
323
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.dto.RiderWithdrawApplyDTO;
import com.diligrp.rider.dto.WithdrawAuditDTO;
import com.diligrp.rider.dto.WithdrawMarkPaidDTO;
import com.diligrp.rider.entity.Rider;
import com.diligrp.rider.entity.RiderBalance;
import com.diligrp.rider.entity.RiderWithdrawApply;
import com.diligrp.rider.mapper.RiderBalanceMapper;
import com.diligrp.rider.mapper.RiderMapper;
import com.diligrp.rider.mapper.RiderWithdrawApplyMapper;
import com.diligrp.rider.service.RiderWithdrawService;
import com.diligrp.rider.vo.PageResultVO;
import com.diligrp.rider.vo.RiderWithdrawApplyVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeParseException;
import java.util.List;
@Service
@RequiredArgsConstructor
public class RiderWithdrawServiceImpl implements RiderWithdrawService {
private static final int PAGE_SIZE = 20;
private static final int STATUS_PENDING = 0;
private static final int STATUS_APPROVED = 1;
private static final int STATUS_REJECTED = 2;
private static final int STATUS_PAID = 3;
private static final ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");
private final RiderWithdrawApplyMapper withdrawMapper;
private final RiderMapper riderMapper;
private final RiderBalanceMapper balanceMapper;
@Override
@Transactional
public void apply(Long riderId, RiderWithdrawApplyDTO dto) {
if (riderId == null || riderId < 1) {
throw new BizException("骑手身份无效");
}
validateApply(dto);
Rider rider = withdrawMapper.selectRiderForUpdate(riderId);
if (rider == null) {
throw new BizException("骑手信息不存在");
}
BigDecimal amount = dto.getAmount().setScale(2, java.math.RoundingMode.HALF_UP);
BigDecimal balance = money(rider.getBalance());
BigDecimal frozenBalance = money(rider.getFrozenBalance());
if (balance.compareTo(amount) < 0) {
throw new BizException("可提现余额不足");
}
long now = now();
RiderWithdrawApply apply = new RiderWithdrawApply();
apply.setWithdrawNo(buildWithdrawNo(riderId));
apply.setRiderId(riderId);
apply.setCityId(rider.getCityId());
apply.setAmount(amount);
apply.setStatus(STATUS_PENDING);
apply.setAccountType(dto.getAccountType());
apply.setAccountName(trim(dto.getAccountName()));
apply.setBankName(trim(dto.getBankName()));
apply.setBankBranch(trim(dto.getBankBranch()));
apply.setAccountNo(trim(dto.getAccountNo()));
apply.setApplyRemark(trim(dto.getApplyRemark()));
apply.setAuditRemark("");
apply.setAuditorId(0L);
apply.setAuditorName("");
apply.setApplyTime(now);
apply.setAuditTime(0L);
apply.setPayTime(0L);
apply.setTransferNo("");
apply.setCreateTime(now);
apply.setUpdateTime(now);
withdrawMapper.insert(apply);
updateRiderMoney(riderId, balance.subtract(amount), frozenBalance.add(amount));
}
@Override
public PageResultVO<RiderWithdrawApplyVO> riderList(Long riderId, Integer status, int page) {
int currentPage = normalizePage(page);
int offset = (currentPage - 1) * PAGE_SIZE;
PageResultVO<RiderWithdrawApplyVO> result = new PageResultVO<>();
result.setList(withdrawMapper.selectRiderList(riderId, normalizeStatus(status), offset, PAGE_SIZE));
result.setPage(currentPage);
result.setPageSize(PAGE_SIZE);
result.setTotal(withdrawMapper.countRiderList(riderId, normalizeStatus(status)));
return result;
}
@Override
public PageResultVO<RiderWithdrawApplyVO> adminList(Long cityId, Integer status, String keyword, String startDate, String endDate, int page) {
int currentPage = normalizePage(page);
int offset = (currentPage - 1) * PAGE_SIZE;
Long startTime = parseStartTime(startDate);
Long endTime = parseEndTime(endDate);
PageResultVO<RiderWithdrawApplyVO> result = new PageResultVO<>();
result.setList(withdrawMapper.selectAdminList(cityId, normalizeStatus(status), normalizeKeyword(keyword), startTime, endTime, offset, PAGE_SIZE));
result.setPage(currentPage);
result.setPageSize(PAGE_SIZE);
result.setTotal(withdrawMapper.countAdminList(cityId, normalizeStatus(status), normalizeKeyword(keyword), startTime, endTime));
return result;
}
@Override
public RiderWithdrawApplyVO adminDetail(Long id, Long cityId) {
if (id == null || id < 1) {
throw new BizException("提现申请ID不能为空");
}
RiderWithdrawApplyVO detail = withdrawMapper.selectAdminDetail(id, cityId);
if (detail == null) {
throw new BizException("提现申请不存在");
}
return detail;
}
@Override
@Transactional
public void approve(Long id, WithdrawAuditDTO dto, Long cityId, Long adminId, String adminRole) {
RiderWithdrawApply apply = lockApply(id, cityId);
if (apply.getStatus() == null || apply.getStatus() != STATUS_PENDING) {
throw new BizException("只有待审核申请可以审核通过");
}
Rider rider = lockRider(apply.getRiderId());
ensureFrozenEnough(rider, apply.getAmount());
long now = now();
apply.setStatus(STATUS_APPROVED);
apply.setAuditRemark(trim(dto == null ? null : dto.getRemark()));
apply.setAuditorId(adminId == null ? 0L : adminId);
apply.setAuditorName(buildAdminName(adminId, adminRole));
apply.setAuditTime(now);
apply.setUpdateTime(now);
withdrawMapper.updateById(apply);
}
@Override
@Transactional
public void reject(Long id, WithdrawAuditDTO dto, Long cityId, Long adminId, String adminRole) {
RiderWithdrawApply apply = lockApply(id, cityId);
if (apply.getStatus() == null || apply.getStatus() != STATUS_PENDING) {
throw new BizException("只有待审核申请可以拒绝");
}
String remark = trim(dto == null ? null : dto.getRemark());
if (remark.isBlank()) {
throw new BizException("拒绝原因不能为空");
}
Rider rider = lockRider(apply.getRiderId());
BigDecimal amount = money(apply.getAmount());
ensureFrozenEnough(rider, amount);
updateRiderMoney(rider.getId(), money(rider.getBalance()).add(amount), money(rider.getFrozenBalance()).subtract(amount));
long now = now();
apply.setStatus(STATUS_REJECTED);
apply.setAuditRemark(remark);
apply.setAuditorId(adminId == null ? 0L : adminId);
apply.setAuditorName(buildAdminName(adminId, adminRole));
apply.setAuditTime(now);
apply.setUpdateTime(now);
withdrawMapper.updateById(apply);
}
@Override
@Transactional
public void markPaid(Long id, WithdrawMarkPaidDTO dto, Long cityId, Long adminId, String adminRole) {
if (dto == null || trim(dto.getTransferNo()).isBlank()) {
throw new BizException("打款流水号不能为空");
}
RiderWithdrawApply apply = lockApply(id, cityId);
if (apply.getStatus() == null || apply.getStatus() != STATUS_APPROVED) {
throw new BizException("只有审核通过待打款申请可以标记已打款");
}
Rider rider = lockRider(apply.getRiderId());
BigDecimal amount = money(apply.getAmount());
ensureFrozenEnough(rider, amount);
BigDecimal nextFrozenBalance = money(rider.getFrozenBalance()).subtract(amount);
BigDecimal currentBalance = money(rider.getBalance());
updateRiderMoney(rider.getId(), currentBalance, nextFrozenBalance);
long now = now();
RiderBalance record = new RiderBalance();
record.setUid(rider.getId());
record.setType(2);
record.setAction("withdraw_paid");
record.setActionId(apply.getId());
record.setOrderNo(apply.getWithdrawNo());
record.setNums(amount.negate());
record.setTotal(currentBalance);
record.setAddTime(now);
balanceMapper.insert(record);
String remark = trim(dto.getRemark());
apply.setStatus(STATUS_PAID);
apply.setAuditRemark(remark.isBlank() ? apply.getAuditRemark() : remark);
apply.setAuditorId(adminId == null ? apply.getAuditorId() : adminId);
apply.setAuditorName(buildAdminName(adminId, adminRole));
apply.setPayTime(now);
apply.setTransferNo(trim(dto.getTransferNo()));
apply.setUpdateTime(now);
withdrawMapper.updateById(apply);
}
private void validateApply(RiderWithdrawApplyDTO dto) {
if (dto == null) {
throw new BizException("提现申请不能为空");
}
if (dto.getAccountType() == null || dto.getAccountType() < 1 || dto.getAccountType() > 3) {
throw new BizException("收款账户类型不正确");
}
if (dto.getAmount() == null || dto.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
throw new BizException("提现金额必须大于0");
}
if (trim(dto.getAccountName()).isBlank()) {
throw new BizException("收款人不能为空");
}
if (trim(dto.getAccountNo()).isBlank()) {
throw new BizException("收款账号不能为空");
}
if (dto.getAccountType() == 1 && trim(dto.getBankName()).isBlank()) {
throw new BizException("银行卡提现需填写开户行");
}
}
private RiderWithdrawApply lockApply(Long id, Long cityId) {
if (id == null || id < 1) {
throw new BizException("提现申请ID不能为空");
}
RiderWithdrawApply apply = withdrawMapper.selectApplyForUpdate(id);
if (apply == null) {
throw new BizException("提现申请不存在");
}
if (cityId != null && !cityId.equals(apply.getCityId())) {
throw new BizException("只能操作当前租户提现申请");
}
return apply;
}
private Rider lockRider(Long riderId) {
Rider rider = withdrawMapper.selectRiderForUpdate(riderId);
if (rider == null) {
throw new BizException("骑手信息不存在");
}
return rider;
}
private void ensureFrozenEnough(Rider rider, BigDecimal amount) {
if (money(rider.getFrozenBalance()).compareTo(money(amount)) < 0) {
throw new BizException("冻结余额不足,请核对提现申请状态");
}
}
private void updateRiderMoney(Long riderId, BigDecimal balance, BigDecimal frozenBalance) {
riderMapper.update(null, new LambdaUpdateWrapper<Rider>()
.eq(Rider::getId, riderId)
.set(Rider::getBalance, money(balance))
.set(Rider::getFrozenBalance, money(frozenBalance)));
}
private BigDecimal money(BigDecimal value) {
return value == null ? BigDecimal.ZERO : value.setScale(2, java.math.RoundingMode.HALF_UP);
}
private long now() {
return System.currentTimeMillis() / 1000;
}
private String trim(String value) {
return value == null ? "" : value.trim();
}
private String buildWithdrawNo(Long riderId) {
return "WD" + System.currentTimeMillis() + String.format("%04d", riderId % 10000);
}
private String buildAdminName(Long adminId, String adminRole) {
if (adminId == null) {
return "系统";
}
return ("substation".equals(adminRole) ? "分站管理员" : "平台管理员") + "#" + adminId;
}
private int normalizePage(int page) {
return Math.max(page, 1);
}
private Integer normalizeStatus(Integer status) {
return status == null || status < 0 ? null : status;
}
private String normalizeKeyword(String keyword) {
return keyword == null || keyword.isBlank() ? null : keyword.trim();
}
private Long parseStartTime(String startDate) {
if (startDate == null || startDate.isBlank()) {
return null;
}
return parseDate(startDate, "开始日期格式不正确").atStartOfDay(ZONE_ID).toEpochSecond();
}
private Long parseEndTime(String endDate) {
if (endDate == null || endDate.isBlank()) {
return null;
}
return parseDate(endDate, "结束日期格式不正确").plusDays(1).atStartOfDay(ZONE_ID).toEpochSecond();
}
private LocalDate parseDate(String value, String errorMessage) {
try {
return LocalDate.parse(value);
} catch (DateTimeParseException ex) {
throw new BizException(errorMessage);
}
}
}