usdt_orders 收款订单禁止修改,只允许删除过期的订单
This commit is contained in:
1 parent
cc5fe279e9
commit
f6babe9358
3 files changed
+92
-16
No files matched your search
+69
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
-14
@@ -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<UsdtOrdersMapper, UsdtOrd
|
||||
@Override
|
||||
public int updateUsdtOrders(UsdtOrders usdtOrders,boolean isUser)
|
||||
{
|
||||
usdtOrders.setUpdatedAt(DateUtils.getNowDate());
|
||||
if(isUser){
|
||||
checkUser(usdtOrders);
|
||||
}
|
||||
return usdtOrdersMapper.updateUsdtOrders(usdtOrders);
|
||||
// 收款订单由支付工作流维护,不允许通过管理端修改金额、状态或归属。
|
||||
throw new CustomException("收款订单禁止修改");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,13 +256,24 @@ public class UsdtOrdersServiceImpl extends ServiceImpl<UsdtOrdersMapper, UsdtOrd
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deleteUsdtOrdersByIds(Long[] ids,boolean isUser)
|
||||
{
|
||||
if(isUser){
|
||||
List<UsdtOrdersPlus> 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<UsdtOrdersMapper, UsdtOrd
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deleteUsdtOrdersById(Long id,boolean isUser)
|
||||
{
|
||||
if(isUser){
|
||||
UsdtOrdersPlus vo = getById(id,UsdtOrdersPlus::getUserId,UsdtOrdersPlus::getId);
|
||||
checkUser(vo);
|
||||
}
|
||||
return usdtOrdersMapper.deleteUsdtOrdersById(id);
|
||||
return deleteUsdtOrdersByIds(new Long[] {id}, isUser);
|
||||
}
|
||||
}
|
||||
@@ -208,11 +208,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
</update>
|
||||
|
||||
<delete id="deleteUsdtOrdersById" parameterType="Long">
|
||||
delete from orders where id = #{id}
|
||||
delete from orders where id = #{id} and status = 3
|
||||
</delete>
|
||||
|
||||
<delete id="deleteUsdtOrdersByIds" parameterType="String">
|
||||
delete from orders where id in
|
||||
delete from orders where status = 3 and id in
|
||||
<foreach item="id" collection="array" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
|
||||
Reference in new issue
Block a user