From a720652ebc842221ed797593f53386d23118c416 Mon Sep 17 00:00:00 2001 From: zhangyeguang Date: Fri, 6 Dec 2024 14:54:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E6=9E=9C=E8=AF=86=E5=88=AB=E5=90=8E?= =?UTF-8?q?=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../jiagutech/common/exception/BizCode.java | 9 ++- .../exception/GlobalExceptionHandler.java | 2 +- .../controller/RecordController.java | 8 ++- .../java/com/jiagutech/entity/UserEntity.java | 2 +- .../jiagutech/record/RecordTaskManager.java | 4 ++ .../jiagutech/record/RecordTaskProcessor.java | 71 +++++++------------ .../jiagutech/service/RecordServiceImpl.java | 71 ++++++++++++++++--- .../jiagutech/service/UserServiceImpl.java | 1 + .../com/jiagutech/utils/RecognitionUtil.java | 25 +++++++ 10 files changed, 135 insertions(+), 64 deletions(-) create mode 100644 src/main/java/com/jiagutech/utils/RecognitionUtil.java diff --git a/pom.xml b/pom.xml index f1d8fac..6b2417e 100644 --- a/pom.xml +++ b/pom.xml @@ -84,6 +84,12 @@ mapstruct ${mapstruct.version} + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + compile + com.alibaba.fastjson2 fastjson2 diff --git a/src/main/java/com/jiagutech/common/exception/BizCode.java b/src/main/java/com/jiagutech/common/exception/BizCode.java index d3f04c4..f4073f5 100644 --- a/src/main/java/com/jiagutech/common/exception/BizCode.java +++ b/src/main/java/com/jiagutech/common/exception/BizCode.java @@ -24,6 +24,10 @@ public enum BizCode { FILE_UPLOAD_ERROR(90002, "文件上传失败"), + /** + * 服务器繁忙 + */ + SERVER_BUSY(500, "服务器繁忙,请稍后再试"), USER_NOT_FOUND(40004, "用户不存在"), @@ -35,7 +39,7 @@ public enum BizCode { TOKEN_TIMEOUT_ERROR(40006, "token已过期"), USER_STOP_FOUND(40008, "用户被禁用"), - USER_STATUS_ERROR(40009,"用户未审核通过"), + USER_STATUS_ERROR(40009, "用户未审核通过"), USER_NOT_LOGIN(40011, "用户未登录,请登录后再操作"), @@ -43,12 +47,11 @@ public enum BizCode { ACCESS_NOT_ALLOWABLE(50001, "服务不允许直接访问"), - PERMISSION_NOT_FOUND(40001,"该用户无操作权限"), + PERMISSION_NOT_FOUND(40001, "该用户无操作权限"), USER_PHONE_EXIST(40012, "用户手机号已存在"), - ; diff --git a/src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java b/src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java index 043a399..422e790 100644 --- a/src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java @@ -45,7 +45,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(BusinessException.class) public ResponseEntity handleBusinessException(BusinessException ex) { - return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.OK); } @ExceptionHandler(MethodArgumentTypeMismatchException.class) diff --git a/src/main/java/com/jiagutech/controller/RecordController.java b/src/main/java/com/jiagutech/controller/RecordController.java index aa828a1..98d8784 100644 --- a/src/main/java/com/jiagutech/controller/RecordController.java +++ b/src/main/java/com/jiagutech/controller/RecordController.java @@ -67,8 +67,9 @@ public class RecordController { @ApiOperation(value = "更新识别记录内容") @PutMapping("/updateRecordContent") - public void updateRecord(@RequestBody RecordContentEntity recordContentEntity) { + public R updateRecord(@RequestBody RecordContentEntity recordContentEntity) { recordService.updateRecordContent(recordContentEntity); + return R.ok("操作成功"); } @@ -80,13 +81,14 @@ public class RecordController { @ApiOperation(value = "比例尺设置") @PutMapping("/scale") - public void updateScale(@RequestBody UpdateScaleRequest request) { + public R updateScale(@RequestBody UpdateScaleRequest request) { recordService.updateScale(request); + return R.ok("操作成功"); } @ApiOperation(value = "删除记录") @DeleteMapping(value = "/delete/{recordId}") - public R deleteRecord(Long recordId) { + public R deleteRecord(@PathVariable Long recordId) { boolean res = recordService.deleteRecord(recordId); return res ? R.ok() : R.fail(); } diff --git a/src/main/java/com/jiagutech/entity/UserEntity.java b/src/main/java/com/jiagutech/entity/UserEntity.java index ba3b5bd..a910707 100644 --- a/src/main/java/com/jiagutech/entity/UserEntity.java +++ b/src/main/java/com/jiagutech/entity/UserEntity.java @@ -24,7 +24,7 @@ public class UserEntity { private int status; - private int role; + private Integer role; } diff --git a/src/main/java/com/jiagutech/record/RecordTaskManager.java b/src/main/java/com/jiagutech/record/RecordTaskManager.java index 9bca3f4..54cc080 100644 --- a/src/main/java/com/jiagutech/record/RecordTaskManager.java +++ b/src/main/java/com/jiagutech/record/RecordTaskManager.java @@ -38,4 +38,8 @@ public class RecordTaskManager { return taskItems; } + public static int taskCount() { + return recordTasks.size(); + } + } diff --git a/src/main/java/com/jiagutech/record/RecordTaskProcessor.java b/src/main/java/com/jiagutech/record/RecordTaskProcessor.java index 1481879..3ce7f51 100644 --- a/src/main/java/com/jiagutech/record/RecordTaskProcessor.java +++ b/src/main/java/com/jiagutech/record/RecordTaskProcessor.java @@ -7,6 +7,7 @@ import com.jiagutech.entity.RecordEntity; import com.jiagutech.mapper.RecordContentMapper; import com.jiagutech.mapper.RecordMapper; import com.jiagutech.utils.HuaweiObs; +import com.jiagutech.utils.RecognitionUtil; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -15,8 +16,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.io.File; -import java.math.BigDecimal; -import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.List; @@ -50,7 +49,7 @@ public class RecordTaskProcessor { int rapeNum = 0; int beakNum = 0; for (ModelPredictResponse item : predict) { - item.setLength(getMaxLength(item.getPoints())); + item.setLength(RecognitionUtil.getMaxLength(item.getPoints())); switch (item.getClassify()) { case 0: rapeNum++; @@ -64,53 +63,35 @@ public class RecordTaskProcessor { } progress += (float) (100 / recordTask.getFiles().length - 10); recordTask.setProgress(progress); - if (!CollectionUtils.isEmpty(predict)) { - String imageUrl; - try { - log.info("存储图片到oss,file:{}", file.getName()); -// imageUrl = huaweiObs.uploadFile(file); - imageUrl = ""; - progress += 2f; - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - file.deleteOnExit(); - } - RecordContentEntity recordContentEntity = new RecordContentEntity() - .setRecordId(recordEntity.getId()).setImageUrl(imageUrl) - .setRecognitionData(predict) - .setImageName(file.getName()) - .setConf(recordTask.getConf()) - .setIou(recordTask.getIou()) - .setHandleNum(handleNum) - .setBeakNum(beakNum) - .setSiliquaNum(rapeNum); - recordContentMapper.insert(recordContentEntity); - log.info("保存处理结果,file:{}", file.getName()); - progress += 3f; +// if (!CollectionUtils.isEmpty(predict)) { + String imageUrl; + try { + log.info("存储图片到oss,file:{}", file.getName()); + imageUrl = huaweiObs.uploadFile(file); +// imageUrl = ""; + progress += 2f; + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + file.deleteOnExit(); } + RecordContentEntity recordContentEntity = new RecordContentEntity() + .setRecordId(recordEntity.getId()).setImageUrl(imageUrl) + .setRecognitionData(predict) + .setImageName(file.getName()) + .setConf(recordTask.getConf()) + .setIou(recordTask.getIou()) + .setHandleNum(handleNum) + .setBeakNum(beakNum) + .setSiliquaNum(rapeNum); + recordContentMapper.insert(recordContentEntity); + log.info("保存处理结果,file:{}", file.getName()); + progress += 3f; +// } } } - private static double calculateDistance(int x1, int y1, int x2, int y2) { - return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); - } - - private float getMaxLength(int[][] points) { - // 计算四条边的长度 - double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB - double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC - double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD - double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA - double max = Math.max(Math.max(ab, bc), Math.max(cd, da)); - BigDecimal b = new BigDecimal(max); - // 找出最大边长 - return b.setScale(2, RoundingMode.HALF_UP).floatValue(); - - - } - } diff --git a/src/main/java/com/jiagutech/service/RecordServiceImpl.java b/src/main/java/com/jiagutech/service/RecordServiceImpl.java index e618d1e..2f8370e 100644 --- a/src/main/java/com/jiagutech/service/RecordServiceImpl.java +++ b/src/main/java/com/jiagutech/service/RecordServiceImpl.java @@ -1,6 +1,7 @@ package com.jiagutech.service; import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.collection.CollectionUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.util.MapUtils; @@ -16,6 +17,8 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jiagutech.client.RecognitionModelClient; +import com.jiagutech.common.exception.BizCode; +import com.jiagutech.common.exception.BusinessException; import com.jiagutech.dto.*; import com.jiagutech.dto.common.LoginUser; import com.jiagutech.dto.common.PageRequest; @@ -35,6 +38,8 @@ import com.jiagutech.record.RecordTaskHandler; import com.jiagutech.record.RecordTaskManager; import com.jiagutech.record.RecordTaskProcessor; import com.jiagutech.utils.LoginUtil; +import com.jiagutech.utils.RecognitionUtil; +import jdk.nashorn.internal.ir.IfNode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.HorizontalAlignment; @@ -76,8 +81,17 @@ public class RecordServiceImpl implements RecordService { return null; } + /** + * 同时处理的任务数小于等于8个 + * + * @param request + * @return + */ @Override public String createRecordAsync(CreateRecordRequest request) { + if (RecordTaskManager.taskCount() >= 9) { + throw new BusinessException(BizCode.SERVER_BUSY); + } String taskId = UUID.randomUUID().toString(); RecordTaskHandler recordTaskHandler = new RecordTaskHandler(customThreadPool, taskProcessor, tmpPath); recordTaskHandler.createTask(request, taskId); @@ -142,8 +156,13 @@ public class RecordServiceImpl implements RecordService { List recognitionData = record.getRecognitionData(); Scale scaleDto = record.getScale(); //把识别数据进行分类 - float size = scaleDto.getSize(); - int scale = scaleDto.getScale(); + float size = 0; + int scale = 0; + if (scaleDto != null) { + size = scaleDto.getSize(); + scale = scaleDto.getScale(); + } + for (ModelPredictResponse item : recognitionData) { float len; if (size > 0 && scale > 0) { @@ -191,11 +210,13 @@ public class RecordServiceImpl implements RecordService { } //创建单元格的合并策略, 前7列数据需要合并 for (int i = 0; i <= 6; i++) { - OnceAbsoluteMergeStrategy mergeStrategy = new OnceAbsoluteMergeStrategy( - startRowIndex, - endRowIndex, - i, i); - mergeStrategies.add(mergeStrategy); + if (endRowIndex > startRowIndex) { + OnceAbsoluteMergeStrategy mergeStrategy = new OnceAbsoluteMergeStrategy( + startRowIndex, + endRowIndex, + i, i); + mergeStrategies.add(mergeStrategy); + } } startRowIndex += len; @@ -294,6 +315,30 @@ public class RecordServiceImpl implements RecordService { @Override public void updateRecordContent(RecordContentEntity recordContentEntity) { + if (CollectionUtil.isNotEmpty(recordContentEntity.getRecognitionData())) { + int handleNum = 0; + int rapeNum = 0; + int beakNum = 0; + for (ModelPredictResponse item : recordContentEntity.getRecognitionData()) { + if (item.getLength() == null || item.getLength() == 0) { + item.setLength(RecognitionUtil.getMaxLength(item.getPoints())); + } + switch (item.getClassify()) { + case 0: + rapeNum++; + break; + case 1: + beakNum++; + break; + default: + handleNum++; + } + } + recordContentEntity.setHandleNum(handleNum) + .setBeakNum(beakNum) + .setSiliquaNum(rapeNum); + } + recordContentMapper.updateById(recordContentEntity); } @@ -314,9 +359,10 @@ public class RecordServiceImpl implements RecordService { own.setScale(request.getScale()); own.setScaleType(request.getScaleType()); own.setId(request.getRecordContentId()); - other = new RecordContentEntity().setScale(request.getScale()); + other = new RecordContentEntity().setScaleType(3).setScale(request.getScale()); queryWrapper.eq("record_id", request.getRecordId()); - queryWrapper.eq("scale_type", 3); + queryWrapper.in("scale_type", 0, 1, 3); + queryWrapper.ne("id", request.getRecordContentId()); break; case 2: own = recordContentMapper.selectById(request.getRecordContentId()); @@ -330,14 +376,16 @@ public class RecordServiceImpl implements RecordService { break; case 3: default: - own = new RecordContentEntity().setScaleType(3); queryWrapper.eq("record_id", request.getRecordId()); queryWrapper.eq("scale_type", 1); RecordContentEntity recordContentEntity = recordContentMapper.selectOne(queryWrapper); if (recordContentEntity != null) { + own = new RecordContentEntity().setScaleType(3); own.setScale(recordContentEntity.getScale()); + own.setId(request.getRecordContentId()); + } else { + throw new BusinessException(BizCode.General_Failure, "当前无统一的比例尺"); } - own.setId(request.getRecordContentId()); } recordContentMapper.updateById(own); @@ -380,4 +428,5 @@ public class RecordServiceImpl implements RecordService { } + } diff --git a/src/main/java/com/jiagutech/service/UserServiceImpl.java b/src/main/java/com/jiagutech/service/UserServiceImpl.java index 7a966ab..b9a2ae3 100644 --- a/src/main/java/com/jiagutech/service/UserServiceImpl.java +++ b/src/main/java/com/jiagutech/service/UserServiceImpl.java @@ -61,6 +61,7 @@ public class UserServiceImpl implements UserService { .setRealName(user.getRealName()) .setUnitName(user.getUnitName()) .setPhone(user.getPhone()) + .setRole(0) .setPassword(BCrypt.hashpw(user.getPassword())); userMapper.insert(userEntity); emailUtil.sendEmail(null, user.getPhone(), user.getUnitName(), user.getRealName()); diff --git a/src/main/java/com/jiagutech/utils/RecognitionUtil.java b/src/main/java/com/jiagutech/utils/RecognitionUtil.java new file mode 100644 index 0000000..05d8a10 --- /dev/null +++ b/src/main/java/com/jiagutech/utils/RecognitionUtil.java @@ -0,0 +1,25 @@ +package com.jiagutech.utils; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class RecognitionUtil { + + private static double calculateDistance(int x1, int y1, int x2, int y2) { + return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); + } + + public static float getMaxLength(int[][] points) { + // 计算四条边的长度 + double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB + double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC + double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD + double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA + double max = Math.max(Math.max(ab, bc), Math.max(cd, da)); + BigDecimal b = new BigDecimal(max); + // 找出最大边长 + return b.setScale(2, RoundingMode.HALF_UP).floatValue(); + + + } +}