You've already forked FrameTour-BE
1
This commit is contained in:
@@ -2,12 +2,10 @@ package com.ycwl.basic.service.printer.impl;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.ycwl.basic.biz.OrderBiz;
|
||||
import com.ycwl.basic.constant.FaceConstant;
|
||||
import com.ycwl.basic.constant.NumberConstant;
|
||||
import com.ycwl.basic.constant.StorageConstant;
|
||||
import com.ycwl.basic.enums.OrderStateEnum;
|
||||
import com.ycwl.basic.exception.BaseException;
|
||||
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
|
||||
import com.ycwl.basic.image.watermark.ImageWatermarkFactory;
|
||||
import com.ycwl.basic.image.watermark.entity.WatermarkInfo;
|
||||
import com.ycwl.basic.image.watermark.enums.ImageWatermarkOperatorEnum;
|
||||
@@ -46,7 +44,6 @@ import com.ycwl.basic.model.printer.req.FromSourceReq;
|
||||
import com.ycwl.basic.model.printer.req.PrinterSyncReq;
|
||||
import com.ycwl.basic.model.printer.req.WorkerAuthReqVo;
|
||||
import com.ycwl.basic.model.printer.resp.PrintTaskResp;
|
||||
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
|
||||
import com.ycwl.basic.model.wx.WXPayOrderReqVO;
|
||||
import com.ycwl.basic.repository.DeviceRepository;
|
||||
import com.ycwl.basic.repository.FaceRepository;
|
||||
@@ -54,9 +51,7 @@ import com.ycwl.basic.repository.OrderRepository;
|
||||
import com.ycwl.basic.repository.ScenicRepository;
|
||||
import com.ycwl.basic.service.mobile.WxPayService;
|
||||
import com.ycwl.basic.service.pc.FaceService;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import com.ycwl.basic.service.printer.PrinterService;
|
||||
import com.ycwl.basic.service.task.TaskFaceService;
|
||||
import com.ycwl.basic.storage.StorageFactory;
|
||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||
import com.ycwl.basic.storage.enums.StorageAcl;
|
||||
@@ -83,15 +78,16 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PrinterServiceImpl implements PrinterService {
|
||||
@@ -128,6 +124,11 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
@Autowired
|
||||
private DeviceRepository deviceRepository;
|
||||
|
||||
// 用于优先打印的线程池,核心线程数根据实际情况调整
|
||||
private final ExecutorService preferPrintExecutor = Executors.newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors() * 2
|
||||
);
|
||||
|
||||
@Override
|
||||
public List<PrinterResp> listByScenicId(Long scenicId) {
|
||||
return printerMapper.listByScenicId(scenicId);
|
||||
@@ -813,7 +814,8 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
*
|
||||
* @param faceId 人脸ID
|
||||
*/
|
||||
private void autoAddPhotosToPreferPrint(Long faceId) {
|
||||
@Override
|
||||
public void autoAddPhotosToPreferPrint(Long faceId) {
|
||||
try {
|
||||
// 1. 获取人脸信息
|
||||
FaceEntity face = faceRepository.getFace(faceId);
|
||||
@@ -851,62 +853,88 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
.filter(source -> source.getDeviceId() != null)
|
||||
.collect(Collectors.groupingBy(SourceEntity::getDeviceId));
|
||||
|
||||
int totalAdded = 0;
|
||||
// 使用原子计数器统计成功添加的数量
|
||||
AtomicInteger totalAdded = new AtomicInteger(0);
|
||||
|
||||
// 创建异步任务列表
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<Long, List<SourceEntity>> entry : sourcesByDevice.entrySet()) {
|
||||
Long deviceId = entry.getKey();
|
||||
List<SourceEntity> deviceSources = entry.getValue();
|
||||
|
||||
// 6. 获取设备配置
|
||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(deviceId);
|
||||
if (deviceConfig == null) {
|
||||
log.debug("设备配置不存在,跳过该设备: deviceId={}", deviceId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 7. 检查是否启用优先打印
|
||||
Boolean preferPrintEnable = deviceConfig.getBoolean("prefer_print_enable");
|
||||
if (preferPrintEnable == null || !preferPrintEnable) {
|
||||
log.debug("设备未启用优先打印,跳过: deviceId={}", deviceId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 8. 获取优先打印数量配置
|
||||
Integer preferPrintCount = deviceConfig.getInteger("prefer_print_count");
|
||||
if (preferPrintCount == null) {
|
||||
log.debug("设备未配置优先打印数量,跳过: deviceId={}", deviceId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 9. 根据配置添加照片到打印列表
|
||||
List<SourceEntity> sourcesToAdd;
|
||||
if (preferPrintCount > 0) {
|
||||
// 如果大于0,按照数量限制添加
|
||||
sourcesToAdd = deviceSources.stream()
|
||||
.limit(preferPrintCount)
|
||||
.collect(Collectors.toList());
|
||||
log.info("设备{}配置优先打印{}张,实际添加{}张",
|
||||
deviceId, preferPrintCount, sourcesToAdd.size());
|
||||
} else {
|
||||
// 如果小于等于0,添加该设备的所有照片
|
||||
sourcesToAdd = deviceSources;
|
||||
log.info("设备{}配置优先打印所有照片,实际添加{}张",
|
||||
deviceId, sourcesToAdd.size());
|
||||
}
|
||||
|
||||
// 10. 批量添加到打印列表
|
||||
for (SourceEntity source : sourcesToAdd) {
|
||||
// 为每个设备创建一个异步任务
|
||||
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
addUserPhoto(memberId, scenicId, source.getUrl(), faceId);
|
||||
totalAdded++;
|
||||
// 6. 获取设备配置
|
||||
DeviceConfigManager deviceConfig = deviceRepository.getDeviceConfigManager(deviceId);
|
||||
if (deviceConfig == null) {
|
||||
log.debug("设备配置不存在,跳过该设备: deviceId={}", deviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 7. 检查是否启用优先打印
|
||||
Boolean preferPrintEnable = deviceConfig.getBoolean("prefer_print_enable");
|
||||
if (preferPrintEnable == null || !preferPrintEnable) {
|
||||
log.debug("设备未启用优先打印,跳过: deviceId={}", deviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 8. 获取优先打印数量配置
|
||||
Integer preferPrintCount = deviceConfig.getInteger("prefer_print_count");
|
||||
if (preferPrintCount == null) {
|
||||
log.debug("设备未配置优先打印数量,跳过: deviceId={}", deviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 9. 根据配置添加照片到打印列表
|
||||
List<SourceEntity> sourcesToAdd;
|
||||
if (preferPrintCount > 0) {
|
||||
// 如果大于0,按照数量限制添加
|
||||
sourcesToAdd = deviceSources.stream()
|
||||
.limit(preferPrintCount)
|
||||
.collect(Collectors.toList());
|
||||
log.info("设备{}配置优先打印{}张,实际添加{}张",
|
||||
deviceId, preferPrintCount, sourcesToAdd.size());
|
||||
} else {
|
||||
// 如果小于等于0,添加该设备的所有照片
|
||||
sourcesToAdd = deviceSources;
|
||||
log.info("设备{}配置优先打印所有照片,实际添加{}张",
|
||||
deviceId, sourcesToAdd.size());
|
||||
}
|
||||
|
||||
// 10. 批量添加到打印列表
|
||||
int deviceAdded = 0;
|
||||
for (SourceEntity source : sourcesToAdd) {
|
||||
try {
|
||||
addUserPhoto(memberId, scenicId, source.getUrl(), faceId);
|
||||
deviceAdded++;
|
||||
} catch (Exception e) {
|
||||
log.warn("添加照片到打印列表失败: sourceId={}, url={}, error={}",
|
||||
source.getId(), source.getUrl(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 累加成功添加的数量
|
||||
totalAdded.addAndGet(deviceAdded);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("添加照片到打印列表失败: sourceId={}, url={}, error={}",
|
||||
source.getId(), source.getUrl(), e.getMessage());
|
||||
log.error("处理设备{}的照片添加任务失败: deviceId={}", deviceId, deviceId, e);
|
||||
}
|
||||
}
|
||||
}, preferPrintExecutor);
|
||||
|
||||
futures.add(future);
|
||||
}
|
||||
|
||||
if (totalAdded > 0) {
|
||||
log.info("自动添加打印完成: faceId={}, 成功添加{}张照片", faceId, totalAdded);
|
||||
// 等待所有任务完成
|
||||
try {
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||
} catch (Exception e) {
|
||||
log.error("等待照片添加任务完成时发生异常: faceId={}", faceId, e);
|
||||
}
|
||||
|
||||
if (totalAdded.get() > 0) {
|
||||
log.info("自动添加打印完成: faceId={}, 成功添加{}张照片", faceId, totalAdded.get());
|
||||
} else {
|
||||
log.debug("自动添加打印完成: faceId={}, 无符合条件的照片", faceId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user