|
@@ -8,25 +8,36 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
|
|
+import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
|
|
import cn.iocoder.yudao.module.infra.api.config.ConfigApi;
|
|
|
import cn.iocoder.yudao.module.infra.api.file.FileApi;
|
|
|
import cn.iocoder.yudao.module.md.controller.admin.vo.UserImportExcelVO;
|
|
|
import cn.iocoder.yudao.module.md.controller.admin.vo.UserImportRespVO;
|
|
|
+import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
|
|
+import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
|
|
import com.sun.jna.Native;
|
|
|
+import org.apache.tomcat.util.http.fileupload.FileUtils;
|
|
|
import org.json.JSONException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.ConstraintViolationException;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
@@ -43,7 +54,7 @@ public class AcsService {
|
|
|
// private HCNetSDK hCNetSDK;
|
|
|
private int lUserID = -1;
|
|
|
|
|
|
-
|
|
|
+ private AdminUserApi adminUserApi;
|
|
|
/**
|
|
|
* 初始化 SDK 并登录设备
|
|
|
*/
|
|
@@ -341,6 +352,89 @@ public class AcsService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
|
|
+ public String importImages(MultipartFile file) throws Exception {
|
|
|
+ // 校验文件类型
|
|
|
+ if (!file.getOriginalFilename().endsWith(".zip")) {
|
|
|
+ throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
|
|
|
+ }
|
|
|
+ // 创建临时目录存放解压后的文件
|
|
|
+ File tempDir = Files.createTempDirectory("specimen_images").toFile();
|
|
|
+ try (ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream())) {
|
|
|
+ ZipEntry entry;
|
|
|
+ while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
+ if (!entry.isDirectory()) {
|
|
|
+ File newFile = new File(tempDir, entry.getName());
|
|
|
+ // 进行解压
|
|
|
+ try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile))) {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = zipInputStream.read(buffer)) > 0) {
|
|
|
+ bos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理每个图片文件
|
|
|
+ File[] imageFiles = tempDir.listFiles();
|
|
|
+ if (imageFiles != null) {
|
|
|
+ for (File imageFile : imageFiles) {
|
|
|
+ String imageName = imageFile.getName();
|
|
|
+ if (!isValidImageName(imageName)) {
|
|
|
+ // 如果不符合格式,抛出异常或记录日志
|
|
|
+ System.err.println("无效的图片格式: " + imageName);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 这里根据图片名称获取对应的学生信息,然后更新学生信息,并且将这些信息下发到机器
|
|
|
+ // 查询方法我先不写了
|
|
|
+// AdminUserRespDTO user = adminUserApi.getUser(imageName);
|
|
|
+
|
|
|
+ String imagePath = fileApi.createFile(Files.readAllBytes(imageFile.toPath()));
|
|
|
+
|
|
|
+ // user.setImagePath(imagePath); TODO 插入新的人脸图片路径
|
|
|
+ // 更新标本信息中的图片路径
|
|
|
|
|
|
+ // TODO 更新信息 adminUserApi.updateUser(specimenInfo);
|
|
|
+
|
|
|
+ // TODO 在这一步就可以导入人脸信息了 addFaceByUrl(user.getSudentId,imagePath)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清理临时文件
|
|
|
+ FileUtils.deleteDirectory(tempDir);
|
|
|
+ return "导入成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查多个图片名称格式
|
|
|
+ private boolean isValidImageName(String imageNames) {
|
|
|
+ if (imageNames == null) return false;
|
|
|
+ String[] names = imageNames.split(",");
|
|
|
+ for (String name : names) {
|
|
|
+ if (!name.matches(".*\\.(jpg|jpeg|png|gif)$")) {
|
|
|
+ return false; // 只要有一个格式不正确就返回 false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class) // 事务管理
|
|
|
+ public void importData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
|
|
|
+
|
|
|
+ // TODO 这里用的字段太少了,下午和晚上要把字段完善,能够导入
|
|
|
+ // 1. 导入标本
|
|
|
+ List<UserImportExcelVO> list = ExcelUtils.read(excelFile, UserImportExcelVO.class);
|
|
|
+ UserImportRespVO importRespVO = importUserList(list, updateSupport);
|
|
|
+
|
|
|
+ // 2. 导入图片
|
|
|
+ String imageImportResult = importImages(imageFile);
|
|
|
+
|
|
|
+ // 可以根据需要记录导入结果
|
|
|
+ System.out.println("标本导入结果: " + importRespVO);
|
|
|
+ System.out.println("标本图片导入结果: " + imageImportResult);
|
|
|
+ }
|
|
|
|
|
|
}
|