diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.java index edd8cea635..4ba95979b7 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.java @@ -17,4 +17,6 @@ public interface ExtCustomFieldTemplateMapper { List getSystemCustomField(@Param("templateId") String templateId, @Param("fieldName") String fieldName); void batchInsert(@Param("customFieldTemplates") List customFieldTemplates); + + Long getLastOrder(@Param("templateId") String templateId, @Param("baseOrder") Long baseOrder); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml index b626176385..7d36db607a 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFieldTemplateMapper.xml @@ -18,6 +18,7 @@ where cft.template_id = #{request.templateId} + order by `order` asc + INSERT INTO diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java index 5755748cac..5626f7e8fe 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java @@ -1,12 +1,12 @@ package io.metersphere.service; - import io.metersphere.base.domain.CustomField; import io.metersphere.base.domain.CustomFieldTemplate; import io.metersphere.base.domain.CustomFieldTemplateExample; import io.metersphere.base.mapper.CustomFieldMapper; import io.metersphere.base.mapper.CustomFieldTemplateMapper; import io.metersphere.base.mapper.ext.ExtCustomFieldTemplateMapper; +import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.dto.CustomFieldDao; import io.metersphere.dto.CustomFieldTemplateDao; import org.apache.commons.collections.CollectionUtils; @@ -44,6 +44,7 @@ public class CustomFieldTemplateService { public List getCustomFields(String templateId) { CustomFieldTemplateExample example = new CustomFieldTemplateExample(); example.createCriteria().andTemplateIdEqualTo(templateId); + example.setOrderByClause("`order` asc"); return customFieldTemplateMapper.selectByExample(example); } @@ -110,15 +111,18 @@ public class CustomFieldTemplateService { CustomFieldTemplateMapper customFieldTemplateMapper = sqlSession.getMapper(CustomFieldTemplateMapper.class); if (CollectionUtils.isNotEmpty(customFields)) { - customFields.forEach(item -> { + Long nextOrder = ServiceUtils.getNextOrder(templateId, extCustomFieldTemplateMapper::getLastOrder); + for (CustomFieldTemplate item : customFields) { item.setId(UUID.randomUUID().toString()); item.setTemplateId(templateId); item.setScene(scene); if (item.getRequired() == null) { item.setRequired(false); } + nextOrder += ServiceUtils.ORDER_STEP; + item.setOrder((int) nextOrder.longValue()); customFieldTemplateMapper.insert(item); - }); + } } sqlSession.flushStatements(); if (sqlSession != null && sqlSessionFactory != null) {