|
@@ -278,58 +278,60 @@ public class AcsService {
|
|
|
|
|
|
//用户添加自己的照片(也可以对考勤机那添加用户信息照片或更新照片)
|
|
|
@Transactional(rollbackFor = Exception.class) //
|
|
|
- public String addSelfImage(MultipartFile image) throws Exception {
|
|
|
- if (isValidImageName(image.getOriginalFilename()).equals("照片格式正确")) {
|
|
|
+ public String addSelfImage(String photoUrl) throws Exception {
|
|
|
Long loginId = SecurityFrameworkUtils.getLoginUserId();
|
|
|
AdminUserDO user = userService.getUser(loginId);
|
|
|
- //创建临时文件目录
|
|
|
- File tempDir = Files.createTempDirectory("user_image").toFile();
|
|
|
- try {
|
|
|
- File newFile =new File(tempDir,image.getOriginalFilename());
|
|
|
- //将内容写入
|
|
|
- image.transferTo(newFile);
|
|
|
- String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
- user.setPhotoUrl(photoUrl);
|
|
|
- String userNumber = user.getUserNumber();
|
|
|
-
|
|
|
- String result = getUser(userNumber);
|
|
|
- Gson gson = new Gson();
|
|
|
- JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
|
|
|
- JsonArray userInfoArray = jsonObject
|
|
|
- .getAsJsonObject("UserInfoSearch")
|
|
|
- .getAsJsonArray("UserInfo");
|
|
|
-
|
|
|
- if (userInfoArray != null) {
|
|
|
- addFaceByUrl(userNumber, user.getPhotoUrl());
|
|
|
+
|
|
|
+ String userNumber = user.getUserNumber();
|
|
|
+
|
|
|
+ //考勤机
|
|
|
+ String result = getUser(userNumber);
|
|
|
+ Gson gson = new Gson();
|
|
|
+ JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
|
|
|
+ JsonArray userInfoArray = jsonObject
|
|
|
+ .getAsJsonObject("UserInfoSearch")
|
|
|
+ .getAsJsonArray("UserInfo");
|
|
|
+
|
|
|
+ if (userInfoArray != null) {//考勤机里有用户
|
|
|
+ String msg = addFaceByUrl(userNumber,photoUrl);
|
|
|
+ if (msg.contains("下发人脸成功") && msg.contains("但是有异常情况")) {
|
|
|
+ return "更新人脸失败";
|
|
|
} else {
|
|
|
- //确保在执行完增加用户后执行增加人脸
|
|
|
- CompletableFuture<Void> userFuture = CompletableFuture.runAsync(() -> {
|
|
|
- try {
|
|
|
- addUser(userNumber, user.getNickname());
|
|
|
- } catch (JSONException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
- userFuture.thenRun(() -> {
|
|
|
- try {
|
|
|
- addFaceByUrl(userNumber, photoUrl);
|
|
|
- } catch (JSONException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
- return "添加用户并且增加照片成功";
|
|
|
+ if (user!=null) {
|
|
|
+ user.setPhotoUrl(photoUrl);
|
|
|
+ userService.updateUser((BeanUtils.toBean(user, UserSaveReqVO.class)));
|
|
|
+ }
|
|
|
+ return "更新人脸成功";
|
|
|
}
|
|
|
- }finally {
|
|
|
- FileUtils.deleteDirectory(tempDir);
|
|
|
+ } else {//考勤及里面没用户
|
|
|
+ //确保在执行完增加用户后执行增加人脸
|
|
|
+ CompletableFuture<Void> userFuture = CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ addUser(userNumber, user.getNickname());
|
|
|
+ } catch (JSONException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ userFuture.thenRun(() -> {
|
|
|
+ try {
|
|
|
+ String msg = addFaceByUrl(userNumber, photoUrl);
|
|
|
+ if (msg.contains("下发人脸成功") && msg.contains("但是有异常情况")) {
|
|
|
+ } else {
|
|
|
+ if (user!=null) {
|
|
|
+ user.setPhotoUrl(photoUrl);
|
|
|
+ userService.updateUser((BeanUtils.toBean(user, UserSaveReqVO.class)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (JSONException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return "添加考勤用户并且增加照片成功";
|
|
|
}
|
|
|
- return "更新照片成功";
|
|
|
- }else{
|
|
|
- return "照片格式有问题";
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -368,7 +370,7 @@ public class AcsService {
|
|
|
|
|
|
//批量导入用户信息
|
|
|
@Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
|
|
- public attendanceImportRespVO importUserList(List<attendanceImportExcelVO> importUsers, boolean isUpdateSupport) {
|
|
|
+ public attendanceImportRespVO importUserList(List<attendanceImportExcelVO> importUsers) {
|
|
|
|
|
|
attendanceImportRespVO respVO = attendanceImportRespVO.builder().createUsernames(new ArrayList<>())
|
|
|
.updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build();
|
|
@@ -439,7 +441,7 @@ public class AcsService {
|
|
|
AdminUserDO user = userService.findUserByUserNumber(userNumber);
|
|
|
String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
//给对应学号的人的照片添加
|
|
|
- String msg = addFaceByUrl(user.getUserNumber(), photoUrl);
|
|
|
+ String msg = addFaceByUrl(userNumber, photoUrl);
|
|
|
if (msg.contains("下发人脸成功") && msg.contains("但是有异常情况")) {
|
|
|
respVO.getFailureImages().add(studentName);//添加失败
|
|
|
} else {
|
|
@@ -464,84 +466,7 @@ public class AcsService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- //直接导入用户人脸
|
|
|
- public String testImages(MultipartFile file) throws Exception {
|
|
|
- // 校验文件类型
|
|
|
- if (!file.getOriginalFilename().endsWith(".zip")) {
|
|
|
- throw exception(UPLOADED_FOLDER_CANNOT_EMPTY);
|
|
|
- }
|
|
|
- // 创建临时目录存放解压后的文件
|
|
|
- File tempDir = Files.createTempDirectory("users_images").toFile();
|
|
|
- List<String> successUsers =new LinkedList<>();
|
|
|
- List<String> failUsers =new LinkedList<>();
|
|
|
- List<String> nullUsers =new LinkedList<>();
|
|
|
- List<String> errorImages =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()).equals("照片格式正确")) {
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
- String result =getUser(userNumber);
|
|
|
- //用户不为空,为用户添加人脸
|
|
|
- Gson gson = new Gson();
|
|
|
- JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
|
|
|
-
|
|
|
- JsonArray userInfoArray = jsonObject
|
|
|
- .getAsJsonObject("UserInfoSearch")
|
|
|
- .getAsJsonArray("UserInfo");
|
|
|
- if (userInfoArray!=null) {
|
|
|
- String photoUrl = fileApi.createFile(Files.readAllBytes(newFile.toPath()));
|
|
|
- //给对应学号的人的照片添加
|
|
|
- String msg = addFaceByUrl(userNumber, photoUrl);
|
|
|
- //System.out.println("数据"+msg);
|
|
|
- if (msg.contains("下发人脸成功") && msg.contains("但是有异常情况")) {
|
|
|
- failUsers.add(studentName);
|
|
|
- } else {
|
|
|
- successUsers.add(studentName);
|
|
|
- }
|
|
|
- } else{
|
|
|
- nullUsers.add(studentName);//添加找不到的用户
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- // 清理临时文件
|
|
|
- FileUtils.deleteDirectory(tempDir);
|
|
|
- return "添加照片成功的用户{"
|
|
|
- +successUsers+
|
|
|
- "}"+
|
|
|
- "添加照片失败的用户{"
|
|
|
- +failUsers+
|
|
|
- "}"+
|
|
|
- "不存在的用户{"+
|
|
|
- nullUsers+
|
|
|
- "}"+
|
|
|
- "照片格式错误{"+
|
|
|
- errorImages+
|
|
|
- "}";
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 检查多个图片名称格式
|
|
|
+ // 检查图片名称格式
|
|
|
private String isValidImageName(String imageName) {
|
|
|
if (imageName == null || imageName.trim().isEmpty()) {
|
|
|
return "照片为空"; // 如果照片名称为空,返回提示信息
|
|
@@ -552,16 +477,15 @@ public class AcsService {
|
|
|
return "照片格式正确"; // 如果格式正确,返回确认消息
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private attendanceImportRespVO importRespVO;
|
|
|
private imageImportRespVO imageImportResult;
|
|
|
@Transactional(rollbackFor = Exception.class) // 事务管理
|
|
|
- public importResponse importData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
|
|
|
+ public importResponse importData(MultipartFile excelFile, MultipartFile imageFile) throws Exception {
|
|
|
importResponse response = importResponse.builder().build();
|
|
|
// 1. 导入用户信息
|
|
|
if (excelFile!=null){
|
|
|
List<attendanceImportExcelVO> list = ExcelUtils.read(excelFile, attendanceImportExcelVO.class);
|
|
|
- importRespVO = importUserList(list, updateSupport);
|
|
|
+ importRespVO = importUserList(list);
|
|
|
System.out.println("用户信息导入结果: " + importRespVO);
|
|
|
}else {
|
|
|
importRespVO=null;
|
|
@@ -581,28 +505,5 @@ public class AcsService {
|
|
|
|
|
|
|
|
|
|
|
|
- @Transactional(rollbackFor = Exception.class) // 是先插入的
|
|
|
- public importResponse testData(MultipartFile excelFile, MultipartFile imageFile, boolean updateSupport) throws Exception {
|
|
|
- importResponse response = importResponse.builder().build();
|
|
|
- // 1. 导入用户信息
|
|
|
- if (excelFile!=null){
|
|
|
- List<attendanceImportExcelVO> list = ExcelUtils.read(excelFile, attendanceImportExcelVO.class);
|
|
|
- importRespVO = importUserList(list, updateSupport);
|
|
|
- System.out.println("用户信息导入结果: " + importRespVO);
|
|
|
- }else {
|
|
|
- importRespVO=null;
|
|
|
- }
|
|
|
- // 2. 导入图片
|
|
|
- if (imageFile!=null){
|
|
|
- imageImportResult = importImages(imageFile);
|
|
|
- System.out.println("图片导入结果: " + imageImportResult);
|
|
|
- }else {
|
|
|
- imageImportResult=null;
|
|
|
- }
|
|
|
- response.setImageImportResult(imageImportResult);
|
|
|
- response.setUserImportResult(importRespVO);
|
|
|
-
|
|
|
- return response;
|
|
|
- }
|
|
|
|
|
|
}
|