大声道撒旦

This commit is contained in:
dengqichen 2025-01-08 16:48:52 +08:00
parent a7a6a1f5bb
commit c117e15bcc
3 changed files with 32 additions and 33 deletions

View File

@ -28,14 +28,6 @@ public interface IRepositoryGroupRepository extends IBaseRepository<RepositoryGr
*/ */
Long countByExternalSystemId(Long externalSystemId); Long countByExternalSystemId(Long externalSystemId);
/**
* 根据外部系统ID和组ID查找仓库组
*
* @param externalSystemId 外部系统ID
* @param groupId 组ID
* @return 仓库组
*/
RepositoryGroup findByExternalSystemIdAndGroupId(Long externalSystemId, Long groupId);
/** /**
* 根据外部系统ID删除所有仓库组 * 根据外部系统ID删除所有仓库组
@ -43,4 +35,6 @@ public interface IRepositoryGroupRepository extends IBaseRepository<RepositoryGr
* @param externalSystemId 外部系统ID * @param externalSystemId 外部系统ID
*/ */
void deleteByExternalSystemId(Long externalSystemId); void deleteByExternalSystemId(Long externalSystemId);
List<RepositoryGroup> findByExternalSystemIdAndDeletedFalse(Long externalSystemId);
} }

View File

@ -59,27 +59,31 @@ public class RepositoryGroupServiceImpl extends BaseServiceImpl<RepositoryGroup,
} }
try { try {
// 3. 清空当前系统中该外部系统的所有仓库组 // 3. 获取本地已存在的仓库组
repositoryGroupRepository.deleteByExternalSystemId(externalSystemId); List<RepositoryGroup> existingGroups = repositoryGroupRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
repositoryGroupRepository.flush(); Map<Long, RepositoryGroup> existingGroupMap = existingGroups.stream()
.collect(Collectors.toMap(RepositoryGroup::getGroupId, Function.identity()));
// 4. 批量保存从Git API获取的仓库组到数据库 // 4. 处理每个远程仓库组
List<RepositoryGroup> groupsToSave = remoteGroups.stream() List<RepositoryGroup> groupsToSave = new ArrayList<>();
.map(remoteGroup -> { for (GitGroupResponse remoteGroup : remoteGroups) {
RepositoryGroup group = new RepositoryGroup(); RepositoryGroup group = existingGroupMap.getOrDefault(remoteGroup.getId(), new RepositoryGroup());
group.setExternalSystemId(externalSystemId);
group.setGroupId(remoteGroup.getId());
group.setName(remoteGroup.getName());
group.setPath(remoteGroup.getPath());
group.setDescription(remoteGroup.getDescription());
group.setVisibility(remoteGroup.getVisibility());
group.setWebUrl(remoteGroup.getWebUrl());
group.setAvatarUrl(remoteGroup.getAvatarUrl());
group.setParentId(remoteGroup.getParentId());
return group;
})
.collect(Collectors.toList());
// 更新基本信息
group.setExternalSystemId(externalSystemId);
group.setGroupId(remoteGroup.getId());
group.setName(remoteGroup.getName());
group.setPath(remoteGroup.getPath());
group.setDescription(remoteGroup.getDescription());
group.setVisibility(remoteGroup.getVisibility());
group.setWebUrl(remoteGroup.getWebUrl());
group.setAvatarUrl(remoteGroup.getAvatarUrl());
group.setParentId(remoteGroup.getParentId());
groupsToSave.add(group);
}
// 5. 保存所有仓库组
List<RepositoryGroup> savedGroups = repositoryGroupRepository.saveAll(groupsToSave); List<RepositoryGroup> savedGroups = repositoryGroupRepository.saveAll(groupsToSave);
log.info("Successfully synchronized {} groups for external system: {}", log.info("Successfully synchronized {} groups for external system: {}",

View File

@ -62,14 +62,15 @@ public class RepositoryProjectServiceImpl extends BaseServiceImpl<RepositoryProj
return 0; return 0;
} }
// 3. 清空当前系统中该外部系统的所有项目 // 3. 获取本地已存在的项目
repositoryProjectRepository.deleteByExternalSystemId(externalSystemId); List<RepositoryProject> existingProjects = repositoryProjectRepository.findByExternalSystemIdAndDeletedFalse(externalSystemId);
repositoryProjectRepository.flush(); Map<Long, RepositoryProject> existingProjectMap = existingProjects.stream()
.collect(Collectors.toMap(RepositoryProject::getProjectId, Function.identity()));
// 4. 批量保存从Git API获取的项目到数据库 // 4. 更新或创建项目
List<RepositoryProject> projectsToSave = remoteProjects.stream() List<RepositoryProject> projectsToSave = remoteProjects.stream()
.map(remoteProject -> { .map(remoteProject -> {
RepositoryProject project = new RepositoryProject(); RepositoryProject project = existingProjectMap.getOrDefault(remoteProject.getId(), new RepositoryProject());
project.setExternalSystemId(externalSystemId); project.setExternalSystemId(externalSystemId);
project.setProjectId(remoteProject.getId()); project.setProjectId(remoteProject.getId());
project.setName(remoteProject.getName()); project.setName(remoteProject.getName());