diff --git a/backend/src/main/java/io/metersphere/base/domain/IssuesDao.java b/backend/src/main/java/io/metersphere/base/domain/IssuesDao.java index 40128d77ce..753ea19e2b 100644 --- a/backend/src/main/java/io/metersphere/base/domain/IssuesDao.java +++ b/backend/src/main/java/io/metersphere/base/domain/IssuesDao.java @@ -1,6 +1,7 @@ package io.metersphere.base.domain; import io.metersphere.dto.CustomFieldDao; +import io.metersphere.dto.CustomFieldItemDTO; import lombok.Getter; import lombok.Setter; @@ -23,4 +24,15 @@ public class IssuesDao extends IssuesWithBLOBs { private String refType; private String refId; private List fields; + + /** + * 缺陷自定义字段相关 + */ + private List customFieldList; + private String customData; + private String issueId; + private String fieldId; + private String fieldName; + private String fieldType; + private String fieldValue; } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java index 7fce60335d..22f357c06e 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.java @@ -39,4 +39,8 @@ public interface ExtIssuesMapper { IssuesDao selectByPrimaryKey(String id); List getIssueCustomField(String id); + + List getIssueCustomFields(List ids); + + List getPlatformIssueByIds(@Param("ids") List ids); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml index b01baecc2d..c6f50c53cb 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtIssuesMapper.xml @@ -139,7 +139,33 @@ from custom_field_issues cfi join custom_field_template cft on cfi.field_id = cft.field_id join custom_field cf on cf.id = cft.field_id - where cfi.resource_id = #{id} + join project on project.id = cf.project_id and project.issue_template_id = cft.template_id + where cfi.resource_id = #{issueId} + + + diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java b/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java index 6be16b635a..7520231837 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldResourceService.java @@ -1,14 +1,10 @@ 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.*; 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; import io.metersphere.base.mapper.ext.ExtIssuesMapper; import io.metersphere.base.mapper.ext.ExtTestCaseMapper; @@ -49,10 +45,6 @@ public class CustomFieldResourceService { @Resource ProjectService projectService; - @Lazy - @Resource - WorkspaceService workspaceService; - @Resource ExtTestCaseMapper extTestCaseMapper; @@ -62,9 +54,6 @@ public class CustomFieldResourceService { @Resource CustomFieldService customFieldService; - @Resource - ExtBaseMapper extBaseMapper; - @Resource ExtCustomFieldResourceMapper extCustomFieldResourceMapper; @@ -143,8 +132,8 @@ public class CustomFieldResourceService { } List fieldIds = resourceFieldMap.get(resourceId); for (CustomFieldResource customFieldResource : list) { + customFieldResource.setResourceId(resourceId); if (CollectionUtils.isEmpty(fieldIds) || !fieldIds.contains(customFieldResource.getFieldId())) { - customFieldResource.setResourceId(resourceId); addList.add(customFieldResource); } else { updateList.add(customFieldResource); @@ -417,4 +406,38 @@ public class CustomFieldResourceService { } } } + + public List getPlatformIssueByIds(List platformIds) { + List issues = extIssuesMapper.getPlatformIssueByIds(platformIds); + if (CollectionUtils.isEmpty(issues)) { + return issues; + } + List issueIds = issues.stream().map(IssuesDao::getId).collect(Collectors.toList()); + List issuesList = extIssuesMapper.getIssueCustomFields(issueIds); + Map> map = new HashMap<>(); + issuesList.forEach(f -> { + List list = map.get(f.getId()); + if (list == null) { + list = new ArrayList<>(); + CustomFieldItemDTO dto = new CustomFieldItemDTO(); + dto.setId(f.getFieldId()); + dto.setName(f.getFieldName()); + dto.setType(f.getFieldType()); + dto.setValue(f.getFieldValue()); + dto.setCustomData(f.getCustomData()); + list.add(dto); + map.put(f.getId(), list); + } else { + CustomFieldItemDTO dto = new CustomFieldItemDTO(); + dto.setId(f.getFieldId()); + dto.setName(f.getFieldName()); + dto.setType(f.getFieldType()); + dto.setValue(f.getFieldValue()); + dto.setCustomData(f.getCustomData()); + list.add(dto); + } + }); + issues.forEach(i -> i.setCustomFieldList(map.getOrDefault(i.getId(), new ArrayList<>()))); + return issues; + } } diff --git a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java index 84441020e6..bf784db9a9 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -528,6 +528,27 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { } } + // 缺陷对象带有自定义字段数据 + protected void mergeIfIssueWithCustomField(IssuesWithBLOBs issue, String defaultCustomField) { + if (StringUtils.isBlank(defaultCustomFields)) { + return; + } + JSONArray fields = JSONArray.parseArray(issue.getCustomFields()); + Set ids = fields.stream().map(i -> ((JSONObject) i).getString("id")).collect(Collectors.toSet()); + JSONArray defaultFields = JSONArray.parseArray(defaultCustomField); + defaultFields.forEach(item -> { // 如果自定义字段里没有模板新加的字段,就把新字段加上 + String id = ((JSONObject) item).getString("id"); + if (StringUtils.isBlank(id)) { + id = ((JSONObject) item).getString("key"); + ((JSONObject) item).put("id", id); + } + if (!ids.contains(id)) { + fields.add(item); + } + }); + issue.setCustomFields(fields.toJSONString()); + } + public T getConfig(String platform, Class clazz) { String config = getPlatformConfig(platform); if (StringUtils.isBlank(config)) { 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 978cdf0187..e1e017cc56 100644 --- a/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/TapdPlatform.java @@ -1,5 +1,6 @@ package io.metersphere.track.issue; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.IssuesDao; @@ -12,6 +13,7 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.dto.CustomFieldItemDTO; import io.metersphere.dto.UserDTO; import io.metersphere.i18n.Translator; import io.metersphere.service.SystemParameterService; @@ -254,6 +256,25 @@ public class TapdPlatform extends AbstractIssuePlatform { return issue; } + protected IssuesWithBLOBs getUpdateIssue(IssuesWithBLOBs issue, JSONObject bug, List customField) { + if (issue == null) { + issue = new IssuesWithBLOBs(); + issue.setCustomFields(defaultCustomFields); + } else { + issue.setCustomFields(JSON.toJSONString(customField)); + mergeIfIssueWithCustomField(issue, defaultCustomFields); + } + TapdBug bugObj = JSONObject.parseObject(bug.toJSONString(), TapdBug.class); + BeanUtils.copyBean(issue, bugObj); + issue.setPlatformStatus(bugObj.getStatus()); + issue.setDescription(htmlDesc2MsDesc(issue.getDescription())); + issue.setCustomFields(syncIssueCustomField(issue.getCustomFields(), bug)); + issue.setPlatform(key); + issue.setCreateTime(bug.getLong("created")); + issue.setUpdateTime(bug.getLong("modified")); + return issue; + } + @Override public String getProjectId(String projectId) { return getProjectId(projectId, Project::getTapdId); diff --git a/backend/src/main/java/io/metersphere/track/service/IssuesService.java b/backend/src/main/java/io/metersphere/track/service/IssuesService.java index 77c7f66db7..b1466b6916 100644 --- a/backend/src/main/java/io/metersphere/track/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/track/service/IssuesService.java @@ -8,10 +8,6 @@ import com.github.pagehelper.PageHelper; import io.metersphere.base.domain.*; import io.metersphere.base.domain.ext.CustomFieldResource; import io.metersphere.base.mapper.*; -import io.metersphere.base.mapper.IssueFollowMapper; -import io.metersphere.base.mapper.IssuesMapper; -import io.metersphere.base.mapper.TestCaseIssuesMapper; -import io.metersphere.base.mapper.TestPlanTestCaseMapper; import io.metersphere.base.mapper.ext.ExtIssuesMapper; import io.metersphere.commons.constants.AttachmentType; import io.metersphere.commons.constants.IssueRefType; @@ -425,9 +421,11 @@ public class IssuesService { issueFileExample.createCriteria().andIssueIdEqualTo(id); List issueFiles = issueFileMapper.selectByExample(issueFileExample); List fileIds = issueFiles.stream().map(IssueFile::getFileId).collect(Collectors.toList()); - FileAttachmentMetadataExample fileAttachmentMetadataExample = new FileAttachmentMetadataExample(); - fileAttachmentMetadataExample.createCriteria().andIdIn(fileIds); - fileAttachmentMetadataMapper.deleteByExample(fileAttachmentMetadataExample); + if (CollectionUtils.isNotEmpty(fileIds)) { + FileAttachmentMetadataExample fileAttachmentMetadataExample = new FileAttachmentMetadataExample(); + fileAttachmentMetadataExample.createCriteria().andIdIn(fileIds); + fileAttachmentMetadataMapper.deleteByExample(fileAttachmentMetadataExample); + } issueFileMapper.deleteByExample(issueFileExample); fileService.deleteAttachment(AttachmentType.ISSUE.type(), id); }