refactor(face): 重构人脸识别服务中的设备数据处理逻辑
All checks were successful
ZhenTu-BE/pipeline/head This commit looks good

- 按设备列表顺序重新排列结果以确保一致性
- 为每个设备创建对应的内容项,包括无源设备的占位符
- 过滤并按设备ID分组源实体数据
- 为无关联设备的源实体补充单独的处理逻辑
- 优化数据流处理提高代码可读性和维护性
This commit is contained in:
2026-02-15 09:05:09 +08:00
parent 9a18ffc167
commit 39c955b55c

View File

@@ -519,12 +519,52 @@ public class FaceServiceImpl implements FaceService {
List<DeviceV2DTO> deviceList = deviceRepository.getAllDeviceByScenicId(face.getScenicId()); List<DeviceV2DTO> deviceList = deviceRepository.getAllDeviceByScenicId(face.getScenicId());
List<SourceEntity> sourceEntityList = sourceMapper.listSourceByFaceRelation(face.getId(), 2); List<SourceEntity> sourceEntityList = sourceMapper.listSourceByFaceRelation(face.getId(), 2);
List<MemberSourceEntity> memberSourceRelations = memberRelationRepository.listSourceByFaceRelation(face.getId(), 2); List<MemberSourceEntity> memberSourceRelations = memberRelationRepository.listSourceByFaceRelation(face.getId(), 2);
// 按 deviceList 顺序排列结果
Set<Long> deviceIds = deviceList.stream().map(DeviceV2DTO::getId).collect(Collectors.toSet());
for (DeviceV2DTO device : deviceList) {
List<SourceEntity> deviceSources = sourceEntityList.stream()
.filter(s -> device.getId().equals(s.getDeviceId()))
.toList();
if (deviceSources.isEmpty()) {
ContentPageVO content = new ContentPageVO();
content.setName(device.getName());
content.setGroup(device.getName());
content.setContentId(device.getId());
content.setGoodsType(2);
content.setContentType(2);
content.setScenicId(face.getScenicId());
content.setSourceType(2);
content.setTemplateCoverUrl("");
content.setIsBuy(0);
content.setLockType(1);
result.add(content);
} else {
for (SourceEntity sourceEntity : deviceSources) {
ContentPageVO content = new ContentPageVO();
content.setName("摄影师拍照");
content.setGroup(device.getName());
content.setContentId(sourceEntity.getId());
content.setGoodsType(2);
content.setContentType(2);
content.setScenicId(sourceEntity.getScenicId());
content.setSourceType(2);
content.setOrigUrl(sourceEntity.getUrl());
content.setTemplateCoverUrl(sourceEntity.getThumbUrl());
memberSourceRelations.stream().filter(relation -> relation.getSourceId().equals(sourceEntity.getId())).findAny().ifPresent(relation -> {
content.setIsBuy(relation.getIsBuy());
});
content.setLockType(-1);
result.add(content);
}
}
}
// 补充不属于任何设备的源
for (SourceEntity sourceEntity : sourceEntityList) { for (SourceEntity sourceEntity : sourceEntityList) {
if (sourceEntity.getDeviceId() != null && deviceIds.contains(sourceEntity.getDeviceId())) {
continue;
}
ContentPageVO content = new ContentPageVO(); ContentPageVO content = new ContentPageVO();
content.setName("摄影师拍照"); content.setName("摄影师拍照");
deviceList.stream().filter(device -> device.getId().equals(sourceEntity.getDeviceId())).findFirst().ifPresent(device -> {
content.setGroup(device.getName());
});
content.setContentId(sourceEntity.getId()); content.setContentId(sourceEntity.getId());
content.setGoodsType(2); content.setGoodsType(2);
content.setContentType(2); content.setContentType(2);
@@ -538,21 +578,6 @@ public class FaceServiceImpl implements FaceService {
content.setLockType(-1); content.setLockType(-1);
result.add(content); result.add(content);
} }
List<Long> containedDeviceId = sourceEntityList.stream().map(SourceEntity::getDeviceId).filter(Objects::nonNull).distinct().toList();
deviceList.stream().filter(device -> !containedDeviceId.contains(device.getId())).forEach(device -> {
ContentPageVO content = new ContentPageVO();
content.setName(device.getName());
content.setGroup(device.getName());
content.setContentId(device.getId());
content.setGoodsType(2);
content.setContentType(2);
content.setScenicId(face.getScenicId());
content.setSourceType(2);
content.setTemplateCoverUrl("");
content.setIsBuy(0);
content.setLockType(1);
result.add(content);
});
return result; return result;
} }
List<TemplateRespVO> templateList = templateRepository.getTemplateListByScenicId(face.getScenicId()); List<TemplateRespVO> templateList = templateRepository.getTemplateListByScenicId(face.getScenicId());