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() {
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
*/
@Service
public class OrderServiceImpl implements OrderService
{
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@ -153,8 +152,7 @@ public class OrderServiceImpl implements OrderService
* @return 订单
*/
@Override
public OrderVO selectOrderById(Long id, boolean scope)
{
public OrderVO selectOrderById(Long id, boolean scope) {
if (id == null) {
return null;
}
@ -179,8 +177,7 @@ public class OrderServiceImpl implements OrderService
* @return 订单
*/
@Override
public List<OrderVO> selectOrderList(OrderQuery order)
{
public List<OrderVO> selectOrderList(OrderQuery order) {
return orderMapper.selectOrderList(order);
}
@ -191,8 +188,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果
*/
@Override
public int insertOrder(Order order)
{
public int insertOrder(Order order) {
order.setCreateTime(DateUtils.getNowDate());
order.setNo(String.valueOf(SnowFlakeUtil.newId()));
return orderMapper.insertOrder(order);
@ -205,8 +201,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果
*/
@Override
public int updateOrder(Order order)
{
public int updateOrder(Order order) {
return orderMapper.updateOrder(order);
}
@ -217,8 +212,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果
*/
@Override
public int deleteOrderByIds(Long[] ids)
{
public int deleteOrderByIds(Long[] ids) {
return orderMapper.deleteOrderByIds(ids);
}
@ -229,8 +223,7 @@ public class OrderServiceImpl implements OrderService
* @return 结果
*/
@Override
public int deleteOrderById(Long id)
{
public int deleteOrderById(Long id) {
return orderMapper.deleteOrderById(id);
}
@ -319,7 +312,6 @@ public class OrderServiceImpl implements OrderService
}, delay, TimeUnit.SECONDS);
}
// 取消订单
@Override
public int cancelOrder(Long id, String remark) {
@ -341,7 +333,6 @@ public class OrderServiceImpl implements OrderService
ServiceUtil.assertion(order == null, "ID为%s的订单不存在", id);
ServiceUtil.assertion(!OrderStatus.canCancelPay().contains(order.getStatus()), "ID为%s的订单当前状态不允许取消", id);
Long deviceId = order.getDeviceId(); // 设备ID
Long orderDeviceId = order.getOrderDeviceId(); // 订单设备ID
@ -422,7 +413,7 @@ public class OrderServiceImpl implements OrderService
// 订单费用
this.setOrderFee(order, area, inParkingVO);
transactionTemplate.execute(status -> {
Integer result = transactionTemplate.execute(status -> {
// 更新订单数据
Order data = orderConverter.toPOByEnd(order);
OrderQuery query = new OrderQuery();
@ -434,9 +425,6 @@ public class OrderServiceImpl implements OrderService
// 当订单状态为已完成时处理订单完成操作
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());
ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId());
@ -450,20 +438,30 @@ public class OrderServiceImpl implements OrderService
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());
vo.setIot(lock.getIot());
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;
}
// 设置订单状态
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());
} else {
@ -481,16 +479,12 @@ public class OrderServiceImpl implements OrderService
order.setTotalFee(orderFee.getTotalFee());
}
// 退还剩余金额
private int refundRemainAmount(Long orderId) {
// 查询订单
OrderVO order = this.selectOrderById(orderId);
private int refundRemainAmount(OrderVO order) {
if (order == null) {
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) {
return this.refund(order, refund, null, null, "系统", RefundType.AUTO.getCode());
}
@ -500,6 +494,7 @@ public class OrderServiceImpl implements OrderService
/**
* 退款
*
* @param order 订单
* @param amount 退款金额
* @param reason 退款原因
@ -513,8 +508,8 @@ public class OrderServiceImpl implements OrderService
ServiceUtil.assertion(amount == null || amount.compareTo(BigDecimal.ZERO) <= 0, "参数错误退款金额不允许为空且必须大于0");
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;
@ -531,7 +526,8 @@ public class OrderServiceImpl implements OrderService
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());
// 支付退款
@ -582,7 +578,8 @@ public class OrderServiceImpl implements OrderService
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()) {
@ -603,13 +600,16 @@ public class OrderServiceImpl implements OrderService
@Override
public DeviceIotVO openDevice(OrderVO order, boolean requiredIot) {
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());
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
@ -621,7 +621,8 @@ public class OrderServiceImpl implements OrderService
OrderDeviceVO orderDevice = orderDeviceService.selectOrderDeviceById(order.getOrderDeviceId());
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());
@ -631,7 +632,8 @@ public class OrderServiceImpl implements OrderService
// 判断是否在禁停区内
List<AreaSubVO> noParkingList = areaSubService.selectNoParkingAreaByAreaId(order.getAreaId());
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.getInNoParking() != null && inParkingVO.getInNoParking(), "禁止在禁停区内停车");
}
@ -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());
// 新设备解锁
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());
return start;
@ -719,17 +723,12 @@ public class OrderServiceImpl implements OrderService
// 更新车损费
if (dto.getDeductionFee() != null && dto.getDeductionFee().compareTo(BigDecimal.ZERO) > 0) {
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());
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());
ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", order.getId());
@ -737,6 +736,14 @@ public class OrderServiceImpl implements OrderService
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;
}