|
@@ -19,6 +19,7 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserSaveReqV
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
|
|
import com.sun.jna.Native;
|
|
|
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
|
|
import org.apache.tomcat.util.http.fileupload.FileUtils;
|
|
|
import org.json.JSONException;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -29,15 +30,9 @@ 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.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.zip.ZipEntry;
|
|
|
import java.util.zip.ZipInputStream;
|
|
@@ -318,7 +313,7 @@ public class AcsService {
|
|
|
// if (existUser == null) {
|
|
|
// 异步添加用户信息
|
|
|
AdminUserDO user = userService.findUserByUserNumber(importUser.getEmployeeNo());
|
|
|
- CompletableFuture<Void> addUserFuture = CompletableFuture.runAsync(() -> {
|
|
|
+// CompletableFuture<Void> addUserFuture = CompletableFuture.runAsync(() -> {
|
|
|
try {
|
|
|
//用户名称与那users表里的相同
|
|
|
if (user.getNickname().equals(importUser.getName())) {
|
|
@@ -328,25 +323,8 @@ public class AcsService {
|
|
|
} catch (UnsupportedEncodingException | InterruptedException | JSONException e) {
|
|
|
throw new RuntimeException("添加用户信息失败: " + e.getMessage(), e);
|
|
|
}
|
|
|
- });
|
|
|
+// });
|
|
|
|
|
|
- // 在用户信息添加成功后添加人脸,人脸照片通过学号去找
|
|
|
- addUserFuture.thenRun(() -> {
|
|
|
- try {
|
|
|
- String photoUrl =user.getPhotoUrl();
|
|
|
- //照片不为空
|
|
|
- if ( photoUrl!=null){
|
|
|
- FaceManage.addFaceByUrl(lUserID, user.getUserNumber(), photoUrl);
|
|
|
- System.out.println("照片路由"+photoUrl);
|
|
|
- }
|
|
|
- } catch (JSONException e) {
|
|
|
- throw new RuntimeException("添加人脸失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
- }).exceptionally(ex -> {
|
|
|
- // 处理异常
|
|
|
- System.err.println("发生异常: " + ex.getMessage());
|
|
|
- return null;
|
|
|
- });
|
|
|
|
|
|
// }
|
|
|
// // 2.2.2 如果存在,判断是否允许更新
|
|
@@ -367,6 +345,8 @@ public class AcsService {
|
|
|
private AdminUserService userService;
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
|
|
+ //一定上传了照片的去添加
|
|
|
+// TODO这里还需判断那个人是否在考勤机的用户列表里
|
|
|
public String importImages(MultipartFile file) throws Exception {
|
|
|
// 校验文件类型
|
|
|
if (!file.getOriginalFilename().endsWith(".zip")) {
|
|
@@ -374,51 +354,113 @@ public class AcsService {
|
|
|
}
|
|
|
// 创建临时目录存放解压后的文件
|
|
|
File tempDir = Files.createTempDirectory("specimen_images").toFile();
|
|
|
- try (ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream())) {
|
|
|
+ List<String> successUsers =new LinkedList<>();
|
|
|
+ List<String> failUsers =new LinkedList<>();
|
|
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
+ ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream)) {
|
|
|
ZipEntry entry;
|
|
|
- while ((entry = zipInputStream.getNextEntry()) != null) {
|
|
|
+ while ((entry = zipInputStream.getNextZipEntry()) != null) {
|
|
|
if (!entry.isDirectory()) {
|
|
|
-
|
|
|
- //获取去除后缀名的文件名,需要设置成学号
|
|
|
- String newFileName = entry.getName().substring(0, entry.getName().lastIndexOf('.'));
|
|
|
- System.out.println("文件名:"+newFileName);
|
|
|
-
|
|
|
- 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);
|
|
|
+ //看名字
|
|
|
+ if (isValidImageName(entry.getName())) {
|
|
|
+ System.out.println(entry.getName());
|
|
|
+ //获取去除后缀名的文件名,需要设置成学号
|
|
|
+ String userNumber = entry.getName().substring(0, entry.getName().lastIndexOf('-'));
|
|
|
+ String studentName = entry.getName().substring(entry.getName().lastIndexOf('-') + 1, entry.getName().lastIndexOf('.'));
|
|
|
+ System.out.println("学号:" + userNumber);
|
|
|
+ System.out.println("学生名字:" + studentName);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
}
|
|
|
+ // 上传文件并获取 URL
|
|
|
+ AdminUserDO user = userService.findUserByUserNumber(userNumber);
|
|
|
+ String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
+ user.setPhotoUrl(photoUrl);
|
|
|
+ userService.updateUser((BeanUtils.toBean(user, UserSaveReqVO.class)));
|
|
|
+ //给对应学号的人的照片添加
|
|
|
+ addFaceByUrl( user.getUserNumber(), photoUrl);
|
|
|
+ successUsers.add(user.getNickname());
|
|
|
+ }else{
|
|
|
+ String userNumber = entry.getName().substring(0, entry.getName().indexOf('-'));
|
|
|
+ AdminUserDO user = userService.findUserByUserNumber(userNumber);
|
|
|
+ failUsers.add(user.getNickname());
|
|
|
}
|
|
|
- File[] imageFiles = tempDir.listFiles();
|
|
|
- if (imageFiles != null) {
|
|
|
- for (File imageFile : imageFiles) {
|
|
|
- String imageName = imageFile.getName();
|
|
|
- if (!isValidImageName(imageName)) {
|
|
|
- // 如果不符合格式,抛出异常或记录日志
|
|
|
- System.err.println("无效的图片格式: " + imageName);
|
|
|
- continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 清理临时文件
|
|
|
+ FileUtils.deleteDirectory(tempDir);
|
|
|
+ return "添加照片成功的用户{"
|
|
|
+ +successUsers+
|
|
|
+ "}"+
|
|
|
+ "添加照片失败的用户{"
|
|
|
+ +failUsers+
|
|
|
+ "}";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String testImages(MultipartFile file) throws Exception {
|
|
|
+ // 校验文件类型
|
|
|
+ if (!file.getOriginalFilename().endsWith(".zip")) {
|
|
|
+ throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
|
|
|
+ }
|
|
|
+ // 创建临时目录存放解压后的文件
|
|
|
+ File tempDir = Files.createTempDirectory("specimen_images").toFile();
|
|
|
+ List<String> successUsers =new LinkedList<>();
|
|
|
+ List<String> failUsers =new LinkedList<>();
|
|
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
+ ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream)) {
|
|
|
+ ZipEntry entry;
|
|
|
+ while ((entry = zipInputStream.getNextZipEntry()) != null) {
|
|
|
+ if (!entry.isDirectory()) {
|
|
|
+ //看名字
|
|
|
+ if (isValidImageName(entry.getName())) {
|
|
|
+ System.out.println(entry.getName());
|
|
|
+ //获取去除后缀名的文件名,需要设置成学号
|
|
|
+ String userNumber = entry.getName().substring(0, entry.getName().lastIndexOf('-'));
|
|
|
+ String studentName = entry.getName().substring(entry.getName().lastIndexOf('-') + 1, entry.getName().lastIndexOf('.'));
|
|
|
+ System.out.println("学号:" + userNumber);
|
|
|
+ System.out.println("学生名字:" + studentName);
|
|
|
+
|
|
|
+ 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);
|
|
|
}
|
|
|
- // 上传文件并获取 URL
|
|
|
- AdminUserDO user = userService.findUserByUserNumber(newFileName);
|
|
|
- String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
- user.setPhotoUrl(photoUrl);
|
|
|
- userService.updateUser( (BeanUtils.toBean(user, UserSaveReqVO.class)));
|
|
|
- System.out.println("照片路径: " + photoUrl);
|
|
|
- //给对应学号的人的照片添加
|
|
|
- addFaceByUrl(newFileName,photoUrl);
|
|
|
}
|
|
|
+ // 上传文件并获取 URL
|
|
|
+// AdminUserDO user = userService.findUserByUserNumber(userNumber);
|
|
|
+ String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
+// user.setPhotoUrl(photoUrl);
|
|
|
+// userService.updateUser((BeanUtils.toBean(user, UserSaveReqVO.class)));
|
|
|
+ //给对应学号的人的照片添加
|
|
|
+ addFaceByUrl( userNumber, photoUrl);
|
|
|
+ successUsers.add(studentName);
|
|
|
+ }else{
|
|
|
+ String userNumber = entry.getName().substring(0, entry.getName().indexOf('-'));
|
|
|
+ AdminUserDO user = userService.findUserByUserNumber(userNumber);
|
|
|
+ failUsers.add(user.getNickname());
|
|
|
}
|
|
|
- System.out.println(111111);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 清理临时文件
|
|
|
FileUtils.deleteDirectory(tempDir);
|
|
|
- return "导入成功";
|
|
|
+ return "添加照片成功的用户{"
|
|
|
+ +successUsers+
|
|
|
+ "}"+
|
|
|
+ "添加照片失败的用户{"
|
|
|
+ +failUsers+
|
|
|
+ "}";
|
|
|
}
|
|
|
|
|
|
// 检查多个图片名称格式
|
|
@@ -433,20 +475,22 @@ public class AcsService {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Transactional(rollbackFor = Exception.class) // 事务管理
|
|
|
- public void importData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
|
|
|
+ public String 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);
|
|
|
-
|
|
|
+ String imageImportResult = testImages(imageFile);
|
|
|
// 可以根据需要记录导入结果
|
|
|
System.out.println("用户信息导入结果: " + importRespVO);
|
|
|
System.out.println("图片导入结果: " + imageImportResult);
|
|
|
+
|
|
|
+ return importRespVO+
|
|
|
+ imageImportResult;
|
|
|
}
|
|
|
|
|
|
}
|