debug,退款、设备管理员开锁

This commit is contained in:
磷叶 2025-05-01 14:58:36 +08:00
parent 09f3d57884
commit 023215ff3f
2 changed files with 64 additions and 56 deletions

View File

@ -44,7 +44,8 @@ public enum DeviceStatus {
// 允许管理员开锁的设备状态 // 允许管理员开锁的设备状态
public static List<String> canAdminUnlock() { public static List<String> canAdminUnlock() {
return CollectionUtils.map(DeviceStatus::getCode, DISPATCHING, STORAGE, AVAILABLE, TEMP_LOCKED, Q_LOCKED); return CollectionUtils.map(DeviceStatus::getCode, DISPATCHING, STORAGE, AVAILABLE, TEMP_LOCKED, Q_LOCKED,
IN_USE);
} }
// 允许用户开锁的设备状态 // 允许用户开锁的设备状态

View File

@ -84,8 +84,7 @@ import com.ruoyi.system.user.service.UserService;
* @date 2025-03-24 * @date 2025-03-24
*/ */
@Service @Service
public class OrderServiceImpl implements OrderService public class OrderServiceImpl implements OrderService {
{
@Autowired @Autowired
private OrderMapper orderMapper; private OrderMapper orderMapper;
@ -153,8 +152,7 @@ public class OrderServiceImpl implements OrderService
* @return 订单 * @return 订单
*/ */
@Override @Override
public OrderVO selectOrderById(Long id, boolean scope) public OrderVO selectOrderById(Long id, boolean scope) {
{
if (id == null) { if (id == null) {
return null; return null;
} }
@ -179,8 +177,7 @@ public class OrderServiceImpl implements OrderService
* @return 订单 * @return 订单
*/ */
@Override @Override
public List<OrderVO> selectOrderList(OrderQuery order) public List<OrderVO> selectOrderList(OrderQuery order) {
{
return orderMapper.selectOrderList(order); return orderMapper.selectOrderList(order);
} }
@ -191,8 +188,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果 * @return 结果
*/ */
@Override @Override
public int insertOrder(Order order) public int insertOrder(Order order) {
{
order.setCreateTime(DateUtils.getNowDate()); order.setCreateTime(DateUtils.getNowDate());
order.setNo(String.valueOf(SnowFlakeUtil.newId())); order.setNo(String.valueOf(SnowFlakeUtil.newId()));
return orderMapper.insertOrder(order); return orderMapper.insertOrder(order);
@ -205,8 +201,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果 * @return 结果
*/ */
@Override @Override
public int updateOrder(Order order) public int updateOrder(Order order) {
{
return orderMapper.updateOrder(order); return orderMapper.updateOrder(order);
} }
@ -217,8 +212,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果 * @return 结果
*/ */
@Override @Override
public int deleteOrderByIds(Long[] ids) public int deleteOrderByIds(Long[] ids) {
{
return orderMapper.deleteOrderByIds(ids); return orderMapper.deleteOrderByIds(ids);
} }
@ -229,8 +223,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果 * @return 结果
*/ */
@Override @Override
public int deleteOrderById(Long id) public int deleteOrderById(Long id) {
{
return orderMapper.deleteOrderById(id); return orderMapper.deleteOrderById(id);
} }
@ -319,7 +312,6 @@ public class OrderServiceImpl implements OrderService
}, delay, TimeUnit.SECONDS); }, delay, TimeUnit.SECONDS);
} }
// 取消订单 // 取消订单
@Override @Override
public int cancelOrder(Long id, String remark) { public int cancelOrder(Long id, String remark) {
@ -341,8 +333,7 @@ public class OrderServiceImpl implements OrderService
ServiceUtil.assertion(order == null, "ID为%s的订单不存在", id); ServiceUtil.assertion(order == null, "ID为%s的订单不存在", id);
ServiceUtil.assertion(!OrderStatus.canCancelPay().contains(order.getStatus()), "ID为%s的订单当前状态不允许取消", id); ServiceUtil.assertion(!OrderStatus.canCancelPay().contains(order.getStatus()), "ID为%s的订单当前状态不允许取消", id);
Long deviceId = order.getDeviceId(); // 设备ID
Long deviceId = order.getDeviceId(); // 设备ID
Long orderDeviceId = order.getOrderDeviceId(); // 订单设备ID Long orderDeviceId = order.getOrderDeviceId(); // 订单设备ID
// 取消订单 // 取消订单
@ -411,7 +402,7 @@ public class OrderServiceImpl implements OrderService
OrderDeviceVO orderDevice = bo.getOrderDevice(); OrderDeviceVO orderDevice = bo.getOrderDevice();
// 设置订单数据 // 设置订单数据
order.setEndTime(LocalDateTime.now()); // 结束时间 order.setEndTime(LocalDateTime.now()); // 结束时间
order.setDuration(Duration.between(order.getStartTime(), order.getEndTime()).getSeconds()); order.setDuration(Duration.between(order.getStartTime(), order.getEndTime()).getSeconds());
order.setDistance(LocationLogUtil.calcDistance(bo.getPositionList())); order.setDistance(LocationLogUtil.calcDistance(bo.getPositionList()));
order.setEndReason(dto.getEndReason()); order.setEndReason(dto.getEndReason());
@ -422,7 +413,7 @@ public class OrderServiceImpl implements OrderService
// 订单费用 // 订单费用
this.setOrderFee(order, area, inParkingVO); this.setOrderFee(order, area, inParkingVO);
transactionTemplate.execute(status -> { Integer result = transactionTemplate.execute(status -> {
// 更新订单数据 // 更新订单数据
Order data = orderConverter.toPOByEnd(order); Order data = orderConverter.toPOByEnd(order);
OrderQuery query = new OrderQuery(); OrderQuery query = new OrderQuery();
@ -434,9 +425,6 @@ public class OrderServiceImpl implements OrderService
// 当订单状态为已完成时处理订单完成操作 // 当订单状态为已完成时处理订单完成操作
if (OrderStatus.FINISHED.getCode().equals(order.getStatus())) { if (OrderStatus.FINISHED.getCode().equals(order.getStatus())) {
int refund = this.refundRemainAmount(order.getId());
ServiceUtil.assertion(refund != 1, "ID为%s的订单退还剩余金额失败", order.getId());
// 预分成 // 预分成
boolean bonus = bonusService.prepayByBst(BonusBstType.ORDER, order.getId()); boolean bonus = bonusService.prepayByBst(BonusBstType.ORDER, order.getId());
ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId()); ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId());
@ -450,20 +438,30 @@ public class OrderServiceImpl implements OrderService
ServiceUtil.assertion(finish != 1, "结束ID为%s的订单设备失败", orderDevice.getId()); ServiceUtil.assertion(finish != 1, "结束ID为%s的订单设备失败", orderDevice.getId());
// 设备上锁必须放最后因为会操作设备 // 设备上锁必须放最后因为会操作设备
DeviceIotVO lock = deviceIotService.lock(orderDevice.getDeviceId(), isAdmin, "订单结束上锁:" + orderDevice.getOrderNo(), false); DeviceIotVO lock = deviceIotService.lock(orderDevice.getDeviceId(), isAdmin,
"订单结束上锁:" + orderDevice.getOrderNo(), false);
ServiceUtil.assertion(lock.getDb() != 1, "ID为%s的设备上锁失败", orderDevice.getDeviceId()); ServiceUtil.assertion(lock.getDb() != 1, "ID为%s的设备上锁失败", orderDevice.getDeviceId());
vo.setIot(lock.getIot()); vo.setIot(lock.getIot());
return rows; return rows;
}); });
boolean isSuccess = result != null && result == 1;
if (isSuccess && OrderStatus.FINISHED.getCode().equals(order.getStatus())) {
scheduledExecutorService.schedule(() -> {
int refund = this.refundRemainAmount(order);
ServiceUtil.assertion(refund != 1, "ID为%s的订单退还剩余金额失败", order.getId());
}, 10, TimeUnit.SECONDS);
}
return vo; return vo;
} }
// 设置订单状态 // 设置订单状态
private void setOrderStatus(OrderVO order, String returnType) { private void setOrderStatus(OrderVO order, String returnType) {
// 根据是否需要审核设置订单状态 // 根据是否需要审核设置订单状态
if (order.getAreaReturnVerify() != null && order.getAreaReturnVerify() && OrderReturnType.needVerify().contains(returnType)) { if (order.getAreaReturnVerify() != null && order.getAreaReturnVerify()
&& OrderReturnType.needVerify().contains(returnType)) {
// 更新订单状态为待审核 // 更新订单状态为待审核
order.setStatus(OrderStatus.WAIT_VERIFY.getCode()); order.setStatus(OrderStatus.WAIT_VERIFY.getCode());
} else { } else {
@ -481,16 +479,12 @@ public class OrderServiceImpl implements OrderService
order.setTotalFee(orderFee.getTotalFee()); order.setTotalFee(orderFee.getTotalFee());
} }
// 退还剩余金额 private int refundRemainAmount(OrderVO order) {
private int refundRemainAmount(Long orderId) {
// 查询订单
OrderVO order = this.selectOrderById(orderId);
if (order == null) { if (order == null) {
return 0; return 0;
} }
// 订单剩余金额退款 // 订单剩余金额退款
BigDecimal refund = MathUtils.subtractDecimal(order.getPayAmount(), order.getTotalFee()); BigDecimal refund = MathUtils.subtractDecimal(order.getPayedAmount(), order.getTotalFee());
if (refund != null && refund.compareTo(BigDecimal.ZERO) > 0) { if (refund != null && refund.compareTo(BigDecimal.ZERO) > 0) {
return this.refund(order, refund, null, null, "系统", RefundType.AUTO.getCode()); return this.refund(order, refund, null, null, "系统", RefundType.AUTO.getCode());
} }
@ -500,10 +494,11 @@ public class OrderServiceImpl implements OrderService
/** /**
* 退款 * 退款
* @param order 订单 *
* @param amount 退款金额 * @param order 订单
* @param reason 退款原因 * @param amount 退款金额
* @param userId 操作人ID * @param reason 退款原因
* @param userId 操作人ID
* @param userName 操作人名称 * @param userName 操作人名称
* @return 结果 * @return 结果
*/ */
@ -513,9 +508,9 @@ public class OrderServiceImpl implements OrderService
ServiceUtil.assertion(amount == null || amount.compareTo(BigDecimal.ZERO) <= 0, "参数错误退款金额不允许为空且必须大于0"); ServiceUtil.assertion(amount == null || amount.compareTo(BigDecimal.ZERO) <= 0, "参数错误退款金额不允许为空且必须大于0");
BigDecimal canRefundAmount = OrderUtil.calcCanRefundAmount(order); BigDecimal canRefundAmount = OrderUtil.calcCanRefundAmount(order);
ServiceUtil.assertion(canRefundAmount.compareTo(amount) < 0, "ID为%s的订单可退款金额不足当前可退款金额为%s元", order.getId(), canRefundAmount); ServiceUtil.assertion(canRefundAmount.compareTo(amount) < 0, "ID为%s的订单可退款金额不足当前可退款金额为%s元", order.getId(),
canRefundAmount);
// 退款原因 // 退款原因
String refundReason = null; String refundReason = null;
if (RefundType.ADMIN.getCode().equals(type)) { if (RefundType.ADMIN.getCode().equals(type)) {
@ -527,11 +522,12 @@ public class OrderServiceImpl implements OrderService
refundReason += "" + reason; refundReason += "" + reason;
} }
String finalRefundReason = refundReason; String finalRefundReason = refundReason;
Integer result = transactionTemplate.execute(status -> { Integer result = transactionTemplate.execute(status -> {
// 分成退款 // 分成退款
boolean bonusRefund = bonusService.refundByBst(BonusBstType.ORDER, order.getId(), amount, order.getPayAmount(), finalRefundReason); boolean bonusRefund = bonusService.refundByBst(BonusBstType.ORDER, order.getId(), amount,
order.getPayAmount(), finalRefundReason);
ServiceUtil.assertion(!bonusRefund, "ID为%s的订单分成退款失败", order.getId()); ServiceUtil.assertion(!bonusRefund, "ID为%s的订单分成退款失败", order.getId());
// 支付退款 // 支付退款
@ -582,7 +578,8 @@ public class OrderServiceImpl implements OrderService
List<AreaSubVO> noParkingList = areaSubService.selectNoParkingAreaByAreaId(area.getId()); List<AreaSubVO> noParkingList = areaSubService.selectNoParkingAreaByAreaId(area.getId());
// 获取是否在停车点内 // 获取是否在停车点内
OrderInParkingVO inParkingVO = OrderUtil.getInParkingVO(area, parkingList, noParkingList, device, dto.getLon(), dto.getLat()); OrderInParkingVO inParkingVO = OrderUtil.getInParkingVO(area, parkingList, noParkingList, device, dto.getLon(),
dto.getLat());
// 还车校验 // 还车校验
if (dto.getCheckLocation() != null && dto.getCheckLocation()) { if (dto.getCheckLocation() != null && dto.getCheckLocation()) {
@ -603,13 +600,16 @@ public class OrderServiceImpl implements OrderService
@Override @Override
public DeviceIotVO openDevice(OrderVO order, boolean requiredIot) { public DeviceIotVO openDevice(OrderVO order, boolean requiredIot) {
ServiceUtil.assertion(order == null, "参数错误order不允许为空"); ServiceUtil.assertion(order == null, "参数错误order不允许为空");
ServiceUtil.assertion(!OrderStatus.inUse().contains(order.getStatus()), "ID为%s的订单当前状态并非使用中无法操作设备", order.getId()); ServiceUtil.assertion(!OrderStatus.inUse().contains(order.getStatus()), "ID为%s的订单当前状态并非使用中无法操作设备",
order.getId());
OrderDeviceVO orderDevice = orderDeviceService.selectOrderDeviceById(order.getOrderDeviceId()); OrderDeviceVO orderDevice = orderDeviceService.selectOrderDeviceById(order.getOrderDeviceId());
ServiceUtil.assertion(orderDevice == null, "ID为%s的订单设备不存在", order.getId()); ServiceUtil.assertion(orderDevice == null, "ID为%s的订单设备不存在", order.getId());
ServiceUtil.assertion(!OrderDeviceStatus.inUse().contains(orderDevice.getStatus()), "ID为%s的订单设备当前状态并非使用中无法操作设备", order.getId()); ServiceUtil.assertion(!OrderDeviceStatus.inUse().contains(orderDevice.getStatus()),
"ID为%s的订单设备当前状态并非使用中无法操作设备", order.getId());
return deviceIotService.unlock(orderDevice.getDeviceId(), DeviceUnLockType.USER, "订单开锁:" + order.getNo(), requiredIot); return deviceIotService.unlock(orderDevice.getDeviceId(), DeviceUnLockType.USER, "订单开锁:" + order.getNo(),
requiredIot);
} }
@Override @Override
@ -621,7 +621,8 @@ public class OrderServiceImpl implements OrderService
OrderDeviceVO orderDevice = orderDeviceService.selectOrderDeviceById(order.getOrderDeviceId()); OrderDeviceVO orderDevice = orderDeviceService.selectOrderDeviceById(order.getOrderDeviceId());
ServiceUtil.assertion(orderDevice == null, "ID为%s的订单设备不存在", order.getId()); ServiceUtil.assertion(orderDevice == null, "ID为%s的订单设备不存在", order.getId());
ServiceUtil.assertion(!OrderDeviceStatus.inUse().contains(orderDevice.getStatus()), "ID为%s的订单设备当前状态并非使用中无法操作设备", order.getId()); ServiceUtil.assertion(!OrderDeviceStatus.inUse().contains(orderDevice.getStatus()),
"ID为%s的订单设备当前状态并非使用中无法操作设备", order.getId());
// 查询设备 // 查询设备
DeviceVO device = deviceService.selectDeviceById(orderDevice.getDeviceId()); DeviceVO device = deviceService.selectDeviceById(orderDevice.getDeviceId());
@ -631,7 +632,8 @@ public class OrderServiceImpl implements OrderService
// 判断是否在禁停区内 // 判断是否在禁停区内
List<AreaSubVO> noParkingList = areaSubService.selectNoParkingAreaByAreaId(order.getAreaId()); List<AreaSubVO> noParkingList = areaSubService.selectNoParkingAreaByAreaId(order.getAreaId());
if (CollectionUtils.isNotEmptyElement(noParkingList)) { if (CollectionUtils.isNotEmptyElement(noParkingList)) {
OrderInParkingVO inParkingVO = OrderUtil.getInParkingVO(null, null, noParkingList, device, dto.getLon(), dto.getLat()); OrderInParkingVO inParkingVO = OrderUtil.getInParkingVO(null, null, noParkingList, device, dto.getLon(),
dto.getLat());
ServiceUtil.assertion(inParkingVO == null, "获取设备位置失败"); ServiceUtil.assertion(inParkingVO == null, "获取设备位置失败");
ServiceUtil.assertion(inParkingVO.getInNoParking() != null && inParkingVO.getInNoParking(), "禁止在禁停区内停车"); ServiceUtil.assertion(inParkingVO.getInNoParking() != null && inParkingVO.getInNoParking(), "禁止在禁停区内停车");
} }
@ -671,7 +673,7 @@ public class OrderServiceImpl implements OrderService
Fault fault = faultConverter.toPo(dto, oldOrderDevice); Fault fault = faultConverter.toPo(dto, oldOrderDevice);
int insertFault = faultService.insertFault(fault); int insertFault = faultService.insertFault(fault);
ServiceUtil.assertion(insertFault != 1, "创建故障信息失败"); ServiceUtil.assertion(insertFault != 1, "创建故障信息失败");
newOrderDevice.setFaultId(fault.getId()); // 设置故障ID newOrderDevice.setFaultId(fault.getId()); // 设置故障ID
} }
// 创建新订单设备 // 创建新订单设备
@ -684,10 +686,12 @@ public class OrderServiceImpl implements OrderService
// 设备操作必须放最后因为会操作设备 // 设备操作必须放最后因为会操作设备
// 旧设备上锁 // 旧设备上锁
DeviceIotVO lock = deviceIotService.lock(oldOrderDevice.getDeviceId(), false, "订单换车上锁:" + order.getNo(), false); DeviceIotVO lock = deviceIotService.lock(oldOrderDevice.getDeviceId(), false, "订单换车上锁:" + order.getNo(),
false);
ServiceUtil.assertion(lock.getDb() != 1, "ID为%s的旧设备上锁失败", oldOrderDevice.getDeviceId()); ServiceUtil.assertion(lock.getDb() != 1, "ID为%s的旧设备上锁失败", oldOrderDevice.getDeviceId());
// 新设备解锁 // 新设备解锁
DeviceIotVO unlock = deviceIotService.unlock(newOrderDevice.getDeviceId(), DeviceUnLockType.USER, "订单换车开锁:" + order.getNo(), false); DeviceIotVO unlock = deviceIotService.unlock(newOrderDevice.getDeviceId(), DeviceUnLockType.USER,
"订单换车开锁:" + order.getNo(), false);
ServiceUtil.assertion(unlock.getDb() != 1, "ID为%s的新设备解锁失败", newOrderDevice.getDeviceId()); ServiceUtil.assertion(unlock.getDb() != 1, "ID为%s的新设备解锁失败", newOrderDevice.getDeviceId());
return start; return start;
@ -719,17 +723,12 @@ public class OrderServiceImpl implements OrderService
// 更新车损费 // 更新车损费
if (dto.getDeductionFee() != null && dto.getDeductionFee().compareTo(BigDecimal.ZERO) > 0) { if (dto.getDeductionFee() != null && dto.getDeductionFee().compareTo(BigDecimal.ZERO) > 0) {
BigDecimal max = MathUtils.subtractDecimal(order.getDepositFee(), order.getTotalFee()); BigDecimal max = MathUtils.subtractDecimal(order.getDepositFee(), order.getTotalFee());
ServiceUtil.assertion(MathUtils.biggerThan(dto.getDeductionFee(), max), "ID为%s的订单车损费不允许超过%s", dto.getId(), max); ServiceUtil.assertion(MathUtils.biggerThan(dto.getDeductionFee(), max), "ID为%s的订单车损费不允许超过%s",
dto.getId(), max);
int add = orderMapper.addDeductionFee(order.getId(), dto.getDeductionFee()); int add = orderMapper.addDeductionFee(order.getId(), dto.getDeductionFee());
ServiceUtil.assertion(add != 1, "ID为%s的订单增加车损费失败请刷新后重试", order.getId()); ServiceUtil.assertion(add != 1, "ID为%s的订单增加车损费失败请刷新后重试", order.getId());
} }
// 若审核通过则退还剩余金额
if (pass && rows > 0) {
int refund = this.refundRemainAmount(order.getId());
ServiceUtil.assertion(refund != 1, "ID为%s的订单退还剩余金额失败", order.getId());
}
// 预分成 // 预分成
boolean bonus = bonusService.prepayByBst(BonusBstType.ORDER, order.getId()); boolean bonus = bonusService.prepayByBst(BonusBstType.ORDER, order.getId());
ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId()); ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId());
@ -737,6 +736,14 @@ public class OrderServiceImpl implements OrderService
return rows; return rows;
}); });
// 若审核通过则退还剩余金额
if (pass && result != null && result > 0) {
scheduledExecutorService.schedule(() -> {
int refund = this.refundRemainAmount(order);
ServiceUtil.assertion(refund != 1, "ID为%s的订单退还剩余金额失败", order.getId());
}, 10, TimeUnit.SECONDS);
}
return result == null ? 0 : result; return result == null ? 0 : result;
} }