class ProjectDependencyServiceImpl implements ProjectDependencyService {

	@Override
	public List<RepoWidgetList> findAllWidgets(Integer projectId) {
		// 获取项目的所有依赖，包含组件仓库的版本信息
		List<ProjectDependency> allDependences = projectDependencyDao.findAllByProjectId(projectId);
		
		// 转换为对应的 API 仓库的版本信息
		return allDependences
			.stream()
			.flatMap(item -> componentRepoVersionDao.findById(item.getComponentRepoVersionId()).stream())
			.map(item -> item.getApiRepoVersionId())
			// 如果多个组件实现的是同一个 api repo version，则先去重
			.distinct()
			.map(apiVersionId -> {
				Optional<ApiRepo> apiRepoOption = apiRepoVersionDao
						.findById(apiVersionId)
						.flatMap(apiRepoVersion -> apiRepoDao.findById(apiRepoVersion.getApiRepoId()));
				ApiRepoVersionInfo result = new ApiRepoVersionInfo();
				result.setApiRepoVersionId(apiVersionId);
				apiRepoOption.ifPresent(apiRepo -> {
					// FIXME: 是否需要该字段名
					result.setApiRepoName(apiRepo.getGitRepoUrl());
					result.setApiRepoId(apiRepo.getId());
					result.setCategory(apiRepo.getCategory());
				});
				return result;
			})
			.filter(apiVersionInfo -> apiVersionInfo.getCategory() == RepoCategory.WIDGET)
			.map(apiVersionInfo -> {
				// 查出依赖中的所有部件
				List<Widget> widgets = apiComponentDao
						.findAllByApiRepoVersionId(apiVersionInfo.getApiRepoVersionId())
						.stream()
						.map(apiComponent -> {
							Widget result = new Widget();
							result.setWidgetId(apiComponent.getId());
							result.setWidgetCode(apiComponent.getCode());
							result.setWidgetName(apiComponent.getName());
							
							result.setApiRepoId(apiVersionInfo.getApiRepoId());
							// 添加属性列表
							List<WidgetProperty> properties = apiComponentAttrDao.findAllByApiWidgetIdOrderByCode(apiComponent.getId()).stream().map(property -> {
								WidgetProperty each = new WidgetProperty();
								each.setCode(property.getCode());
								each.setName(property.getName());
								each.setValueType(property.getValueType());
								each.setDefaultValue(property.getDefaultValue());
								
								// 添加事件参数列表
								if(property.getValueType().equals(WidgetPropertyValueType.FUNCTION.getKey())) {
									List<EventArgument> eventArgs = apiComponentAttrFunArgDao.findAllByApiWidgetPropertyId(property.getId()).stream().map(eventArg -> {
										EventArgument ea = new EventArgument();
										ea.setCode(eventArg.getCode());
										ea.setName(eventArg.getName());
										ea.setValueType(eventArg.getValueType());
										ea.setDefaultValue(eventArg.getDefaultValue());
										return ea;
									}).collect(Collectors.toList());
									each.setArguments(eventArgs);
								}
								return each;
							}).collect(Collectors.toList());
							result.setProperties(properties);
							return result;
						}).collect(Collectors.toList());
				// 对部件进行分组
				// 当前都归到未分类下
				WidgetCategory category = new WidgetCategory();
				category.setName("_");
				category.setWidgets(widgets);
				
				RepoWidgetList widgetRepo = new RepoWidgetList();
				widgetRepo.setApiRepoId(apiVersionInfo.getApiRepoId());
				widgetRepo.setApiRepoName(apiVersionInfo.getApiRepoName());
				widgetRepo.setWidgetCategories(Collections.singletonList(category));
				return widgetRepo;
			}).collect(Collectors.toList());
	}

}