feat(printer): 优化自动添加照片到打印列表逻辑

- 修改autoAddPhotosToPreferPrint方法返回值为List<SourceEntity>
- 当自动添加成功时直接返回添加的照片列表
- 添加失败或无照片时返回空列表而非数量
- 控制器根据返回结果判断是否生成二维码URL
- 提升代码可读性和维护性
This commit is contained in:
2025-12-04 21:23:23 +08:00
parent 42540e2dc4
commit eade5f8092
3 changed files with 31 additions and 19 deletions

View File

@@ -230,16 +230,23 @@ public class PrinterTvController {
// 3. 在景区人脸库中搜索(注意:这里使用scenicId作为数据库名,搜索的是景区内的人脸样本)
pcFaceService.matchFaceId(faceId);
// 4. 自动添加照片到打印列表
int addedCount = printerService.autoAddPhotosToPreferPrint(faceId);
// 4. 自动添加照片到打印列表,并获取添加成功的照片列表
List<SourceEntity> addedSources = printerService.autoAddPhotosToPreferPrint(faceId);
// 5. 查询匹配到的图像素材(type=2)
List<SourceEntity> sources = new ArrayList<>();
List<MemberSourceEntity> memberSourceEntities = memberRelationRepository.listSourceByFaceRelation(faceId, 2);
for (MemberSourceEntity memberSourceEntity : memberSourceEntities) {
SourceEntity source = sourceRepository.getSource(memberSourceEntity.getSourceId());
if (source != null) {
sources.add(source);
// 5. 根据自动添加结果决定返回的sources
List<SourceEntity> sources;
if (addedSources != null && !addedSources.isEmpty()) {
// 如果自动添加成功,返回添加的照片列表
sources = addedSources;
} else {
// 如果自动添加为空,按原逻辑查询匹配到的图像素材(type=2)
sources = new ArrayList<>();
List<MemberSourceEntity> memberSourceEntities = memberRelationRepository.listSourceByFaceRelation(faceId, 2);
for (MemberSourceEntity memberSourceEntity : memberSourceEntities) {
SourceEntity source = sourceRepository.getSource(memberSourceEntity.getSourceId());
if (source != null) {
sources.add(source);
}
}
}
@@ -250,7 +257,7 @@ public class PrinterTvController {
resp.setScenicId(scenicId);
resp.setSources(sources);
// 只有当添加了照片时才返回二维码URL
if (addedCount > 0) {
if (addedSources != null && !addedSources.isEmpty()) {
resp.setQrcodeUrl("https://zhentuai.com/printer/v1/tv/face/" + faceId + "/qrcode");
} else {
resp.setQrcodeUrl(null);

View File

@@ -7,6 +7,7 @@ import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
import com.ycwl.basic.model.pc.printer.req.ReprintRequest;
import com.ycwl.basic.model.pc.printer.resp.MemberPrintResp;
import com.ycwl.basic.model.pc.printer.resp.PrinterResp;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.model.printer.req.FromSourceReq;
import com.ycwl.basic.model.printer.req.PrinterSyncReq;
import com.ycwl.basic.model.printer.resp.PrintTaskResp;
@@ -61,7 +62,7 @@ public interface PrinterService {
FaceRecognizeResp useSample(Long userId, Long sampleId);
int autoAddPhotosToPreferPrint(Long faceId);
List<SourceEntity> autoAddPhotosToPreferPrint(Long faceId);
/**
* 查询待审核的打印任务

View File

@@ -1126,16 +1126,19 @@ public class PrinterServiceImpl implements PrinterService {
* 根据景区和设备配置自动添加type=2的照片到用户打印列表
*
* @param faceId 人脸ID
* @return 成功添加的照片数量
* @return 成功添加的照片列表
*/
@Override
public int autoAddPhotosToPreferPrint(Long faceId) {
public List<SourceEntity> autoAddPhotosToPreferPrint(Long faceId) {
// 使用线程安全的List收集成功添加的SourceEntity
List<SourceEntity> addedSources = Collections.synchronizedList(new ArrayList<>());
try {
// 1. 获取人脸信息
FaceEntity face = faceRepository.getFace(faceId);
if (face == null) {
log.warn("人脸不存在,无法自动添加打印: faceId={}", faceId);
return 0;
return Collections.emptyList();
}
Long scenicId = face.getScenicId();
@@ -1145,21 +1148,21 @@ public class PrinterServiceImpl implements PrinterService {
ScenicConfigManager scenicConfig = scenicRepository.getScenicConfigManager(scenicId);
if (scenicConfig == null) {
log.warn("景区配置不存在,跳过自动添加打印: scenicId={}", scenicId);
return 0;
return Collections.emptyList();
}
// 3. 检查景区是否启用打印功能
Boolean printEnable = scenicConfig.getBoolean("print_enable");
if (printEnable == null || !printEnable) {
log.debug("景区未启用打印功能,跳过自动添加: scenicId={}", scenicId);
return 0;
return Collections.emptyList();
}
// 4. 查询该faceId关联的所有type=2的照片
List<SourceEntity> imageSources = sourceMapper.listImageSourcesByFaceId(faceId);
if (imageSources == null || imageSources.isEmpty()) {
log.debug("该人脸没有关联的照片,跳过自动添加: faceId={}", faceId);
return 0;
return Collections.emptyList();
}
// 5. 按照deviceId分组处理
@@ -1223,6 +1226,7 @@ public class PrinterServiceImpl implements PrinterService {
try {
addUserPhoto(memberId, scenicId, source.getUrl(), faceId, source.getId());
deviceAdded++;
addedSources.add(source); // 添加成功的source
} catch (Exception e) {
log.warn("添加照片到打印列表失败: sourceId={}, url={}, error={}",
source.getId(), source.getUrl(), e.getMessage());
@@ -1254,12 +1258,12 @@ public class PrinterServiceImpl implements PrinterService {
log.debug("自动添加打印完成: faceId={}, 无符合条件的照片", faceId);
}
return added;
return addedSources;
} catch (Exception e) {
// 出现异常则放弃,不影响主流程
log.error("自动添加打印失败,已忽略: faceId={}", faceId, e);
return 0;
return Collections.emptyList();
}
}
/**