diff --git a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/UsdtOrdersRestrictionTest.java b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/UsdtOrdersRestrictionTest.java new file mode 100644 index 0000000..3f3cf35 --- /dev/null +++ b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/UsdtOrdersRestrictionTest.java @@ -0,0 +1,69 @@ +package com.ruoyi.web.controller.system; + +import com.ruoyi.common.exception.CustomException; +import com.ruoyi.system.domain.UsdtOrders; +import com.ruoyi.system.domain.UsdtOrdersPlus; +import com.ruoyi.system.mapper.UsdtOrdersMapper; +import com.ruoyi.system.service.impl.UsdtOrdersServiceImpl; +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.util.ReflectionTestUtils; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.*; + +/** 不访问数据库,验证人工修改禁用及整批删除状态检查。 */ +public class UsdtOrdersRestrictionTest { + private UsdtOrdersMapper mapper; + private UsdtOrdersServiceImpl service; + + @Before + public void setup() { + mapper = mock(UsdtOrdersMapper.class); + service = new UsdtOrdersServiceImpl(); + ReflectionTestUtils.setField(service, "usdtOrdersMapper", mapper); + } + + @Test + public void rejectsManualUpdatesForBothScopes() { + rejects(() -> service.updateUsdtOrders(new UsdtOrders(), false)); + rejects(() -> service.updateUsdtOrders(new UsdtOrders(), true)); + verify(mapper, never()).updateUsdtOrders(any()); + } + + @Test + public void rejectsWholeBatchContainingPendingOrPaidOrder() { + when(mapper.selectByIdForUpdate(1L)).thenReturn(order(3L)); + for (long status : new long[] {1L, 2L}) { + when(mapper.selectByIdForUpdate(2L)).thenReturn(order(status)); + rejects(() -> service.deleteUsdtOrdersByIds(new Long[] {1L, 2L}, false)); + } + verify(mapper, never()).deleteUsdtOrdersByIds(any()); + } + + @Test + public void acceptsOnlyExistingExpiredOrdersAndDeduplicatesIds() { + when(mapper.selectByIdForUpdate(1L)).thenReturn(order(3L)); + when(mapper.deleteUsdtOrdersByIds(any())).thenReturn(1); + assertEquals(1, service.deleteUsdtOrdersByIds(new Long[] {1L, 1L}, false)); + verify(mapper).deleteUsdtOrdersByIds(new Long[] {1L}); + rejects(() -> service.deleteUsdtOrdersByIds(new Long[] {2L}, false)); + rejects(() -> service.deleteUsdtOrdersByIds(new Long[0], false)); + rejects(() -> service.deleteUsdtOrdersById(null, false)); + } + + private UsdtOrdersPlus order(Long status) { + UsdtOrdersPlus order = new UsdtOrdersPlus(); + order.setStatus(status); + return order; + } + + private void rejects(Runnable action) { + try { + action.run(); + fail("应拒绝此订单操作"); + } catch (CustomException expected) { + assertNotNull(expected.getMessage()); + } + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UsdtOrdersServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UsdtOrdersServiceImpl.java index 996bea4..7f0fdd8 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UsdtOrdersServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UsdtOrdersServiceImpl.java @@ -15,6 +15,8 @@ import com.ruoyi.system.service.IUsdtOrdersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import com.ruoyi.common.exception.CustomException; import java.util.*; /** @@ -243,11 +245,8 @@ public class UsdtOrdersServiceImpl extends ServiceImpl list= getByIds(Arrays.asList(ids),false,UsdtOrdersPlus::getUserId,UsdtOrdersPlus::getId); - checkUserPlus(list); + if (ids == null || ids.length == 0 || Arrays.asList(ids).contains(null)) { + throw new CustomException("请选择需要删除的已过期订单"); } - return usdtOrdersMapper.deleteUsdtOrdersByIds(ids); + // 固定加锁顺序;整批校验通过才删除,避免混入待支付/已支付订单导致部分删除。 + Long[] uniqueIds = new TreeSet<>(Arrays.asList(ids)).toArray(new Long[0]); + for (Long id : uniqueIds) { + UsdtOrdersPlus order = usdtOrdersMapper.selectByIdForUpdate(id); + if (order == null || (isUser && !Objects.equals(order.getUserId(), SecurityUtils.getUserId()))) { + throw new CustomException("订单不存在或无权删除"); + } + if (!Long.valueOf(3L).equals(order.getStatus())) { + throw new CustomException("仅允许删除已过期的收款订单"); + } + } + return usdtOrdersMapper.deleteUsdtOrdersByIds(uniqueIds); } /** @@ -273,12 +283,9 @@ public class UsdtOrdersServiceImpl extends ServiceImpl - delete from orders where id = #{id} + delete from orders where id = #{id} and status = 3 - delete from orders where id in + delete from orders where status = 3 and id in #{id}