From 5c97b750fcff2ca7d81319b42187a48fa3bfe42e Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 8 Jul 2022 11:00:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=BC=BA=E9=99=B7=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=90=8C=E6=AD=A5=E6=97=B6=E7=BC=BA=E9=99=B7?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CustomFieldIssuesService.java | 5 ++ .../service/CustomFieldResourceService.java | 51 +++++++++++++++++++ .../metersphere/track/issue/JiraPlatform.java | 4 +- .../metersphere/track/issue/TapdPlatform.java | 10 ++-- .../track/issue/ZentaoPlatform.java | 6 ++- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldIssuesService.java b/backend/src/main/java/io/metersphere/service/CustomFieldIssuesService.java index b4a616f38d..f1ebcc61a9 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldIssuesService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldIssuesService.java @@ -5,6 +5,7 @@ import io.metersphere.dto.CustomFieldDao; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,6 +27,10 @@ public class CustomFieldIssuesService extends CustomFieldResourceService { super.batchEditFields(TABLE_NAME, resourceId, fields); } + public void batchEditFields(HashMap> customFieldMap) { + super.batchEditFields(TABLE_NAME, customFieldMap); + } + public int updateByPrimaryKeySelective(CustomFieldResource field) { return super.updateByPrimaryKeySelective(TABLE_NAME, field); } diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java b/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java index 6fabe9bb15..6be16b635a 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java @@ -2,8 +2,11 @@ package io.metersphere.service; import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.CustomField; +import io.metersphere.base.domain.CustomFieldIssues; +import io.metersphere.base.domain.CustomFieldIssuesExample; import io.metersphere.base.domain.Project; import io.metersphere.base.domain.ext.CustomFieldResource; +import io.metersphere.base.mapper.CustomFieldIssuesMapper; import io.metersphere.base.mapper.CustomFieldMapper; import io.metersphere.base.mapper.ext.ExtBaseMapper; import io.metersphere.base.mapper.ext.ExtCustomFieldResourceMapper; @@ -33,6 +36,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -68,6 +72,9 @@ public class CustomFieldResourceService { @Resource SystemParameterService systemParameterService; + @Resource + CustomFieldIssuesMapper customFieldIssuesMapper; + private int initCount = 0; protected void addFields(String tableName, String resourceId, List addFields) { @@ -108,6 +115,50 @@ public class CustomFieldResourceService { } } + + protected void batchEditFields(String tableName, HashMap> customFieldMap) { + if (customFieldMap == null || customFieldMap.size() == 0) { + return; + } + this.checkInit(); + SqlSession sqlSession = ServiceUtils.getBatchSqlSession(); + ExtCustomFieldResourceMapper batchMapper = sqlSession.getMapper(ExtCustomFieldResourceMapper.class); + List addList = new ArrayList<>(); + List updateList = new ArrayList<>(); + + Set set = customFieldMap.keySet(); + if (CollectionUtils.isEmpty(set)) { + return; + } + CustomFieldIssuesExample example = new CustomFieldIssuesExample(); + example.createCriteria().andResourceIdIn(new ArrayList<>(set)); + List customFieldIssues = customFieldIssuesMapper.selectByExample(example); + Map> resourceFieldMap = customFieldIssues.stream() + .collect(Collectors.groupingBy(CustomFieldIssues::getResourceId, Collectors.mapping(CustomFieldIssues::getFieldId, Collectors.toList()))); + + for (String resourceId : customFieldMap.keySet()) { + List list = customFieldMap.get(resourceId); + if (CollectionUtils.isEmpty(list)) { + continue; + } + List fieldIds = resourceFieldMap.get(resourceId); + for (CustomFieldResource customFieldResource : list) { + if (CollectionUtils.isEmpty(fieldIds) || !fieldIds.contains(customFieldResource.getFieldId())) { + customFieldResource.setResourceId(resourceId); + addList.add(customFieldResource); + } else { + updateList.add(customFieldResource); + } + } + } + addList.forEach(l -> batchMapper.insert(tableName, l)); + updateList.forEach(l -> batchMapper.updateByPrimaryKeySelective(tableName, l)); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } + } + private void createOrUpdateFields(String tableName, String resourceId, CustomFieldResource field) { long count = extCustomFieldResourceMapper.countFieldResource(tableName, resourceId, field.getFieldId()); field.setResourceId(resourceId); diff --git a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java index 9cc59b36dc..f07d5ae064 100644 --- a/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/JiraPlatform.java @@ -539,6 +539,7 @@ public class JiraPlatform extends AbstractIssuePlatform { @Override public void syncIssues(Project project, List issues) { super.isThirdPartTemplate = isThirdPartTemplate(); + HashMap> customFieldMap = new HashMap<>(); IssuesService issuesService = CommonBeanFactory.getBean(IssuesService.class); if (project.getThirdPartTemplate()) { @@ -551,7 +552,7 @@ public class JiraPlatform extends AbstractIssuePlatform { String customFields = item.getCustomFields(); // 把自定义字段存入新表 List customFieldResource = customFieldService.getCustomFieldResource(customFields); - customFieldIssuesService.addFields(item.getId(), customFieldResource); + customFieldMap.put(item.getId(), customFieldResource); issuesMapper.updateByPrimaryKeySelective(item); } catch (HttpClientErrorException e) { if (e.getRawStatusCode() == 404) { @@ -563,6 +564,7 @@ public class JiraPlatform extends AbstractIssuePlatform { LogUtil.error(e); } }); + customFieldIssuesService.batchEditFields(customFieldMap); } @Override diff --git a/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java index 3e6de14641..2ca0daf3cc 100644 --- a/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.IssuesDao; import io.metersphere.base.domain.IssuesWithBLOBs; import io.metersphere.base.domain.Project; +import io.metersphere.base.domain.ext.CustomFieldResource; import io.metersphere.commons.constants.IssuesManagePlatform; import io.metersphere.commons.constants.IssuesStatus; import io.metersphere.commons.exception.MSException; @@ -28,10 +29,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; public class TapdPlatform extends AbstractIssuePlatform { @@ -201,6 +199,7 @@ public class TapdPlatform extends AbstractIssuePlatform { .collect(Collectors.toList()); if (CollectionUtils.isEmpty(ids)) return; + HashMap> customFieldMap = new HashMap<>(); Map statusMap = tapdClient.getStatusMap(project.getTapdId()); @@ -218,7 +217,7 @@ public class TapdPlatform extends AbstractIssuePlatform { IssuesWithBLOBs updateIssue = getUpdateIssue(issuesMapper.selectByPrimaryKey(id), bug, statusMap); updateIssue.setId(id); updateIssue.setCustomFields(syncIssueCustomField(updateIssue.getCustomFields(), bug)); - customFieldIssuesService.addFields(id, customFieldService.getCustomFieldResource(updateIssue.getCustomFields())); + customFieldMap.put(id, customFieldService.getCustomFieldResource(updateIssue.getCustomFields())); issuesMapper.updateByPrimaryKeySelective(updateIssue); ids.remove(platformId); }); @@ -233,6 +232,7 @@ public class TapdPlatform extends AbstractIssuePlatform { issuesMapper.updateByPrimaryKeySelective(issuesDao); } }); + customFieldIssuesService.batchEditFields(customFieldMap); } protected IssuesWithBLOBs getUpdateIssue(IssuesWithBLOBs issue, JSONObject bug, Map statusMap) { diff --git a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java index 85d5eeab99..b950a3b708 100644 --- a/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/ZentaoPlatform.java @@ -8,6 +8,7 @@ import io.metersphere.base.domain.IssuesDao; import io.metersphere.base.domain.IssuesExample; import io.metersphere.base.domain.IssuesWithBLOBs; import io.metersphere.base.domain.Project; +import io.metersphere.base.domain.ext.CustomFieldResource; import io.metersphere.commons.constants.IssuesManagePlatform; import io.metersphere.commons.constants.IssuesStatus; import io.metersphere.commons.constants.ZentaoIssuePlatformStatus; @@ -377,14 +378,17 @@ public class ZentaoPlatform extends AbstractIssuePlatform { @Override public void syncIssues(Project project, List issues) { + HashMap> customFieldMap = new HashMap<>(); + issues.forEach(item -> { IssuesWithBLOBs issue = issuesMapper.selectByPrimaryKey(item.getId()); JSONObject bug = zentaoClient.getBugById(item.getPlatformId()); issue = getUpdateIssues(issue, bug); - customFieldIssuesService.addFields(item.getId(), customFieldService.getCustomFieldResource(issue.getCustomFields())); + customFieldMap.put(item.getId(), customFieldService.getCustomFieldResource(issue.getCustomFields())); issue.setId(item.getId()); issuesMapper.updateByPrimaryKeySelective(issue); }); + customFieldIssuesService.batchEditFields(customFieldMap); } public List getBuilds() {