From 89684c404c9bd181bb17208c87fb72c277928b27 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 9 Sep 2021 13:46:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E6=94=AF=E6=8C=81=E6=8B=96=E6=8B=BD=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/mapper/ext/ExtTestCaseMapper.xml | 2 +- .../commons/utils/ServiceUtils.java | 111 +++++++++++++++++- .../listener/AppStartListener.java | 29 ++--- .../metersphere/service/ProjectService.java | 39 +++--- .../track/controller/TestCaseController.java | 2 +- .../track/service/TestCaseService.java | 86 +++----------- 6 files changed, 169 insertions(+), 100 deletions(-) diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml index b4485f3ec6..3d3782f54d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml @@ -81,7 +81,7 @@ left join test_case_review_test_case as T2 on test_case.id=T2.case_id and T2.review_id =#{request.reviewId} and T2.case_id is null - ORDER BY test_case.update_time DESC + diff --git a/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java b/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java index 4648affe17..5c46ddbfe1 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java @@ -5,17 +5,24 @@ import io.metersphere.base.domain.User; import io.metersphere.commons.exception.MSException; import io.metersphere.controller.request.BaseQueryRequest; import io.metersphere.controller.request.OrderRequest; +import io.metersphere.controller.request.ResetOrderRequest; import io.metersphere.service.ProjectService; import io.metersphere.service.UserService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; public class ServiceUtils { @@ -24,10 +31,18 @@ public class ServiceUtils { return getDefaultOrder(null, orders); } + public static List getDefaultSortOrder(List orders) { + return getDefaultOrderByField(null, orders, "order"); + } + public static List getDefaultOrder(String prefix, List orders) { + return getDefaultOrderByField(prefix, orders, "update_time"); + } + + private static List getDefaultOrderByField(String prefix, List orders, String field) { if (orders == null || orders.size() < 1) { OrderRequest orderRequest = new OrderRequest(); - orderRequest.setName("update_time"); + orderRequest.setName(field); orderRequest.setType("desc"); if (StringUtils.isNotBlank(prefix)) { orderRequest.setPrefix(prefix); @@ -100,4 +115,98 @@ public class ServiceUtils { }); return nameMap; } + + /** + * 初始化 order 列 + * @param clazz + * @param mapClazz + * @param selectProjectIdsFunc + * @param getIdsOrderByCreateTimeFunc + * @param + * @param + */ + public static void initOrderField(Class clazz, Class mapClazz, + Supplier> selectProjectIdsFunc, + Function> getIdsOrderByCreateTimeFunc) { + + try { + + SqlSessionFactory sqlSessionFactory = CommonBeanFactory.getBean(SqlSessionFactory.class); + Method setId = clazz.getMethod("setId", String.class); + Method setOrder = clazz.getMethod("setOrder", Long.class); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + Object mapper = sqlSession.getMapper(mapClazz); + + List projectIds = selectProjectIdsFunc.get(); + for (String projectId : projectIds) { + Long order = 0L; + List ids = getIdsOrderByCreateTimeFunc.apply(projectId); + for (String id : ids) { + T item = null; + item = (T) clazz.newInstance(); + setId.invoke(item, id); + setOrder.invoke(item, order); + order += 5000; + Method updateByPrimaryKeySelectiveFunc = mapper.getClass().getMethod("updateByPrimaryKeySelective", clazz); + updateByPrimaryKeySelectiveFunc.invoke(mapper, item); + } + sqlSession.flushStatements(); + } + } catch (Throwable e) { + LogUtil.error(e.getMessage(), e); + } + } + + /** + * + * @param request + * @param clazz + * @param selectByPrimaryKeyFunc + * @param getPreOrderFunc + * @param getLastOrderFunc + * @param updateByPrimaryKeySelectiveFuc + * @param + */ + public static void updateOrderField(ResetOrderRequest request, Class clazz, + Function selectByPrimaryKeyFunc, + BiFunction getPreOrderFunc, + BiFunction getLastOrderFunc, + Consumer updateByPrimaryKeySelectiveFuc) { + Long order = null; + Long lastOrPreOrder = null; + try { + Method getOrder = clazz.getMethod("getOrder"); + Method setId = clazz.getMethod("setId", String.class); + Method setOrder = clazz.getMethod("setOrder", Long.class); + + // 获取移动的参考对象 + T target = selectByPrimaryKeyFunc.apply(request.getTargetId()); + Long targetOrder = (Long) getOrder.invoke(target); + + if (request.getMoveMode().equals(ResetOrderRequest.MoveMode.AFTER.name())) { + // 追加到参考对象的之后 + order = targetOrder - 5000; + // ,因为是降序排,则查找比目标 order 小的一个order + lastOrPreOrder = getPreOrderFunc.apply(request.getProjectId(), targetOrder); + } else { + // 追加到前面 + order = targetOrder + 5000; + // 因为是降序排,则查找比目标 order 更大的一个order + lastOrPreOrder = getLastOrderFunc.apply(request.getProjectId(), targetOrder); + } + if (lastOrPreOrder != null) { + // 如果不是第一个或最后一个则取中间值 + order = (targetOrder + lastOrPreOrder) / 2; + } + + // 更新order值 + T updateObj = (T) clazz.newInstance(); + setId.invoke(updateObj, request.getMoveId()); + setOrder.invoke(updateObj, order); + updateByPrimaryKeySelectiveFuc.accept(updateObj); + } catch (Throwable e) { + LogUtil.error(e.getMessage(), e); + MSException.throwException("更新 order 字段失败"); + } + } } diff --git a/backend/src/main/java/io/metersphere/listener/AppStartListener.java b/backend/src/main/java/io/metersphere/listener/AppStartListener.java index 8a923c7cb8..0d468e4a67 100644 --- a/backend/src/main/java/io/metersphere/listener/AppStartListener.java +++ b/backend/src/main/java/io/metersphere/listener/AppStartListener.java @@ -63,22 +63,13 @@ public class AppStartListener implements ApplicationListener projectList = projectMapper.selectByExample(example); + try { + ProjectExample example = new ProjectExample(); + Integer portInteger = new Integer(0); + Boolean statusBoolean = new Boolean(true); + example.createCriteria().andIsMockTcpOpenEqualTo(statusBoolean).andMockTcpPortNotEqualTo(portInteger); + List projectList = projectMapper.selectByExample(example); - List opendPortList = new ArrayList<>(); - for (Project p : projectList) { - boolean isPortInRange = this.isMockTcpPortIsInRange(p.getMockTcpPort()); - if(isPortInRange && !opendPortList.contains(p.getMockTcpPort())){ - opendPortList.add(p.getMockTcpPort()); - this.openMockTcp(p); - }else { - if(opendPortList.contains(p.getMockTcpPort())){ - p.setMockTcpPort(0); + List opendPortList = new ArrayList<>(); + for (Project p : projectList) { + boolean isPortInRange = this.isMockTcpPortIsInRange(p.getMockTcpPort()); + if(isPortInRange && !opendPortList.contains(p.getMockTcpPort())){ + opendPortList.add(p.getMockTcpPort()); + this.openMockTcp(p); + }else { + if(opendPortList.contains(p.getMockTcpPort())){ + p.setMockTcpPort(0); + } + p.setIsMockTcpOpen(false); + projectMapper.updateByPrimaryKeySelective(p); } - p.setIsMockTcpOpen(false); - projectMapper.updateByPrimaryKeySelective(p); } + }catch (Exception e){ + e.printStackTrace(); } } diff --git a/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java b/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java index a24361dfb3..e155338b1f 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java @@ -190,7 +190,7 @@ public class TestCaseController { @PostMapping("/edit/order") public void orderCase(@RequestBody ResetOrderRequest request) { checkPermissionService.checkTestCaseOwner(request.getMoveId()); - testCaseService.orderCase(request); + testCaseService.updateOrder(request); } @PostMapping(value = "/edit", consumes = {"multipart/form-data"}) diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 97f7e2b43b..169772f0d6 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -352,14 +352,7 @@ public class TestCaseService { return returnList; } public void setDefaultOrder(QueryTestCaseRequest request) { - List orders = request.getOrders(); - if (CollectionUtils.isEmpty(orders)) { - OrderRequest order = new OrderRequest(); - order.setName("order"); - order.setType("desc"); - orders = new ArrayList<>(); - orders.add(order); - } + List orders = ServiceUtils.getDefaultSortOrder(request.getOrders()); OrderRequest order = new OrderRequest(); // 对模板导入的测试用例排序 order.setName("sort"); @@ -466,13 +459,10 @@ public class TestCaseService { } public List getReviewCase(QueryTestCaseRequest request) { - List orderList = ServiceUtils.getDefaultOrder(request.getOrders()); - OrderRequest order = new OrderRequest(); - // 对模板导入的测试用例排序 - order.setName("sort"); - order.setType("desc"); - orderList.add(order); - request.setOrders(orderList); + setDefaultOrder(request); + request.getOrders().forEach(order -> { + order.setPrefix("test_case"); + }); return extTestCaseMapper.getTestCaseByNotInReview(request); } @@ -484,7 +474,7 @@ public class TestCaseService { criteria.andMaintainerEqualTo(request.getUserId()); if (StringUtils.isNotBlank(request.getProjectId())) { criteria.andProjectIdEqualTo(request.getProjectId()); - testCaseExample.setOrderByClause("update_time desc, sort desc"); + testCaseExample.setOrderByClause("order desc, sort desc"); return testCaseMapper.selectByExample(testCaseExample); } return new ArrayList<>(); @@ -1004,6 +994,7 @@ public class TestCaseService { } returnDatas.add(list); } + return returnDatas; } @@ -1013,7 +1004,7 @@ public class TestCaseService { QueryTestCaseRequest condition = request.getCondition(); List orderList = new ArrayList<>(); if (condition != null) { - orderList = ServiceUtils.getDefaultOrder(condition.getOrders()); + orderList = ServiceUtils.getDefaultSortOrder(request.getOrders()); } OrderRequest order = new OrderRequest(); order.setName("sort"); @@ -1408,13 +1399,7 @@ public class TestCaseService { } public List listTestCaseIds(QueryTestCaseRequest request) { - List orderList = ServiceUtils.getDefaultOrder(request.getOrders()); - OrderRequest order = new OrderRequest(); - // 对模板导入的测试用例排序 - order.setName("sort"); - order.setType("desc"); - orderList.add(order); - request.setOrders(orderList); + setDefaultOrder(request); List selectFields = new ArrayList<>(); selectFields.add("id"); selectFields.add("name"); @@ -1494,14 +1479,7 @@ public class TestCaseService { } public List listTestCaseForMinder(QueryTestCaseRequest request) { - List orderList = ServiceUtils.getDefaultOrder(request.getOrders()); - OrderRequest order = new OrderRequest(); - // 对模板导入的测试用例排序 - order.setName("sort"); - order.setType("desc"); - orderList.add(order); - request.setOrders(orderList); - request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); + setDefaultOrder(request); return extTestCaseMapper.listForMinder(request); } @@ -1919,46 +1897,20 @@ public class TestCaseService { } public void initOrderField() { - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); - List projectIds = extTestCaseMapper.selectProjectIds(); - projectIds.forEach((projectId) -> { - Long order = 0L; - List ids = extTestCaseMapper.getIdsOrderByCreateTime(projectId); - for (String id : ids) { - TestCaseWithBLOBs testCase = new TestCaseWithBLOBs(); - testCase.setId(id); - testCase.setOrder(order); - order += 5000; - mapper.updateByPrimaryKeySelective(testCase); - } - sqlSession.flushStatements(); - }); + ServiceUtils.initOrderField(TestCaseWithBLOBs.class, TestCaseMapper.class, + extTestCaseMapper::selectProjectIds, + extTestCaseMapper::getIdsOrderByCreateTime); } /** * 用例自定义排序 * @param request */ - public void orderCase(ResetOrderRequest request) { - Long order = null; - Long lastOrPreOrder = null; - TestCaseWithBLOBs target = testCaseMapper.selectByPrimaryKey(request.getTargetId()); - if (request.getMoveMode().equals(ResetOrderRequest.MoveMode.AFTER.name())) { - order = target.getOrder() - 5000; - lastOrPreOrder = extTestCaseMapper.getPreOrder(request.getProjectId(), target.getOrder()); - } else { - order = target.getOrder() + 5000; - // 追加到前面,因为是降序排,则查找比目标 order 更大的一个order - lastOrPreOrder = extTestCaseMapper.getLastOrder(request.getProjectId(), target.getOrder()); - } - if (lastOrPreOrder != null) { - // 如果不是第一个或最后一个则取中间值 - order = (target.getOrder() + lastOrPreOrder) / 2; - } - TestCaseWithBLOBs testCaseWithBLOBs = new TestCaseWithBLOBs(); - testCaseWithBLOBs.setId(request.getMoveId()); - testCaseWithBLOBs.setOrder(order); - testCaseMapper.updateByPrimaryKeySelective(testCaseWithBLOBs); + public void updateOrder(ResetOrderRequest request) { + ServiceUtils.updateOrderField(request, TestCaseWithBLOBs.class, + testCaseMapper::selectByPrimaryKey, + extTestCaseMapper::getPreOrder, + extTestCaseMapper::getLastOrder, + testCaseMapper::updateByPrimaryKeySelective); } }